Skip to contentSkip to conversation

memcg: Keep socket_pressure fresh on 32-bit kernel.

PATCH In review Kuniyuki Iwashima 3 participants 9 messages Jul 16, 2025 – Jul 17, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memcg: Keep socket_pressure fresh on 32-bit kernel.

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.

Quoted 1 line
Quoted 1 line
Quoted 1 line

(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.

Quoted 1 line
Quoted 1 line
Quoted 1 line

(2 ** 63) / 1000 / 60 / 60 / 24 / 365

292471208.6775361

Fixes 8e8ae645249b8 ("mm: memcontrol: hook up vmpressure to socket pressure")
---
 include/linux/vmpressure.h |  3 +++
 mm/vmpressure.c            | 52 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
include/linux/vmpressure.h +3 -0
index 6a2f51ebbfd35..946c1b284d4d4 100644
@@ -25,6 +25,9 @@ struct vmpressure {
 	struct mutex events_lock;
 
 	struct work_struct work;
+#if BITS_PER_LONG == 32
+	struct delayed_work delayed_work;
+#endif
 };
 
 struct mem_cgroup;
mm/vmpressure.c +52 -0

68 lines not on this page · raw message

Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
maintainer
#1 lore raw
Quoted 44 lines · Kuniyuki Iwashima

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.

Quoted 1 line
Quoted 1 line
Quoted 1 line

(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.

Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

(2 ** 63) / 1000 / 60 / 60 / 24 / 365

292471208.6775361

Fixes 8e8ae645249b8 ("mm: memcontrol: hook up vmpressure to socket pressure")

Is this a real issue which you have seen in the production? I wonder if we can just reset memcg->socket_pressure in mem_cgroup_under_socket_pressure() for 32 bit systems if we see overflow?

#2 lore raw

From: Shakeel Butt <[email protected]>
Date: Wed, 16 Jul 2025 12:59:13 -0700

Quoted 45 lines · Kuniyuki Iwashima
Quoted 44 lines

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.

Quoted 1 line
Quoted 1 line
Quoted 1 line

(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.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

(2 ** 63) / 1000 / 60 / 60 / 24 / 365

292471208.6775361

Fixes 8e8ae645249b8 ("mm: memcontrol: hook up vmpressure to socket pressure")

Is this a real issue which you have seen in the production?

No, this is a theoretical issue that Neal found a while ago.

Quoted 4 lines

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.

include/linux/memcontrol.h +4 -0
index 87b6688f124a7..2bf92514b67ff 100644
@@ -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)));
maintainer
#3 lore raw
In reply to Kuniyuki Iwashima
Quoted 24 lines · On Wed, 16 Jul 2025 04:29:12 +0000 Kuniyuki Iwashima <[email protected]> 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.

Quoted 1 line
Quoted 1 line
Quoted 1 line

(2 ** 31) / 1000 / 60 / 60 / 24

24.855134814814818

Quoted 8 lines

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.

Can't we simply convert ->socket_preesure to a 64-bit type? timespec64/time64_t/etc?

#4 lore raw
Quoted 31 lines · Andrew Morton
Quoted 28 lines · On Wed, 16 Jul 2025 04:29:12 +0000 Kuniyuki Iwashima <[email protected]> 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.

Quoted 1 line
Quoted 1 line
Quoted 1 line

(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.

Quoted 7 lines
Quoted 4 lines

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.

Can't we simply convert ->socket_preesure to a 64-bit type? timespec64/time64_t/etc?

I think it's doable with get_jiffies_64() & time_before64().

My thought was a delayed work would be better than adding two seqlock in the networking fast path.

maintainer
#5 lore raw
Quoted 5 lines · On Wed, 16 Jul 2025 16:13:54 -0700 Kuniyuki Iwashima <[email protected]> wrote:
Quoted 5 lines
Quoted 4 lines

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.

Quoted 7 lines
Quoted 2 lines

Can't we simply convert ->socket_preesure to a 64-bit type? timespec64/time64_t/etc?

I think it's doable with get_jiffies_64() & time_before64().

My thought was a delayed work would be better than adding two seqlock in the networking fast path.

Is it on a very fast path? mem_cgroup_under_socket_pressure() itself doesn't look very fastpath-friendly and seqcounts are fast. Bearing in mind that this affects 32-bit machines only.

If a get_jiffies_64() call is demonstrated to be a performance issue then perhaps there's something sneaky we can do along the lines of reading jiffies_64 directly then falling back to get_jiffies_64() in the rare something-went-wrong path. Haven't thought about it :)

Dunno, the proposed patch just feels like overkill for a silly 32/66-bit issue?

#6 lore raw
Quoted 20 lines · Andrew Morton
Quoted 5 lines · On Wed, 16 Jul 2025 16:13:54 -0700 Kuniyuki Iwashima <[email protected]> wrote:
Quoted 5 lines
Quoted 4 lines

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.

Quoted 7 lines
Quoted 2 lines

Can't we simply convert ->socket_preesure to a 64-bit type? timespec64/time64_t/etc?

I think it's doable with get_jiffies_64() & time_before64().

My thought was a delayed work would be better than adding two seqlock in the networking fast path.

Is it on a very fast path? mem_cgroup_under_socket_pressure() itself doesn't look very fastpath-friendly and seqcounts are fast. Bearing in mind that this affects 32-bit machines only.

Quoted 7 lines

If a get_jiffies_64() call is demonstrated to be a performance issue then perhaps there's something sneaky we can do along the lines of reading jiffies_64 directly then falling back to get_jiffies_64() in the rare something-went-wrong path. Haven't thought about it :)

Dunno, the proposed patch just feels like overkill for a silly 32/66-bit issue?

Fair enough, I'll make it u64 in v2.

Thank you!

maintainer
#7 lore raw
Quoted 26 lines · On Wed, 16 Jul 2025 16:58:29 -0700 Kuniyuki Iwashima <[email protected]> wrote:
Quoted 25 lines · Andrew Morton
Quoted 5 lines · On Wed, 16 Jul 2025 16:13:54 -0700 Kuniyuki Iwashima <[email protected]> wrote:
Quoted 5 lines
Quoted 4 lines

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.

Quoted 7 lines
Quoted 2 lines

Can't we simply convert ->socket_preesure to a 64-bit type? timespec64/time64_t/etc?

I think it's doable with get_jiffies_64() & time_before64().

My thought was a delayed work would be better than adding two seqlock in the networking fast path.

Is it on a very fast path? mem_cgroup_under_socket_pressure() itself doesn't look very fastpath-friendly and seqcounts are fast. Bearing in mind that this affects 32-bit machines only.

If a get_jiffies_64() call is demonstrated to be a performance issue then perhaps there's something sneaky we can do along the lines of reading jiffies_64 directly then falling back to get_jiffies_64() in the rare something-went-wrong path. Haven't thought about it :)

Quoted 5 lines
Quoted 2 lines

Dunno, the proposed patch just feels like overkill for a silly 32/66-bit issue?

Fair enough, I'll make it u64 in v2.

Well, please do pay attention to any performance impact. Measurements with some simple microbenchmark if possible?

#8 lore raw
Quoted 29 lines · Andrew Morton
Quoted 26 lines · On Wed, 16 Jul 2025 16:58:29 -0700 Kuniyuki Iwashima <[email protected]> wrote:
Quoted 25 lines · Andrew Morton
Quoted 5 lines · On Wed, 16 Jul 2025 16:13:54 -0700 Kuniyuki Iwashima <[email protected]> wrote:
Quoted 5 lines
Quoted 4 lines

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.

Quoted 7 lines
Quoted 2 lines

Can't we simply convert ->socket_preesure to a 64-bit type? timespec64/time64_t/etc?

I think it's doable with get_jiffies_64() & time_before64().

My thought was a delayed work would be better than adding two seqlock in the networking fast path.

Is it on a very fast path? mem_cgroup_under_socket_pressure() itself doesn't look very fastpath-friendly and seqcounts are fast. Bearing in mind that this affects 32-bit machines only.

If a get_jiffies_64() call is demonstrated to be a performance issue then perhaps there's something sneaky we can do along the lines of reading jiffies_64 directly then falling back to get_jiffies_64() in the rare something-went-wrong path. Haven't thought about it :)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Dunno, the proposed patch just feels like overkill for a silly 32/66-bit issue?

Fair enough, I'll make it u64 in v2.

Well, please do pay attention to any performance impact. Measurements with some simple microbenchmark if possible?

I don't have a real 32-bit machine so this is a result on QEMU, but with/without the u64 jiffie patch, the time spent in mem_cgroup_under_socket_pressure() was 1~5us and I didn't see any measurable delta.

no patch applied:
iperf3 273 [000] 137.296248:
probe:mem_cgroup_under_socket_pressure: (c13660d0)

                c13660d1 mem_cgroup_under_socket_pressure+0x1

([kernel.kallsyms])
iperf3 273 [000] 137.296249:
probe:mem_cgroup_under_socket_pressure__return: (c13660d0 <- c1d8fd7f)
iperf3 273 [000] 137.296251:
probe:mem_cgroup_under_socket_pressure: (c13660d0)

                c13660d1 mem_cgroup_under_socket_pressure+0x1

([kernel.kallsyms])
iperf3 273 [000] 137.296253:
probe:mem_cgroup_under_socket_pressure__return: (c13660d0 <- c1d8fd7f)

u64 jiffies patch applied:
iperf3 308 [001] 330.669370:
probe:mem_cgroup_under_socket_pressure: (c12ddba0)

                c12ddba1 mem_cgroup_under_socket_pressure+0x1

([kernel.kallsyms])
iperf3 308 [001] 330.669371:
probe:mem_cgroup_under_socket_pressure__return: (c12ddba0 <- c1ce98bf)
iperf3 308 [001] 330.669382:
probe:mem_cgroup_under_socket_pressure: (c12ddba0)

                c12ddba1 mem_cgroup_under_socket_pressure+0x1

([kernel.kallsyms])
iperf3 308 [001] 330.669384:
probe:mem_cgroup_under_socket_pressure__return: (c12ddba0 <- c1ce98bf)

So, the u64 approach is good enough :)

Thanks!