From: Kuniyuki Iwashima <kuniyu@google.com>
To: shakeel.butt@linux.dev
Cc: akpm@linux-foundation.org, davem@davemloft.net,
hannes@cmpxchg.org, kuni1840@gmail.com, kuniyu@google.com,
linux-mm@kvack.org, ncardwell@google.com,
vdavydov.dev@gmail.com
Subject: Re: [PATCH] memcg: Keep socket_pressure fresh on 32-bit kernel.
Date: Wed, 16 Jul 2025 21:16:37 +0000 [thread overview]
Message-ID: <20250716211845.354258-1-kuniyu@google.com> (raw)
In-Reply-To: <74yeh3tde3uzugxplhpr7mfvrtnoqoek2weqbwujegb53yybcw@lgqtoskse6ef>
From: Shakeel Butt <shakeel.butt@linux.dev>
Date: Wed, 16 Jul 2025 12:59:13 -0700
> On Wed, Jul 16, 2025 at 04:29:12AM +0000, Kuniyuki Iwashima wrote:
> > memcg->socket_pressure is initialised with jiffies when the memcg
> > is created.
> >
> > Once vmpressure detects that the cgroup is under memory pressure,
> > the field is updated with jiffies + HZ to signal the fact to the
> > socket layer and suppress memory allocation for one second.
> >
> > Otherwise, the field is not updated.
> >
> > mem_cgroup_under_socket_pressure() uses time_before() to check if
> > jiffies is less than memcg->socket_pressure, and this has a bug on
> > 32-bit kernel.
> >
> > if (time_before(jiffies, memcg->socket_pressure))
> > return true;
> >
> > As time_before() casts the final result to long, the acceptable delta
> > between two timestamps is 2 ^ (BITS_PER_LONG - 1).
> >
> > On 32-bit kernel with CONFIG_HZ=1000, this is about 24 days.
> >
> > >>> (2 ** 31) / 1000 / 60 / 60 / 24
> > 24.855134814814818
> >
> > Once 24 days have passed since the last update of socket_pressure,
> > mem_cgroup_under_socket_pressure() starts to lie until the next
> > 24 days pass.
> >
> > Thus, we need to update socket_pressure to a recent timestamp
> > periodically on 32-bit kernel.
> >
> > Let's do that every 24 hours, with a variation of about 0 to 4 hours.
> >
> > The variation is to avoid bursting by cgroups created within a small
> > timeframe, like boot-up.
> >
> > The work could be racy but does not take vmpr->sr_lock nor re-evaluate
> > vmpressure_calc_level() under the assumption that socket_pressure will
> > get updated soon if under memory pressure, because it persists only
> > for one second.
> >
> > Note that we don't need to worry about 64-bit machines unless they
> > serve for 300 million years.
> >
> > >>> (2 ** 63) / 1000 / 60 / 60 / 24 / 365
> > 292471208.6775361
> >
> > Fixes: 8e8ae645249b8 ("mm: memcontrol: hook up vmpressure to socket pressure")
> > Reported-by: Neal Cardwell <ncardwell@google.com>
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
>
> Is this a real issue which you have seen in the production?
No, this is a theoretical issue that Neal found a while ago.
> I wonder if
> we can just reset memcg->socket_pressure in
> mem_cgroup_under_socket_pressure() for 32 bit systems if we see
> overflow?
I guess you mean like below ? This doesn't work when jiffies is close
to ULONG_MAX and socket_pressure is close to 0 and the 'actual' time delta
is within HZ.
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 87b6688f124a7..2bf92514b67ff 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1609,6 +1609,10 @@ static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg)
return !!memcg->tcpmem_pressure;
#endif /* CONFIG_MEMCG_V1 */
do {
+#if BITS_PER_LONG == 32
+ if (jiffies - READ_ONCE(memcg->socket_pressure) > LONG_MAX) {
+ WRITE_ONCE(memcg->socket_pressure, jiffies - HZ);
+#endif
if (time_before(jiffies, READ_ONCE(memcg->socket_pressure)))
return true;
} while ((memcg = parent_mem_cgroup(memcg)));
next prev parent reply other threads:[~2025-07-16 21:18 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-16 4:29 Kuniyuki Iwashima
2025-07-16 19:59 ` Shakeel Butt
2025-07-16 21:16 ` Kuniyuki Iwashima [this message]
2025-07-16 22:43 ` Andrew Morton
2025-07-16 23:13 ` Kuniyuki Iwashima
2025-07-16 23:52 ` Andrew Morton
2025-07-16 23:58 ` Kuniyuki Iwashima
2025-07-17 1:02 ` Andrew Morton
2025-07-17 19:37 ` Kuniyuki Iwashima
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=20250716211845.354258-1-kuniyu@google.com \
--to=kuniyu@google.com \
--cc=akpm@linux-foundation.org \
--cc=davem@davemloft.net \
--cc=hannes@cmpxchg.org \
--cc=kuni1840@gmail.com \
--cc=linux-mm@kvack.org \
--cc=ncardwell@google.com \
--cc=shakeel.butt@linux.dev \
--cc=vdavydov.dev@gmail.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