Skip to contentSkip to conversation

ipc/shm: serialize orphan cleanup with shm_nattch updates

PATCH Applied Ren Wei 3 participants 3 messages Apr 30 – May 2 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ren Wei

author
#1 lore raw

PATCH 1/1 ipc/shm: serialize orphan cleanup with shm_nattch updates

From: Yilin Zhu <[email protected]>

shm_destroy_orphaned() walks the shm idr under shm_ids(ns).rwsem, but that does not serialize all fields tested by shm_may_destroy(). In particular, shm_nattch is updated while holding shm_perm.lock, and attach paths can do that without holding the rwsem.

Do not decide that an orphaned segment is unused before taking the object lock. Move the shm_may_destroy() check under shm_perm.lock, matching the other destroy paths, and unlock the segment when it no longer qualifies for removal.

Fixes 4c677e2eefdb ("shm: optimize locking and ipc_namespace getting")
---
 ipc/shm.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
ipc/shm.c +6 -4
index a95dae447707..b3e8a58e177d 100644
@@ -418,15 +418,17 @@ static int shm_try_destroy_orphaned(int id, void *p, void *data)
 	 * We want to destroy segments without users and with already
 	 * exit'ed originating process.
 	 *
-	 * As shp->* are changed under rwsem, it's safe to skip shp locking.
+	 * shm_nattch can be changed under shm_perm.lock without holding the
+	 * rwsem, so take the object lock before checking shm_may_destroy().
 	 */
 	if (!list_empty(&shp->shm_clist))
 		return 0;
 
-	if (shm_may_destroy(shp)) {
-		shm_lock_by_ptr(shp);
+	shm_lock_by_ptr(shp);
+	if (shm_may_destroy(shp))
 		shm_destroy(ns, shp);
-	}
+	else
+		shm_unlock(shp);
 	return 0;
 }
 
Signature · 1 line
2.47.3
#2 lore raw
Quoted 11 lines · On Thu, 30 Apr 2026 13:21:34 +0800 Ren Wei <[email protected]> wrote:

From: Yilin Zhu <[email protected]>

shm_destroy_orphaned() walks the shm idr under shm_ids(ns).rwsem, but that does not serialize all fields tested by shm_may_destroy(). In particular, shm_nattch is updated while holding shm_perm.lock, and attach paths can do that without holding the rwsem.

Do not decide that an orphaned segment is unused before taking the object lock. Move the shm_may_destroy() check under shm_perm.lock, matching the other destroy paths, and unlock the segment when it no longer qualifies for removal.

Thanks.

Quoted 1 line
Fixes 4c677e2eefdb ("shm: optimize locking and ipc_namespace getting")

Let's cc more people who were involved in 4c677e2eefdb.

And Davidlohr, who might have opinions.

Quoted 36 lines
---
 ipc/shm.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
ipc/shm.c +6 -4
index a95dae447707..b3e8a58e177d 100644
@@ -418,15 +418,17 @@ static int shm_try_destroy_orphaned(int id, void *p, void *data)
 	 * We want to destroy segments without users and with already
 	 * exit'ed originating process.
 	 *
-	 * As shp->* are changed under rwsem, it's safe to skip shp locking.
+	 * shm_nattch can be changed under shm_perm.lock without holding the
+	 * rwsem, so take the object lock before checking shm_may_destroy().
 	 */
 	if (!list_empty(&shp->shm_clist))
 		return 0;
 
-	if (shm_may_destroy(shp)) {
-		shm_lock_by_ptr(shp);
+	shm_lock_by_ptr(shp);
+	if (shm_may_destroy(shp))
 		shm_destroy(ns, shp);
-	}
+	else
+		shm_unlock(shp);
 	return 0;
 }
#3 lore raw
Quoted 14 lines · Andrew Morton
Quoted 11 lines · On Thu, 30 Apr 2026 13:21:34 +0800 Ren Wei <[email protected]> wrote:

From: Yilin Zhu <[email protected]>

shm_destroy_orphaned() walks the shm idr under shm_ids(ns).rwsem, but that does not serialize all fields tested by shm_may_destroy(). In particular, shm_nattch is updated while holding shm_perm.lock, and attach paths can do that without holding the rwsem.

Do not decide that an orphaned segment is unused before taking the object lock. Move the shm_may_destroy() check under shm_perm.lock, matching the other destroy paths, and unlock the segment when it no longer qualifies for removal.

Quoted 7 lines

Thanks.

Quoted 1 line
Fixes 4c677e2eefdb ("shm: optimize locking and ipc_namespace getting")

Let's cc more people who were involved in 4c677e2eefdb.

And Davidlohr, who might have opinions.

The same feedback as the previous (duplicate) patch - there is no need to unconditionally take the lock.

https://lore.kernel.org/all/20260406193322.cu2rrtfmpixlv7yy@offworld/

Thanks,
Davidlohr