Skip to contentSkip to conversation

memcg: expose socket memory pressure in a cgroup

PATCH In review Daniel Sedlak 6 participants 21 messages Oct 7, 2025 – Oct 15, 2025

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

PATCH v5 1/1 memcg: expose socket memory pressure in a cgroup

This patch is a result of our long-standing debug sessions, where it all started as "networking is slow", and TCP network throughput suddenly dropped from tens of Gbps to few Mbps, and we could not see anything in the kernel log or netstat counters.

Currently, we have two memory pressure counters for TCP sockets [1], which we manipulate only when the memory pressure is signalled through the proto struct [2]. However, the memory pressure can also be signaled through the cgroup memory subsystem, which we do not reflect in the netstat counters. In the end, when the cgroup memory subsystem signals that it is under pressure, we silently reduce the advertised TCP window with tcp_adjust_rcv_ssthresh() to 4*advmss, which causes a significant throughput reduction.

Keep in mind that when the cgroup memory subsystem signals the socket memory pressure for a given cgroup, it affects all sockets used in that cgroup, including children cgroups.

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Just to summarize the proposals of different methods of hierarchical propagation of the memory.net.throttled_usec.

1) None - keeping the reported duration local to that cgroup:

value = self

Would not be too out of place, since memory.events.local already does not accumulate hierarchically.
To determine whether sockets in a memcg were throttled, we would traverse the /sys/fs/cgroup/ hierarchy from root to the cgroup of interest and sum those local durations.

2) Propagating the duration upwards (using rstat or simple iteration towards root memcg during write):

value = self + sum of children

Most semantically consistent with other exposed stat files. Could be added as an entry into memory.stat.
Since the pressure gets applied from ancestors to children (see mem_cgroup_under_socket_pressure()), determining the duration of throttling for sockets in some cgroup would be hardest in this variant.

It would involve iterating from the root to the examined cgroup and at each node subtracting the values of its children from that nodes value, then the sum of that would correspond to the total duration throttled.

3) Propagating the duration downwards (write only locally, read traversing hierarchy upwards):

value = self + sum of ancestors

Mirrors the logic used in mem_cgroup_under_socket_pressure(), increase in the reported value for a memcg would coincide with more throttling being done to the sockets of that memcg.

We chose variant 1, that is why it is a separate file instead of another counter in mem.stat. Variant 2 seems to be most fitting however the calculated value would be misleading and hard to interpret. Ideally, we would go with variant 3 as this mirrors the logic of mem_cgroup_under_socket_pressure(), but the third variant can be also calculated manually from variant 1, and thus we chose the variant 1 as it is the most versatile one without leaking the internal implementation that can change in the future.

---
Sorry for the delay between the versions.

Changes:
v4 -> v5:
- Rebased
- Extend commit message with design decisions
- Rename cgroup counter
- Link to v4: https://lore.kernel.org/netdev/[email protected]/

v3 -> v4:
- Add documentation
- Expose pressure as cummulative counter in microseconds
- Link to v3: https://lore.kernel.org/netdev/[email protected]/

v2 -> v3:
- Expose the socket memory pressure on the cgroups instead of netstat
- Split patch
- Link to v2: https://lore.kernel.org/netdev/[email protected]/

v1 -> v2:
- Add tracepoint
- Link to v1: https://lore.kernel.org/netdev/[email protected]/

 Documentation/admin-guide/cgroup-v2.rst | 10 ++++++
 include/linux/memcontrol.h              | 41 +++++++++++++++----------
 mm/memcontrol.c                         | 31 ++++++++++++++++++-
 3 files changed, 65 insertions(+), 17 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +10 -0
index 51c0bc4c2dc5..fe81a134c156 100644
@@ -1887,6 +1887,16 @@ The following nested keys are defined.
 	Shows pressure stall information for memory. See
 	:ref:`Documentation/accounting/psi.rst <psi>` for details.
 
+  memory.net.throttled_usec
+	A read-only single value file showing how many microseconds this cgroup
+	contributed to throttling the throughput of network sockets.
+
+	Socket throttling is applied to a cgroup and to all its children,
+	as a consequence of high reclaim pressure.
+
+	Observing throttling of sockets in a particular cgroup can be done
+	by checking this file for that cgroup and also for all its ancestors.
+
 
 Usage Guidelines
 ~~~~~~~~~~~~~~~~
include/linux/memcontrol.h +25 -16

76 lines not on this page · raw message

mm/memcontrol.c +30 -1
index df3e9205c9e6..d29147223822 100644
@@ -3755,7 +3755,10 @@ static struct mem_cgroup *mem_cgroup_alloc(struct mem_cgroup *parent)
 	INIT_LIST_HEAD(&memcg->swap_peaks);
 	spin_lock_init(&memcg->peaks_lock);
 	memcg->socket_pressure = get_jiffies_64();
-#if BITS_PER_LONG < 64
+	memcg->socket_pressure_duration = 0;
+#if BITS_PER_LONG >= 64
+	spin_lock_init(&memcg->socket_pressure_spinlock);
+#else
 	seqlock_init(&memcg->socket_pressure_seqlock);
 #endif
 	memcg1_memcg_init(memcg);
@@ -4579,6 +4582,27 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
 	return nbytes;
 }
 
+static int memory_net_throttled_usec_show(struct seq_file *m, void *v)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
+	u64 throttled_usec;
+
+#if BITS_PER_LONG >= 64
+	throttled_usec = READ_ONCE(memcg->socket_pressure_duration);
+#else
+	unsigned int seq;
+
+	do {
+		seq = read_seqbegin(&memcg->socket_pressure_seqlock);
+		throttled_usec = memcg->socket_pressure_duration;
+	} while (read_seqretry(&memcg->socket_pressure_seqlock, seq));
+#endif
+
+	seq_printf(m, "%llu\n", throttled_usec);
+
+	return 0;
+}
+
 static struct cftype memory_files[] = {
 	{
 		.name = "current",
@@ -4650,6 +4674,11 @@ static struct cftype memory_files[] = {
 		.flags = CFTYPE_NS_DELEGATABLE,
 		.write = memory_reclaim,
 	},
+	{
+		.name = "net.throttled_usec",
+		.flags = CFTYPE_NOT_ON_ROOT,
+		.seq_show = memory_net_throttled_usec_show,
+	},
 	{ }	/* terminate */
 };
 

base-commit: 312e6f7676e63bbb9b81e5c68e580a9f776cc6f0

Signature · 1 line
2.39.5
maintainer
#1 lore raw

On Tue, Oct 07, 2025 at 02:50:56PM +0200, Daniel Sedlak wrote:
...

Quoted 4 lines

1) None - keeping the reported duration local to that cgroup:
2) Propagating the duration upwards (using rstat or simple iteration
3) Propagating the duration downwards (write only locally, read traversing hierarchy upwards):

...

Quoted 8 lines

We chose variant 1, that is why it is a separate file instead of another counter in mem.stat. Variant 2 seems to be most fitting however the calculated value would be misleading and hard to interpret. Ideally, we would go with variant 3 as this mirrors the logic of mem_cgroup_under_socket_pressure(), but the third variant can be also calculated manually from variant 1, and thus we chose the variant 1 as it is the most versatile one without leaking the internal implementation that can change in the future.

I'm not against going 1) but let's not do a separate file for this. Can't you do memory.stat.local? It'd be better to have aggregation in memory.stat but we can worry about that later.

Thanks.

Signature · 1 line
tejun
#2 lore raw

Hello,

Quoted 2 lines

I'm not against going 1) but let's not do a separate file for this. Can't you do memory.stat.local?

I can't find memory.stat.local, so should we create it and add the counter as an entry there?

Regarding the code, is there anything you would like us to improve? I had to rewrite it a bit

because of the recent changes.

Thanks,

Matyas

maintainer
#3 lore raw
Quoted 6 lines · Matyas Hurtik

Hello,

Quoted 2 lines

I'm not against going 1) but let's not do a separate file for this. Can't you do memory.stat.local?

I can't find memory.stat.local, so should we create it and add the counter as an entry there?

Yes.

Quoted 4 lines

Regarding the code, is there anything you would like us to improve? I had to rewrite it a bit

because of the recent changes.

I think memcg guys will have much better idea on the actual code. Let's wait for them to chime in.

Thanks.

Signature · 1 line
tejun
maintainer
#4 lore raw
In reply to Daniel Sedlak
Quoted 18 lines · Daniel Sedlak

This patch is a result of our long-standing debug sessions, where it all started as "networking is slow", and TCP network throughput suddenly dropped from tens of Gbps to few Mbps, and we could not see anything in the kernel log or netstat counters.

Currently, we have two memory pressure counters for TCP sockets [1], which we manipulate only when the memory pressure is signalled through the proto struct [2]. However, the memory pressure can also be signaled through the cgroup memory subsystem, which we do not reflect in the netstat counters. In the end, when the cgroup memory subsystem signals that it is under pressure, we silently reduce the advertised TCP window with tcp_adjust_rcv_ssthresh() to 4*advmss, which causes a significant throughput reduction.

Keep in mind that when the cgroup memory subsystem signals the socket memory pressure for a given cgroup, it affects all sockets used in that cgroup, including children cgroups.

Quoted 6 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!

How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Thanks!

#5 lore raw

Hi Roman,

Quoted 5 lines · Roman Gushchin
Quoted 5 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

Quoted 7 lines
Quoted 1 line

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!

How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Just incrementing a counter each time the vmpressure() happens IMO provides bad semantics of what is actually happening, because it can hide important details, mainly the _time_ for how long the network traffic was slowed down.

For example, when memory.events::net_throttled=1000, it can mean that the network was slowed down for 1 second or 1000 seconds or something between, and the memory.net.throttled_usec proposed by this patch disambiguates it.

In addition, v1/v2 of this series started that way, then from v3 we rewrote it to calculate the duration instead, which proved to be better information for debugging, as it is easier to understand implications.

Thanks!
Daniel

maintainer
#6 lore raw
Quoted 24 lines · Daniel Sedlak

Hi Roman,

Quoted 10 lines · Roman Gushchin
Quoted 6 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!
How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Just incrementing a counter each time the vmpressure() happens IMO provides bad semantics of what is actually happening, because it can hide important details, mainly the _time_ for how long the network traffic was slowed down.

For example, when memory.events::net_throttled=1000, it can mean that the network was slowed down for 1 second or 1000 seconds or something between, and the memory.net.throttled_usec proposed by this patch disambiguates it.

Quoted 4 lines

In addition, v1/v2 of this series started that way, then from v3 we rewrote it to calculate the duration instead, which proved to be better information for debugging, as it is easier to understand implications.

But how are you planning to use this information? Is this just "networking is under pressure for non-trivial amount of time -> raise the memcg limit" or something more complicated?

I am bit concerned about making this metric the part of cgroup API simple because it's too implementation-defined and in my opinion lack the fundamental meaning.

Vmpressure is calculated based on scanned/reclaimed ratio (which is also not always the best proxy for the memory pressure level), then if it reaches some level we basically throttle networking for 1s. So it's all very arbitrary.

I totally get it from the debugging perspective, but not sure about usefulness of it as a permanent metric. This is why I'm asking if there are lighter alternatives, e.g. memory.events or maybe even tracepoints.

Thanks!

maintainer
#7 lore raw
Quoted 39 lines · Roman Gushchin
Quoted 24 lines · Daniel Sedlak

Hi Roman,

Quoted 10 lines · Roman Gushchin
Quoted 6 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!
How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Just incrementing a counter each time the vmpressure() happens IMO provides bad semantics of what is actually happening, because it can hide important details, mainly the _time_ for how long the network traffic was slowed down.

For example, when memory.events::net_throttled=1000, it can mean that the network was slowed down for 1 second or 1000 seconds or something between, and the memory.net.throttled_usec proposed by this patch disambiguates it.

Quoted 4 lines

In addition, v1/v2 of this series started that way, then from v3 we rewrote it to calculate the duration instead, which proved to be better information for debugging, as it is easier to understand implications.

But how are you planning to use this information? Is this just "networking is under pressure for non-trivial amount of time -> raise the memcg limit" or something more complicated?

I am bit concerned about making this metric the part of cgroup API simple because it's too implementation-defined and in my opinion lack the fundamental meaning.

Quoted 9 lines

Vmpressure is calculated based on scanned/reclaimed ratio (which is also not always the best proxy for the memory pressure level), then if it reaches some level we basically throttle networking for 1s. So it's all very arbitrary.

I totally get it from the debugging perspective, but not sure about usefulness of it as a permanent metric. This is why I'm asking if there are lighter alternatives, e.g. memory.events or maybe even tracepoints.

I also have a very similar opinion that if we expose the current implementation detail through a stable interface, we might get stuck with this implementation and I want to change this in future.

Coming back to what information should we expose that will be helpful for Daniel & Matyas and will be beneficial in general. After giving some thought, I think the time "network was slowed down" or more specifically time window when mem_cgroup_sk_under_memory_pressure() returns true might not be that useful without the actual network activity. Basically if no one is calling mem_cgroup_sk_under_memory_pressure() and doing some actions, the time window is not that useful.

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

WDYT?

maintainer
#8 lore raw
Quoted 62 lines · Shakeel Butt
Quoted 39 lines · Roman Gushchin
Quoted 24 lines · Daniel Sedlak

Hi Roman,

Quoted 10 lines · Roman Gushchin
Quoted 6 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!
How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Just incrementing a counter each time the vmpressure() happens IMO provides bad semantics of what is actually happening, because it can hide important details, mainly the _time_ for how long the network traffic was slowed down.

For example, when memory.events::net_throttled=1000, it can mean that the network was slowed down for 1 second or 1000 seconds or something between, and the memory.net.throttled_usec proposed by this patch disambiguates it.

Quoted 4 lines

In addition, v1/v2 of this series started that way, then from v3 we rewrote it to calculate the duration instead, which proved to be better information for debugging, as it is easier to understand implications.

But how are you planning to use this information? Is this just "networking is under pressure for non-trivial amount of time -> raise the memcg limit" or something more complicated?

I am bit concerned about making this metric the part of cgroup API simple because it's too implementation-defined and in my opinion lack the fundamental meaning.

Quoted 9 lines

Vmpressure is calculated based on scanned/reclaimed ratio (which is also not always the best proxy for the memory pressure level), then if it reaches some level we basically throttle networking for 1s. So it's all very arbitrary.

I totally get it from the debugging perspective, but not sure about usefulness of it as a permanent metric. This is why I'm asking if there are lighter alternatives, e.g. memory.events or maybe even tracepoints.

I also have a very similar opinion that if we expose the current implementation detail through a stable interface, we might get stuck with this implementation and I want to change this in future.

Coming back to what information should we expose that will be helpful for Daniel & Matyas and will be beneficial in general. After giving some thought, I think the time "network was slowed down" or more specifically time window when mem_cgroup_sk_under_memory_pressure() returns true might not be that useful without the actual network activity. Basically if no one is calling mem_cgroup_sk_under_memory_pressure() and doing some actions, the time window is not that useful.

Quoted 7 lines

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

WDYT?

I feel like if it's mostly intended for debugging purposes, a combination of a trace point and bpftrace can work pretty well, so there is no need to create a new sysfs interface.

maintainer
#9 lore raw
Quoted 70 lines · Roman Gushchin
Quoted 68 lines · Shakeel Butt
Quoted 39 lines · Roman Gushchin
Quoted 24 lines · Daniel Sedlak

Hi Roman,

Quoted 10 lines · Roman Gushchin
Quoted 6 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!
How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Just incrementing a counter each time the vmpressure() happens IMO provides bad semantics of what is actually happening, because it can hide important details, mainly the _time_ for how long the network traffic was slowed down.

For example, when memory.events::net_throttled=1000, it can mean that the network was slowed down for 1 second or 1000 seconds or something between, and the memory.net.throttled_usec proposed by this patch disambiguates it.

Quoted 4 lines

In addition, v1/v2 of this series started that way, then from v3 we rewrote it to calculate the duration instead, which proved to be better information for debugging, as it is easier to understand implications.

But how are you planning to use this information? Is this just "networking is under pressure for non-trivial amount of time -> raise the memcg limit" or something more complicated?

I am bit concerned about making this metric the part of cgroup API simple because it's too implementation-defined and in my opinion lack the fundamental meaning.

Quoted 9 lines

Vmpressure is calculated based on scanned/reclaimed ratio (which is also not always the best proxy for the memory pressure level), then if it reaches some level we basically throttle networking for 1s. So it's all very arbitrary.

I totally get it from the debugging perspective, but not sure about usefulness of it as a permanent metric. This is why I'm asking if there are lighter alternatives, e.g. memory.events or maybe even tracepoints.

I also have a very similar opinion that if we expose the current implementation detail through a stable interface, we might get stuck with this implementation and I want to change this in future.

Coming back to what information should we expose that will be helpful for Daniel & Matyas and will be beneficial in general. After giving some thought, I think the time "network was slowed down" or more specifically time window when mem_cgroup_sk_under_memory_pressure() returns true might not be that useful without the actual network activity. Basically if no one is calling mem_cgroup_sk_under_memory_pressure() and doing some actions, the time window is not that useful.

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

Quoted 6 lines
Quoted 1 line

WDYT?

I feel like if it's mostly intended for debugging purposes, a combination of a trace point and bpftrace can work pretty well, so there is no need to create a new sysfs interface.

Definitely not a new interface but I think having such information in memory.events or memory.stat would be more convenient. Basically the number of times the sockets in this memcg have to be clamped due to memory pressure would be useful in general.

maintainer
#10 lore raw
Quoted 78 lines · Shakeel Butt
Quoted 70 lines · Roman Gushchin
Quoted 68 lines · Shakeel Butt
Quoted 39 lines · Roman Gushchin
Quoted 24 lines · Daniel Sedlak

Hi Roman,

Quoted 10 lines · Roman Gushchin
Quoted 6 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!
How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Just incrementing a counter each time the vmpressure() happens IMO provides bad semantics of what is actually happening, because it can hide important details, mainly the _time_ for how long the network traffic was slowed down.

For example, when memory.events::net_throttled=1000, it can mean that the network was slowed down for 1 second or 1000 seconds or something between, and the memory.net.throttled_usec proposed by this patch disambiguates it.

Quoted 4 lines

In addition, v1/v2 of this series started that way, then from v3 we rewrote it to calculate the duration instead, which proved to be better information for debugging, as it is easier to understand implications.

But how are you planning to use this information? Is this just "networking is under pressure for non-trivial amount of time -> raise the memcg limit" or something more complicated?

I am bit concerned about making this metric the part of cgroup API simple because it's too implementation-defined and in my opinion lack the fundamental meaning.

Quoted 9 lines

Vmpressure is calculated based on scanned/reclaimed ratio (which is also not always the best proxy for the memory pressure level), then if it reaches some level we basically throttle networking for 1s. So it's all very arbitrary.

I totally get it from the debugging perspective, but not sure about usefulness of it as a permanent metric. This is why I'm asking if there are lighter alternatives, e.g. memory.events or maybe even tracepoints.

I also have a very similar opinion that if we expose the current implementation detail through a stable interface, we might get stuck with this implementation and I want to change this in future.

Coming back to what information should we expose that will be helpful for Daniel & Matyas and will be beneficial in general. After giving some thought, I think the time "network was slowed down" or more specifically time window when mem_cgroup_sk_under_memory_pressure() returns true might not be that useful without the actual network activity. Basically if no one is calling mem_cgroup_sk_under_memory_pressure() and doing some actions, the time window is not that useful.

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

Quoted 6 lines
Quoted 1 line

WDYT?

I feel like if it's mostly intended for debugging purposes, a combination of a trace point and bpftrace can work pretty well, so there is no need to create a new sysfs interface.

Quoted 4 lines

Definitely not a new interface but I think having such information in memory.events or memory.stat would be more convenient. Basically the number of times the sockets in this memcg have to be clamped due to memory pressure would be useful in general.

Yeah, if we're going to add something, memory.events looks like the best option, also because it allows to poll and get notified when the event occurs.

#11 lore raw
Quoted 32 lines · Roman Gushchin
Quoted 30 lines · Shakeel Butt
Quoted 29 lines · Roman Gushchin
Quoted 27 lines · Shakeel Butt
Quoted 26 lines · Roman Gushchin
Quoted 24 lines · Daniel Sedlak

Hi Roman,

Quoted 10 lines · Roman Gushchin
Quoted 6 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!
How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Just incrementing a counter each time the vmpressure() happens IMO provides bad semantics of what is actually happening, because it can hide important details, mainly the _time_ for how long the network traffic was slowed down.

For example, when memory.events::net_throttled=1000, it can mean that the network was slowed down for 1 second or 1000 seconds or something between, and the memory.net.throttled_usec proposed by this patch disambiguates it.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

In addition, v1/v2 of this series started that way, then from v3 we rewrote it to calculate the duration instead, which proved to be better information for debugging, as it is easier to understand implications.

But how are you planning to use this information? Is this just "networking is under pressure for non-trivial amount of time -> raise the memcg limit" or something more complicated?

We plan to use it mostly for observability purposes and to better understand which traffic patterns affect the socket pressure the most (so we can try to fix/delay/improve it). We do not know how commonly this issue appears in other deployments, but in our deployment, many of servers were affected by this slowdown, which varied in terms of hardware and software configuration. Currently, it is very hard to detect if the socket is under pressure without using tools like bpftrace, so we would like to expose this metric in a more accessible way. So in the end, we do not really care in which file this "socket pressure happened" notification will be stored.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I totally get it from the debugging perspective, but not sure about usefulness of it as a permanent metric. This is why I'm asking if there are lighter alternatives, e.g. memory.events or maybe even tracepoints.

If the combination of memory.events(.local) and tracepoint hook(s) is okay with you(?), we can use that and export the same information as in the current patch version. We can incorporate that into the next version.

Also, would it be possible to make the socket pressure signal configurable, e.g., allowing it to be configured via sysctl or per cgroup not to trigger the socket pressure signal? I cannot find the reasoning why this throttling cannot (maybe it can) be opt-out.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

I also have a very similar opinion that if we expose the current implementation detail through a stable interface, we might get stuck with this implementation and I want to change this in future.

Coming back to what information should we expose that will be helpful for Daniel & Matyas and will be beneficial in general. After giving some thought, I think the time "network was slowed down" or more specifically time window when mem_cgroup_sk_under_memory_pressure() returns true might not be that useful without the actual network activity. Basically if no one is calling mem_cgroup_sk_under_memory_pressure() and doing some actions, the time window is not that useful.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

I am not against it, but I feel that conveying those tracked actions (or how to represent them) to the user will be much harder. Are there already existing APIs to push this information to the user?

Thanks!
Daniel.

maintainer
#12 lore raw
Quoted 52 lines · Daniel Sedlak
Quoted 32 lines · Roman Gushchin
Quoted 30 lines · Shakeel Butt
Quoted 29 lines · Roman Gushchin
Quoted 27 lines · Shakeel Butt
Quoted 26 lines · Roman Gushchin
Quoted 24 lines · Daniel Sedlak

Hi Roman,

Quoted 10 lines · Roman Gushchin
Quoted 6 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!
How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Just incrementing a counter each time the vmpressure() happens IMO provides bad semantics of what is actually happening, because it can hide important details, mainly the _time_ for how long the network traffic was slowed down.

For example, when memory.events::net_throttled=1000, it can mean that the network was slowed down for 1 second or 1000 seconds or something between, and the memory.net.throttled_usec proposed by this patch disambiguates it.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

In addition, v1/v2 of this series started that way, then from v3 we rewrote it to calculate the duration instead, which proved to be better information for debugging, as it is easier to understand implications.

But how are you planning to use this information? Is this just "networking is under pressure for non-trivial amount of time -> raise the memcg limit" or something more complicated?

We plan to use it mostly for observability purposes and to better understand which traffic patterns affect the socket pressure the most (so we can try to fix/delay/improve it). We do not know how commonly this issue appears in other deployments, but in our deployment, many of servers were affected by this slowdown, which varied in terms of hardware and software configuration. Currently, it is very hard to detect if the socket is under pressure without using tools like bpftrace, so we would like to expose this metric in a more accessible way. So in the end, we do not really care in which file this "socket pressure happened" notification will be stored.

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I totally get it from the debugging perspective, but not sure about usefulness of it as a permanent metric. This is why I'm asking if there are lighter alternatives, e.g. memory.events or maybe even tracepoints.

If the combination of memory.events(.local) and tracepoint hook(s) is okay with you(?), we can use that and export the same information as in the current patch version. We can incorporate that into the next version.

In my opinion
tracepoint > memory.events entry > memory.stat entry > new cgroupfs file.

Quoted 5 lines

Also, would it be possible to make the socket pressure signal configurable, e.g., allowing it to be configured via sysctl or per cgroup not to trigger the socket pressure signal? I cannot find the reasoning why this throttling cannot (maybe it can) be opt-out.

It's a good point.

First, I think that vmpressure implementation is not the best and we might want to switch to PSI (or something else) there. This is why I'm resistant to exposing implementation-specific metrics.

That said, I believe that some level of customization here is justified. Maybe opting out completely is too much, but in the end it's hard for the kernel to balance the importance of e.g. page cache vs networking buffers as it might be really workload-dependent. Or some workloads would prefer to risk being oom-killed rather than to tolerate a sub-par networking performance.

Thanks!

#13 lore raw
Quoted 58 lines · Roman Gushchin
Quoted 56 lines · Daniel Sedlak
Quoted 32 lines · Roman Gushchin
Quoted 30 lines · Shakeel Butt
Quoted 29 lines · Roman Gushchin
Quoted 27 lines · Shakeel Butt
Quoted 26 lines · Roman Gushchin
Quoted 24 lines · Daniel Sedlak

Hi Roman,

Quoted 10 lines · Roman Gushchin
Quoted 6 lines

This patch exposes a new file for each cgroup in sysfs which is a read-only single value file showing how many microseconds this cgroup contributed to throttling the throughput of network sockets. The file is accessible in the following path.

/sys/fs/cgroup/**/<cgroup name>/memory.net.throttled_usec

Hi Daniel!
How this value is going to be used? In other words, do you need an exact number or something like memory.events::net_throttled would be enough for your case?

Just incrementing a counter each time the vmpressure() happens IMO provides bad semantics of what is actually happening, because it can hide important details, mainly the _time_ for how long the network traffic was slowed down.

For example, when memory.events::net_throttled=1000, it can mean that the network was slowed down for 1 second or 1000 seconds or something between, and the memory.net.throttled_usec proposed by this patch disambiguates it.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

In addition, v1/v2 of this series started that way, then from v3 we rewrote it to calculate the duration instead, which proved to be better information for debugging, as it is easier to understand implications.

But how are you planning to use this information? Is this just "networking is under pressure for non-trivial amount of time -> raise the memcg limit" or something more complicated?

We plan to use it mostly for observability purposes and to better understand which traffic patterns affect the socket pressure the most (so we can try to fix/delay/improve it). We do not know how commonly this issue appears in other deployments, but in our deployment, many of servers were affected by this slowdown, which varied in terms of hardware and software configuration. Currently, it is very hard to detect if the socket is under pressure without using tools like bpftrace, so we would like to expose this metric in a more accessible way. So in the end, we do not really care in which file this "socket pressure happened" notification will be stored.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I totally get it from the debugging perspective, but not sure about usefulness of it as a permanent metric. This is why I'm asking if there are lighter alternatives, e.g. memory.events or maybe even tracepoints.

Quoted 7 lines
Quoted 4 lines

If the combination of memory.events(.local) and tracepoint hook(s) is okay with you(?), we can use that and export the same information as in the current patch version. We can incorporate that into the next version.

In my opinion
tracepoint > memory.events entry > memory.stat entry > new cgroupfs file.

Thanks, noted, we will incorporate it to the next version.

Quoted 12 lines
Quoted 4 lines

Also, would it be possible to make the socket pressure signal configurable, e.g., allowing it to be configured via sysctl or per cgroup not to trigger the socket pressure signal? I cannot find the reasoning why this throttling cannot (maybe it can) be opt-out.

It's a good point.

First, I think that vmpressure implementation is not the best and we might want to switch to PSI (or something else) there. This is why I'm resistant to exposing implementation-specific metrics.

Quoted 6 lines

That said, I believe that some level of customization here is justified. Maybe opting out completely is too much, but in the end it's hard for the kernel to balance the importance of e.g. page cache vs networking buffers as it might be really workload-dependent. Or some workloads would prefer to risk being oom-killed rather than to tolerate a sub-par networking performance.

As of now, socket pressure throttling can be disabled by moving processes, causing the pressure, into the root cgroup. So we would definitely benefit from disabling it more idiomatically.

This bpftrace output is captured from a production server using nginx proxy (the left-most column is a timestamp in nanoseconds) which we use as a HTTP cache. As you can see, it fluctuates a lot.

26920285712831843, unified:/system.slice/nginx.service, scanned: 556, reclaimed: 146, pressure: 73 26920285731493743, unified:/system.slice/nginx.service, scanned: 22886, reclaimed: 13606, pressure: 40 26920285779559500, unified:/system.slice/nginx.service, scanned: 21775, reclaimed: 11525, pressure: 47 26920285784845147, unified:/system.slice/nginx.service, scanned: 698, reclaimed: 522, pressure: 25 26920285833808666, unified:/system.slice/nginx.service, scanned: 740, reclaimed: 232, pressure: 68 26920285835668081, unified:/system.slice/nginx.service, scanned: 1475, reclaimed: 1224, pressure: 17 26920285838877445, unified:/system.slice/nginx.service, scanned: 2919, reclaimed: 2334, pressure: 20 26920285854811898, unified:/system.slice/nginx.service, scanned: 11586, reclaimed: 7666, pressure: 33 26920285873634643, unified:/system.slice/nginx.service, scanned: 22898, reclaimed: 13387, pressure: 41 26920285899176135, unified:/system.slice/nginx.service, scanned: 10957, reclaimed: 7077, pressure: 35 26920285901529378, unified:/system.slice/nginx.service, scanned: 587, reclaimed: 156, pressure: 73 26920286020702357, unified:/system.slice/nginx.service, scanned: 563, reclaimed: 87, pressure: 84 26920286037434038, unified:/system.slice/nginx.service, scanned: 22072, reclaimed: 14161, pressure: 35 26920285789562313, unified:/system.slice/nginx.service, scanned: 2810, reclaimed: 1696, pressure: 39 26920285879597883, unified:/system.slice/nginx.service, scanned: 693, reclaimed: 625, pressure: 9 26920285884686863, unified:/system.slice/nginx.service, scanned: 2768, reclaimed: 2284, pressure: 17

We believe that the issue originates from suboptimally chosen constants, as seen in [1]. Currently, the vmpressure triggers when it cannot reclaim a few MiB of memory on a server that has over 500 GiB of memory.

We would like to work on that more after this patch to try to find a better constant or at least make it _more configurable_ if that makes sense for you.

Thanks!
Daniel

maintainer
#14 lore raw
In reply to Daniel Sedlak

On Mon, Oct 13, 2025 at 04:30:53PM +0200, Daniel Sedlak wrote:
[...]

Quoted 10 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

I am not against it, but I feel that conveying those tracked actions (or how to represent them) to the user will be much harder. Are there already existing APIs to push this information to the user?

I discussed with Wei Wang and she suggested we should start tracking the calls to tcp_adjust_rcv_ssthresh() first. So, something like the following. I would like feedback frm networking folks as well:

From 54bd2bf6681c1c694295646532f2a62a205ee41a Mon Sep 17 00:00:00 2001
From: Shakeel Butt <[email protected]>
Date: Tue, 14 Oct 2025 13:27:36 -0700
Subject: [PATCH] memcg: track network throttling due to memcg memory pressure

---
 include/linux/memcontrol.h | 1 +
 mm/memcontrol.c            | 2 ++
 net/ipv4/tcp_input.c       | 5 ++++-
 net/ipv4/tcp_output.c      | 8 ++++++--
 4 files changed, 13 insertions(+), 3 deletions(-)
include/linux/memcontrol.h +1 -0
index 873e510d6f8d..5fe254813123 100644
@@ -52,6 +52,7 @@ enum memcg_memory_event {
 	MEMCG_SWAP_HIGH,
 	MEMCG_SWAP_MAX,
 	MEMCG_SWAP_FAIL,
+	MEMCG_SOCK_THROTTLED,
 	MEMCG_NR_MEMORY_EVENTS,
 };
 
mm/memcontrol.c +2 -0
index 4deda33625f4..9207bba34e2e 100644
@@ -4463,6 +4463,8 @@ static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
 		   atomic_long_read(&events[MEMCG_OOM_KILL]));
 	seq_printf(m, "oom_group_kill %lu\n",
 		   atomic_long_read(&events[MEMCG_OOM_GROUP_KILL]));
+	seq_printf(m, "sock_throttled %lu\n",
+		   atomic_long_read(&events[MEMCG_SOCK_THROTTLED]));
 }
 
 static int memory_events_show(struct seq_file *m, void *v)
net/ipv4/tcp_input.c +4 -1
index 31ea5af49f2d..2206968fb505 100644
@@ -713,6 +713,7 @@ static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb,
 		 * Adjust rcv_ssthresh according to reserved mem
 		 */
 		tcp_adjust_rcv_ssthresh(sk);
+		memcg_memory_event(sk->sk_memcg, MEMCG_SOCK_THROTTLED);
 	}
 }
 
@@ -5764,8 +5765,10 @@ static int tcp_prune_queue(struct sock *sk, const struct sk_buff *in_skb)
 
 	if (!tcp_can_ingest(sk, in_skb))
 		tcp_clamp_window(sk);
-	else if (tcp_under_memory_pressure(sk))
+	else if (tcp_under_memory_pressure(sk)) {
 		tcp_adjust_rcv_ssthresh(sk);
+		memcg_memory_event(sk->sk_memcg, MEMCG_SOCK_THROTTLED);
+	}
 
 	if (tcp_can_ingest(sk, in_skb))
 		return 0;
net/ipv4/tcp_output.c +6 -2
index bb3576ac0ad7..8fe8d973d7ac 100644
@@ -3275,8 +3275,10 @@ u32 __tcp_select_window(struct sock *sk)
 	if (free_space < (full_space >> 1)) {
 		icsk->icsk_ack.quick = 0;
 
-		if (tcp_under_memory_pressure(sk))
+		if (tcp_under_memory_pressure(sk)) {
 			tcp_adjust_rcv_ssthresh(sk);
+			memcg_memory_event(sk->sk_memcg, MEMCG_SOCK_THROTTLED);
+		}
 
 		/* free_space might become our new window, make sure we don't
 		 * increase it due to wscale.
@@ -3334,8 +3336,10 @@ u32 __tcp_select_window(struct sock *sk)
 	if (free_space < (full_space >> 1)) {
 		icsk->icsk_ack.quick = 0;
 
-		if (tcp_under_memory_pressure(sk))
+		if (tcp_under_memory_pressure(sk)) {
 			tcp_adjust_rcv_ssthresh(sk);
+			memcg_memory_event(sk->sk_memcg, MEMCG_SOCK_THROTTLED);
+		}
 
 		/* if free space is too low, return a zero window */
 		if (free_space < (allowed_space >> 4) || free_space < mss ||
Signature · 1 line
2.47.3
#15 lore raw
Quoted 8 lines · Shakeel Butt

On Mon, Oct 13, 2025 at 04:30:53PM +0200, Daniel Sedlak wrote:
[...]

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

Quoted 8 lines
Quoted 4 lines

I am not against it, but I feel that conveying those tracked actions (or how to represent them) to the user will be much harder. Are there already existing APIs to push this information to the user?

I discussed with Wei Wang and she suggested we should start tracking the calls to tcp_adjust_rcv_ssthresh() first. So, something like the following. I would like feedback frm networking folks as well:

Looks like a good start. Are you planning on sending this patch separately, or can we include it in our v6 (with maybe slight modifications)?

include/linux/memcontrol.h enum memcg_memory_event { · line 55

Quoted 10 lines
include/linux/memcontrol.h +1 -0
index 873e510d6f8d..5fe254813123 100644
@@ -52,6 +52,7 @@ enum memcg_memory_event {
  	MEMCG_SWAP_HIGH,
  	MEMCG_SWAP_MAX,
  	MEMCG_SWAP_FAIL,
+	MEMCG_SOCK_THROTTLED,

This probably should be MEMCG_TCP_SOCK_THROTTLED, because it checks only tcp_under_memory_pressure, however there is also the sk_under_memory_pressure used in net/sctp/sm_statefuns.c:6597 to also reduce the sending rate. Or also add the counter there and keep the name?

Thanks!
Daniel

maintainer
#16 lore raw
Quoted 14 lines · Daniel Sedlak
Quoted 13 lines · Shakeel Butt

On Mon, Oct 13, 2025 at 04:30:53PM +0200, Daniel Sedlak wrote:
[...]

Quoted 10 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

I am not against it, but I feel that conveying those tracked actions (or how to represent them) to the user will be much harder. Are there already existing APIs to push this information to the user?

Quoted 6 lines
Quoted 3 lines

I discussed with Wei Wang and she suggested we should start tracking the calls to tcp_adjust_rcv_ssthresh() first. So, something like the following. I would like feedback frm networking folks as well:

Looks like a good start. Are you planning on sending this patch separately, or can we include it in our v6 (with maybe slight modifications)?

What else you are planning to add in v6?

Quoted 15 lines

include/linux/memcontrol.h enum memcg_memory_event { · line 55

Quoted 10 lines
include/linux/memcontrol.h +1 -0
index 873e510d6f8d..5fe254813123 100644
@@ -52,6 +52,7 @@ enum memcg_memory_event {
  	MEMCG_SWAP_HIGH,
  	MEMCG_SWAP_MAX,
  	MEMCG_SWAP_FAIL,
+	MEMCG_SOCK_THROTTLED,

This probably should be MEMCG_TCP_SOCK_THROTTLED, because it checks only tcp_under_memory_pressure, however there is also the sk_under_memory_pressure used in net/sctp/sm_statefuns.c:6597 to also reduce the sending rate. Or also add the counter there and keep the name?

Yeah makes sense to add the counter in sctp as well.

#17 lore raw
In reply to Shakeel Butt
Quoted 9 lines · Shakeel Butt

On Mon, Oct 13, 2025 at 04:30:53PM +0200, Daniel Sedlak wrote:
[...]

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

Quoted 8 lines
Quoted 4 lines

I am not against it, but I feel that conveying those tracked actions (or how to represent them) to the user will be much harder. Are there already existing APIs to push this information to the user?

I discussed with Wei Wang and she suggested we should start tracking the calls to tcp_adjust_rcv_ssthresh() first. So, something like the following. I would like feedback frm networking folks as well:

I think we could simply put memcg_memory_event() in mem_cgroup_sk_under_memory_pressure() when it returns true.

Other than tcp_adjust_rcv_ssthresh(), if tcp_under_memory_pressure() returns true, it indicates something bad will happen, failure to expand rcvbuf and sndbuf, need to prune out-of-order queue more aggressively, FIN deferred to a retransmitted packet.

Also, we could cover mptcp and sctp too.

Quoted 95 lines

From 54bd2bf6681c1c694295646532f2a62a205ee41a Mon Sep 17 00:00:00 2001
From: Shakeel Butt <[email protected]>
Date: Tue, 14 Oct 2025 13:27:36 -0700
Subject: [PATCH] memcg: track network throttling due to memcg memory pressure

---
 include/linux/memcontrol.h | 1 +
 mm/memcontrol.c            | 2 ++
 net/ipv4/tcp_input.c       | 5 ++++-
 net/ipv4/tcp_output.c      | 8 ++++++--
 4 files changed, 13 insertions(+), 3 deletions(-)
include/linux/memcontrol.h +1 -0
index 873e510d6f8d..5fe254813123 100644
@@ -52,6 +52,7 @@ enum memcg_memory_event {
        MEMCG_SWAP_HIGH,
        MEMCG_SWAP_MAX,
        MEMCG_SWAP_FAIL,
+       MEMCG_SOCK_THROTTLED,
        MEMCG_NR_MEMORY_EVENTS,
 };
 
mm/memcontrol.c +2 -0
index 4deda33625f4..9207bba34e2e 100644
@@ -4463,6 +4463,8 @@ static void __memory_events_show(struct seq_file *m, atomic_long_t *events)
                   atomic_long_read(&events[MEMCG_OOM_KILL]));
        seq_printf(m, "oom_group_kill %lu\n",
                   atomic_long_read(&events[MEMCG_OOM_GROUP_KILL]));
+       seq_printf(m, "sock_throttled %lu\n",
+                  atomic_long_read(&events[MEMCG_SOCK_THROTTLED]));
 }
 
 static int memory_events_show(struct seq_file *m, void *v)
net/ipv4/tcp_input.c +4 -1
index 31ea5af49f2d..2206968fb505 100644
@@ -713,6 +713,7 @@ static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb,
                 * Adjust rcv_ssthresh according to reserved mem
                 */
                tcp_adjust_rcv_ssthresh(sk);
+               memcg_memory_event(sk->sk_memcg, MEMCG_SOCK_THROTTLED);
        }
 }
 
@@ -5764,8 +5765,10 @@ static int tcp_prune_queue(struct sock *sk, const struct sk_buff *in_skb)
 
        if (!tcp_can_ingest(sk, in_skb))
                tcp_clamp_window(sk);
-       else if (tcp_under_memory_pressure(sk))
+       else if (tcp_under_memory_pressure(sk)) {
                tcp_adjust_rcv_ssthresh(sk);
+               memcg_memory_event(sk->sk_memcg, MEMCG_SOCK_THROTTLED);
+       }
 
        if (tcp_can_ingest(sk, in_skb))
                return 0;
net/ipv4/tcp_output.c +6 -3
index bb3576ac0ad7..8fe8d973d7ac 100644
@@ -3275,8 +3275,10 @@ u32 __tcp_select_window(struct sock *sk)
        if (free_space < (full_space >> 1)) {
                icsk->icsk_ack.quick = 0;
 
-               if (tcp_under_memory_pressure(sk))
+               if (tcp_under_memory_pressure(sk)) {
                        tcp_adjust_rcv_ssthresh(sk);
+                       memcg_memory_event(sk->sk_memcg, MEMCG_SOCK_THROTTLED);
+               }
 
                /* free_space might become our new window, make sure we don't
                 * increase it due to wscale.
@@ -3334,8 +3336,10 @@ u32 __tcp_select_window(struct sock *sk)
        if (free_space < (full_space >> 1)) {
                icsk->icsk_ack.quick = 0;
 
-               if (tcp_under_memory_pressure(sk))
+               if (tcp_under_memory_pressure(sk)) {
                        tcp_adjust_rcv_ssthresh(sk);
+                       memcg_memory_event(sk->sk_memcg, MEMCG_SOCK_THROTTLED);
+               }
 
                /* if free space is too low, return a zero window */
                if (free_space < (allowed_space >> 4) || free_space < mss ||
--

2.47.3

maintainer
#18 lore raw
Quoted 23 lines · Kuniyuki Iwashima
Quoted 9 lines · Shakeel Butt

On Mon, Oct 13, 2025 at 04:30:53PM +0200, Daniel Sedlak wrote:
[...]

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

Quoted 8 lines
Quoted 4 lines

I am not against it, but I feel that conveying those tracked actions (or how to represent them) to the user will be much harder. Are there already existing APIs to push this information to the user?

I discussed with Wei Wang and she suggested we should start tracking the calls to tcp_adjust_rcv_ssthresh() first. So, something like the following. I would like feedback frm networking folks as well:

I think we could simply put memcg_memory_event() in mem_cgroup_sk_under_memory_pressure() when it returns true.

Quoted 7 lines

Other than tcp_adjust_rcv_ssthresh(), if tcp_under_memory_pressure() returns true, it indicates something bad will happen, failure to expand rcvbuf and sndbuf, need to prune out-of-order queue more aggressively, FIN deferred to a retransmitted packet.

Also, we could cover mptcp and sctp too.

I wanted to start simple and focus on one specific action but I am open to other actins as well. Do we want a generic network throttled metric or do we want different metric for different action? At the moment I think for memcg, a single metric would be sufficient and then we can have tracepoints for more fine grained debugging.

#19 lore raw
Quoted 30 lines · Shakeel Butt
Quoted 28 lines · Kuniyuki Iwashima
Quoted 9 lines · Shakeel Butt

On Mon, Oct 13, 2025 at 04:30:53PM +0200, Daniel Sedlak wrote:
[...]

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

Quoted 8 lines
Quoted 4 lines

I am not against it, but I feel that conveying those tracked actions (or how to represent them) to the user will be much harder. Are there already existing APIs to push this information to the user?

I discussed with Wei Wang and she suggested we should start tracking the calls to tcp_adjust_rcv_ssthresh() first. So, something like the following. I would like feedback frm networking folks as well:

I think we could simply put memcg_memory_event() in mem_cgroup_sk_under_memory_pressure() when it returns true.

Other than tcp_adjust_rcv_ssthresh(), if tcp_under_memory_pressure() returns true, it indicates something bad will happen, failure to expand rcvbuf and sndbuf, need to prune out-of-order queue more aggressively, FIN deferred to a retransmitted packet.

Quoted 8 lines
Quoted 2 lines

Also, we could cover mptcp and sctp too.

I wanted to start simple and focus on one specific action but I am open to other actins as well. Do we want a generic network throttled metric or do we want different metric for different action? At the moment I think for memcg, a single metric would be sufficient and then we can have tracepoints for more fine grained debugging.

I agree that a single metric would be enough if it can signal something bad is happening as a first step, then we can take further action with tracepoint, bpftrace, whatever.

maintainer
#20 lore raw
Quoted 40 lines · Kuniyuki Iwashima
Quoted 30 lines · Shakeel Butt
Quoted 28 lines · Kuniyuki Iwashima
Quoted 9 lines · Shakeel Butt

On Mon, Oct 13, 2025 at 04:30:53PM +0200, Daniel Sedlak wrote:
[...]

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

How about we track the actions taken by the callers of mem_cgroup_sk_under_memory_pressure()? Basically if network stack reduces the buffer size or whatever the other actions it may take when mem_cgroup_sk_under_memory_pressure() returns, tracking those actions is what I think is needed here, at least for the debugging use-case.

Quoted 8 lines
Quoted 4 lines

I am not against it, but I feel that conveying those tracked actions (or how to represent them) to the user will be much harder. Are there already existing APIs to push this information to the user?

I discussed with Wei Wang and she suggested we should start tracking the calls to tcp_adjust_rcv_ssthresh() first. So, something like the following. I would like feedback frm networking folks as well:

I think we could simply put memcg_memory_event() in mem_cgroup_sk_under_memory_pressure() when it returns true.

Other than tcp_adjust_rcv_ssthresh(), if tcp_under_memory_pressure() returns true, it indicates something bad will happen, failure to expand rcvbuf and sndbuf, need to prune out-of-order queue more aggressively, FIN deferred to a retransmitted packet.

Quoted 8 lines
Quoted 2 lines

Also, we could cover mptcp and sctp too.

I wanted to start simple and focus on one specific action but I am open to other actins as well. Do we want a generic network throttled metric or do we want different metric for different action? At the moment I think for memcg, a single metric would be sufficient and then we can have tracepoints for more fine grained debugging.

Quoted 3 lines

I agree that a single metric would be enough if it can signal something bad is happening as a first step, then we can take further action with tracepoint, bpftrace, whatever.

+1 to a single metric