Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / ipc / util.c
blobc6d0af6a033aaae4aa4d5fc0c6b86a8dcd0a831f
1 /*
2 * linux/ipc/util.c
3 * Copyright (C) 1992 Krishna Balasubramanian
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>
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>
28 #if defined(CONFIG_SYSVIPC)
30 #include "util.h"
32 /**
33 * ipc_init - initialise IPC subsystem
35 * The various system5 IPC resources (semaphores, messages and shared
36 * memory are initialised
39 void __init ipc_init (void)
41 sem_init();
42 msg_init();
43 shm_init();
44 return;
47 /**
48 * ipc_init_ids - initialise IPC identifiers
49 * @ids: Identifier set
50 * @size: Number of identifiers
52 * Given a size for the ipc identifier range (limited below IPCMNI)
53 * set up the sequence range to use then allocate and initialise the
54 * array itself.
57 void __init ipc_init_ids(struct ipc_ids* ids, int size)
59 int i;
60 sema_init(&ids->sem,1);
62 if(size > IPCMNI)
63 size = IPCMNI;
64 ids->size = size;
65 ids->in_use = 0;
66 ids->max_id = -1;
67 ids->seq = 0;
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;
76 ids->entries = ipc_rcu_alloc(sizeof(struct ipc_id)*size);
78 if(ids->entries == NULL) {
79 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
80 ids->size = 0;
82 for(i=0;i<ids->size;i++)
83 ids->entries[i].p = NULL;
86 /**
87 * ipc_findkey - find a key in an ipc identifier set
88 * @ids: Identifier set
89 * @key: The key to find
91 * Requires ipc_ids.sem locked.
92 * Returns the identifier if found or -1 if not.
95 int ipc_findkey(struct ipc_ids* ids, key_t key)
97 int id;
98 struct kern_ipc_perm* p;
99 int max_id = ids->max_id;
102 * read_barrier_depends is not needed here
103 * since ipc_ids.sem is held
105 for (id = 0; id <= max_id; id++) {
106 p = ids->entries[id].p;
107 if(p==NULL)
108 continue;
109 if (key == p->key)
110 return id;
112 return -1;
116 * Requires ipc_ids.sem locked
118 static int grow_ary(struct ipc_ids* ids, int newsize)
120 struct ipc_id* new;
121 struct ipc_id* old;
122 int i;
124 if(newsize > IPCMNI)
125 newsize = IPCMNI;
126 if(newsize <= ids->size)
127 return newsize;
129 new = ipc_rcu_alloc(sizeof(struct ipc_id)*newsize);
130 if(new == NULL)
131 return ids->size;
132 memcpy(new, ids->entries, sizeof(struct ipc_id)*ids->size);
133 for(i=ids->size;i<newsize;i++) {
134 new[i].p = NULL;
136 old = ids->entries;
137 i = ids->size;
140 * before setting the ids->entries to the new array, there must be a
141 * smp_wmb() to make sure the memcpyed contents of the new array are
142 * visible before the new array becomes visible.
144 smp_wmb(); /* prevent seeing new array uninitialized. */
145 ids->entries = new;
146 smp_wmb(); /* prevent indexing into old array based on new size. */
147 ids->size = newsize;
149 ipc_rcu_free(old, sizeof(struct ipc_id)*i);
150 return ids->size;
154 * ipc_addid - add an IPC identifier
155 * @ids: IPC identifier set
156 * @new: new IPC permission set
157 * @size: new size limit for the id array
159 * Add an entry 'new' to the IPC arrays. The permissions object is
160 * initialised and the first free entry is set up and the id assigned
161 * is returned. The list is returned in a locked state on success.
162 * On failure the list is not locked and -1 is returned.
164 * Called with ipc_ids.sem held.
167 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
169 int id;
171 size = grow_ary(ids,size);
174 * read_barrier_depends() is not needed here since
175 * ipc_ids.sem is held
177 for (id = 0; id < size; id++) {
178 if(ids->entries[id].p == NULL)
179 goto found;
181 return -1;
182 found:
183 ids->in_use++;
184 if (id > ids->max_id)
185 ids->max_id = id;
187 new->cuid = new->uid = current->euid;
188 new->gid = new->cgid = current->egid;
190 new->seq = ids->seq++;
191 if(ids->seq > ids->seq_max)
192 ids->seq = 0;
194 new->lock = SPIN_LOCK_UNLOCKED;
195 new->deleted = 0;
196 rcu_read_lock();
197 spin_lock(&new->lock);
198 ids->entries[id].p = new;
199 return id;
203 * ipc_rmid - remove an IPC identifier
204 * @ids: identifier set
205 * @id: Identifier to remove
207 * The identifier must be valid, and in use. The kernel will panic if
208 * fed an invalid identifier. The entry is removed and internal
209 * variables recomputed. The object associated with the identifier
210 * is returned.
211 * ipc_ids.sem and the spinlock for this ID is hold before this function
212 * is called, and remain locked on the exit.
215 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
217 struct kern_ipc_perm* p;
218 int lid = id % SEQ_MULTIPLIER;
219 if(lid >= ids->size)
220 BUG();
223 * do not need a read_barrier_depends() here to force ordering
224 * on Alpha, since the ipc_ids.sem is held.
226 p = ids->entries[lid].p;
227 ids->entries[lid].p = NULL;
228 if(p==NULL)
229 BUG();
230 ids->in_use--;
232 if (lid == ids->max_id) {
233 do {
234 lid--;
235 if(lid == -1)
236 break;
237 } while (ids->entries[lid].p == NULL);
238 ids->max_id = lid;
240 p->deleted = 1;
241 return p;
245 * ipc_alloc - allocate ipc space
246 * @size: size desired
248 * Allocate memory from the appropriate pools and return a pointer to it.
249 * NULL is returned if the allocation fails
252 void* ipc_alloc(int size)
254 void* out;
255 if(size > PAGE_SIZE)
256 out = vmalloc(size);
257 else
258 out = kmalloc(size, GFP_KERNEL);
259 return out;
263 * ipc_free - free ipc space
264 * @ptr: pointer returned by ipc_alloc
265 * @size: size of block
267 * Free a block created with ipc_alloc. The caller must know the size
268 * used in the allocation call.
271 void ipc_free(void* ptr, int size)
273 if(size > PAGE_SIZE)
274 vfree(ptr);
275 else
276 kfree(ptr);
279 struct ipc_rcu_kmalloc
281 struct rcu_head rcu;
282 /* "void *" makes sure alignment of following data is sane. */
283 void *data[0];
286 struct ipc_rcu_vmalloc
288 struct rcu_head rcu;
289 struct work_struct work;
290 /* "void *" makes sure alignment of following data is sane. */
291 void *data[0];
294 static inline int rcu_use_vmalloc(int size)
296 /* Too big for a single page? */
297 if (sizeof(struct ipc_rcu_kmalloc) + size > PAGE_SIZE)
298 return 1;
299 return 0;
303 * ipc_rcu_alloc - allocate ipc and rcu space
304 * @size: size desired
306 * Allocate memory for the rcu header structure + the object.
307 * Returns the pointer to the object.
308 * NULL is returned if the allocation fails.
311 void* ipc_rcu_alloc(int size)
313 void* out;
315 * We prepend the allocation with the rcu struct, and
316 * workqueue if necessary (for vmalloc).
318 if (rcu_use_vmalloc(size)) {
319 out = vmalloc(sizeof(struct ipc_rcu_vmalloc) + size);
320 if (out) out += sizeof(struct ipc_rcu_vmalloc);
321 } else {
322 out = kmalloc(sizeof(struct ipc_rcu_kmalloc)+size, GFP_KERNEL);
323 if (out) out += sizeof(struct ipc_rcu_kmalloc);
326 return out;
330 * ipc_schedule_free - free ipc + rcu space
332 * Since RCU callback function is called in bh,
333 * we need to defer the vfree to schedule_work
335 static void ipc_schedule_free(void* arg)
337 struct ipc_rcu_vmalloc *free = arg;
339 INIT_WORK(&free->work, vfree, free);
340 schedule_work(&free->work);
343 void ipc_rcu_free(void* ptr, int size)
345 if (rcu_use_vmalloc(size)) {
346 struct ipc_rcu_vmalloc *free;
347 free = ptr - sizeof(*free);
348 call_rcu(&free->rcu, ipc_schedule_free, free);
349 } else {
350 struct ipc_rcu_kmalloc *free;
351 free = ptr - sizeof(*free);
352 /* kfree takes a "const void *" so gcc warns. So we cast. */
353 call_rcu(&free->rcu, (void (*)(void *))kfree, free);
359 * ipcperms - check IPC permissions
360 * @ipcp: IPC permission set
361 * @flag: desired permission set.
363 * Check user, group, other permissions for access
364 * to ipc resources. return 0 if allowed
367 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
368 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
369 int requested_mode, granted_mode;
371 requested_mode = (flag >> 6) | (flag >> 3) | flag;
372 granted_mode = ipcp->mode;
373 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
374 granted_mode >>= 6;
375 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
376 granted_mode >>= 3;
377 /* is there some bit set in requested_mode but not in granted_mode? */
378 if ((requested_mode & ~granted_mode & 0007) &&
379 !capable(CAP_IPC_OWNER))
380 return -1;
382 return security_ipc_permission(ipcp, flag);
386 * Functions to convert between the kern_ipc_perm structure and the
387 * old/new ipc_perm structures
391 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
392 * @in: kernel permissions
393 * @out: new style IPC permissions
395 * Turn the kernel object 'in' into a set of permissions descriptions
396 * for returning to userspace (out).
400 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
402 out->key = in->key;
403 out->uid = in->uid;
404 out->gid = in->gid;
405 out->cuid = in->cuid;
406 out->cgid = in->cgid;
407 out->mode = in->mode;
408 out->seq = in->seq;
412 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
413 * @in: new style IPC permissions
414 * @out: old style IPC permissions
416 * Turn the new style permissions object in into a compatibility
417 * object and store it into the 'out' pointer.
420 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
422 out->key = in->key;
423 out->uid = NEW_TO_OLD_UID(in->uid);
424 out->gid = NEW_TO_OLD_GID(in->gid);
425 out->cuid = NEW_TO_OLD_UID(in->cuid);
426 out->cgid = NEW_TO_OLD_GID(in->cgid);
427 out->mode = in->mode;
428 out->seq = in->seq;
432 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
433 * is called with shm_ids.sem locked. Since grow_ary() is also called with
434 * shm_ids.sem down(for Shared Memory), there is no need to add read
435 * barriers here to gurantee the writes in grow_ary() are seen in order
436 * here (for Alpha).
438 * However ipc_get() itself does not necessary require ipc_ids.sem down. So
439 * if in the future ipc_get() is used by other places without ipc_ids.sem
440 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
442 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
444 struct kern_ipc_perm* out;
445 int lid = id % SEQ_MULTIPLIER;
446 if(lid >= ids->size)
447 return NULL;
448 out = ids->entries[lid].p;
449 return out;
452 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
454 struct kern_ipc_perm* out;
455 int lid = id % SEQ_MULTIPLIER;
456 struct ipc_id* entries;
458 rcu_read_lock();
459 if(lid >= ids->size) {
460 rcu_read_unlock();
461 return NULL;
465 * Note: The following two read barriers are corresponding
466 * to the two write barriers in grow_ary(). They guarantee
467 * the writes are seen in the same order on the read side.
468 * smp_rmb() has effect on all CPUs. read_barrier_depends()
469 * is used if there are data dependency between two reads, and
470 * has effect only on Alpha.
472 smp_rmb(); /* prevent indexing old array with new size */
473 entries = ids->entries;
474 read_barrier_depends(); /*prevent seeing new array unitialized */
475 out = entries[lid].p;
476 if(out == NULL) {
477 rcu_read_unlock();
478 return NULL;
480 spin_lock(&out->lock);
482 /* ipc_rmid() may have already freed the ID while ipc_lock
483 * was spinning: here verify that the structure is still valid
485 if (out->deleted) {
486 spin_unlock(&out->lock);
487 rcu_read_unlock();
488 return NULL;
490 return out;
493 void ipc_unlock(struct kern_ipc_perm* perm)
495 spin_unlock(&perm->lock);
496 rcu_read_unlock();
499 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
501 return SEQ_MULTIPLIER*seq + id;
504 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
506 if(uid/SEQ_MULTIPLIER != ipcp->seq)
507 return 1;
508 return 0;
511 #if !defined(__ia64__) && !defined(__x86_64__)
514 * ipc_parse_version - IPC call version
515 * @cmd: pointer to command
517 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
518 * The cmd value is turned from an encoding command and version into
519 * just the command code.
522 int ipc_parse_version (int *cmd)
524 if (*cmd & IPC_64) {
525 *cmd ^= IPC_64;
526 return IPC_64;
527 } else {
528 return IPC_OLD;
532 #endif /* __ia64__ */
534 #else
536 * Dummy functions when SYSV IPC isn't configured
539 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
541 return 0;
544 void exit_sem(struct task_struct *tsk)
546 return;
549 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
551 return -ENOSYS;
554 asmlinkage long sys_semop (int semid, struct sembuf *sops, unsigned nsops)
556 return -ENOSYS;
559 asmlinkage long sys_semtimedop(int semid, struct sembuf *sops, unsigned nsops,
560 const struct timespec *timeout)
562 return -ENOSYS;
566 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
568 return -ENOSYS;
571 asmlinkage long sys_msgget (key_t key, int msgflg)
573 return -ENOSYS;
576 asmlinkage long sys_msgsnd (int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg)
578 return -ENOSYS;
581 asmlinkage long sys_msgrcv (int msqid, struct msgbuf *msgp, size_t msgsz, long msgtyp,
582 int msgflg)
584 return -ENOSYS;
587 asmlinkage long sys_msgctl (int msqid, int cmd, struct msqid_ds *buf)
589 return -ENOSYS;
592 asmlinkage long sys_shmget (key_t key, size_t size, int shmflag)
594 return -ENOSYS;
597 asmlinkage long sys_shmat (int shmid, char *shmaddr, int shmflg, ulong *addr)
599 return -ENOSYS;
602 asmlinkage long sys_shmdt (char *shmaddr)
604 return -ENOSYS;
607 asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds *buf)
609 return -ENOSYS;
612 #endif /* CONFIG_SYSVIPC */