From: Andrew Morton <akpm@linux-foundation.org>
To: David Laight <david.laight.linux@gmail.com>
Cc: linux-kernel@vger.kernel.org,
Manfred Spraul <manfred@colorfullife.com>,
Davidlohr Bueso <dave@stgolabs.net>
Subject: Re: [PATCH 05/44] ipc/msg: Fix saturation of percpu counts in msgctl_info()
Date: Thu, 29 Oct 2026 09:55:44 -0700 [thread overview]
Message-ID: <20261029095544.06f396cf1a298d7eba11b557@linux-foundation.org> (raw)
In-Reply-To: <20261029094543.34076789@pumpkin>
On Thu, 29 Oct 2026 09:45:43 +0000 David Laight <david.laight.linux@gmail.com> wrote:
> On Wed, 28 Oct 2026 21:04:54 -0700
> Andrew Morton <akpm@linux-foundation.org> wrote:
>
> > On Wed, 28 Oct 2026 18:15:02 -0700 Andrew Morton <akpm@linux-foundation.org> wrote:
> >
> > > > diff --git a/ipc/msg.c b/ipc/msg.c
> > > > index ee6af4fe52bf..34e607add006 100644
> > > > --- a/ipc/msg.c
> > > > +++ b/ipc/msg.c
> > > > @@ -501,11 +501,9 @@ static int msgctl_info(struct ipc_namespace *ns, int msqid,
> > > > max_idx = ipc_get_maxidx(&msg_ids(ns));
> > > > up_read(&msg_ids(ns).rwsem);
> > > > if (cmd == MSG_INFO) {
> > > > - msginfo->msgmap = min_t(int,
> > > > - percpu_counter_sum(&ns->percpu_msg_hdrs),
> > > > + msginfo->msgmap = min(percpu_counter_sum(&ns->percpu_msg_hdrs),
> > > > INT_MAX);
> > > > - msginfo->msgtql = min_t(int,
> > > > - percpu_counter_sum(&ns->percpu_msg_bytes),
> > > > + msginfo->msgtql = min(percpu_counter_sum(&ns->percpu_msg_bytes),
> > > > INT_MAX);
> > > > } else {
> > > > msginfo->msgmap = MSGMAP;
> > >
> > > Added, thanks.
> > >
> >
> > LLM found another bug in there. Please review:
>
> Seems reasonable, if unlikely to ever happen.
> Description is overlong ...
>
> Might be worth doing a scan of all uses of percpu_counter_sum(),
> I can imagine it is a common problem.
I had LLM do a scan:
: The scan found four similar reporting problems:
:
: - Ceph: metrics_file_show() in fs/ceph/debugfs.c prints the raw
: opened-inode count as signed. ceph_mdsc_send_metrics() in
: fs/ceph/metric.c converts it to unsigned for the MDS. An open/close
: pair on different CPUs can yield -1, appearing as a negative count
: in debugfs or U64_MAX in the metric.
:
: - ext4: ext4_attr_show() in fs/ext4/sysfs.c uses the raw dirty-cluster
: count for delayed_allocation_blocks. Reserving and then consuming
: or releasing a reservation on different CPUs can produce a
: negative sample. With one block per cluster, -1 is reported as
: U64_MAX blocks.
:
: - BPF: htab_map_mem_usage() in kernel/bpf/hashtab.c converts the raw
: entry count to unsigned before calculating memory usage. This
: affects nonpreallocated hash maps using per-CPU counters. An
: insert/delete pair can yield -1 entries and an incorrect memory
: estimate, including less than the empty-map overhead.
:
: - tmpfs: shmem_statfs() in mm/shmem.c subtracts the raw used-block
: count from the block limit. An allocation/free pair can yield -1
: used blocks, reporting more free blocks than total blocks. Tools
: calculating used space as total minus free can consequently
: report negative usage.
:
: These are source-level findings, not runtime reproductions. No
: corruption or resource-limit bypass was established.
:
: Other callers already floor negative samples, count cumulative events,
: check accounting after teardown, or use deliberately signed values.
: Drain checks and some XFS statistics still need further investigation.
: This should not be a blanket conversion, and flooring negative samples
: does not make the statistics an atomic snapshot.
> Oh and the code could use clamp(per_cpu_counter_sum(...), 0, INT_MAX);
> which is exactly the same but maybe clearer?
Sounds good. How does the below look?
From: Andrew Morton <akpm@linux-foundation.org>
Subject: ipc/msg: clamp negative MSG_INFO usage counts
Date: Thu, 29 Oct 2026 01:43:24 +0000
MSG_INFO can report negative usage during ordinary queue traffic. For
example, ipcs -q -u prints negative header counts and converts negative
byte counts into enormous unsigned used-space values.
percpu_counter_sum() can sample the sending CPU before an enqueue and the
receiving CPU after the matching dequeue, yielding a negative total.
Clamp both counts to [0, INT_MAX] before converting to int. The
statistics remain an approximate, non-atomic snapshot.
Fixes: 72d1e611082e ("ipc/msg: mitigate the lock contention with percpu counter")
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: David Laight <david.laight.linux@gmail.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: <stable@vger.kernel.org>
---
ipc/msg.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
--- a/ipc/msg.c~ipc-msg-clamp-negative-msg_info-usage-counts
+++ a/ipc/msg.c
@@ -501,10 +501,10 @@ static int msgctl_info(struct ipc_namesp
max_idx = ipc_get_maxidx(&msg_ids(ns));
up_read(&msg_ids(ns).rwsem);
if (cmd == MSG_INFO) {
- msginfo->msgmap = min(percpu_counter_sum(&ns->percpu_msg_hdrs),
- INT_MAX);
- msginfo->msgtql = min(percpu_counter_sum(&ns->percpu_msg_bytes),
- INT_MAX);
+ msginfo->msgmap = clamp(percpu_counter_sum(&ns->percpu_msg_hdrs),
+ 0, INT_MAX);
+ msginfo->msgtql = clamp(percpu_counter_sum(&ns->percpu_msg_bytes),
+ 0, INT_MAX);
} else {
msginfo->msgmap = MSGMAP;
msginfo->msgpool = MSGPOOL;
_
next prev parent reply other threads:[~2026-10-29 16:55 UTC|newest]
Thread overview: 113+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-19 22:40 [PATCH 00/44] Change a lot of min_t() that might mask high bits david.laight.linux
2025-11-19 22:40 ` [PATCH 01/44] x86/asm/bitops: Change the return type of variable__ffs() to unsigned int david.laight.linux
2025-11-20 15:54 ` Yury Norov
2025-11-20 18:29 ` David Laight
2025-11-20 18:33 ` Yury Norov
2025-11-20 21:18 ` David Laight
2025-11-24 14:58 ` Yury Norov
2025-11-24 18:18 ` David Laight
2025-11-19 22:40 ` [PATCH 02/44] ext4: Fix saturation of 64bit inode times for old filesystems david.laight.linux
2025-11-19 22:40 ` [PATCH 03/44] perf: Fix branch stack callchain limit david.laight.linux
2025-11-26 5:25 ` Mi, Dapeng
2025-11-19 22:41 ` [PATCH 04/44] io_uring/net: Change some dubious min_t() david.laight.linux
2025-11-20 14:48 ` Jens Axboe
2025-11-20 15:48 ` David Laight
2025-11-20 15:53 ` Jens Axboe
2025-11-22 11:31 ` David Laight
2025-11-19 22:41 ` [PATCH 05/44] ipc/msg: Fix saturation of percpu counts in msgctl_info() david.laight.linux
2026-10-29 1:15 ` Andrew Morton
2026-10-29 4:04 ` Andrew Morton
2026-10-29 9:45 ` David Laight
2026-10-29 16:55 ` Andrew Morton [this message]
2025-11-19 22:41 ` [PATCH 06/44] bpf: Verifier, remove some unusual uses of min_t() and max_t() david.laight.linux
2025-11-21 21:40 ` Alexei Starovoitov
2025-11-21 22:21 ` David Laight
2025-11-23 16:39 ` Alexei Starovoitov
2025-11-23 18:07 ` David Laight
2025-11-23 19:20 ` Alexei Starovoitov
2025-11-23 23:03 ` David Laight
2025-11-19 22:41 ` [PATCH 07/44] net/core/flow_dissector: Fix cap of __skb_flow_dissect() return value david.laight.linux
2025-11-19 22:41 ` [PATCH 08/44] net: ethtool: Use min3() instead of nested min_t(u16,...) david.laight.linux
2025-11-19 22:41 ` [PATCH 09/44] ipv6: __ip6_append_data() don't abuse max_t() casts david.laight.linux
2025-11-20 0:32 ` bot+bpf-ci
2025-11-20 11:16 ` David Laight
2025-11-20 13:50 ` Chris Mason
2025-11-19 22:41 ` [PATCH 10/44] x86/crypto: ctr_crypt() use min() instead of min_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 11/44] arch/x96/kvm: " david.laight.linux
2025-11-19 22:41 ` [PATCH 12/44] block: " david.laight.linux
2025-11-20 14:44 ` Jens Axboe
2025-11-19 22:41 ` [PATCH 13/44] drivers/acpi: " david.laight.linux
2025-11-24 19:48 ` Rafael J. Wysocki
2025-11-19 22:41 ` [PATCH 14/44] drivers/char/hw_random: use min3() instead of nested min_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 15/44] drivers/char/tpm: use min() instead of min_t() david.laight.linux
2025-11-21 20:11 ` Jarkko Sakkinen
2025-11-19 22:41 ` [PATCH 16/44] drivers/crypto/ccp: " david.laight.linux
2025-11-19 22:41 ` [PATCH 17/44] drivers/cxl: " david.laight.linux
2025-11-19 23:50 ` Dave Jiang
2025-11-19 22:41 ` [PATCH 18/44] drivers/gpio: " david.laight.linux
2025-11-20 8:01 ` Andy Shevchenko
2025-11-20 9:37 ` David Laight
2025-11-25 9:55 ` Andy Shevchenko
2025-11-19 22:41 ` [PATCH 19/44] drivers/gpu/drm/amd: " david.laight.linux
2025-11-19 22:41 ` [PATCH 20/44] drivers/i2c/busses: " david.laight.linux
2026-01-20 13:19 ` Andi Shyti
2025-11-19 22:41 ` [PATCH 21/44] drivers/net/ethernet/realtek: " david.laight.linux
2025-11-19 22:41 ` [PATCH 22/44] drivers/nvme: " david.laight.linux
2025-11-19 22:41 ` [PATCH 23/44] arch/x86/mm: " david.laight.linux
2025-11-19 22:41 ` [PATCH 24/44] drivers/nvmem: " david.laight.linux
2025-11-19 22:41 ` [PATCH 25/44] drivers/pci: " david.laight.linux
2025-11-24 21:04 ` Bjorn Helgaas
2025-11-24 21:42 ` David Laight
2025-11-19 22:41 ` [PATCH 26/44] drivers/scsi: " david.laight.linux
2025-11-19 23:09 ` Bart Van Assche
2025-11-20 18:44 ` David Laight
2025-11-22 21:50 ` David Laight
2025-11-19 22:41 ` [PATCH 27/44] drivers/tty/vt: use umin() instead of min_t(u16, ...) for row/col limits david.laight.linux
2025-11-20 7:23 ` Jiri Slaby
2025-11-19 22:41 ` [PATCH 28/44] drivers/usb/storage: use min() instead of min_t() david.laight.linux
2025-11-20 2:59 ` Alan Stern
2025-11-20 9:18 ` David Laight
2025-11-20 14:39 ` [usb-storage] " Alan Stern
2025-11-19 22:41 ` [PATCH 29/44] drivers/xen: " david.laight.linux
2025-11-20 8:13 ` Jürgen Groß
2025-11-19 22:41 ` [PATCH 30/44] fs: use min() or umin() " david.laight.linux
2025-11-25 9:06 ` Christian Brauner
2026-01-12 21:51 ` Brian Masney
2026-01-13 9:42 ` David Laight
2026-01-13 16:56 ` Mark Brown
2026-01-13 18:33 ` David Laight
2026-01-13 19:10 ` Mark Brown
2026-01-13 19:24 ` David Laight
2025-11-19 22:41 ` [PATCH 31/44] block: bvec.h: use min() " david.laight.linux
2025-11-19 22:41 ` [PATCH 32/44] nodemask: " david.laight.linux
2025-11-20 15:19 ` Yury Norov
2025-11-19 22:41 ` [PATCH 33/44] ipc: " david.laight.linux
2025-11-19 22:41 ` [PATCH 34/44] bpf: " david.laight.linux
2025-11-19 22:41 ` [PATCH 35/44] " david.laight.linux
2025-11-19 22:41 ` [PATCH 36/44] lib/bucket_locks: " david.laight.linux
2025-11-19 22:41 ` [PATCH 37/44] lib/crypto/mpi: " david.laight.linux
2025-11-19 22:41 ` [PATCH 38/44] lib/dynamic_queue_limits: use max() instead of max_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 39/44] mm: use min() instead of min_t() david.laight.linux
2025-11-20 9:20 ` David Hildenbrand (Red Hat)
2025-11-20 9:59 ` David Laight
2025-11-20 23:45 ` Eric Biggers
2025-11-21 8:27 ` David Hildenbrand (Red Hat)
2025-11-21 9:15 ` David Laight
2025-11-20 10:36 ` Lorenzo Stoakes
2025-11-20 12:09 ` Lorenzo Stoakes
2025-11-20 12:55 ` David Laight
2025-11-20 13:42 ` David Hildenbrand (Red Hat)
2025-11-20 15:44 ` David Laight
2025-11-21 8:24 ` David Hildenbrand (Red Hat)
2025-11-19 22:41 ` [PATCH 40/44] net: Don't pass bitfields to max_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 41/44] net/core: Change loop conditions so min() can be used david.laight.linux
2025-11-19 22:41 ` [PATCH 42/44] net: use min() instead of min_t() david.laight.linux
2025-11-19 22:41 ` [PATCH 43/44] net/netlink: Use umin() to avoid min_t(int, ...) discarding high bits david.laight.linux
2025-11-19 22:41 ` [PATCH 44/44] net/mptcp: Change some dubious min_t(int, ...) to min() david.laight.linux
2025-12-18 17:33 ` Matthieu Baerts
2025-12-18 20:15 ` David Laight
2025-12-19 10:48 ` Matthieu Baerts
2025-11-20 1:47 ` [PATCH 00/44] Change a lot of min_t() that might mask high bits Jakub Kicinski
2025-11-20 9:38 ` Lorenzo Stoakes
2025-11-20 14:52 ` (subset) " Jens Axboe
2025-11-24 9:49 ` Herbert Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261029095544.06f396cf1a298d7eba11b557@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=dave@stgolabs.net \
--cc=david.laight.linux@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=manfred@colorfullife.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®