Skip to content

Commit 82061c5

Browse files
Davidlohr Buesotorvalds
authored andcommitted
ipc: drop ipc_lock()
ipc/util.c contains multiple functions to get the ipc object pointer given an id number. There are two sets of function: One set verifies the sequence counter part of the id number, other functions do not check the sequence counter. The standard for function names in ipc/util.c is - ..._check() functions verify the sequence counter - ..._idr() functions do not verify the sequence counter ipc_lock() is an exception: It does not verify the sequence counter value, but this is not obvious from the function name. Furthermore, shm.c is the only user of this helper. Thus, we can simply move the logic into shm_lock() and get rid of the function altogether. [manfred@colorfullife.com: most of changelog] Link: http://lkml.kernel.org/r/20180712185241.4017-7-manfred@colorfullife.com Signed-off-by: Davidlohr Bueso <dbueso@suse.de> Signed-off-by: Manfred Spraul <manfred@colorfullife.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Herbert Xu <herbert@gondor.apana.org.au> Cc: Kees Cook <keescook@chromium.org> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Cc: Michal Hocko <mhocko@suse.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2e5ceb4 commit 82061c5

File tree

3 files changed

+23
-43
lines changed

3 files changed

+23
-43
lines changed

ipc/shm.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,33 @@ static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace
180180
*/
181181
static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
182182
{
183-
struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
183+
struct kern_ipc_perm *ipcp;
184+
185+
rcu_read_lock();
186+
ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
187+
if (IS_ERR(ipcp))
188+
goto err;
184189

190+
ipc_lock_object(ipcp);
191+
/*
192+
* ipc_rmid() may have already freed the ID while ipc_lock_object()
193+
* was spinning: here verify that the structure is still valid.
194+
* Upon races with RMID, return -EIDRM, thus indicating that
195+
* the ID points to a removed identifier.
196+
*/
197+
if (ipc_valid_object(ipcp)) {
198+
/* return a locked ipc object upon success */
199+
return container_of(ipcp, struct shmid_kernel, shm_perm);
200+
}
201+
202+
ipc_unlock_object(ipcp);
203+
err:
204+
rcu_read_unlock();
185205
/*
186206
* Callers of shm_lock() must validate the status of the returned ipc
187-
* object pointer (as returned by ipc_lock()), and error out as
188-
* appropriate.
207+
* object pointer and error out as appropriate.
189208
*/
190-
if (IS_ERR(ipcp))
191-
return (void *)ipcp;
192-
return container_of(ipcp, struct shmid_kernel, shm_perm);
209+
return (void *)ipcp;
193210
}
194211

195212
static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)

ipc/util.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -588,42 +588,6 @@ struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
588588
return out;
589589
}
590590

591-
/**
592-
* ipc_lock - lock an ipc structure without rwsem held
593-
* @ids: ipc identifier set
594-
* @id: ipc id to look for
595-
*
596-
* Look for an id in the ipc ids idr and lock the associated ipc object.
597-
*
598-
* The ipc object is locked on successful exit.
599-
*/
600-
struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
601-
{
602-
struct kern_ipc_perm *out;
603-
604-
rcu_read_lock();
605-
out = ipc_obtain_object_idr(ids, id);
606-
if (IS_ERR(out))
607-
goto err;
608-
609-
spin_lock(&out->lock);
610-
611-
/*
612-
* ipc_rmid() may have already freed the ID while ipc_lock()
613-
* was spinning: here verify that the structure is still valid.
614-
* Upon races with RMID, return -EIDRM, thus indicating that
615-
* the ID points to a removed identifier.
616-
*/
617-
if (ipc_valid_object(out))
618-
return out;
619-
620-
spin_unlock(&out->lock);
621-
out = ERR_PTR(-EIDRM);
622-
err:
623-
rcu_read_unlock();
624-
return out;
625-
}
626-
627591
/**
628592
* ipc_obtain_object_check
629593
* @ids: ipc identifier set

ipc/util.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ int ipc_rcu_getref(struct kern_ipc_perm *ptr);
142142
void ipc_rcu_putref(struct kern_ipc_perm *ptr,
143143
void (*func)(struct rcu_head *head));
144144

145-
struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
146145
struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
147146

148147
void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);

0 commit comments

Comments
 (0)