This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / include / linux / vs_memory.h
1 #ifndef _VX_VS_MEMORY_H
2 #define _VX_VS_MEMORY_H
3
4
5 // #define VX_DEBUG
6
7 #include <linux/kernel.h>
8 #include <linux/rcupdate.h>
9 #include <linux/sched.h>
10
11 #include "vserver/context.h"
12 #include "vserver/limit.h"
13
14
15 #define VX_DEBUG_ACC_RSS   0
16 #define VX_DEBUG_ACC_VM    0
17 #define VX_DEBUG_ACC_VML   0
18
19 #if     (VX_DEBUG_ACC_RSS) || (VX_DEBUG_ACC_VM) || (VX_DEBUG_ACC_VML)
20 #define vxdprintk(x...) printk("vxd: " x)
21 #else
22 #define vxdprintk(x...)
23 #endif
24
25 #define vx_acc_page(m, d, v, r) \
26         __vx_acc_page(&(m->v), m->mm_vx_info, r, d, __FILE__, __LINE__)
27
28 static inline void __vx_acc_page(unsigned long *v, struct vx_info *vxi,
29                 int res, int dir, char *file, int line)
30 {
31         if (v) {
32                 if (dir > 0)
33                         ++(*v);
34                 else
35                         --(*v);
36         }
37         if (vxi) {
38                 if (dir > 0)
39                         atomic_inc(&vxi->limit.rcur[res]);
40                 else
41                         atomic_dec(&vxi->limit.rcur[res]);
42         }
43 }
44
45
46 #define vx_acc_pages(m, p, v, r) \
47         __vx_acc_pages(&(m->v), m->mm_vx_info, r, p, __FILE__, __LINE__)
48
49 static inline void __vx_acc_pages(unsigned long *v, struct vx_info *vxi,
50                 int res, int pages, char *file, int line)
51 {
52         if ((res == RLIMIT_RSS && VX_DEBUG_ACC_RSS) ||
53                 (res == RLIMIT_AS && VX_DEBUG_ACC_VM) ||
54                 (res == RLIMIT_MEMLOCK && VX_DEBUG_ACC_VML))
55                 vxdprintk("vx_acc_pages  [%5d,%2d]: %5d += %5d in %s:%d\n",
56                         (vxi?vxi->vx_id:-1), res,
57                         (vxi?atomic_read(&vxi->limit.res[res]):0),
58                         pages, file, line);
59         if (pages == 0)
60                 return;
61         if (v)
62                 *v += pages;
63         if (vxi)
64                 atomic_add(pages, &vxi->limit.rcur[res]);
65 }
66
67
68
69 #define vx_acc_vmpage(m,d)     vx_acc_page(m, d, total_vm,  RLIMIT_AS)
70 #define vx_acc_vmlpage(m,d)    vx_acc_page(m, d, locked_vm, RLIMIT_MEMLOCK)
71 #define vx_acc_rsspage(m,d)    vx_acc_page(m, d, rss,       RLIMIT_RSS)
72
73 #define vx_acc_vmpages(m,p)    vx_acc_pages(m, p, total_vm,  RLIMIT_AS)
74 #define vx_acc_vmlpages(m,p)   vx_acc_pages(m, p, locked_vm, RLIMIT_MEMLOCK)
75 #define vx_acc_rsspages(m,p)   vx_acc_pages(m, p, rss,       RLIMIT_RSS)
76
77 #define vx_pages_add(s,r,p)    __vx_acc_pages(0, s, r, p, __FILE__, __LINE__)
78 #define vx_pages_sub(s,r,p)    __vx_pages_add(s, r, -(p))
79
80 #define vx_vmpages_inc(m)      vx_acc_vmpage(m, 1)
81 #define vx_vmpages_dec(m)      vx_acc_vmpage(m,-1)
82 #define vx_vmpages_add(m,p)    vx_acc_vmpages(m, p)
83 #define vx_vmpages_sub(m,p)    vx_acc_vmpages(m,-(p))
84
85 #define vx_vmlocked_inc(m)     vx_acc_vmlpage(m, 1)
86 #define vx_vmlocked_dec(m)     vx_acc_vmlpage(m,-1)
87 #define vx_vmlocked_add(m,p)   vx_acc_vmlpages(m, p)
88 #define vx_vmlocked_sub(m,p)   vx_acc_vmlpages(m,-(p))
89
90 #define vx_rsspages_inc(m)     vx_acc_rsspage(m, 1)
91 #define vx_rsspages_dec(m)     vx_acc_rsspage(m,-1)
92 #define vx_rsspages_add(m,p)   vx_acc_rsspages(m, p)
93 #define vx_rsspages_sub(m,p)   vx_acc_rsspages(m,-(p))
94
95
96
97 #define vx_pages_avail(m, p, r) \
98         __vx_pages_avail((m)->mm_vx_info, (r), (p), __FILE__, __LINE__)
99
100 static inline int __vx_pages_avail(struct vx_info *vxi,
101                 int res, int pages, char *file, int line)
102 {
103         unsigned long value;
104
105         if ((res == RLIMIT_RSS && VX_DEBUG_ACC_RSS) ||
106                 (res == RLIMIT_AS && VX_DEBUG_ACC_VM) ||
107                 (res == RLIMIT_MEMLOCK && VX_DEBUG_ACC_VML))
108                 printk("vx_pages_avail[%5d,%2d]: %5ld > %5d + %5d in %s:%d\n",
109                         (vxi?vxi->vx_id:-1), res,
110                         (vxi?vxi->limit.rlim[res]:1),
111                         (vxi?atomic_read(&vxi->limit.rcur[res]):0),
112                         pages, file, line);
113         if (!vxi)
114                 return 1;
115         value = atomic_read(&vxi->limit.rcur[res]);     
116         if (value > vxi->limit.rmax[res])
117                 vxi->limit.rmax[res] = value;
118         if (vxi->limit.rlim[res] == RLIM_INFINITY)
119                 return 1;
120         if (value + pages <= vxi->limit.rlim[res])
121                 return 1;
122         atomic_inc(&vxi->limit.lhit[res]);
123         return 0;
124 }
125
126 #define vx_vmpages_avail(m,p)  vx_pages_avail(m, p, RLIMIT_AS)
127 #define vx_vmlocked_avail(m,p) vx_pages_avail(m, p, RLIMIT_MEMLOCK)
128 #define vx_rsspages_avail(m,p) vx_pages_avail(m, p, RLIMIT_RSS)
129
130 #else
131 #warning duplicate inclusion
132 #endif