The patch titled Subject: ipc: introduce obtaining a lockless ipc object has been added to the -mm tree. Its filename is ipc-introduce-obtaining-a-lockless-ipc-object.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/SubmitChecklist when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Davidlohr Bueso <davidlohr.bueso@xxxxxx> Subject: ipc: introduce obtaining a lockless ipc object Through ipc_lock() and therefore ipc_lock_check() we currently return the locked ipc object. This is not necessary for all situations and can, therefore, cause unnecessary ipc lock contention. Introduce analogous ipc_obtain_object() and ipc_obtain_object_check() functions that only lookup and return the ipc object. Both these functions must be called within the RCU read critical section. Signed-off-by: Davidlohr Bueso <davidlohr.bueso@xxxxxx> Signed-off-by: Rik van Riel <riel@xxxxxxxxxx> Reviewed-by: Chegu Vinod <chegu_vinod@xxxxxx> Acked-by: Michel Lespinasse <walken@xxxxxxxxxx> Cc: Emmanuel Benisty <benisty.e@xxxxxxxxx> Cc: Jason Low <jason.low2@xxxxxx> Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> Cc: Peter Hurley <peter@xxxxxxxxxxxxxxxxxx> Cc: Stanislav Kinsbursky <skinsbursky@xxxxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- ipc/util.c | 69 ++++++++++++++++++++++++++++++++++++++++++--------- ipc/util.h | 2 + 2 files changed, 59 insertions(+), 12 deletions(-) diff -puN ipc/util.c~ipc-introduce-obtaining-a-lockless-ipc-object ipc/util.c --- a/ipc/util.c~ipc-introduce-obtaining-a-lockless-ipc-object +++ a/ipc/util.c @@ -668,6 +668,28 @@ void ipc64_perm_to_ipc_perm (struct ipc6 } /** + * ipc_obtain_object + * @ids: ipc identifier set + * @id: ipc id to look for + * + * Look for an id in the ipc ids idr and return associated ipc object. + * + * Call inside the RCU critical section. + * The ipc object is *not* locked on exit. + */ +struct kern_ipc_perm *ipc_obtain_object(struct ipc_ids *ids, int id) +{ + struct kern_ipc_perm *out; + int lid = ipcid_to_idx(id); + + out = idr_find(&ids->ipcs_idr, lid); + if (!out) + return ERR_PTR(-EINVAL); + + return out; +} + +/** * ipc_lock - Lock an ipc structure without rw_mutex held * @ids: IPC identifier set * @id: ipc id to look for @@ -680,26 +702,49 @@ void ipc64_perm_to_ipc_perm (struct ipc6 struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id) { struct kern_ipc_perm *out; - int lid = ipcid_to_idx(id); rcu_read_lock(); - out = idr_find(&ids->ipcs_idr, lid); - if (out == NULL) { - rcu_read_unlock(); - return ERR_PTR(-EINVAL); - } + out = ipc_obtain_object(ids, id); + if (IS_ERR(out)) + goto err1; spin_lock(&out->lock); - + /* ipc_rmid() may have already freed the ID while ipc_lock * was spinning: here verify that the structure is still valid */ - if (out->deleted) { - spin_unlock(&out->lock); - rcu_read_unlock(); - return ERR_PTR(-EINVAL); - } + if (out->deleted) + goto err0; + + return out; +err0: + spin_unlock(&out->lock); +err1: + rcu_read_unlock(); + return ERR_PTR(-EINVAL); +} + +/** + * ipc_obtain_object_check + * @ids: ipc identifier set + * @id: ipc id to look for + * + * Similar to ipc_obtain_object() but also checks + * the ipc object reference counter. + * + * Call inside the RCU critical section. + * The ipc object is *not* locked on exit. + */ +struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id) +{ + struct kern_ipc_perm *out = ipc_obtain_object(ids, id); + + if (IS_ERR(out)) + goto out; + if (ipc_checkid(out, id)) + return ERR_PTR(-EIDRM); +out: return out; } diff -puN ipc/util.h~ipc-introduce-obtaining-a-lockless-ipc-object ipc/util.h --- a/ipc/util.h~ipc-introduce-obtaining-a-lockless-ipc-object +++ a/ipc/util.h @@ -123,6 +123,7 @@ void ipc_rcu_getref(void *ptr); void ipc_rcu_putref(void *ptr); struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int); +struct kern_ipc_perm *ipc_obtain_object(struct ipc_ids *ids, int id); void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out); void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out); @@ -168,6 +169,7 @@ static inline void ipc_unlock(struct ker } struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id); +struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id); int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids, struct ipc_ops *ops, struct ipc_params *params); void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids, _ Patches currently in -mm which might be from davidlohr.bueso@xxxxxx are lib-int_sqrtc-optimize-square-root-algorithm.patch ipc-remove-bogus-lock-comment-for-ipc_checkid.patch ipc-introduce-obtaining-a-lockless-ipc-object.patch ipc-introduce-obtaining-a-lockless-ipc-object-fix.patch ipc-introduce-lockless-pre_down-ipcctl.patch ipcsem-do-not-hold-ipc-lock-more-than-necessary.patch ipcsem-open-code-and-rename-sem_lock.patch ipcsem-open-code-and-rename-sem_lock-fix.patch ipcsem-have-only-one-list-in-struct-sem_queue.patch ipcsem-fine-grained-locking-for-semtimedop.patch -- To unsubscribe from this list: send the line "unsubscribe mm-commits" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html