Skip to content

Commit 823e75f

Browse files
committed
Merge branch 'ipc-scalability'
Merge IPC cleanup and scalability patches from Andrew Morton. This cleans up many of the oddities in the IPC code, uses the list iterator helpers, splits out locking and adds per-semaphore locks for greater scalability of the IPC semaphore code. Most normal user-level locking by now uses futexes (ie pthreads, but also a lot of specialized locks), but SysV IPC semaphores are apparently still used in some big applications, either for portability reasons, or because they offer tracking and undo (and you don't need to have a special shared memory area for them). Our IPC semaphore scalability was pitiful. We used to lock much too big ranges, and we used to have a single ipc lock per ipc semaphore array. Most loads never cared, but some do. There are some numbers in the individual commits. * ipc-scalability: ipc: sysv shared memory limited to 8TiB ipc/msg.c: use list_for_each_entry_[safe] for list traversing ipc,sem: fine grained locking for semtimedop ipc,sem: have only one list in struct sem_queue ipc,sem: open code and rename sem_lock ipc,sem: do not hold ipc lock more than necessary ipc: introduce lockless pre_down ipcctl ipc: introduce obtaining a lockless ipc object ipc: remove bogus lock comment for ipc_checkid ipc/msgutil.c: use linux/uaccess.h ipc: refactor msg list search into separate function ipc: simplify msg list search ipc: implement MSG_COPY as a new receive mode ipc: remove msg handling from queue scan ipc: set EFAULT as default error in load_msg() ipc: tighten msg copy loops ipc: separate msg allocation from userspace copy ipc: clamp with min()
2 parents 149b306 + d69f3ba commit 823e75f

7 files changed

Lines changed: 540 additions & 341 deletions

File tree

‎include/linux/ipc_namespace.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ struct ipc_namespace {
4343

4444
size_t shm_ctlmax;
4545
size_t shm_ctlall;
46+
unsigned long shm_tot;
4647
int shm_ctlmni;
47-
int shm_tot;
4848
/*
4949
* Defines whether IPC_RMID is forced for _all_ shm segments regardless
5050
* of shmctl()

‎ipc/msg.c‎

Lines changed: 50 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct msg_sender {
6666
#define SEARCH_EQUAL 2
6767
#define SEARCH_NOTEQUAL 3
6868
#define SEARCH_LESSEQUAL 4
69+
#define SEARCH_NUMBER 5
6970

7071
#define msg_ids(ns) ((ns)->ids[IPC_MSG_IDS])
7172

@@ -237,14 +238,9 @@ static inline void ss_del(struct msg_sender *mss)
237238

238239
static void ss_wakeup(struct list_head *h, int kill)
239240
{
240-
struct list_head *tmp;
241+
struct msg_sender *mss, *t;
241242

242-
tmp = h->next;
243-
while (tmp != h) {
244-
struct msg_sender *mss;
245-
246-
mss = list_entry(tmp, struct msg_sender, list);
247-
tmp = tmp->next;
243+
list_for_each_entry_safe(mss, t, h, list) {
248244
if (kill)
249245
mss->list.next = NULL;
250246
wake_up_process(mss->tsk);
@@ -253,14 +249,9 @@ static void ss_wakeup(struct list_head *h, int kill)
253249

254250
static void expunge_all(struct msg_queue *msq, int res)
255251
{
256-
struct list_head *tmp;
257-
258-
tmp = msq->q_receivers.next;
259-
while (tmp != &msq->q_receivers) {
260-
struct msg_receiver *msr;
252+
struct msg_receiver *msr, *t;
261253

262-
msr = list_entry(tmp, struct msg_receiver, r_list);
263-
tmp = tmp->next;
254+
list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
264255
msr->r_msg = NULL;
265256
wake_up_process(msr->r_tsk);
266257
smp_mb();
@@ -278,19 +269,15 @@ static void expunge_all(struct msg_queue *msq, int res)
278269
*/
279270
static void freeque(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
280271
{
281-
struct list_head *tmp;
272+
struct msg_msg *msg, *t;
282273
struct msg_queue *msq = container_of(ipcp, struct msg_queue, q_perm);
283274

284275
expunge_all(msq, -EIDRM);
285276
ss_wakeup(&msq->q_senders, 1);
286277
msg_rmid(ns, msq);
287278
msg_unlock(msq);
288279

289-
tmp = msq->q_messages.next;
290-
while (tmp != &msq->q_messages) {
291-
struct msg_msg *msg = list_entry(tmp, struct msg_msg, m_list);
292-
293-
tmp = tmp->next;
280+
list_for_each_entry_safe(msg, t, &msq->q_messages, m_list) {
294281
atomic_dec(&ns->msg_hdrs);
295282
free_msg(msg);
296283
}
@@ -583,6 +570,7 @@ static int testmsg(struct msg_msg *msg, long type, int mode)
583570
switch(mode)
584571
{
585572
case SEARCH_ANY:
573+
case SEARCH_NUMBER:
586574
return 1;
587575
case SEARCH_LESSEQUAL:
588576
if (msg->m_type <=type)
@@ -602,14 +590,9 @@ static int testmsg(struct msg_msg *msg, long type, int mode)
602590

603591
static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)
604592
{
605-
struct list_head *tmp;
593+
struct msg_receiver *msr, *t;
606594

607-
tmp = msq->q_receivers.next;
608-
while (tmp != &msq->q_receivers) {
609-
struct msg_receiver *msr;
610-
611-
msr = list_entry(tmp, struct msg_receiver, r_list);
612-
tmp = tmp->next;
595+
list_for_each_entry_safe(msr, t, &msq->q_receivers, r_list) {
613596
if (testmsg(msg, msr->r_msgtype, msr->r_mode) &&
614597
!security_msg_queue_msgrcv(msq, msg, msr->r_tsk,
615598
msr->r_msgtype, msr->r_mode)) {
@@ -685,7 +668,12 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext,
685668
goto out_unlock_free;
686669
}
687670
ss_add(msq, &s);
688-
ipc_rcu_getref(msq);
671+
672+
if (!ipc_rcu_getref(msq)) {
673+
err = -EIDRM;
674+
goto out_unlock_free;
675+
}
676+
689677
msg_unlock(msq);
690678
schedule();
691679

@@ -738,6 +726,8 @@ SYSCALL_DEFINE4(msgsnd, int, msqid, struct msgbuf __user *, msgp, size_t, msgsz,
738726

739727
static inline int convert_mode(long *msgtyp, int msgflg)
740728
{
729+
if (msgflg & MSG_COPY)
730+
return SEARCH_NUMBER;
741731
/*
742732
* find message of correct type.
743733
* msgtyp = 0 => get first.
@@ -774,14 +764,10 @@ static long do_msg_fill(void __user *dest, struct msg_msg *msg, size_t bufsz)
774764
* This function creates new kernel message structure, large enough to store
775765
* bufsz message bytes.
776766
*/
777-
static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz,
778-
int msgflg, long *msgtyp,
779-
unsigned long *copy_number)
767+
static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
780768
{
781769
struct msg_msg *copy;
782770

783-
*copy_number = *msgtyp;
784-
*msgtyp = 0;
785771
/*
786772
* Create dummy message to copy real message to.
787773
*/
@@ -797,9 +783,7 @@ static inline void free_copy(struct msg_msg *copy)
797783
free_msg(copy);
798784
}
799785
#else
800-
static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz,
801-
int msgflg, long *msgtyp,
802-
unsigned long *copy_number)
786+
static inline struct msg_msg *prepare_copy(void __user *buf, size_t bufsz)
803787
{
804788
return ERR_PTR(-ENOSYS);
805789
}
@@ -809,6 +793,30 @@ static inline void free_copy(struct msg_msg *copy)
809793
}
810794
#endif
811795

796+
static struct msg_msg *find_msg(struct msg_queue *msq, long *msgtyp, int mode)
797+
{
798+
struct msg_msg *msg;
799+
long count = 0;
800+
801+
list_for_each_entry(msg, &msq->q_messages, m_list) {
802+
if (testmsg(msg, *msgtyp, mode) &&
803+
!security_msg_queue_msgrcv(msq, msg, current,
804+
*msgtyp, mode)) {
805+
if (mode == SEARCH_LESSEQUAL && msg->m_type != 1) {
806+
*msgtyp = msg->m_type - 1;
807+
} else if (mode == SEARCH_NUMBER) {
808+
if (*msgtyp == count)
809+
return msg;
810+
} else
811+
return msg;
812+
count++;
813+
}
814+
}
815+
816+
return ERR_PTR(-EAGAIN);
817+
}
818+
819+
812820
long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
813821
int msgflg,
814822
long (*msg_handler)(void __user *, struct msg_msg *, size_t))
@@ -818,15 +826,13 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
818826
int mode;
819827
struct ipc_namespace *ns;
820828
struct msg_msg *copy = NULL;
821-
unsigned long copy_number = 0;
822829

823830
ns = current->nsproxy->ipc_ns;
824831

825832
if (msqid < 0 || (long) bufsz < 0)
826833
return -EINVAL;
827834
if (msgflg & MSG_COPY) {
828-
copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax),
829-
msgflg, &msgtyp, &copy_number);
835+
copy = prepare_copy(buf, min_t(size_t, bufsz, ns->msg_ctlmax));
830836
if (IS_ERR(copy))
831837
return PTR_ERR(copy);
832838
}
@@ -840,45 +846,13 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
840846

841847
for (;;) {
842848
struct msg_receiver msr_d;
843-
struct list_head *tmp;
844-
long msg_counter = 0;
845849

846850
msg = ERR_PTR(-EACCES);
847851
if (ipcperms(ns, &msq->q_perm, S_IRUGO))
848852
goto out_unlock;
849853

850-
msg = ERR_PTR(-EAGAIN);
851-
tmp = msq->q_messages.next;
852-
while (tmp != &msq->q_messages) {
853-
struct msg_msg *walk_msg;
854-
855-
walk_msg = list_entry(tmp, struct msg_msg, m_list);
856-
if (testmsg(walk_msg, msgtyp, mode) &&
857-
!security_msg_queue_msgrcv(msq, walk_msg, current,
858-
msgtyp, mode)) {
859-
860-
msg = walk_msg;
861-
if (mode == SEARCH_LESSEQUAL &&
862-
walk_msg->m_type != 1) {
863-
msgtyp = walk_msg->m_type - 1;
864-
} else if (msgflg & MSG_COPY) {
865-
if (copy_number == msg_counter) {
866-
/*
867-
* Found requested message.
868-
* Copy it.
869-
*/
870-
msg = copy_msg(msg, copy);
871-
if (IS_ERR(msg))
872-
goto out_unlock;
873-
break;
874-
}
875-
msg = ERR_PTR(-EAGAIN);
876-
} else
877-
break;
878-
msg_counter++;
879-
}
880-
tmp = tmp->next;
881-
}
854+
msg = find_msg(msq, &msgtyp, mode);
855+
882856
if (!IS_ERR(msg)) {
883857
/*
884858
* Found a suitable message.
@@ -892,8 +866,10 @@ long do_msgrcv(int msqid, void __user *buf, size_t bufsz, long msgtyp,
892866
* If we are copying, then do not unlink message and do
893867
* not update queue parameters.
894868
*/
895-
if (msgflg & MSG_COPY)
869+
if (msgflg & MSG_COPY) {
870+
msg = copy_msg(msg, copy);
896871
goto out_unlock;
872+
}
897873
list_del(&msg->m_list);
898874
msq->q_qnum--;
899875
msq->q_rtime = get_seconds();

0 commit comments

Comments
 (0)