ipc: some misc updates & optimizations
cover ipc: some misc updates & optimizations
A few updates to the ipc code, mostly some very needed cleanups (yes, we still have loads more to do, but it's a start). Nothing particularly interesting, except perhaps for the last two patches, which include a locking optimization and barrier documentation for message queues. Absolutely no functional changes.
Applies on top of linux-next + Manfred's 'whitespace cleanup' patch. Tested with LTP.
Thanks!
Davidlohr Bueso (7):
ipc: standardize code comments
ipc: remove braces for single statements
ipc: remove useless return statement
ipc: simplify sysvipc_proc_open return
ipc: delete seq_max field in struct ipc_ids
ipc: share ids rwsem when possible in ipcget_public ipc,msg: document barriers
include/linux/ipc_namespace.h | 1 - ipc/compat.c | 18 +-- ipc/mqueue.c | 6 +- ipc/msg.c | 19 ++- ipc/sem.c | 32 ++--- ipc/shm.c | 1 - ipc/util.c | 328 ++++++++++++++++++++---------------------- ipc/util.h | 1 + 8 files changed, 198 insertions(+), 208 deletions(-)
Signature · 1 line
1.8.1.4
PATCH 1/7 ipc: standardize code comments
IPC commenting style is all over the place, *specially* in util.c. This patch orders things a bit.
--- ipc/sem.c | 24 +++---- ipc/shm.c | 1 - ipc/util.c | 239 +++++++++++++++++++++++++++++-------------------------------- 3 files changed, 125 insertions(+), 139 deletions(-)
ipc/sem.c +11 -13
@@ -225,7 +225,7 @@ static void unmerge_queues(struct sem_array *sma) }/** -* merge_queues - Merge single semop queues into global queue +* merge_queues - merge single semop queues into global queue * @sma: semaphore array * * This function merges all per-semaphore queues into the global queue.
@@ -474,7 +474,6 @@ static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)* * Called with sem_ids.rwsem held (as a writer) */ -static int newary (struct ipc_namespace *ns,struct ipc_params *params) {int id;
@@ -682,7 +681,7 @@ static void wake_up_sem_queue_prepare(struct list_head *pt, }/** -* wake_up_sem_queue_do(pt) - do the actual wake-up +* wake_up_sem_queue_do - do the actual wake-up * @pt: list of tasks to be woken up * * Do the actual wake-up.
@@ -748,7 +747,7 @@ static int check_restart(struct sem_array *sma, struct sem_queue *q) }/** -* wake_const_ops(sma, semnum, pt) - Wake up non-alter tasks +* wake_const_ops - wake up non-alter tasks * @sma: semaphore array. * @semnum: semaphore that was modified. * @pt: list head for the tasks that must be woken up.
@@ -798,15 +797,14 @@ static int wake_const_ops(struct sem_array *sma, int semnum, }/** -* do_smart_wakeup_zero(sma, sops, nsops, pt) - wakeup all wait for zero tasks +* do_smart_wakeup_zero - wakeup all wait for zero tasks * @sma: semaphore array * @sops: operations that were performed * @nsops: number of operations * @pt: list head of the tasks that must be woken up. * -* do_smart_wakeup_zero() checks all required queue for wait-for-zero -* operations, based on the actual changes that were performed on the -* semaphore array. +* Checks all required queue for wait-for-zero operations, based +* on the actual changes that were performed on the semaphore array. * The function returns 1 if at least one operation was completed successfully. */ static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
@@ -850,7 +848,7 @@ static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,/** -* update_queue(sma, semnum): Look for tasks that can be completed. +* update_queue - look for tasks that can be completed. * @sma: semaphore array. * @semnum: semaphore that was modified. * @pt: list head for the tasks that must be woken up.
@@ -920,7 +918,7 @@ again: }/** -* set_semotime(sma, sops) - set sem_otime +* set_semotime - set sem_otime * @sma: semaphore array * @sops: operations that modified the array, may be NULL *
@@ -938,7 +936,7 @@ static void set_semotime(struct sem_array *sma, struct sembuf *sops) }/** -* do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue +* do_smart_update - optimized update_queue * @sma: semaphore array * @sops: operations that were performed * @nsops: number of operations
@@ -1647,7 +1645,7 @@ static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid) }/** -* find_alloc_undo - Lookup (and if not present create) undo array +* find_alloc_undo - lookup (and if not present create) undo array * @ns: namespace * @semid: semaphore array id *
@@ -1737,7 +1735,7 @@ out:/** -* get_queue_result - Retrieve the result code from sem_queue +* get_queue_result - retrieve the result code from sem_queue * @q: Pointer to queue structure * * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
ipc/shm.c +0 -1
@@ -477,7 +477,6 @@ static const struct vm_operations_struct shm_vm_ops = {* * Called with shm_ids.rwsem held as a writer. */ -static int newseg (struct ipc_namespace *ns,struct ipc_params *params) {key_t key = params->key;
ipc/util.c +114 -125
394 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH 2/7 ipc: remove braces for single statements
In reply to Davidlohr BuesoDeal with checkpatch messages:
WARNING: braces {} are not necessary for single statement blocks
--- ipc/compat.c | 18 +++++++++--------- ipc/mqueue.c | 6 +++--- ipc/sem.c | 8 +++----- ipc/util.c | 3 +-- 4 files changed, 16 insertions(+), 19 deletions(-)
ipc/compat.c +9 -9
@@ -288,11 +288,11 @@ static long do_compat_semctl(int first, int second, int third, u32 pad)break ;case IPC_SET : -if (version ==IPC_64 ) { +if (version ==IPC_64 ) err =get_compat_semid64_ds (&s64,compat_ptr (pad)); - }else { +else err =get_compat_semid_ds (&s64,compat_ptr (pad)); - } + up64 =compat_alloc_user_space (sizeof (s64));if (copy_to_user (up64, &s64,sizeof (s64))) err = -EFAULT ;
@@ -515,11 +515,11 @@ long compat_sys_msgctl(int first, int second, void __user *uptr)break ;case IPC_SET : -if (version ==IPC_64 ) { +if (version ==IPC_64 ) err =get_compat_msqid64 (&m64, uptr); - }else { +else err =get_compat_msqid (&m64, uptr); - } +if (err)break ; p =compat_alloc_user_space (sizeof (m64));
@@ -702,11 +702,11 @@ long compat_sys_shmctl(int first, int second, void __user *uptr)case IPC_SET : -if (version ==IPC_64 ) { +if (version ==IPC_64 ) err =get_compat_shmid64_ds (&s64, uptr); - }else { +else err =get_compat_shmid_ds (&s64, uptr); - } +if (err)break ; p =compat_alloc_user_space (sizeof (s64));
ipc/mqueue.c +3 -3
@@ -1303,11 +1303,11 @@ retry: out_fput:fdput (f); out: -if (sock) { +if (sock)netlink_detachskb (sock, nc); - }else if (nc) { +else if (nc)dev_kfree_skb (nc); - } +return ret; }
ipc/sem.c +3 -5
@@ -492,9 +492,9 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params) size =sizeof (*sma) + nsems *sizeof (struct sem ); sma =ipc_rcu_alloc (size); -if (!sma) { +if (!sma)return -ENOMEM ; - } +memset (sma,0 , size); sma->sem_perm.mode = (semflg &S_IRWXUGO );
@@ -1967,10 +1967,8 @@ sleep_again:* If queue.status != -EINTR we are woken up by another process. * Leave without unlink_queue(), but with sem_unlock(). */ - -if (error != -EINTR ) { +if (error != -EINTR ) goto out_unlock_free; - }/* * If an interrupt occurred we have to clean up the queue
ipc/util.c +1 -2
@@ -183,9 +183,8 @@ void __init ipc_init_proc_interface(const char *path, const char *header,NULL ,/* parent dir */ &sysvipc_proc_fops, iface); -if (!pde) { +if (!pde) kfree(iface); - } }#endif
Signature · 1 line
1.8.1.4
PATCH 3/7 ipc: remove useless return statement
In reply to Davidlohr BuesoOnly found in ipc_rmid().
--- ipc/util.c | 4 ---- 1 file changed, 4 deletions(-)
ipc/util.c +0 -4
@@ -439,12 +439,8 @@ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)int lid =ipcid_to_idx (ipcp->id);idr_remove (&ids->ipcs_idr, lid); - ids->in_use--; - ipcp->deleted = true; - -return ; }/**
Signature · 1 line
1.8.1.4
PATCH 4/7 ipc: simplify sysvipc_proc_open return
In reply to Davidlohr BuesoGet rid of silly/useless label jumping.
--- ipc/util.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
ipc/util.c +4 -5
@@ -911,8 +911,10 @@ static int sysvipc_proc_open(struct inode *inode, struct file *file) goto out; ret =seq_open (file, &sysvipc_proc_seqops); -if (ret) - goto out_kfree; +if (ret) { +kfree (iter); + goto out; + } seq = file->private_data; seq->private = iter;
@@ -921,9 +923,6 @@ static int sysvipc_proc_open(struct inode *inode, struct file *file) iter->ns =get_ipc_ns (current->nsproxy->ipc_ns); out:return ret; -out_kfree: -kfree (iter); - goto out; }static int sysvipc_proc_release (struct inode *inode,struct file *file)
Signature · 1 line
1.8.1.4
PATCH 5/7 ipc: delete seq_max field in struct ipc_ids
In reply to Davidlohr BuesoThis field is only used to reset the ids seq number if it exceeds the smaller of INT_MAX/SEQ_MULTIPLIER and USHRT_MAX, and can therefore be moved out of the structure and into its own macro. Since each ipc_namespace contains a table of 3 pointers to struct ipc_ids we can save space in instruction text:
text data bss dec hex filename 56232 2348 24 58604 e4ec ipc/built-in.o 56216 2348 24 58588 e4dc ipc/built-in.o-after
--- include/linux/ipc_namespace.h | 1 - ipc/util.c | 13 ++----------- ipc/util.h | 1 + 3 files changed, 3 insertions(+), 12 deletions(-)
include/linux/ipc_namespace.h +0 -1
@@ -21,7 +21,6 @@ struct user_namespace;struct ipc_ids {int in_use;unsigned short seq; -unsigned short seq_max;struct rw_semaphore rwsem;struct idr ipcs_idr;int next_id;
ipc/util.c +2 -11
@@ -139,19 +139,10 @@ __initcall(ipc_init);*/ void ipc_init_ids (struct ipc_ids *ids) { -init_rwsem (&ids->rwsem); - ids->in_use =0 ; ids->seq =0 ; ids->next_id =-1 ; - { -int seq_limit =INT_MAX /SEQ_MULTIPLIER ; -if (seq_limit >USHRT_MAX ) - ids->seq_max =USHRT_MAX ; -else - ids->seq_max = seq_limit; - } - +init_rwsem (&ids->rwsem);idr_init (&ids->ipcs_idr); }
@@ -304,7 +295,7 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int size)if (next_id <0 ) { new->seq = ids->seq++; -if (ids->seq > ids->seq_max) +if (ids->seq >IPCID_SEQ_MAX ) ids->seq =0 ; }else { new->seq =ipcid_to_seqx (next_id);
ipc/util.h +1 -0
@@ -100,6 +100,7 @@ void __init ipc_init_proc_interface(const char *path, const char *header,#define ipcid_to_idx (id) ((id) % SEQ_MULTIPLIER)#define ipcid_to_seqx (id) ((id) / SEQ_MULTIPLIER) +#define IPCID_SEQ_MAX min_t(int, INT_MAX/SEQ_MULTIPLIER, USHRT_MAX)/* must be called with ids->rwsem acquired for writing */ int ipc_addid (struct ipc_ids *,struct kern_ipc_perm *,int );
Signature · 1 line
1.8.1.4
PATCH 6/7 ipc: share ids rwsem when possible in ipcget_public
In reply to Davidlohr Bueso... and rewrite the function. For scenarios where the key is found and we end up just doing different routinary checks, we can downgrade the ids->rwsem and share it among concurrent readers. These checks include the following, which are all safe to share the lock:
ops->more_checks() >> sem_more_checks(), shm_more_checks() ipc_check_perms() >> ipcperms(),ops->associate() >> [lsm]_[ipctype]_associate()
--- ipc/util.c | 60 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 27 deletions(-)
ipc/util.c +33 -27
@@ -183,7 +183,7 @@ void __init ipc_init_proc_interface(const char *path, const char *header,* ipc_findkey - find a key in an ipc identifier set * @ids: ipc identifier set * @key: key to find -* +* * Returns the locked pointer to the ipc structure if found or NULL * otherwise. If key is found ipc points to the owning ipc structure *
@@ -375,48 +375,54 @@ static int ipc_check_perms(struct ipc_namespace *ns,* On success, the ipc id is returned. */ static int ipcget_public (struct ipc_namespace *ns,struct ipc_ids *ids, -struct ipc_ops *ops,struct ipc_params *params) +struct ipc_ops *ops,struct ipc_params *params) {struct kern_ipc_perm *ipcp;int flg = params->flg; -int err; +int err =0 ; -/* -* Take the lock as a writer since we are potentially going to add -* a new entry + read locks are not "upgradable" -*/ down_write (&ids->rwsem); ipcp =ipc_findkey (ids, params->key); -if (ipcp ==NULL ) { + +if (!ipcp) {/* key not used */ if (!(flg &IPC_CREAT )) err = -ENOENT ; -else +else /* create new ipc object */ err = ops->getnew (ns, params); - }else { -/* ipc object has been locked by ipc_findkey() */ - -if (flg &IPC_CREAT && flg &IPC_EXCL ) - err = -EEXIST ; -else { - err =0 ; -if (ops->more_checks) - err = ops->more_checks (ipcp, params); -if (!err) -/* -* ipc_check_perms returns the IPC id on -* success -*/ - err =ipc_check_perms (ns, ipcp, ops, params); - } + + goto done_write; + } + +if ((flg &IPC_CREAT ) && (flg &IPC_EXCL )) { +/* ipc object was locked by successful ipc_findkey() lookup */ ipc_unlock (ipcp); + err = -ENOENT ; + + goto done_write; } -up_write (&ids->rwsem); +/* +* The key was found, so we will just perform routinary checks on +* ipc the object. Share the lock among other readers. +*/ +downgrade_write (&ids->rwsem); + +if (ops->more_checks) + err = ops->more_checks (ipcp, params); +if (!err) +/* returns the IPC id on success */ + err =ipc_check_perms (ns, ipcp, ops, params); + +ipc_unlock (ipcp); + +up_read (&ids->rwsem); +return err; +done_write: +up_write (&ids->rwsem);return err; } -/** * ipc_rmid - remove an ipc identifier * @ids: ipc identifier set
Signature · 1 line
1.8.1.4
PATCH 7/7 ipc,msg: document barriers
In reply to Davidlohr BuesoBoth expunge_all() and pipeline_send() rely on both a nil msg value and a full barrier to guarantee the correct ordering when waking up a task. While its counter part at the receiving end is well documented for the lockless recv algorithm, we still need to document these specific smp_mb() calls.
--- ipc/msg.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
ipc/msg.c +17 -2
@@ -253,8 +253,14 @@ static void expunge_all(struct msg_queue *msq, int res)struct msg_receiver *msr, *t;list_for_each_entry_safe (msr, t, &msq->q_receivers, r_list) { - msr->r_msg =NULL ; + msr->r_msg =NULL ;/* initialize expunge ordering */ wake_up_process (msr->r_tsk); +/* +* Ensure that the wakeup is visible before setting r_msg as +* the receiving end depends on it: either spinning on a nil, +* or dealing with -EAGAIN cases. See lockless reveice part 1 +* and 2 in do_msgrcv(). +*/ smp_mb (); msr->r_msg =ERR_PTR (res); }
@@ -638,15 +644,22 @@ static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)list_del (&msr->r_list);if (msr->r_maxsize < msg->m_ts) { +/* initialize pipelined send ordering */ msr->r_msg =NULL ;wake_up_process (msr->r_tsk); -smp_mb (); +smp_mb ();/* see barrier comment below */ msr->r_msg =ERR_PTR (-E2BIG ); }else { msr->r_msg =NULL ; msq->q_lrpid =task_pid_vnr (msr->r_tsk); msq->q_rtime =get_seconds ();wake_up_process (msr->r_tsk); +/* +* Ensure that the wakeup is visible before +* setting r_msg, as the receiving end depends +* on it. See lockless reveice part 1 and 2 in +* do_msgrcv(). +*/ smp_mb (); msr->r_msg = msg;
@@ -654,6 +667,7 @@ static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg) } } } +return 0 ; }
@@ -716,6 +730,7 @@ long do_msgsnd(int msqid, long mtype, void __user *mtext, goto out_unlock0; } +/* enqueue the sender and prepare to block */ ss_add (msq, &s);if (!ipc_rcu_getref (msq)) {
Signature · 1 line
1.8.1.4
Re: [PATCH 7/7] ipc,msg: document barriers
Quoted 15 lines · Davidlohr Bueso
Both
expunge_all()andpipeline_send()rely on both a nil msg value and a full barrier to guarantee the correct ordering when waking up a task. While its counter part at the receiving end is well documented for the lockless recv algorithm, we still need to document these specificsmp_mb()calls.Signed-off-by Davidlohr Bueso <[email protected]>--- ipc/msg.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)ipc/msg.c +0
-0
ipc/msg.c static void expunge_all(struct msg_queue *msq, int res) · line 261
Quoted 10 lines
ipc/msg.c +5
-1@@ -253,8 +253,14 @@ static void expunge_all(struct msg_queue *msq, int res)struct msg_receiver *msr, *t;list_for_each_entry_safe (msr, t, &msq->q_receivers, r_list) { - msr->r_msg =NULL ; + msr->r_msg =NULL ;/* initialize expunge ordering */ wake_up_process (msr->r_tsk); +/* +* Ensure that the wakeup is visible before setting r_msg as +* the receiving end depends on it: either spinning on a nil, +* or dealing with -EAGAIN cases. See lockless reveice part 1
^^^^^^^ lysdexic fingers
Quoted 14 lines
ipc/msg.c +4
-1+* and 2 in do_msgrcv(). +*/ smp_mb (); msr->r_msg =ERR_PTR (res); }@@ -638,15 +644,22 @@ static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg)list_del (&msr->r_list);if (msr->r_maxsize < msg->m_ts) { +/* initialize pipelined send ordering */ msr->r_msg =NULL ;wake_up_process (msr->r_tsk); -smp_mb (); +smp_mb ();/* see barrier comment below */
ipc/msg.c static inline int pipelined_send(struct msg_queue *msq, struct msg_msg *msg) · line 660
Quoted 10 lines
ipc/msg.c +4
-0msr->r_msg =ERR_PTR (-E2BIG ); }else { msr->r_msg =NULL ; msq->q_lrpid =task_pid_vnr (msr->r_tsk); msq->q_rtime =get_seconds ();wake_up_process (msr->r_tsk); +/* +* Ensure that the wakeup is visible before +* setting r_msg, as the receiving end depends +* on it. See lockless reveice part 1 and 2 in
^^^^^^^ ditto
Hi David,
Quoted 7 lines · Davidlohr Bueso
A few updates to the ipc code, mostly some very needed cleanups (yes, we still have loads more to do, but it's a start). Nothing particularly interesting, except perhaps for the last two patches, which include a locking optimization and barrier documentation for message queues. Absolutely no functional changes.
Applies on top of linux-next + Manfred's 'whitespace cleanup' patch. Tested with LTP.
Now you confuse me.
In your other mail, you write that my patch doesn't apply to linux-next:
Quoted 1 line · Davidlohr Bueso
This patch doesn't apply on top of linux-next (which now includes Rafael's changes), could you please resend? I'm planning some more cleanups on top of this so, if Andrew agrees, I can include these changes to my patchset. Thanks, Davidlohr
Perhaps: Could you include (and update, if required) my patch into your patchset? It's probably simpler if we have only one set of ipc cleanups around.
Quoted 8 lines
Thanks!
Davidlohr Bueso (7):
ipc: standardize code comments
ipc: remove braces for single statements
ipc: remove useless return statement
ipc: simplifysysvipc_proc_openreturn
ipc: deleteseq_maxfield in structipc_ids
Quoted 1 line
ipc: share ids rwsem when possible in
ipcget_public
This patch is a real change. Could you at least make it the last one in the series? I will try to review it, but I don't know yet when I will find the time.
Quoted 1 line
ipc,msg: document barriers
--
Manfred
Quoted 8 lines · Manfred Spraul
Hi David,
Quoted 5 lines · Davidlohr Bueso
A few updates to the ipc code, mostly some very needed cleanups (yes, we still have loads more to do, but it's a start). Nothing particularly interesting, except perhaps for the last two patches, which include a locking optimization and barrier documentation for message queues. Absolutely no functional changes.
Quoted 4 lines
Quoted 2 lines
Applies on top of linux-next + Manfred's 'whitespace cleanup' patch. Tested with LTP.
Now you confuse me.
In your other mail, you write that my patch doesn't apply to linux-next:
Just ignore that email, Andrew then applied (and fixed the merge conflict) your whitespace cleanup patch and was already in linux-next by the time I sent this patchset, so no need to worry/coordinate.
Quoted 19 lines · Davidlohr Bueso
Quoted 4 lines
This patch doesn't apply on top of linux-next (which now includes Rafael's changes), could you please resend? I'm planning some more cleanups on top of this so, if Andrew agrees, I can include these changes to my patchset. Thanks, Davidlohr
Perhaps: Could you include (and update, if required) my patch into your patchset?
It's probably simpler if we have only one set of ipc cleanups around.Quoted 8 lines
Thanks!
Davidlohr Bueso (7):
ipc: standardize code comments
ipc: remove braces for single statements
ipc: remove useless return statement
ipc: simplifysysvipc_proc_openreturn
ipc: deleteseq_maxfield in structipc_ids
Quoted 5 lines
Acked-by Manfred Spraul <[email protected]>Quoted 1 line
ipc: share ids rwsem when possible in
ipcget_publicThis patch is a real change. Could you at least make it the last one in the series?
I will try to review it, but I don't know yet when I will find the time.
Thanks, I definitely want your ack/review for this one.
Quoted 6 lines
Quoted 2 lines
ipc,msg: document barriers
Acked-by Manfred Spraul <[email protected]>--
Manfred