This commit was manufactured by cvs2svn to create tag
[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          * rcu_dereference() 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
144         /*
145          * before setting the ids->entries to the new array, there must be a
146          * smp_wmb() to make sure the memcpyed contents of the new array are
147          * visible before the new array becomes visible.
148          */
149         smp_wmb();      /* prevent seeing new array uninitialized. */
150         ids->entries = new;
151         smp_wmb();      /* prevent indexing into old array based on new size. */
152         ids->size = newsize;
153
154         ipc_rcu_putref(old);
155         return ids->size;
156 }
157
158 /**
159  *      ipc_addid       -       add an IPC identifier
160  *      @ids: IPC identifier set
161  *      @new: new IPC permission set
162  *      @size: new size limit for the id array
163  *
164  *      Add an entry 'new' to the IPC arrays. The permissions object is
165  *      initialised and the first free entry is set up and the id assigned
166  *      is returned. The list is returned in a locked state on success.
167  *      On failure the list is not locked and -1 is returned.
168  *
169  *      Called with ipc_ids.sem held.
170  */
171  
172 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
173 {
174         int id;
175
176         size = grow_ary(ids,size);
177
178         /*
179          * rcu_dereference()() is not needed here since
180          * ipc_ids.sem is held
181          */
182         for (id = 0; id < size; id++) {
183                 if(ids->entries[id].p == NULL)
184                         goto found;
185         }
186         return -1;
187 found:
188         ids->in_use++;
189         if (id > ids->max_id)
190                 ids->max_id = id;
191
192         new->cuid = new->uid = current->euid;
193         new->gid = new->cgid = current->egid;
194
195         new->seq = ids->seq++;
196         if(ids->seq > ids->seq_max)
197                 ids->seq = 0;
198
199         new->lock = SPIN_LOCK_UNLOCKED;
200         new->deleted = 0;
201         rcu_read_lock();
202         spin_lock(&new->lock);
203         ids->entries[id].p = new;
204         return id;
205 }
206
207 /**
208  *      ipc_rmid        -       remove an IPC identifier
209  *      @ids: identifier set
210  *      @id: Identifier to remove
211  *
212  *      The identifier must be valid, and in use. The kernel will panic if
213  *      fed an invalid identifier. The entry is removed and internal
214  *      variables recomputed. The object associated with the identifier
215  *      is returned.
216  *      ipc_ids.sem and the spinlock for this ID is hold before this function
217  *      is called, and remain locked on the exit.
218  */
219  
220 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
221 {
222         struct kern_ipc_perm* p;
223         int lid = id % SEQ_MULTIPLIER;
224         if(lid >= ids->size)
225                 BUG();
226
227         /* 
228          * do not need a rcu_dereference()() here to force ordering
229          * on Alpha, since the ipc_ids.sem is held.
230          */     
231         p = ids->entries[lid].p;
232         ids->entries[lid].p = NULL;
233         if(p==NULL)
234                 BUG();
235         ids->in_use--;
236
237         if (lid == ids->max_id) {
238                 do {
239                         lid--;
240                         if(lid == -1)
241                                 break;
242                 } while (ids->entries[lid].p == NULL);
243                 ids->max_id = lid;
244         }
245         p->deleted = 1;
246         return p;
247 }
248
249 /**
250  *      ipc_alloc       -       allocate ipc space
251  *      @size: size desired
252  *
253  *      Allocate memory from the appropriate pools and return a pointer to it.
254  *      NULL is returned if the allocation fails
255  */
256  
257 void* ipc_alloc(int size)
258 {
259         void* out;
260         if(size > PAGE_SIZE)
261                 out = vmalloc(size);
262         else
263                 out = kmalloc(size, GFP_KERNEL);
264         return out;
265 }
266
267 /**
268  *      ipc_free        -       free ipc space
269  *      @ptr: pointer returned by ipc_alloc
270  *      @size: size of block
271  *
272  *      Free a block created with ipc_alloc. The caller must know the size
273  *      used in the allocation call.
274  */
275
276 void ipc_free(void* ptr, int size)
277 {
278         if(size > PAGE_SIZE)
279                 vfree(ptr);
280         else
281                 kfree(ptr);
282 }
283
284 /*
285  * rcu allocations:
286  * There are three headers that are prepended to the actual allocation:
287  * - during use: ipc_rcu_hdr.
288  * - during the rcu grace period: ipc_rcu_grace.
289  * - [only if vmalloc]: ipc_rcu_sched.
290  * Their lifetime doesn't overlap, thus the headers share the same memory.
291  * Unlike a normal union, they are right-aligned, thus some container_of
292  * forward/backward casting is necessary:
293  */
294 struct ipc_rcu_hdr
295 {
296         int refcount;
297         int is_vmalloc;
298         void *data[0];
299 };
300
301
302 struct ipc_rcu_grace
303 {
304         struct rcu_head rcu;
305         /* "void *" makes sure alignment of following data is sane. */
306         void *data[0];
307 };
308
309 struct ipc_rcu_sched
310 {
311         struct work_struct work;
312         /* "void *" makes sure alignment of following data is sane. */
313         void *data[0];
314 };
315
316 #define HDRLEN_KMALLOC          (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
317                                         sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
318 #define HDRLEN_VMALLOC          (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
319                                         sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
320
321 static inline int rcu_use_vmalloc(int size)
322 {
323         /* Too big for a single page? */
324         if (HDRLEN_KMALLOC + size > PAGE_SIZE)
325                 return 1;
326         return 0;
327 }
328
329 /**
330  *      ipc_rcu_alloc   -       allocate ipc and rcu space 
331  *      @size: size desired
332  *
333  *      Allocate memory for the rcu header structure +  the object.
334  *      Returns the pointer to the object.
335  *      NULL is returned if the allocation fails. 
336  */
337  
338 void* ipc_rcu_alloc(int size)
339 {
340         void* out;
341         /* 
342          * We prepend the allocation with the rcu struct, and
343          * workqueue if necessary (for vmalloc). 
344          */
345         if (rcu_use_vmalloc(size)) {
346                 out = vmalloc(HDRLEN_VMALLOC + size);
347                 if (out) {
348                         out += HDRLEN_VMALLOC;
349                         container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
350                         container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
351                 }
352         } else {
353                 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
354                 if (out) {
355                         out += HDRLEN_KMALLOC;
356                         container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
357                         container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
358                 }
359         }
360
361         return out;
362 }
363
364 void ipc_rcu_getref(void *ptr)
365 {
366         container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
367 }
368
369 /**
370  *      ipc_schedule_free       - free ipc + rcu space
371  * 
372  * Since RCU callback function is called in bh,
373  * we need to defer the vfree to schedule_work
374  */
375 static void ipc_schedule_free(struct rcu_head *head)
376 {
377         struct ipc_rcu_grace *grace =
378                 container_of(head, struct ipc_rcu_grace, rcu);
379         struct ipc_rcu_sched *sched =
380                         container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
381
382         INIT_WORK(&sched->work, vfree, sched);
383         schedule_work(&sched->work);
384 }
385
386 /**
387  *      ipc_immediate_free      - free ipc + rcu space
388  *
389  *      Free from the RCU callback context
390  *
391  */
392 static void ipc_immediate_free(struct rcu_head *head)
393 {
394         struct ipc_rcu_grace *free =
395                 container_of(head, struct ipc_rcu_grace, rcu);
396         kfree(free);
397 }
398
399 void ipc_rcu_putref(void *ptr)
400 {
401         if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
402                 return;
403
404         if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
405                 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
406                                 ipc_schedule_free);
407         } else {
408                 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
409                                 ipc_immediate_free);
410         }
411 }
412
413 /**
414  *      ipcperms        -       check IPC permissions
415  *      @ipcp: IPC permission set
416  *      @flag: desired permission set.
417  *
418  *      Check user, group, other permissions for access
419  *      to ipc resources. return 0 if allowed
420  */
421  
422 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
423 {       /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
424         int requested_mode, granted_mode;
425
426         if (!vx_check(ipcp->xid, VX_ADMIN|VX_IDENT)) /* maybe just VX_IDENT? */
427                 return -1;
428         requested_mode = (flag >> 6) | (flag >> 3) | flag;
429         granted_mode = ipcp->mode;
430         if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
431                 granted_mode >>= 6;
432         else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
433                 granted_mode >>= 3;
434         /* is there some bit set in requested_mode but not in granted_mode? */
435         if ((requested_mode & ~granted_mode & 0007) && 
436             !capable(CAP_IPC_OWNER))
437                 return -1;
438
439         return security_ipc_permission(ipcp, flag);
440 }
441
442 /*
443  * Functions to convert between the kern_ipc_perm structure and the
444  * old/new ipc_perm structures
445  */
446
447 /**
448  *      kernel_to_ipc64_perm    -       convert kernel ipc permissions to user
449  *      @in: kernel permissions
450  *      @out: new style IPC permissions
451  *
452  *      Turn the kernel object 'in' into a set of permissions descriptions
453  *      for returning to userspace (out).
454  */
455  
456
457 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
458 {
459         out->key        = in->key;
460         out->uid        = in->uid;
461         out->gid        = in->gid;
462         out->cuid       = in->cuid;
463         out->cgid       = in->cgid;
464         out->mode       = in->mode;
465         out->seq        = in->seq;
466 }
467
468 /**
469  *      ipc64_perm_to_ipc_perm  -       convert old ipc permissions to new
470  *      @in: new style IPC permissions
471  *      @out: old style IPC permissions
472  *
473  *      Turn the new style permissions object in into a compatibility
474  *      object and store it into the 'out' pointer.
475  */
476  
477 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
478 {
479         out->key        = in->key;
480         SET_UID(out->uid, in->uid);
481         SET_GID(out->gid, in->gid);
482         SET_UID(out->cuid, in->cuid);
483         SET_GID(out->cgid, in->cgid);
484         out->mode       = in->mode;
485         out->seq        = in->seq;
486 }
487
488 /*
489  * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
490  * is called with shm_ids.sem locked.  Since grow_ary() is also called with
491  * shm_ids.sem down(for Shared Memory), there is no need to add read 
492  * barriers here to gurantee the writes in grow_ary() are seen in order 
493  * here (for Alpha).
494  *
495  * However ipc_get() itself does not necessary require ipc_ids.sem down. So
496  * if in the future ipc_get() is used by other places without ipc_ids.sem
497  * down, then ipc_get() needs read memery barriers as ipc_lock() does.
498  */
499 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
500 {
501         struct kern_ipc_perm* out;
502         int lid = id % SEQ_MULTIPLIER;
503         if(lid >= ids->size)
504                 return NULL;
505         out = ids->entries[lid].p;
506         return out;
507 }
508
509 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
510 {
511         struct kern_ipc_perm* out;
512         int lid = id % SEQ_MULTIPLIER;
513         struct ipc_id* entries;
514
515         rcu_read_lock();
516         if(lid >= ids->size) {
517                 rcu_read_unlock();
518                 return NULL;
519         }
520
521         /* 
522          * Note: The following two read barriers are corresponding
523          * to the two write barriers in grow_ary(). They guarantee 
524          * the writes are seen in the same order on the read side. 
525          * smp_rmb() has effect on all CPUs.  rcu_dereference()
526          * is used if there are data dependency between two reads, and 
527          * has effect only on Alpha.
528          */
529         smp_rmb(); /* prevent indexing old array with new size */
530         entries = rcu_dereference(ids->entries);
531         out = entries[lid].p;
532         if(out == NULL) {
533                 rcu_read_unlock();
534                 return NULL;
535         }
536         spin_lock(&out->lock);
537         
538         /* ipc_rmid() may have already freed the ID while ipc_lock
539          * was spinning: here verify that the structure is still valid
540          */
541         if (out->deleted) {
542                 spin_unlock(&out->lock);
543                 rcu_read_unlock();
544                 return NULL;
545         }
546         return out;
547 }
548
549 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
550 {
551         rcu_read_lock();
552         spin_lock(&perm->lock);
553 }
554
555 void ipc_unlock(struct kern_ipc_perm* perm)
556 {
557         spin_unlock(&perm->lock);
558         rcu_read_unlock();
559 }
560
561 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
562 {
563         return SEQ_MULTIPLIER*seq + id;
564 }
565
566 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
567 {
568         if(uid/SEQ_MULTIPLIER != ipcp->seq)
569                 return 1;
570         return 0;
571 }
572
573 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
574
575
576 /**
577  *      ipc_parse_version       -       IPC call version
578  *      @cmd: pointer to command
579  *
580  *      Return IPC_64 for new style IPC and IPC_OLD for old style IPC. 
581  *      The cmd value is turned from an encoding command and version into
582  *      just the command code.
583  */
584  
585 int ipc_parse_version (int *cmd)
586 {
587         if (*cmd & IPC_64) {
588                 *cmd ^= IPC_64;
589                 return IPC_64;
590         } else {
591                 return IPC_OLD;
592         }
593 }
594
595 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */