util.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * linux/ipc/util.h
  3. * Copyright (C) 1999 Christoph Rohland
  4. *
  5. * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
  6. * namespaces support. 2006 OpenVZ, SWsoft Inc.
  7. * Pavel Emelianov <xemul@openvz.org>
  8. */
  9. #ifndef _IPC_UTIL_H
  10. #define _IPC_UTIL_H
  11. #include <linux/unistd.h>
  12. #include <linux/err.h>
  13. #define SEQ_MULTIPLIER (IPCMNI)
  14. void sem_init(void);
  15. void msg_init(void);
  16. void shm_init(void);
  17. struct ipc_namespace;
  18. #ifdef CONFIG_POSIX_MQUEUE
  19. extern void mq_clear_sbinfo(struct ipc_namespace *ns);
  20. extern void mq_put_mnt(struct ipc_namespace *ns);
  21. #else
  22. static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
  23. static inline void mq_put_mnt(struct ipc_namespace *ns) { }
  24. #endif
  25. #ifdef CONFIG_SYSVIPC
  26. void sem_init_ns(struct ipc_namespace *ns);
  27. void msg_init_ns(struct ipc_namespace *ns);
  28. void shm_init_ns(struct ipc_namespace *ns);
  29. void sem_exit_ns(struct ipc_namespace *ns);
  30. void msg_exit_ns(struct ipc_namespace *ns);
  31. void shm_exit_ns(struct ipc_namespace *ns);
  32. #else
  33. static inline void sem_init_ns(struct ipc_namespace *ns) { }
  34. static inline void msg_init_ns(struct ipc_namespace *ns) { }
  35. static inline void shm_init_ns(struct ipc_namespace *ns) { }
  36. static inline void sem_exit_ns(struct ipc_namespace *ns) { }
  37. static inline void msg_exit_ns(struct ipc_namespace *ns) { }
  38. static inline void shm_exit_ns(struct ipc_namespace *ns) { }
  39. #endif
  40. struct ipc_rcu {
  41. struct rcu_head rcu;
  42. atomic_t refcount;
  43. } ____cacheline_aligned_in_smp;
  44. #define ipc_rcu_to_struct(p) ((void *)(p+1))
  45. /*
  46. * Structure that holds the parameters needed by the ipc operations
  47. * (see after)
  48. */
  49. struct ipc_params {
  50. key_t key;
  51. int flg;
  52. union {
  53. size_t size; /* for shared memories */
  54. int nsems; /* for semaphores */
  55. } u; /* holds the getnew() specific param */
  56. };
  57. /*
  58. * Structure that holds some ipc operations. This structure is used to unify
  59. * the calls to sys_msgget(), sys_semget(), sys_shmget()
  60. * . routine to call to create a new ipc object. Can be one of newque,
  61. * newary, newseg
  62. * . routine to call to check permissions for a new ipc object.
  63. * Can be one of security_msg_associate, security_sem_associate,
  64. * security_shm_associate
  65. * . routine to call for an extra check if needed
  66. */
  67. struct ipc_ops {
  68. int (*getnew)(struct ipc_namespace *, struct ipc_params *);
  69. int (*associate)(struct kern_ipc_perm *, int);
  70. int (*more_checks)(struct kern_ipc_perm *, struct ipc_params *);
  71. };
  72. struct seq_file;
  73. struct ipc_ids;
  74. void ipc_init_ids(struct ipc_ids *);
  75. #ifdef CONFIG_PROC_FS
  76. void __init ipc_init_proc_interface(const char *path, const char *header,
  77. int ids, int (*show)(struct seq_file *, void *));
  78. #else
  79. #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
  80. #endif
  81. #define IPC_SEM_IDS 0
  82. #define IPC_MSG_IDS 1
  83. #define IPC_SHM_IDS 2
  84. #define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
  85. #define ipcid_to_seqx(id) ((id) / SEQ_MULTIPLIER)
  86. #define IPCID_SEQ_MAX min_t(int, INT_MAX/SEQ_MULTIPLIER, USHRT_MAX)
  87. /* must be called with ids->rwsem acquired for writing */
  88. int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
  89. /* must be called with ids->rwsem acquired for reading */
  90. int ipc_get_maxid(struct ipc_ids *);
  91. /* must be called with both locks acquired. */
  92. void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
  93. /* must be called with ipcp locked */
  94. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
  95. /* for rare, potentially huge allocations.
  96. * both function can sleep
  97. */
  98. void *ipc_alloc(int size);
  99. void ipc_free(void *ptr);
  100. /*
  101. * For allocation that need to be freed by RCU.
  102. * Objects are reference counted, they start with reference count 1.
  103. * getref increases the refcount, the putref call that reduces the recount
  104. * to 0 schedules the rcu destruction. Caller must guarantee locking.
  105. */
  106. void *ipc_rcu_alloc(int size);
  107. int ipc_rcu_getref(void *ptr);
  108. void ipc_rcu_putref(void *ptr, void (*func)(struct rcu_head *head));
  109. void ipc_rcu_free(struct rcu_head *head);
  110. struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
  111. struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
  112. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
  113. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
  114. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
  115. struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
  116. struct ipc_ids *ids, int id, int cmd,
  117. struct ipc64_perm *perm, int extra_perm);
  118. #ifndef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  119. /* On IA-64, we always use the "64-bit version" of the IPC structures. */
  120. # define ipc_parse_version(cmd) IPC_64
  121. #else
  122. int ipc_parse_version(int *cmd);
  123. #endif
  124. extern void free_msg(struct msg_msg *msg);
  125. extern struct msg_msg *load_msg(const void __user *src, size_t len);
  126. extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
  127. extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len);
  128. extern void recompute_msgmni(struct ipc_namespace *);
  129. static inline int ipc_buildid(int id, int seq)
  130. {
  131. return SEQ_MULTIPLIER * seq + id;
  132. }
  133. static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid)
  134. {
  135. return uid / SEQ_MULTIPLIER != ipcp->seq;
  136. }
  137. static inline void ipc_lock_object(struct kern_ipc_perm *perm)
  138. {
  139. spin_lock(&perm->lock);
  140. }
  141. static inline void ipc_unlock_object(struct kern_ipc_perm *perm)
  142. {
  143. spin_unlock(&perm->lock);
  144. }
  145. static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm)
  146. {
  147. assert_spin_locked(&perm->lock);
  148. }
  149. static inline void ipc_unlock(struct kern_ipc_perm *perm)
  150. {
  151. ipc_unlock_object(perm);
  152. rcu_read_unlock();
  153. }
  154. /*
  155. * ipc_valid_object() - helper to sort out IPC_RMID races for codepaths
  156. * where the respective ipc_ids.rwsem is not being held down.
  157. * Checks whether the ipc object is still around or if it's gone already, as
  158. * ipc_rmid() may have already freed the ID while the ipc lock was spinning.
  159. * Needs to be called with kern_ipc_perm.lock held -- exception made for one
  160. * checkpoint case at sys_semtimedop() as noted in code commentary.
  161. */
  162. static inline bool ipc_valid_object(struct kern_ipc_perm *perm)
  163. {
  164. return !perm->deleted;
  165. }
  166. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
  167. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  168. const struct ipc_ops *ops, struct ipc_params *params);
  169. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  170. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
  171. #endif