patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / ipc / util.c
1 /*
2  * linux/ipc/util.c
3  * Copyright (C) 1992 Krishna Balasubramanian
4  *
5  * Sep 1997 - Call suser() last after "normal" permission checks so we
6  *            get BSD style process accounting right.
7  *            Occurs in several places in the IPC code.
8  *            Chris Evans, <chris@ferret.lmh.ox.ac.uk>
9  * Nov 1999 - ipc helper functions, unified SMP locking
10  *            Manfred Spraul <manfreds@colorfullife.com>
11  * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12  *            Mingming Cao <cmm@us.ibm.com>
13  */
14
15 #include <linux/config.h>
16 #include <linux/mm.h>
17 #include <linux/shm.h>
18 #include <linux/init.h>
19 #include <linux/msg.h>
20 #include <linux/smp_lock.h>
21 #include <linux/vmalloc.h>
22 #include <linux/slab.h>
23 #include <linux/highuid.h>
24 #include <linux/security.h>
25 #include <linux/rcupdate.h>
26 #include <linux/workqueue.h>
27 #include <linux/vs_base.h>
28
29 #include <asm/unistd.h>
30
31 #include "util.h"
32
33 /**
34  *      ipc_init        -       initialise IPC subsystem
35  *
36  *      The various system5 IPC resources (semaphores, messages and shared
37  *      memory are initialised
38  */
39  
40 static int __init ipc_init(void)
41 {
42         sem_init();
43         msg_init();
44         shm_init();
45         return 0;
46 }
47 __initcall(ipc_init);
48
49 /**
50  *      ipc_init_ids            -       initialise IPC identifiers
51  *      @ids: Identifier set
52  *      @size: Number of identifiers
53  *
54  *      Given a size for the ipc identifier range (limited below IPCMNI)
55  *      set up the sequence range to use then allocate and initialise the
56  *      array itself. 
57  */
58  
59 void __init ipc_init_ids(struct ipc_ids* ids, int size)
60 {
61         int i;
62         sema_init(&ids->sem,1);
63
64         if(size > IPCMNI)
65                 size = IPCMNI;
66         ids->size = size;
67         ids->in_use = 0;
68         ids->max_id = -1;
69         ids->seq = 0;
70         {
71                 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
72                 if(seq_limit > USHRT_MAX)
73                         ids->seq_max = USHRT_MAX;
74                  else
75                         ids->seq_max = seq_limit;
76         }
77
78         ids->entries = ipc_rcu_alloc(sizeof(struct ipc_id)*size);
79
80         if(ids->entries == NULL) {
81                 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
82                 ids->size = 0;
83         }
84         for(i=0;i<ids->size;i++)
85                 ids->entries[i].p = NULL;
86 }
87
88 /**
89  *      ipc_findkey     -       find a key in an ipc identifier set     
90  *      @ids: Identifier set
91  *      @key: The key to find
92  *      
93  *      Requires ipc_ids.sem locked.
94  *      Returns the identifier if found or -1 if not.
95  */
96  
97 int ipc_findkey(struct ipc_ids* ids, key_t key)
98 {
99         int id;
100         struct kern_ipc_perm* p;
101         int max_id = ids->max_id;
102
103         /*
104          * read_barrier_depends is not needed here
105          * since ipc_ids.sem is held
106          */
107         for (id = 0; id <= max_id; id++) {
108                 p = ids->entries[id].p;
109                 if (p==NULL)
110                         continue;
111                 if (!vx_check(p->xid, VX_IDENT))
112                         continue;       
113                 if (key == p->key)
114                         return id;
115         }
116         return -1;
117 }
118
119 /*
120  * Requires ipc_ids.sem locked
121  */
122 static int grow_ary(struct ipc_ids* ids, int newsize)
123 {
124         struct ipc_id* new;
125         struct ipc_id* old;
126         int i;
127
128         if(newsize > IPCMNI)
129                 newsize = IPCMNI;
130         if(newsize <= ids->size)
131                 return newsize;
132
133         new = ipc_rcu_alloc(sizeof(struct ipc_id)*newsize);
134         if(new == NULL)
135                 return ids->size;
136         memcpy(new, ids->entries, sizeof(struct ipc_id)*ids->size);
137         for(i=ids->size;i<newsize;i++) {
138                 new[i].p = NULL;
139         }
140         old = ids->entries;
141         i = ids->size;
142
143         /*
144          * before setting the ids->entries to the new array, there must be a
145          * smp_wmb() to make sure the memcpyed contents of the new array are
146          * visible before the new array becomes visible.
147          */
148         smp_wmb();      /* prevent seeing new array uninitialized. */
149         ids->entries = new;
150         smp_wmb();      /* prevent indexing into old array based on new size. */
151         ids->size = newsize;
152
153         ipc_rcu_free(old, sizeof(struct ipc_id)*i);
154         return ids->size;
155 }
156
157 /**
158  *      ipc_addid       -       add an IPC identifier
159  *      @ids: IPC identifier set
160  *      @new: new IPC permission set
161  *      @size: new size limit for the id array
162  *
163  *      Add an entry 'new' to the IPC arrays. The permissions object is
164  *      initialised and the first free entry is set up and the id assigned
165  *      is returned. The list is returned in a locked state on success.
166  *      On failure the list is not locked and -1 is returned.
167  *
168  *      Called with ipc_ids.sem held.
169  */
170  
171 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
172 {
173         int id;
174
175         size = grow_ary(ids,size);
176
177         /*
178          * read_barrier_depends() is not needed here since
179          * ipc_ids.sem is held
180          */
181         for (id = 0; id < size; id++) {
182                 if(ids->entries[id].p == NULL)
183                         goto found;
184         }
185         return -1;
186 found:
187         ids->in_use++;
188         if (id > ids->max_id)
189                 ids->max_id = id;
190
191         new->cuid = new->uid = current->euid;
192         new->gid = new->cgid = current->egid;
193
194         new->seq = ids->seq++;
195         if(ids->seq > ids->seq_max)
196                 ids->seq = 0;
197
198         new->lock = SPIN_LOCK_UNLOCKED;
199         new->deleted = 0;
200         rcu_read_lock();
201         spin_lock(&new->lock);
202         ids->entries[id].p = new;
203         return id;
204 }
205
206 /**
207  *      ipc_rmid        -       remove an IPC identifier
208  *      @ids: identifier set
209  *      @id: Identifier to remove
210  *
211  *      The identifier must be valid, and in use. The kernel will panic if
212  *      fed an invalid identifier. The entry is removed and internal
213  *      variables recomputed. The object associated with the identifier
214  *      is returned.
215  *      ipc_ids.sem and the spinlock for this ID is hold before this function
216  *      is called, and remain locked on the exit.
217  */
218  
219 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
220 {
221         struct kern_ipc_perm* p;
222         int lid = id % SEQ_MULTIPLIER;
223         if(lid >= ids->size)
224                 BUG();
225
226         /* 
227          * do not need a read_barrier_depends() here to force ordering
228          * on Alpha, since the ipc_ids.sem is held.
229          */     
230         p = ids->entries[lid].p;
231         ids->entries[lid].p = NULL;
232         if(p==NULL)
233                 BUG();
234         ids->in_use--;
235
236         if (lid == ids->max_id) {
237                 do {
238                         lid--;
239                         if(lid == -1)
240                                 break;
241                 } while (ids->entries[lid].p == NULL);
242                 ids->max_id = lid;
243         }
244         p->deleted = 1;
245         return p;
246 }
247
248 /**
249  *      ipc_alloc       -       allocate ipc space
250  *      @size: size desired
251  *
252  *      Allocate memory from the appropriate pools and return a pointer to it.
253  *      NULL is returned if the allocation fails
254  */
255  
256 void* ipc_alloc(int size)
257 {
258         void* out;
259         if(size > PAGE_SIZE)
260                 out = vmalloc(size);
261         else
262                 out = kmalloc(size, GFP_KERNEL);
263         return out;
264 }
265
266 /**
267  *      ipc_free        -       free ipc space
268  *      @ptr: pointer returned by ipc_alloc
269  *      @size: size of block
270  *
271  *      Free a block created with ipc_alloc. The caller must know the size
272  *      used in the allocation call.
273  */
274
275 void ipc_free(void* ptr, int size)
276 {
277         if(size > PAGE_SIZE)
278                 vfree(ptr);
279         else
280                 kfree(ptr);
281 }
282
283 struct ipc_rcu_kmalloc
284 {
285         struct rcu_head rcu;
286         /* "void *" makes sure alignment of following data is sane. */
287         void *data[0];
288 };
289
290 struct ipc_rcu_vmalloc
291 {
292         struct rcu_head rcu;
293         struct work_struct work;
294         /* "void *" makes sure alignment of following data is sane. */
295         void *data[0];
296 };
297
298 static inline int rcu_use_vmalloc(int size)
299 {
300         /* Too big for a single page? */
301         if (sizeof(struct ipc_rcu_kmalloc) + size > PAGE_SIZE)
302                 return 1;
303         return 0;
304 }
305
306 /**
307  *      ipc_rcu_alloc   -       allocate ipc and rcu space 
308  *      @size: size desired
309  *
310  *      Allocate memory for the rcu header structure +  the object.
311  *      Returns the pointer to the object.
312  *      NULL is returned if the allocation fails. 
313  */
314  
315 void* ipc_rcu_alloc(int size)
316 {
317         void* out;
318         /* 
319          * We prepend the allocation with the rcu struct, and
320          * workqueue if necessary (for vmalloc). 
321          */
322         if (rcu_use_vmalloc(size)) {
323                 out = vmalloc(sizeof(struct ipc_rcu_vmalloc) + size);
324                 if (out) out += sizeof(struct ipc_rcu_vmalloc);
325         } else {
326                 out = kmalloc(sizeof(struct ipc_rcu_kmalloc)+size, GFP_KERNEL);
327                 if (out) out += sizeof(struct ipc_rcu_kmalloc);
328         }
329
330         return out;
331 }
332
333 /**
334  *      ipc_schedule_free       - free ipc + rcu space
335  * 
336  * Since RCU callback function is called in bh,
337  * we need to defer the vfree to schedule_work
338  */
339 static void ipc_schedule_free(void* arg)
340 {
341         struct ipc_rcu_vmalloc *free = arg;
342
343         INIT_WORK(&free->work, vfree, free);
344         schedule_work(&free->work);
345 }
346
347 void ipc_rcu_free(void* ptr, int size)
348 {
349         if (rcu_use_vmalloc(size)) {
350                 struct ipc_rcu_vmalloc *free;
351                 free = ptr - sizeof(*free);
352                 call_rcu(&free->rcu, ipc_schedule_free, free);
353         } else {
354                 struct ipc_rcu_kmalloc *free;
355                 free = ptr - sizeof(*free);
356                 /* kfree takes a "const void *" so gcc warns.  So we cast. */
357                 call_rcu(&free->rcu, (void (*)(void *))kfree, free);
358         }
359
360 }
361
362 /**
363  *      ipcperms        -       check IPC permissions
364  *      @ipcp: IPC permission set
365  *      @flag: desired permission set.
366  *
367  *      Check user, group, other permissions for access
368  *      to ipc resources. return 0 if allowed
369  */
370  
371 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
372 {       /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
373         int requested_mode, granted_mode;
374
375         if (!vx_check(ipcp->xid, VX_ADMIN|VX_IDENT)) /* maybe just VX_IDENT? */
376                 return -1;
377         requested_mode = (flag >> 6) | (flag >> 3) | flag;
378         granted_mode = ipcp->mode;
379         if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
380                 granted_mode >>= 6;
381         else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
382                 granted_mode >>= 3;
383         /* is there some bit set in requested_mode but not in granted_mode? */
384         if ((requested_mode & ~granted_mode & 0007) && 
385             !capable(CAP_IPC_OWNER))
386                 return -1;
387
388         return security_ipc_permission(ipcp, flag);
389 }
390
391 /*
392  * Functions to convert between the kern_ipc_perm structure and the
393  * old/new ipc_perm structures
394  */
395
396 /**
397  *      kernel_to_ipc64_perm    -       convert kernel ipc permissions to user
398  *      @in: kernel permissions
399  *      @out: new style IPC permissions
400  *
401  *      Turn the kernel object 'in' into a set of permissions descriptions
402  *      for returning to userspace (out).
403  */
404  
405
406 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
407 {
408         out->key        = in->key;
409         out->uid        = in->uid;
410         out->gid        = in->gid;
411         out->cuid       = in->cuid;
412         out->cgid       = in->cgid;
413         out->mode       = in->mode;
414         out->seq        = in->seq;
415 }
416
417 /**
418  *      ipc64_perm_to_ipc_perm  -       convert old ipc permissions to new
419  *      @in: new style IPC permissions
420  *      @out: old style IPC permissions
421  *
422  *      Turn the new style permissions object in into a compatibility
423  *      object and store it into the 'out' pointer.
424  */
425  
426 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
427 {
428         out->key        = in->key;
429         SET_UID(out->uid, in->uid);
430         SET_GID(out->gid, in->gid);
431         SET_UID(out->cuid, in->cuid);
432         SET_GID(out->cgid, in->cgid);
433         out->mode       = in->mode;
434         out->seq        = in->seq;
435 }
436
437 /*
438  * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
439  * is called with shm_ids.sem locked.  Since grow_ary() is also called with
440  * shm_ids.sem down(for Shared Memory), there is no need to add read 
441  * barriers here to gurantee the writes in grow_ary() are seen in order 
442  * here (for Alpha).
443  *
444  * However ipc_get() itself does not necessary require ipc_ids.sem down. So
445  * if in the future ipc_get() is used by other places without ipc_ids.sem
446  * down, then ipc_get() needs read memery barriers as ipc_lock() does.
447  */
448 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
449 {
450         struct kern_ipc_perm* out;
451         int lid = id % SEQ_MULTIPLIER;
452         if(lid >= ids->size)
453                 return NULL;
454         out = ids->entries[lid].p;
455         return out;
456 }
457
458 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
459 {
460         struct kern_ipc_perm* out;
461         int lid = id % SEQ_MULTIPLIER;
462         struct ipc_id* entries;
463
464         rcu_read_lock();
465         if(lid >= ids->size) {
466                 rcu_read_unlock();
467                 return NULL;
468         }
469
470         /* 
471          * Note: The following two read barriers are corresponding
472          * to the two write barriers in grow_ary(). They guarantee 
473          * the writes are seen in the same order on the read side. 
474          * smp_rmb() has effect on all CPUs.  read_barrier_depends() 
475          * is used if there are data dependency between two reads, and 
476          * has effect only on Alpha.
477          */
478         smp_rmb(); /* prevent indexing old array with new size */
479         entries = ids->entries;
480         read_barrier_depends(); /*prevent seeing new array unitialized */
481         out = entries[lid].p;
482         if(out == NULL) {
483                 rcu_read_unlock();
484                 return NULL;
485         }
486         spin_lock(&out->lock);
487         
488         /* ipc_rmid() may have already freed the ID while ipc_lock
489          * was spinning: here verify that the structure is still valid
490          */
491         if (out->deleted) {
492                 spin_unlock(&out->lock);
493                 rcu_read_unlock();
494                 return NULL;
495         }
496         return out;
497 }
498
499 void ipc_unlock(struct kern_ipc_perm* perm)
500 {
501         spin_unlock(&perm->lock);
502         rcu_read_unlock();
503 }
504
505 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
506 {
507         return SEQ_MULTIPLIER*seq + id;
508 }
509
510 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
511 {
512         if(uid/SEQ_MULTIPLIER != ipcp->seq)
513                 return 1;
514         return 0;
515 }
516
517 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
518
519
520 /**
521  *      ipc_parse_version       -       IPC call version
522  *      @cmd: pointer to command
523  *
524  *      Return IPC_64 for new style IPC and IPC_OLD for old style IPC. 
525  *      The cmd value is turned from an encoding command and version into
526  *      just the command code.
527  */
528  
529 int ipc_parse_version (int *cmd)
530 {
531         if (*cmd & IPC_64) {
532                 *cmd ^= IPC_64;
533                 return IPC_64;
534         } else {
535                 return IPC_OLD;
536         }
537 }
538
539 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */