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

From: Davidlohr Bueso

Date: Sat May 02 2026 - 14:51:58 EST


On Thu, 30 Apr 2026, Andrew Morton wrote:

On Thu, 30 Apr 2026 13:21:34 +0800 Ren Wei <n05ec@xxxxxxxxxx> wrote:

From: Yilin Zhu <zylzyl2333@xxxxxxxxx>

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.

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