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