namespace.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * linux/ipc/namespace.c
  3. * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
  4. */
  5. #include <linux/ipc.h>
  6. #include <linux/msg.h>
  7. #include <linux/ipc_namespace.h>
  8. #include <linux/rcupdate.h>
  9. #include <linux/nsproxy.h>
  10. #include <linux/slab.h>
  11. #include <linux/fs.h>
  12. #include <linux/mount.h>
  13. #include "util.h"
  14. static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
  15. {
  16. struct ipc_namespace *ns;
  17. int err;
  18. ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
  19. if (ns == NULL)
  20. return ERR_PTR(-ENOMEM);
  21. atomic_set(&ns->count, 1);
  22. err = mq_init_ns(ns);
  23. if (err) {
  24. kfree(ns);
  25. return ERR_PTR(err);
  26. }
  27. atomic_inc(&nr_ipc_ns);
  28. sem_init_ns(ns);
  29. msg_init_ns(ns);
  30. shm_init_ns(ns);
  31. /*
  32. * msgmni has already been computed for the new ipc ns.
  33. * Thus, do the ipcns creation notification before registering that
  34. * new ipcns in the chain.
  35. */
  36. ipcns_notify(IPCNS_CREATED);
  37. register_ipcns_notifier(ns);
  38. return ns;
  39. }
  40. struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
  41. {
  42. struct ipc_namespace *new_ns;
  43. BUG_ON(!ns);
  44. get_ipc_ns(ns);
  45. if (!(flags & CLONE_NEWIPC))
  46. return ns;
  47. new_ns = clone_ipc_ns(ns);
  48. put_ipc_ns(ns);
  49. return new_ns;
  50. }
  51. /*
  52. * free_ipcs - free all ipcs of one type
  53. * @ns: the namespace to remove the ipcs from
  54. * @ids: the table of ipcs to free
  55. * @free: the function called to free each individual ipc
  56. *
  57. * Called for each kind of ipc when an ipc_namespace exits.
  58. */
  59. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  60. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  61. {
  62. struct kern_ipc_perm *perm;
  63. int next_id;
  64. int total, in_use;
  65. down_write(&ids->rw_mutex);
  66. in_use = ids->in_use;
  67. for (total = 0, next_id = 0; total < in_use; next_id++) {
  68. perm = idr_find(&ids->ipcs_idr, next_id);
  69. if (perm == NULL)
  70. continue;
  71. ipc_lock_by_ptr(perm);
  72. free(ns, perm);
  73. total++;
  74. }
  75. up_write(&ids->rw_mutex);
  76. }
  77. /*
  78. * put_ipc_ns - drop a reference to an ipc namespace.
  79. * @ns: the namespace to put
  80. *
  81. * If this is the last task in the namespace exiting, and
  82. * it is dropping the refcount to 0, then it can race with
  83. * a task in another ipc namespace but in a mounts namespace
  84. * which has this ipcns's mqueuefs mounted, doing some action
  85. * with one of the mqueuefs files. That can raise the refcount.
  86. * So dropping the refcount, and raising the refcount when
  87. * accessing it through the VFS, are protected with mq_lock.
  88. *
  89. * (Clearly, a task raising the refcount on its own ipc_ns
  90. * needn't take mq_lock since it can't race with the last task
  91. * in the ipcns exiting).
  92. */
  93. void put_ipc_ns(struct ipc_namespace *ns)
  94. {
  95. if (atomic_dec_and_lock(&ns->count, &mq_lock)) {
  96. mq_clear_sbinfo(ns);
  97. spin_unlock(&mq_lock);
  98. mq_put_mnt(ns);
  99. free_ipc_ns(ns);
  100. }
  101. }
  102. void free_ipc_ns(struct ipc_namespace *ns)
  103. {
  104. /*
  105. * Unregistering the hotplug notifier at the beginning guarantees
  106. * that the ipc namespace won't be freed while we are inside the
  107. * callback routine. Since the blocking_notifier_chain_XXX routines
  108. * hold a rw lock on the notifier list, unregister_ipcns_notifier()
  109. * won't take the rw lock before blocking_notifier_call_chain() has
  110. * released the rd lock.
  111. */
  112. unregister_ipcns_notifier(ns);
  113. sem_exit_ns(ns);
  114. msg_exit_ns(ns);
  115. shm_exit_ns(ns);
  116. kfree(ns);
  117. atomic_dec(&nr_ipc_ns);
  118. /*
  119. * Do the ipcns removal notification after decrementing nr_ipc_ns in
  120. * order to have a correct value when recomputing msgmni.
  121. */
  122. ipcns_notify(IPCNS_REMOVED);
  123. }