Skip to content

Commit 3e148c7

Browse files
Nadia DerbeyLinus Torvalds
authored andcommitted
fix idr_find() locking
This is a patch that fixes the way idr_find() used to be called in ipc_lock(): in all the paths that don't imply an update of the ipcs idr, it was called without the idr tree being locked. The changes are: . in ipc_ids, the mutex has been changed into a reader/writer semaphore. . ipc_lock() now takes the mutex as a reader during the idr_find(). . a new routine ipc_lock_down() has been defined: it doesn't take the mutex, assuming that it is being held by the caller. This is the routine that is now called in all the update paths. Signed-off-by: Nadia Derbey <Nadia.Derbey@bull.net> Acked-by: Jarek Poplawski <jarkao2@o2.pl> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f4566f0 commit 3e148c7

5 files changed

Lines changed: 198 additions & 70 deletions

File tree

‎ipc/msg.c‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include <linux/syscalls.h>
3535
#include <linux/audit.h>
3636
#include <linux/seq_file.h>
37-
#include <linux/mutex.h>
37+
#include <linux/rwsem.h>
3838
#include <linux/nsproxy.h>
3939

4040
#include <asm/current.h>
@@ -110,7 +110,7 @@ void msg_exit_ns(struct ipc_namespace *ns)
110110
int next_id;
111111
int total, in_use;
112112

113-
mutex_lock(&msg_ids(ns).mutex);
113+
down_write(&msg_ids(ns).rw_mutex);
114114

115115
in_use = msg_ids(ns).in_use;
116116

@@ -122,7 +122,8 @@ void msg_exit_ns(struct ipc_namespace *ns)
122122
freeque(ns, msq);
123123
total++;
124124
}
125-
mutex_unlock(&msg_ids(ns).mutex);
125+
126+
up_write(&msg_ids(ns).rw_mutex);
126127

127128
kfree(ns->ids[IPC_MSG_IDS]);
128129
ns->ids[IPC_MSG_IDS] = NULL;
@@ -136,6 +137,22 @@ void __init msg_init(void)
136137
IPC_MSG_IDS, sysvipc_msg_proc_show);
137138
}
138139

140+
/*
141+
* This routine is called in the paths where the rw_mutex is held to protect
142+
* access to the idr tree.
143+
*/
144+
static inline struct msg_queue *msg_lock_check_down(struct ipc_namespace *ns,
145+
int id)
146+
{
147+
struct kern_ipc_perm *ipcp = ipc_lock_check_down(&msg_ids(ns), id);
148+
149+
return container_of(ipcp, struct msg_queue, q_perm);
150+
}
151+
152+
/*
153+
* msg_lock_(check_) routines are called in the paths where the rw_mutex
154+
* is not held.
155+
*/
139156
static inline struct msg_queue *msg_lock(struct ipc_namespace *ns, int id)
140157
{
141158
struct kern_ipc_perm *ipcp = ipc_lock(&msg_ids(ns), id);
@@ -161,7 +178,7 @@ static inline void msg_rmid(struct ipc_namespace *ns, struct msg_queue *s)
161178
* @ns: namespace
162179
* @params: ptr to the structure that contains the key and msgflg
163180
*
164-
* Called with msg_ids.mutex held
181+
* Called with msg_ids.rw_mutex held (writer)
165182
*/
166183
static int newque(struct ipc_namespace *ns, struct ipc_params *params)
167184
{
@@ -260,8 +277,8 @@ static void expunge_all(struct msg_queue *msq, int res)
260277
* removes the message queue from message queue ID IDR, and cleans up all the
261278
* messages associated with this queue.
262279
*
263-
* msg_ids.mutex and the spinlock for this message queue are held
264-
* before freeque() is called. msg_ids.mutex remains locked on exit.
280+
* msg_ids.rw_mutex (writer) and the spinlock for this message queue are held
281+
* before freeque() is called. msg_ids.rw_mutex remains locked on exit.
265282
*/
266283
static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
267284
{
@@ -286,7 +303,7 @@ static void freeque(struct ipc_namespace *ns, struct msg_queue *msq)
286303
}
287304

288305
/*
289-
* Called with msg_ids.mutex and ipcp locked.
306+
* Called with msg_ids.rw_mutex and ipcp locked.
290307
*/
291308
static inline int msg_security(struct kern_ipc_perm *ipcp, int msgflg)
292309
{
@@ -444,7 +461,7 @@ asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
444461
msginfo.msgmnb = ns->msg_ctlmnb;
445462
msginfo.msgssz = MSGSSZ;
446463
msginfo.msgseg = MSGSEG;
447-
mutex_lock(&msg_ids(ns).mutex);
464+
down_read(&msg_ids(ns).rw_mutex);
448465
if (cmd == MSG_INFO) {
449466
msginfo.msgpool = msg_ids(ns).in_use;
450467
msginfo.msgmap = atomic_read(&msg_hdrs);
@@ -455,7 +472,7 @@ asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
455472
msginfo.msgtql = MSGTQL;
456473
}
457474
max_id = ipc_get_maxid(&msg_ids(ns));
458-
mutex_unlock(&msg_ids(ns).mutex);
475+
up_read(&msg_ids(ns).rw_mutex);
459476
if (copy_to_user(buf, &msginfo, sizeof(struct msginfo)))
460477
return -EFAULT;
461478
return (max_id < 0) ? 0 : max_id;
@@ -516,8 +533,8 @@ asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
516533
return -EINVAL;
517534
}
518535

519-
mutex_lock(&msg_ids(ns).mutex);
520-
msq = msg_lock_check(ns, msqid);
536+
down_write(&msg_ids(ns).rw_mutex);
537+
msq = msg_lock_check_down(ns, msqid);
521538
if (IS_ERR(msq)) {
522539
err = PTR_ERR(msq);
523540
goto out_up;
@@ -576,7 +593,7 @@ asmlinkage long sys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf)
576593
}
577594
err = 0;
578595
out_up:
579-
mutex_unlock(&msg_ids(ns).mutex);
596+
up_write(&msg_ids(ns).rw_mutex);
580597
return err;
581598
out_unlock_up:
582599
msg_unlock(msq);

‎ipc/sem.c‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
#include <linux/audit.h>
8181
#include <linux/capability.h>
8282
#include <linux/seq_file.h>
83-
#include <linux/mutex.h>
83+
#include <linux/rwsem.h>
8484
#include <linux/nsproxy.h>
8585

8686
#include <asm/uaccess.h>
@@ -148,7 +148,7 @@ void sem_exit_ns(struct ipc_namespace *ns)
148148
int next_id;
149149
int total, in_use;
150150

151-
mutex_lock(&sem_ids(ns).mutex);
151+
down_write(&sem_ids(ns).rw_mutex);
152152

153153
in_use = sem_ids(ns).in_use;
154154

@@ -160,7 +160,7 @@ void sem_exit_ns(struct ipc_namespace *ns)
160160
freeary(ns, sma);
161161
total++;
162162
}
163-
mutex_unlock(&sem_ids(ns).mutex);
163+
up_write(&sem_ids(ns).rw_mutex);
164164

165165
kfree(ns->ids[IPC_SEM_IDS]);
166166
ns->ids[IPC_SEM_IDS] = NULL;
@@ -174,6 +174,22 @@ void __init sem_init (void)
174174
IPC_SEM_IDS, sysvipc_sem_proc_show);
175175
}
176176

177+
/*
178+
* This routine is called in the paths where the rw_mutex is held to protect
179+
* access to the idr tree.
180+
*/
181+
static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
182+
int id)
183+
{
184+
struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
185+
186+
return container_of(ipcp, struct sem_array, sem_perm);
187+
}
188+
189+
/*
190+
* sem_lock_(check_) routines are called in the paths where the rw_mutex
191+
* is not held.
192+
*/
177193
static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
178194
{
179195
struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
@@ -233,7 +249,7 @@ static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
233249
* @ns: namespace
234250
* @params: ptr to the structure that contains key, semflg and nsems
235251
*
236-
* Called with sem_ids.mutex held
252+
* Called with sem_ids.rw_mutex held (as a writer)
237253
*/
238254

239255
static int newary(struct ipc_namespace *ns, struct ipc_params *params)
@@ -290,7 +306,7 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
290306

291307

292308
/*
293-
* Called with sem_ids.mutex and ipcp locked.
309+
* Called with sem_ids.rw_mutex and ipcp locked.
294310
*/
295311
static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
296312
{
@@ -301,7 +317,7 @@ static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
301317
}
302318

303319
/*
304-
* Called with sem_ids.mutex and ipcp locked.
320+
* Called with sem_ids.rw_mutex and ipcp locked.
305321
*/
306322
static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
307323
struct ipc_params *params)
@@ -528,9 +544,9 @@ static int count_semzcnt (struct sem_array * sma, ushort semnum)
528544
return semzcnt;
529545
}
530546

531-
/* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
532-
* the spinlock for this semaphore set hold. sem_ids.mutex remains locked
533-
* on exit.
547+
/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
548+
* as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
549+
* remains locked on exit.
534550
*/
535551
static void freeary(struct ipc_namespace *ns, struct sem_array *sma)
536552
{
@@ -615,7 +631,7 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
615631
seminfo.semmnu = SEMMNU;
616632
seminfo.semmap = SEMMAP;
617633
seminfo.semume = SEMUME;
618-
mutex_lock(&sem_ids(ns).mutex);
634+
down_read(&sem_ids(ns).rw_mutex);
619635
if (cmd == SEM_INFO) {
620636
seminfo.semusz = sem_ids(ns).in_use;
621637
seminfo.semaem = ns->used_sems;
@@ -624,7 +640,7 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
624640
seminfo.semaem = SEMAEM;
625641
}
626642
max_id = ipc_get_maxid(&sem_ids(ns));
627-
mutex_unlock(&sem_ids(ns).mutex);
643+
up_read(&sem_ids(ns).rw_mutex);
628644
if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
629645
return -EFAULT;
630646
return (max_id < 0) ? 0: max_id;
@@ -895,7 +911,7 @@ static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
895911
if(copy_semid_from_user (&setbuf, arg.buf, version))
896912
return -EFAULT;
897913
}
898-
sma = sem_lock_check(ns, semid);
914+
sma = sem_lock_check_down(ns, semid);
899915
if (IS_ERR(sma))
900916
return PTR_ERR(sma);
901917

@@ -976,9 +992,9 @@ asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
976992
return err;
977993
case IPC_RMID:
978994
case IPC_SET:
979-
mutex_lock(&sem_ids(ns).mutex);
995+
down_write(&sem_ids(ns).rw_mutex);
980996
err = semctl_down(ns,semid,semnum,cmd,version,arg);
981-
mutex_unlock(&sem_ids(ns).mutex);
997+
up_write(&sem_ids(ns).rw_mutex);
982998
return err;
983999
default:
9841000
return -EINVAL;

0 commit comments

Comments
 (0)