namespace.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 *create_ipc_ns(void)
  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. if (!(flags & CLONE_NEWIPC))
  43. return get_ipc_ns(ns);
  44. return create_ipc_ns();
  45. }
  46. /*
  47. * free_ipcs - free all ipcs of one type
  48. * @ns: the namespace to remove the ipcs from
  49. * @ids: the table of ipcs to free
  50. * @free: the function called to free each individual ipc
  51. *
  52. * Called for each kind of ipc when an ipc_namespace exits.
  53. */
  54. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  55. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  56. {
  57. struct kern_ipc_perm *perm;
  58. int next_id;
  59. int total, in_use;
  60. down_write(&ids->rw_mutex);
  61. in_use = ids->in_use;
  62. for (total = 0, next_id = 0; total < in_use; next_id++) {
  63. perm = idr_find(&ids->ipcs_idr, next_id);
  64. if (perm == NULL)
  65. continue;
  66. ipc_lock_by_ptr(perm);
  67. free(ns, perm);
  68. total++;
  69. }
  70. up_write(&ids->rw_mutex);
  71. }
  72. static void free_ipc_ns(struct ipc_namespace *ns)
  73. {
  74. /*
  75. * Unregistering the hotplug notifier at the beginning guarantees
  76. * that the ipc namespace won't be freed while we are inside the
  77. * callback routine. Since the blocking_notifier_chain_XXX routines
  78. * hold a rw lock on the notifier list, unregister_ipcns_notifier()
  79. * won't take the rw lock before blocking_notifier_call_chain() has
  80. * released the rd lock.
  81. */
  82. unregister_ipcns_notifier(ns);
  83. sem_exit_ns(ns);
  84. msg_exit_ns(ns);
  85. shm_exit_ns(ns);
  86. kfree(ns);
  87. atomic_dec(&nr_ipc_ns);
  88. /*
  89. * Do the ipcns removal notification after decrementing nr_ipc_ns in
  90. * order to have a correct value when recomputing msgmni.
  91. */
  92. ipcns_notify(IPCNS_REMOVED);
  93. }
  94. /*
  95. * put_ipc_ns - drop a reference to an ipc namespace.
  96. * @ns: the namespace to put
  97. *
  98. * If this is the last task in the namespace exiting, and
  99. * it is dropping the refcount to 0, then it can race with
  100. * a task in another ipc namespace but in a mounts namespace
  101. * which has this ipcns's mqueuefs mounted, doing some action
  102. * with one of the mqueuefs files. That can raise the refcount.
  103. * So dropping the refcount, and raising the refcount when
  104. * accessing it through the VFS, are protected with mq_lock.
  105. *
  106. * (Clearly, a task raising the refcount on its own ipc_ns
  107. * needn't take mq_lock since it can't race with the last task
  108. * in the ipcns exiting).
  109. */
  110. void put_ipc_ns(struct ipc_namespace *ns)
  111. {
  112. if (atomic_dec_and_lock(&ns->count, &mq_lock)) {
  113. mq_clear_sbinfo(ns);
  114. spin_unlock(&mq_lock);
  115. mq_put_mnt(ns);
  116. free_ipc_ns(ns);
  117. }
  118. }