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