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