Merge branch 'drm-patches' of ssh://master.kernel.org/pub/scm/linux/kernel/git/airlie...
[GitHub/mt8127/android_kernel_alcatel_ttab.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 <manfred@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 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
15 * Jun 2006 - namespaces ssupport
16 * OpenVZ, SWsoft Inc.
17 * Pavel Emelianov <xemul@openvz.org>
18 */
19
20 #include <linux/mm.h>
21 #include <linux/shm.h>
22 #include <linux/init.h>
23 #include <linux/msg.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/capability.h>
27 #include <linux/highuid.h>
28 #include <linux/security.h>
29 #include <linux/rcupdate.h>
30 #include <linux/workqueue.h>
31 #include <linux/seq_file.h>
32 #include <linux/proc_fs.h>
33 #include <linux/audit.h>
34 #include <linux/nsproxy.h>
35
36 #include <asm/unistd.h>
37
38 #include "util.h"
39
40 struct ipc_proc_iface {
41 const char *path;
42 const char *header;
43 int ids;
44 int (*show)(struct seq_file *, void *);
45 };
46
47 struct ipc_namespace init_ipc_ns = {
48 .kref = {
49 .refcount = ATOMIC_INIT(2),
50 },
51 };
52
53 static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
54 {
55 int err;
56 struct ipc_namespace *ns;
57
58 err = -ENOMEM;
59 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
60 if (ns == NULL)
61 goto err_mem;
62
63 err = sem_init_ns(ns);
64 if (err)
65 goto err_sem;
66 err = msg_init_ns(ns);
67 if (err)
68 goto err_msg;
69 err = shm_init_ns(ns);
70 if (err)
71 goto err_shm;
72
73 kref_init(&ns->kref);
74 return ns;
75
76 err_shm:
77 msg_exit_ns(ns);
78 err_msg:
79 sem_exit_ns(ns);
80 err_sem:
81 kfree(ns);
82 err_mem:
83 return ERR_PTR(err);
84 }
85
86 struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
87 {
88 struct ipc_namespace *new_ns;
89
90 BUG_ON(!ns);
91 get_ipc_ns(ns);
92
93 if (!(flags & CLONE_NEWIPC))
94 return ns;
95
96 new_ns = clone_ipc_ns(ns);
97
98 put_ipc_ns(ns);
99 return new_ns;
100 }
101
102 void free_ipc_ns(struct kref *kref)
103 {
104 struct ipc_namespace *ns;
105
106 ns = container_of(kref, struct ipc_namespace, kref);
107 sem_exit_ns(ns);
108 msg_exit_ns(ns);
109 shm_exit_ns(ns);
110 kfree(ns);
111 }
112
113 /**
114 * ipc_init - initialise IPC subsystem
115 *
116 * The various system5 IPC resources (semaphores, messages and shared
117 * memory) are initialised
118 */
119
120 static int __init ipc_init(void)
121 {
122 sem_init();
123 msg_init();
124 shm_init();
125 return 0;
126 }
127 __initcall(ipc_init);
128
129 /**
130 * ipc_init_ids - initialise IPC identifiers
131 * @ids: Identifier set
132 * @size: Number of identifiers
133 *
134 * Given a size for the ipc identifier range (limited below IPCMNI)
135 * set up the sequence range to use then allocate and initialise the
136 * array itself.
137 */
138
139 void ipc_init_ids(struct ipc_ids* ids, int size)
140 {
141 int i;
142
143 mutex_init(&ids->mutex);
144
145 if(size > IPCMNI)
146 size = IPCMNI;
147 ids->in_use = 0;
148 ids->max_id = -1;
149 ids->seq = 0;
150 {
151 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
152 if(seq_limit > USHRT_MAX)
153 ids->seq_max = USHRT_MAX;
154 else
155 ids->seq_max = seq_limit;
156 }
157
158 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
159 sizeof(struct ipc_id_ary));
160
161 if(ids->entries == NULL) {
162 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
163 size = 0;
164 ids->entries = &ids->nullentry;
165 }
166 ids->entries->size = size;
167 for(i=0;i<size;i++)
168 ids->entries->p[i] = NULL;
169 }
170
171 #ifdef CONFIG_PROC_FS
172 static const struct file_operations sysvipc_proc_fops;
173 /**
174 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
175 * @path: Path in procfs
176 * @header: Banner to be printed at the beginning of the file.
177 * @ids: ipc id table to iterate.
178 * @show: show routine.
179 */
180 void __init ipc_init_proc_interface(const char *path, const char *header,
181 int ids, int (*show)(struct seq_file *, void *))
182 {
183 struct proc_dir_entry *pde;
184 struct ipc_proc_iface *iface;
185
186 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
187 if (!iface)
188 return;
189 iface->path = path;
190 iface->header = header;
191 iface->ids = ids;
192 iface->show = show;
193
194 pde = create_proc_entry(path,
195 S_IRUGO, /* world readable */
196 NULL /* parent dir */);
197 if (pde) {
198 pde->data = iface;
199 pde->proc_fops = &sysvipc_proc_fops;
200 } else {
201 kfree(iface);
202 }
203 }
204 #endif
205
206 /**
207 * ipc_findkey - find a key in an ipc identifier set
208 * @ids: Identifier set
209 * @key: The key to find
210 *
211 * Requires ipc_ids.mutex locked.
212 * Returns the identifier if found or -1 if not.
213 */
214
215 int ipc_findkey(struct ipc_ids* ids, key_t key)
216 {
217 int id;
218 struct kern_ipc_perm* p;
219 int max_id = ids->max_id;
220
221 /*
222 * rcu_dereference() is not needed here
223 * since ipc_ids.mutex is held
224 */
225 for (id = 0; id <= max_id; id++) {
226 p = ids->entries->p[id];
227 if(p==NULL)
228 continue;
229 if (key == p->key)
230 return id;
231 }
232 return -1;
233 }
234
235 /*
236 * Requires ipc_ids.mutex locked
237 */
238 static int grow_ary(struct ipc_ids* ids, int newsize)
239 {
240 struct ipc_id_ary* new;
241 struct ipc_id_ary* old;
242 int i;
243 int size = ids->entries->size;
244
245 if(newsize > IPCMNI)
246 newsize = IPCMNI;
247 if(newsize <= size)
248 return newsize;
249
250 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
251 sizeof(struct ipc_id_ary));
252 if(new == NULL)
253 return size;
254 new->size = newsize;
255 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
256 for(i=size;i<newsize;i++) {
257 new->p[i] = NULL;
258 }
259 old = ids->entries;
260
261 /*
262 * Use rcu_assign_pointer() to make sure the memcpyed contents
263 * of the new array are visible before the new array becomes visible.
264 */
265 rcu_assign_pointer(ids->entries, new);
266
267 __ipc_fini_ids(ids, old);
268 return newsize;
269 }
270
271 /**
272 * ipc_addid - add an IPC identifier
273 * @ids: IPC identifier set
274 * @new: new IPC permission set
275 * @size: new size limit for the id array
276 *
277 * Add an entry 'new' to the IPC arrays. The permissions object is
278 * initialised and the first free entry is set up and the id assigned
279 * is returned. The list is returned in a locked state on success.
280 * On failure the list is not locked and -1 is returned.
281 *
282 * Called with ipc_ids.mutex held.
283 */
284
285 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
286 {
287 int id;
288
289 size = grow_ary(ids,size);
290
291 /*
292 * rcu_dereference()() is not needed here since
293 * ipc_ids.mutex is held
294 */
295 for (id = 0; id < size; id++) {
296 if(ids->entries->p[id] == NULL)
297 goto found;
298 }
299 return -1;
300 found:
301 ids->in_use++;
302 if (id > ids->max_id)
303 ids->max_id = id;
304
305 new->cuid = new->uid = current->euid;
306 new->gid = new->cgid = current->egid;
307
308 new->seq = ids->seq++;
309 if(ids->seq > ids->seq_max)
310 ids->seq = 0;
311
312 spin_lock_init(&new->lock);
313 new->deleted = 0;
314 rcu_read_lock();
315 spin_lock(&new->lock);
316 ids->entries->p[id] = new;
317 return id;
318 }
319
320 /**
321 * ipc_rmid - remove an IPC identifier
322 * @ids: identifier set
323 * @id: Identifier to remove
324 *
325 * The identifier must be valid, and in use. The kernel will panic if
326 * fed an invalid identifier. The entry is removed and internal
327 * variables recomputed. The object associated with the identifier
328 * is returned.
329 * ipc_ids.mutex and the spinlock for this ID is hold before this function
330 * is called, and remain locked on the exit.
331 */
332
333 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
334 {
335 struct kern_ipc_perm* p;
336 int lid = id % SEQ_MULTIPLIER;
337 BUG_ON(lid >= ids->entries->size);
338
339 /*
340 * do not need a rcu_dereference()() here to force ordering
341 * on Alpha, since the ipc_ids.mutex is held.
342 */
343 p = ids->entries->p[lid];
344 ids->entries->p[lid] = NULL;
345 BUG_ON(p==NULL);
346 ids->in_use--;
347
348 if (lid == ids->max_id) {
349 do {
350 lid--;
351 if(lid == -1)
352 break;
353 } while (ids->entries->p[lid] == NULL);
354 ids->max_id = lid;
355 }
356 p->deleted = 1;
357 return p;
358 }
359
360 /**
361 * ipc_alloc - allocate ipc space
362 * @size: size desired
363 *
364 * Allocate memory from the appropriate pools and return a pointer to it.
365 * NULL is returned if the allocation fails
366 */
367
368 void* ipc_alloc(int size)
369 {
370 void* out;
371 if(size > PAGE_SIZE)
372 out = vmalloc(size);
373 else
374 out = kmalloc(size, GFP_KERNEL);
375 return out;
376 }
377
378 /**
379 * ipc_free - free ipc space
380 * @ptr: pointer returned by ipc_alloc
381 * @size: size of block
382 *
383 * Free a block created with ipc_alloc(). The caller must know the size
384 * used in the allocation call.
385 */
386
387 void ipc_free(void* ptr, int size)
388 {
389 if(size > PAGE_SIZE)
390 vfree(ptr);
391 else
392 kfree(ptr);
393 }
394
395 /*
396 * rcu allocations:
397 * There are three headers that are prepended to the actual allocation:
398 * - during use: ipc_rcu_hdr.
399 * - during the rcu grace period: ipc_rcu_grace.
400 * - [only if vmalloc]: ipc_rcu_sched.
401 * Their lifetime doesn't overlap, thus the headers share the same memory.
402 * Unlike a normal union, they are right-aligned, thus some container_of
403 * forward/backward casting is necessary:
404 */
405 struct ipc_rcu_hdr
406 {
407 int refcount;
408 int is_vmalloc;
409 void *data[0];
410 };
411
412
413 struct ipc_rcu_grace
414 {
415 struct rcu_head rcu;
416 /* "void *" makes sure alignment of following data is sane. */
417 void *data[0];
418 };
419
420 struct ipc_rcu_sched
421 {
422 struct work_struct work;
423 /* "void *" makes sure alignment of following data is sane. */
424 void *data[0];
425 };
426
427 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
428 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
429 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
430 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
431
432 static inline int rcu_use_vmalloc(int size)
433 {
434 /* Too big for a single page? */
435 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
436 return 1;
437 return 0;
438 }
439
440 /**
441 * ipc_rcu_alloc - allocate ipc and rcu space
442 * @size: size desired
443 *
444 * Allocate memory for the rcu header structure + the object.
445 * Returns the pointer to the object.
446 * NULL is returned if the allocation fails.
447 */
448
449 void* ipc_rcu_alloc(int size)
450 {
451 void* out;
452 /*
453 * We prepend the allocation with the rcu struct, and
454 * workqueue if necessary (for vmalloc).
455 */
456 if (rcu_use_vmalloc(size)) {
457 out = vmalloc(HDRLEN_VMALLOC + size);
458 if (out) {
459 out += HDRLEN_VMALLOC;
460 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
461 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
462 }
463 } else {
464 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
465 if (out) {
466 out += HDRLEN_KMALLOC;
467 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
468 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
469 }
470 }
471
472 return out;
473 }
474
475 void ipc_rcu_getref(void *ptr)
476 {
477 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
478 }
479
480 static void ipc_do_vfree(struct work_struct *work)
481 {
482 vfree(container_of(work, struct ipc_rcu_sched, work));
483 }
484
485 /**
486 * ipc_schedule_free - free ipc + rcu space
487 * @head: RCU callback structure for queued work
488 *
489 * Since RCU callback function is called in bh,
490 * we need to defer the vfree to schedule_work().
491 */
492 static void ipc_schedule_free(struct rcu_head *head)
493 {
494 struct ipc_rcu_grace *grace =
495 container_of(head, struct ipc_rcu_grace, rcu);
496 struct ipc_rcu_sched *sched =
497 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
498
499 INIT_WORK(&sched->work, ipc_do_vfree);
500 schedule_work(&sched->work);
501 }
502
503 /**
504 * ipc_immediate_free - free ipc + rcu space
505 * @head: RCU callback structure that contains pointer to be freed
506 *
507 * Free from the RCU callback context.
508 */
509 static void ipc_immediate_free(struct rcu_head *head)
510 {
511 struct ipc_rcu_grace *free =
512 container_of(head, struct ipc_rcu_grace, rcu);
513 kfree(free);
514 }
515
516 void ipc_rcu_putref(void *ptr)
517 {
518 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
519 return;
520
521 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
522 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
523 ipc_schedule_free);
524 } else {
525 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
526 ipc_immediate_free);
527 }
528 }
529
530 /**
531 * ipcperms - check IPC permissions
532 * @ipcp: IPC permission set
533 * @flag: desired permission set.
534 *
535 * Check user, group, other permissions for access
536 * to ipc resources. return 0 if allowed
537 */
538
539 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
540 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
541 int requested_mode, granted_mode, err;
542
543 if (unlikely((err = audit_ipc_obj(ipcp))))
544 return err;
545 requested_mode = (flag >> 6) | (flag >> 3) | flag;
546 granted_mode = ipcp->mode;
547 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
548 granted_mode >>= 6;
549 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
550 granted_mode >>= 3;
551 /* is there some bit set in requested_mode but not in granted_mode? */
552 if ((requested_mode & ~granted_mode & 0007) &&
553 !capable(CAP_IPC_OWNER))
554 return -1;
555
556 return security_ipc_permission(ipcp, flag);
557 }
558
559 /*
560 * Functions to convert between the kern_ipc_perm structure and the
561 * old/new ipc_perm structures
562 */
563
564 /**
565 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
566 * @in: kernel permissions
567 * @out: new style IPC permissions
568 *
569 * Turn the kernel object @in into a set of permissions descriptions
570 * for returning to userspace (@out).
571 */
572
573
574 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
575 {
576 out->key = in->key;
577 out->uid = in->uid;
578 out->gid = in->gid;
579 out->cuid = in->cuid;
580 out->cgid = in->cgid;
581 out->mode = in->mode;
582 out->seq = in->seq;
583 }
584
585 /**
586 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
587 * @in: new style IPC permissions
588 * @out: old style IPC permissions
589 *
590 * Turn the new style permissions object @in into a compatibility
591 * object and store it into the @out pointer.
592 */
593
594 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
595 {
596 out->key = in->key;
597 SET_UID(out->uid, in->uid);
598 SET_GID(out->gid, in->gid);
599 SET_UID(out->cuid, in->cuid);
600 SET_GID(out->cgid, in->cgid);
601 out->mode = in->mode;
602 out->seq = in->seq;
603 }
604
605 /*
606 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
607 * is called with shm_ids.mutex locked. Since grow_ary() is also called with
608 * shm_ids.mutex down(for Shared Memory), there is no need to add read
609 * barriers here to gurantee the writes in grow_ary() are seen in order
610 * here (for Alpha).
611 *
612 * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
613 * if in the future ipc_get() is used by other places without ipc_ids.mutex
614 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
615 */
616 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
617 {
618 struct kern_ipc_perm* out;
619 int lid = id % SEQ_MULTIPLIER;
620 if(lid >= ids->entries->size)
621 return NULL;
622 out = ids->entries->p[lid];
623 return out;
624 }
625
626 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
627 {
628 struct kern_ipc_perm* out;
629 int lid = id % SEQ_MULTIPLIER;
630 struct ipc_id_ary* entries;
631
632 rcu_read_lock();
633 entries = rcu_dereference(ids->entries);
634 if(lid >= entries->size) {
635 rcu_read_unlock();
636 return NULL;
637 }
638 out = entries->p[lid];
639 if(out == NULL) {
640 rcu_read_unlock();
641 return NULL;
642 }
643 spin_lock(&out->lock);
644
645 /* ipc_rmid() may have already freed the ID while ipc_lock
646 * was spinning: here verify that the structure is still valid
647 */
648 if (out->deleted) {
649 spin_unlock(&out->lock);
650 rcu_read_unlock();
651 return NULL;
652 }
653 return out;
654 }
655
656 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
657 {
658 rcu_read_lock();
659 spin_lock(&perm->lock);
660 }
661
662 void ipc_unlock(struct kern_ipc_perm* perm)
663 {
664 spin_unlock(&perm->lock);
665 rcu_read_unlock();
666 }
667
668 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
669 {
670 return SEQ_MULTIPLIER*seq + id;
671 }
672
673 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
674 {
675 if(uid/SEQ_MULTIPLIER != ipcp->seq)
676 return 1;
677 return 0;
678 }
679
680 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
681
682
683 /**
684 * ipc_parse_version - IPC call version
685 * @cmd: pointer to command
686 *
687 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
688 * The @cmd value is turned from an encoding command and version into
689 * just the command code.
690 */
691
692 int ipc_parse_version (int *cmd)
693 {
694 if (*cmd & IPC_64) {
695 *cmd ^= IPC_64;
696 return IPC_64;
697 } else {
698 return IPC_OLD;
699 }
700 }
701
702 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
703
704 #ifdef CONFIG_PROC_FS
705 struct ipc_proc_iter {
706 struct ipc_namespace *ns;
707 struct ipc_proc_iface *iface;
708 };
709
710 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
711 {
712 struct ipc_proc_iter *iter = s->private;
713 struct ipc_proc_iface *iface = iter->iface;
714 struct kern_ipc_perm *ipc = it;
715 loff_t p;
716 struct ipc_ids *ids;
717
718 ids = iter->ns->ids[iface->ids];
719
720 /* If we had an ipc id locked before, unlock it */
721 if (ipc && ipc != SEQ_START_TOKEN)
722 ipc_unlock(ipc);
723
724 /*
725 * p = *pos - 1 (because id 0 starts at position 1)
726 * + 1 (because we increment the position by one)
727 */
728 for (p = *pos; p <= ids->max_id; p++) {
729 if ((ipc = ipc_lock(ids, p)) != NULL) {
730 *pos = p + 1;
731 return ipc;
732 }
733 }
734
735 /* Out of range - return NULL to terminate iteration */
736 return NULL;
737 }
738
739 /*
740 * File positions: pos 0 -> header, pos n -> ipc id + 1.
741 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
742 */
743 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
744 {
745 struct ipc_proc_iter *iter = s->private;
746 struct ipc_proc_iface *iface = iter->iface;
747 struct kern_ipc_perm *ipc;
748 loff_t p;
749 struct ipc_ids *ids;
750
751 ids = iter->ns->ids[iface->ids];
752
753 /*
754 * Take the lock - this will be released by the corresponding
755 * call to stop().
756 */
757 mutex_lock(&ids->mutex);
758
759 /* pos < 0 is invalid */
760 if (*pos < 0)
761 return NULL;
762
763 /* pos == 0 means header */
764 if (*pos == 0)
765 return SEQ_START_TOKEN;
766
767 /* Find the (pos-1)th ipc */
768 for (p = *pos - 1; p <= ids->max_id; p++) {
769 if ((ipc = ipc_lock(ids, p)) != NULL) {
770 *pos = p + 1;
771 return ipc;
772 }
773 }
774 return NULL;
775 }
776
777 static void sysvipc_proc_stop(struct seq_file *s, void *it)
778 {
779 struct kern_ipc_perm *ipc = it;
780 struct ipc_proc_iter *iter = s->private;
781 struct ipc_proc_iface *iface = iter->iface;
782 struct ipc_ids *ids;
783
784 /* If we had a locked segment, release it */
785 if (ipc && ipc != SEQ_START_TOKEN)
786 ipc_unlock(ipc);
787
788 ids = iter->ns->ids[iface->ids];
789 /* Release the lock we took in start() */
790 mutex_unlock(&ids->mutex);
791 }
792
793 static int sysvipc_proc_show(struct seq_file *s, void *it)
794 {
795 struct ipc_proc_iter *iter = s->private;
796 struct ipc_proc_iface *iface = iter->iface;
797
798 if (it == SEQ_START_TOKEN)
799 return seq_puts(s, iface->header);
800
801 return iface->show(s, it);
802 }
803
804 static struct seq_operations sysvipc_proc_seqops = {
805 .start = sysvipc_proc_start,
806 .stop = sysvipc_proc_stop,
807 .next = sysvipc_proc_next,
808 .show = sysvipc_proc_show,
809 };
810
811 static int sysvipc_proc_open(struct inode *inode, struct file *file)
812 {
813 int ret;
814 struct seq_file *seq;
815 struct ipc_proc_iter *iter;
816
817 ret = -ENOMEM;
818 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
819 if (!iter)
820 goto out;
821
822 ret = seq_open(file, &sysvipc_proc_seqops);
823 if (ret)
824 goto out_kfree;
825
826 seq = file->private_data;
827 seq->private = iter;
828
829 iter->iface = PDE(inode)->data;
830 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
831 out:
832 return ret;
833 out_kfree:
834 kfree(iter);
835 goto out;
836 }
837
838 static int sysvipc_proc_release(struct inode *inode, struct file *file)
839 {
840 struct seq_file *seq = file->private_data;
841 struct ipc_proc_iter *iter = seq->private;
842 put_ipc_ns(iter->ns);
843 return seq_release_private(inode, file);
844 }
845
846 static const struct file_operations sysvipc_proc_fops = {
847 .open = sysvipc_proc_open,
848 .read = seq_read,
849 .llseek = seq_lseek,
850 .release = sysvipc_proc_release,
851 };
852 #endif /* CONFIG_PROC_FS */