Re: [PATCH v5 0/3] ipc/sem: account dynamic sops arrays and validate SEMOPM

From
Andrew Morton <akpm@linux-foundation.org>
Date
2026-11-15 22:12:07
Also in
cgroups, linux-hardening, linux-man, linux-mm, lkml
Reply to
Yingjie Wang, 2026-10-29
On Thu, 29 Oct 2026 05:03:24 +0000 Yingjie Wang [off-list ref] wrote:
Shakeel, thanks for the suggestion.
Thanks.  Shakeel, please review [1/3]?
Patch 1 now uses GFP_KERNEL_ACCOUNT for every dynamically allocated sops
array. The first 64 operations still use the existing stack buffer. The
fixed 1000-operation exemption and its conditional allocation flags have
been removed.

I compared this version with v4 in a 4-vCPU KVM guest. The benchmark process
was pinned to one vCPU. Each number is the median of 21 samples from three
fresh boots of each kernel. In a leaf memory cgroup, the changes were:

                                      65       500      1000
  successful semop()                   +3.2%     +3.2%     +2.3%
  allocation-focused invalid semid     +5.6%     +9.1%    +11.9%

The invalid-sem-id case allocates and copies the operation array before
returning EINVAL, so it emphasizes allocation overhead. The 64-operation
stack controls changed by 0.9% and 0.5%, respectively. The already-accounted
1001-operation controls changed by 1.0% and -0.2%. Successful calls in the
root cgroup changed by at most 2.3%, including the controls.

The sysctl handler parses writes into a temporary array while holding a
per-IPC-namespace mutex. The mutex covers the snapshot, validation, and
publication, so a failed write leaves the live settings unchanged and a
partial write cannot restore fields from an older snapshot. The snapshot and
publication use READ_ONCE() and WRITE_ONCE(), and reads continue through
proc_dointvec(). The unused sem_check_semmni() helper is also removed.

The UAPI warning about physical fragmentation is obsolete now that the
operation array uses kvmalloc(). The header describes SEMOPM as the
compiled-in default and retains the uniform <= INT_MAX range comment.

Patches 2 and 3 are unchanged from v4 and retain Manfred's Reviewed-by tags.
The series is based on 7be4bc52fd9e ("Merge tag 'driver-core-7.4-rc1' of
git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core").
Thing is, the above is largely about what-changed-since-last-time. 
It's useful info, but it's short-term development-time stuff.  Most of
this is inappropriate for the long-term kernel record.

I had LLM suggest a replacement [0/N].  wdyt?

: An IPC namespace owner can raise SEMOPM and make semop() allocate a large
: operation array.  A blocked call retains the array.  These allocations are
: currently uncharged to the caller's memory cgroup, so repeated calls can
: consume host memory beyond a container's limit.  In one test, a call
: retained 60,936,448 bytes while a 32 MiB cgroup recorded less than 1 MiB. 
: Sixteen concurrent calls retained 100,860,416 bytes while the cgroup
: recorded about 1.3 MiB.
: 
: The SEMOPM sysctl also accepts negative values.  Because the operation
: count is unsigned, SEMOPM=-1 becomes an effective limit of UINT_MAX. 
: Separately, a rejected kernel.sem write can leave earlier fields changed:
: an invalid SEMMNI can return ERANGE after setting SEMOPM to zero, causing
: subsequent nonempty semop() calls in that IPC namespace to fail with
: E2BIG.
: 
: Patch 1 charges dynamically allocated operation arrays in the regular
: semop() path to the caller's memory cgroup.  Patch 2 parses and validates
: kernel.sem writes before publishing them and serializes writers within
: each IPC namespace.  Patch 3 removes the obsolete warning that SEMOPM
: above 1000 risks physical memory fragmentation; the operation array uses
: kvmalloc().
: 
: Accounting contains the large-array case when the caller's memory cgroup
: has an effective limit.  The series does not impose a maximum on positive
: SEMOPM values, so a root or unlimited cgroup can still request large
: allocations.
: 
: Validation included x86-64 and i386 builds, initial and user-owned IPC
: namespace checks, concurrent sysctl writes, operation-count boundaries,
: and allocation cleanup on success and error paths.  In a limited memory
: cgroup, accounting increased median successful-call time by 2.3% to 3.2%
: for 65, 500 and 1000 operations; the allocation-focused invalid semaphore
: ID case increased by 5.6% to 11.9%.

Also, [1/3] and [2/3] are cc:stable but include no clear description of
why we're recommending backports.  Backportable patches should clearly
state the userspace-visible runtime effects of the bug which is being
fixed.  Our downstream people (-stable maintainers and further
downstream) are swamped with bugfixes, so let's help them out!

So for [1/3] I inserted this statement:

: A caller can raise SEMOPM and keep large, uncharged operation arrays alive
: in blocked semop() calls.  Repeating this from a memory-limited cgroup can
: exhaust host memory outside that cgroup, causing reclaim, stalls, or OOM
: kills of unrelated processes.  Charging the arrays lets an effective memcg
: limit contain that memory use.

and for [2/3] I inserted

: A negative SEMOPM becomes a large unsigned limit in semop(), allowing
: operation counts that the configured limit should reject.  Separately, a
: kernel.sem write can return ERANGE for an invalid SEMMNI while leaving
: earlier fields changed; if it leaves SEMOPM at zero, subsequent nonempty
: semop() calls in that IPC namespace fail with E2BIG.  Validate the whole
: write before changing the live settings.

Please check these also.
Thread (11 messages) 11 messages, 3 authors, last reply 10h ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help