Skip to content

Commit 7ca7e56

Browse files
Nadia DerbeyLinus Torvalds
authored andcommitted
ipc: store ipcs into IDRs
This patch introduces ipcs storage into IDRs. The main changes are: . This ipc_ids structure is changed: the entries array is changed into a root idr structure. . The grow_ary() routine is removed: it is not needed anymore when adding an ipc structure, since we are now using the IDR facility. . The ipc_rmid() routine interface is changed: . there is no need for this routine to return the pointer passed in as argument: it is now declared as a void . since the id is now part of the kern_ipc_perm structure, no need to have it as an argument to the routine Signed-off-by: Nadia Derbey <Nadia.Derbey@bull.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d2b20b1 commit 7ca7e56

9 files changed

Lines changed: 331 additions & 313 deletions

File tree

‎include/linux/ipc.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct kern_ipc_perm
8989
{
9090
spinlock_t lock;
9191
int deleted;
92+
int id;
9293
key_t key;
9394
uid_t uid;
9495
gid_t gid;

‎include/linux/msg.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ struct msg_msg {
7777
/* one msq_queue structure for each present queue on the system */
7878
struct msg_queue {
7979
struct kern_ipc_perm q_perm;
80-
int q_id;
8180
time_t q_stime; /* last msgsnd time */
8281
time_t q_rtime; /* last msgrcv time */
8382
time_t q_ctime; /* last change time */

‎include/linux/sem.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ struct sem {
9090
/* One sem_array data structure for each set of semaphores in the system. */
9191
struct sem_array {
9292
struct kern_ipc_perm sem_perm; /* permissions .. see ipc.h */
93-
int sem_id;
9493
time_t sem_otime; /* last semop time */
9594
time_t sem_ctime; /* last change time */
9695
struct sem *sem_base; /* ptr to first semaphore in array */

‎include/linux/shm.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ struct shmid_kernel /* private to the kernel */
7979
{
8080
struct kern_ipc_perm shm_perm;
8181
struct file * shm_file;
82-
int id;
8382
unsigned long shm_nattch;
8483
unsigned long shm_segsz;
8584
time_t shm_atim;

‎ipc/msg.c‎

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ static struct ipc_ids init_msg_ids;
7575

7676
#define msg_lock(ns, id) ((struct msg_queue*)ipc_lock(&msg_ids(ns), id))
7777
#define msg_unlock(msq) ipc_unlock(&(msq)->q_perm)
78-
#define msg_rmid(ns, id) ((struct msg_queue*)ipc_rmid(&msg_ids(ns), id))
7978
#define msg_checkid(ns, msq, msgid) \
8079
ipc_checkid(&msg_ids(ns), &msq->q_perm, msgid)
8180
#define msg_buildid(ns, id, seq) \
8281
ipc_buildid(&msg_ids(ns), id, seq)
8382

84-
static void freeque (struct ipc_namespace *ns, struct msg_queue *msq, int id);
83+
static void freeque(struct ipc_namespace *, struct msg_queue *);
8584
static int newque (struct ipc_namespace *ns, key_t key, int msgflg);
8685
#ifdef CONFIG_PROC_FS
8786
static int sysvipc_msg_proc_show(struct seq_file *s, void *it);
@@ -93,7 +92,7 @@ static void __msg_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
9392
ns->msg_ctlmax = MSGMAX;
9493
ns->msg_ctlmnb = MSGMNB;
9594
ns->msg_ctlmni = MSGMNI;
96-
ipc_init_ids(ids, ns->msg_ctlmni);
95+
ipc_init_ids(ids);
9796
}
9897

9998
int msg_init_ns(struct ipc_namespace *ns)
@@ -110,20 +109,24 @@ int msg_init_ns(struct ipc_namespace *ns)
110109

111110
void msg_exit_ns(struct ipc_namespace *ns)
112111
{
113-
int i;
114112
struct msg_queue *msq;
113+
int next_id;
114+
int total, in_use;
115115

116116
mutex_lock(&msg_ids(ns).mutex);
117-
for (i = 0; i <= msg_ids(ns).max_id; i++) {
118-
msq = msg_lock(ns, i);
117+
118+
in_use = msg_ids(ns).in_use;
119+
120+
for (total = 0, next_id = 0; total < in_use; next_id++) {
121+
msq = idr_find(&msg_ids(ns).ipcs_idr, next_id);
119122
if (msq == NULL)
120123
continue;
121-
122-
freeque(ns, msq, i);
124+
ipc_lock_by_ptr(&msq->q_perm);
125+
freeque(ns, msq);
126+
total++;
123127
}
124128
mutex_unlock(&msg_ids(ns).mutex);
125129

126-
ipc_fini_ids(ns->ids[IPC_MSG_IDS]);
127130
kfree(ns->ids[IPC_MSG_IDS]);
128131
ns->ids[IPC_MSG_IDS] = NULL;
129132
}
@@ -136,6 +139,11 @@ void __init msg_init(void)
136139
IPC_MSG_IDS, sysvipc_msg_proc_show);
137140
}
138141

142+
static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
143+
{
144+
ipc_rmid(&msg_ids(ns), &s->q_perm);
145+
}
146+
139147
static int newque (struct ipc_namespace *ns, key_t key, int msgflg)
140148
{
141149
struct msg_queue *msq;
@@ -155,14 +163,17 @@ static int newque (struct ipc_namespace *ns, key_t key, int msgflg)
155163
return retval;
156164
}
157165

166+
/*
167+
* ipc_addid() locks msq
168+
*/
158169
id = ipc_addid(&msg_ids(ns), &msq->q_perm, ns->msg_ctlmni);
159170
if (id == -1) {
160171
security_msg_queue_free(msq);
161172
ipc_rcu_putref(msq);
162173
return -ENOSPC;
163174
}
164175

165-
msq->q_id = msg_buildid(ns, id, msq->q_perm.seq);
176+
msq->q_perm.id = msg_buildid(ns, id, msq->q_perm.seq);
166177
msq->q_stime = msq->q_rtime = 0;
167178
msq->q_ctime = get_seconds();
168179
msq->q_cbytes = msq->q_qnum = 0;
@@ -171,9 +182,10 @@ static int newque (struct ipc_namespace *ns, key_t key, int msgflg)
171182
INIT_LIST_HEAD(&msq->q_messages);
172183
INIT_LIST_HEAD(&msq->q_receivers);
173184
INIT_LIST_HEAD(&msq->q_senders);
185+
174186
msg_unlock(msq);
175187

176-
return msq->q_id;
188+
return msq->q_perm.id;
177189
}
178190

179191
static inline void ss_add(struct msg_queue *msq, struct msg_sender *mss)
@@ -225,18 +237,18 @@ static void expunge_all(struct msg_queue *msq, int res)
225237
/*
226238
* freeque() wakes up waiters on the sender and receiver waiting queue,
227239
* removes the message queue from message queue ID
228-
* array, and cleans up all the messages associated with this queue.
240+
* IDR, and cleans up all the messages associated with this queue.
229241
*
230-
* msg_ids.mutex and the spinlock for this message queue is hold
242+
* msg_ids.mutex and the spinlock for this message queue are held
231243
* before freeque() is called. msg_ids.mutex remains locked on exit.
232244
*/
233-
static void freeque(struct ipc_namespace *ns, struct msg_queue *msq, int id)
245+
static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
234246
{
235247
struct list_head *tmp;
236248

237249
expunge_all(msq, -EIDRM);
238250
ss_wakeup(&msq->q_senders, 1);
239-
msq = msg_rmid(ns, id);
251+
msg_rmid(ns, msq);
240252
msg_unlock(msq);
241253

242254
tmp = msq->q_messages.next;
@@ -255,36 +267,51 @@ static void freeque(struct ipc_namespace *ns, struct msg_queue *msq, int id)
255267
asmlinkage long sys_msgget(key_t key, int msgflg)
256268
{
257269
struct msg_queue *msq;
258-
int id, ret = -EPERM;
270+
int ret;
259271
struct ipc_namespace *ns;
260272

261273
ns = current->nsproxy->ipc_ns;
262-
263-
mutex_lock(&msg_ids(ns).mutex);
264-
if (key == IPC_PRIVATE)
265-
ret = newque(ns, key, msgflg);
266-
else if ((id = ipc_findkey(&msg_ids(ns), key)) == -1) { /* key not used */
267-
if (!(msgflg & IPC_CREAT))
268-
ret = -ENOENT;
269-
else
274+
275+
ret = idr_pre_get(&msg_ids(ns).ipcs_idr, GFP_KERNEL);
276+
277+
if (key == IPC_PRIVATE) {
278+
if (!ret)
279+
ret = -ENOMEM;
280+
else {
281+
mutex_lock(&msg_ids(ns).mutex);
270282
ret = newque(ns, key, msgflg);
271-
} else if (msgflg & IPC_CREAT && msgflg & IPC_EXCL) {
272-
ret = -EEXIST;
283+
mutex_unlock(&msg_ids(ns).mutex);
284+
}
273285
} else {
274-
msq = msg_lock(ns, id);
275-
BUG_ON(msq == NULL);
276-
if (ipcperms(&msq->q_perm, msgflg))
277-
ret = -EACCES;
278-
else {
279-
int qid = msg_buildid(ns, id, msq->q_perm.seq);
280-
281-
ret = security_msg_queue_associate(msq, msgflg);
282-
if (!ret)
283-
ret = qid;
286+
mutex_lock(&msg_ids(ns).mutex);
287+
msq = (struct msg_queue *) ipc_findkey(&msg_ids(ns), key);
288+
if (msq == NULL) {
289+
/* key not used */
290+
if (!(msgflg & IPC_CREAT))
291+
ret = -ENOENT;
292+
else if (!ret)
293+
ret = -ENOMEM;
294+
else
295+
ret = newque(ns, key, msgflg);
296+
} else {
297+
/* msq has been locked by ipc_findkey() */
298+
299+
if (msgflg & IPC_CREAT && msgflg & IPC_EXCL)
300+
ret = -EEXIST;
301+
else {
302+
if (ipcperms(&msq->q_perm, msgflg))
303+
ret = -EACCES;
304+
else {
305+
ret = security_msg_queue_associate(
306+
msq, msgflg);
307+
if (!ret)
308+
ret = msq->q_perm.id;
309+
}
310+
}
311+
msg_unlock(msq);
284312
}
285-
msg_unlock(msq);
313+
mutex_unlock(&msg_ids(ns).mutex);
286314
}
287-
mutex_unlock(&msg_ids(ns).mutex);
288315

289316
return ret;
290317
}
@@ -430,22 +457,20 @@ asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
430457
msginfo.msgpool = MSGPOOL;
431458
msginfo.msgtql = MSGTQL;
432459
}
433-
max_id = msg_ids(ns).max_id;
460+
max_id = ipc_get_maxid(&msg_ids(ns));
434461
mutex_unlock(&msg_ids(ns).mutex);
435462
if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
436463
return -EFAULT;
437464
return (max_id < 0) ? 0 : max_id;
438465
}
439-
case MSG_STAT:
466+
case MSG_STAT: /* msqid is an index rather than a msg queue id */
440467
case IPC_STAT:
441468
{
442469
struct msqid64_ds tbuf;
443470
int success_return;
444471

445472
if (!buf)
446473
return -EFAULT;
447-
if (cmd == MSG_STAT && msqid >= msg_ids(ns).entries->size)
448-
return -EINVAL;
449474

450475
memset(&tbuf, 0, sizeof(tbuf));
451476

@@ -454,7 +479,7 @@ asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
454479
return -EINVAL;
455480

456481
if (cmd == MSG_STAT) {
457-
success_return = msg_buildid(ns, msqid, msq->q_perm.seq);
482+
success_return = msq->q_perm.id;
458483
} else {
459484
err = -EIDRM;
460485
if (msg_checkid(ns, msq, msqid))
@@ -552,7 +577,7 @@ asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
552577
break;
553578
}
554579
case IPC_RMID:
555-
freeque(ns, msq, msqid);
580+
freeque(ns, msq);
556581
break;
557582
}
558583
err = 0;
@@ -926,7 +951,7 @@ static int sysvipc_msg_proc_show(struct seq_file *s, void *it)
926951
return seq_printf(s,
927952
"%10d %10d %4o %10lu %10lu %5u %5u %5u %5u %5u %5u %10lu %10lu %10lu\n",
928953
msq->q_perm.key,
929-
msq->q_id,
954+
msq->q_perm.id,
930955
msq->q_perm.mode,
931956
msq->q_cbytes,
932957
msq->q_qnum,

0 commit comments

Comments
 (0)