Skip to contentSkip to conversation

net: sysctl: allow dump_cpumask to handle higher numbers of CPUs

PATCH Applied Antoine Tenart 3 participants 8 messages Oct 17, 2024 – Oct 23, 2024

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

cover net: sysctl: allow dump_cpumask to handle higher numbers of CPUs

Hello,

The main goal of this series is to allow dump_cpumask to handle higher numbers of CPUs (patch 3). While doing so I had the opportunity to make the function a bit simpler, which is done in patches 1-2.

None of those is net material IMO.

Thanks,
Antoine

Antoine Tenart (3):
net: sysctl: remove always-true condition
net: sysctl: do not reserve an extra char in dump_cpumask temporary
buffer
net: sysctl: allow dump_cpumask to handle higher numbers of CPUs

 net/core/sysctl_net_core.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/3 net: sysctl: remove always-true condition

Before adding a new line at the end of the temporary buffer in dump_cpumask, a length check is performed to ensure there is space for it.

  len = min(sizeof(kbuf) - 1, *lenp);
  len = scnprintf(kbuf, len, ...);
  if (len < *lenp)
          kbuf[len++] = '\n';

Note that the check is currently logically wrong, the written length is compared against the output buffer, not the temporary one. However this has no consequence as this is always true, even if fixed: scnprintf includes a null char at the end of the buffer but the returned length do not include it and there is always space for overriding it with a newline.

Remove the condition.

---
 net/core/sysctl_net_core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/core/sysctl_net_core.c +4 -2
index b60fac380cec..e7c0121dfaa1 100644
@@ -69,8 +69,10 @@ static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos,
 		return;
 	}
 
-	if (len < *lenp)
-		kbuf[len++] = '\n';
+	/* scnprintf writes a trailing null char not counted in the returned
+	 * length, override it with a newline.
+	 */
+	kbuf[len++] = '\n';
 	memcpy(buffer, kbuf, len);
 	*lenp = len;
 	*ppos += len;
Signature · 1 line
2.47.0
reviewer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH net-next 1/3] net: sysctl: remove always-true condition

Quoted 16 lines · Antoine Tenart

Before adding a new line at the end of the temporary buffer in dump_cpumask, a length check is performed to ensure there is space for it.

  len = min(sizeof(kbuf) - 1, *lenp);
  len = scnprintf(kbuf, len, ...);
  if (len < *lenp)
          kbuf[len++] = '\n';

Note that the check is currently logically wrong, the written length is compared against the output buffer, not the temporary one. However this has no consequence as this is always true, even if fixed: scnprintf includes a null char at the end of the buffer but the returned length do not include it and there is always space for overriding it with a newline.

Quoted 3 lines

Remove the condition.

Thanks for separating out this and patch 2/3. It makes it much easier to reason with these changes.

#3 lore raw

PATCH 2/3 net: sysctl: do not reserve an extra char in dump_cpumask temporary buffer

In reply to Antoine Tenart

When computing the length we'll be able to use out of the buffers, one char is removed from the temporary one to make room for a newline. It should be removed from the output buffer length too, but in reality this is not needed as the later call to scnprintf makes sure a null char is written at the end of the buffer which we override with the newline.

---
 net/core/sysctl_net_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/sysctl_net_core.c +1 -1
index e7c0121dfaa1..8dc07f7b1772 100644
@@ -62,7 +62,7 @@ static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos,
 		return;
 	}
 
-	len = min(sizeof(kbuf) - 1, *lenp);
+	len = min(sizeof(kbuf), *lenp);
 	len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
 	if (!len) {
 		*lenp = 0;
Signature · 1 line
2.47.0
#4 lore raw

PATCH 3/3 net: sysctl: allow dump_cpumask to handle higher numbers of CPUs

In reply to Antoine Tenart

This fixes the output of rps_default_mask and flow_limit_cpu_bitmap when the CPU count is > 448, as it was truncated.

The underlying values are actually stored correctly when writing to these sysctl but displaying them uses a fixed length temporary buffer in dump_cpumask. This buffer can be too small if the CPU count is > 448.

Fix this by dynamically allocating the buffer in dump_cpumask, using a guesstimate of what we need.

---
 net/core/sysctl_net_core.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)
net/core/sysctl_net_core.c +23 -9
index 8dc07f7b1772..cb8d32e5c14e 100644
@@ -51,22 +51,32 @@ int sysctl_devconf_inherit_init_net __read_mostly;
 EXPORT_SYMBOL(sysctl_devconf_inherit_init_net);
 
 #if IS_ENABLED(CONFIG_NET_FLOW_LIMIT) || IS_ENABLED(CONFIG_RPS)
-static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos,
-			 struct cpumask *mask)
+static int dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos,
+			struct cpumask *mask)
 {
-	char kbuf[128];
+	char *kbuf;
 	int len;
 
 	if (*ppos || !*lenp) {
 		*lenp = 0;
-		return;
+		return 0;
+	}
+
+	/* CPUs are displayed as a hex bitmap + a comma between each groups of 8
+	 * nibbles (except the last one which has a newline instead).
+	 * Guesstimate the buffer size at the group granularity level.
+	 */
+	len = min(DIV_ROUND_UP(nr_cpumask_bits, 32) * (8 + 1), *lenp);
+	kbuf = kmalloc(len, GFP_KERNEL);
+	if (!kbuf) {
+		*lenp = 0;
+		return -ENOMEM;
 	}
 
-	len = min(sizeof(kbuf), *lenp);
 	len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
 	if (!len) {
 		*lenp = 0;
-		return;
+		goto free_buf;
 	}
 
 	/* scnprintf writes a trailing null char not counted in the returned
@@ -76,6 +86,10 @@ static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos,
 	memcpy(buffer, kbuf, len);
 	*lenp = len;
 	*ppos += len;
+
+free_buf:
+	kfree(kbuf);
+	return 0;
 }
 #endif
 
@@ -119,8 +133,8 @@ static int rps_default_mask_sysctl(const struct ctl_table *table, int write,
 		if (err)
 			goto done;
 	} else {
-		dump_cpumask(buffer, lenp, ppos,
-			     net->core.rps_default_mask ? : cpu_none_mask);
+		err = dump_cpumask(buffer, lenp, ppos,
+				   net->core.rps_default_mask ? : cpu_none_mask);
 	}
 
 done:
@@ -249,7 +263,7 @@ static int flow_limit_cpu_sysctl(const struct ctl_table *table, int write,
 		}
 		rcu_read_unlock();
 
-		dump_cpumask(buffer, lenp, ppos, mask);
+		ret = dump_cpumask(buffer, lenp, ppos, mask);
 	}
 
 done:
Signature · 1 line
2.47.0
reviewer Reviewed-by on v1 3/3
#5 lore raw
Quoted 11 lines · Antoine Tenart

This fixes the output of rps_default_mask and flow_limit_cpu_bitmap when the CPU count is > 448, as it was truncated.

The underlying values are actually stored correctly when writing to these sysctl but displaying them uses a fixed length temporary buffer in dump_cpumask. This buffer can be too small if the CPU count is > 448.

Fix this by dynamically allocating the buffer in dump_cpumask, using a guesstimate of what we need.

+1 for using 'nibbles' in a comment in this patch.

reviewer Reviewed-by on v1 3/3
#6 lore raw
In reply to Antoine Tenart
Quoted 11 lines · Antoine Tenart

This fixes the output of rps_default_mask and flow_limit_cpu_bitmap when the CPU count is > 448, as it was truncated.

The underlying values are actually stored correctly when writing to these sysctl but displaying them uses a fixed length temporary buffer in dump_cpumask. This buffer can be too small if the CPU count is > 448.

Fix this by dynamically allocating the buffer in dump_cpumask, using a guesstimate of what we need.

#7 lore raw
In reply to Antoine Tenart
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Thu, 17 Oct 2024 17:24:16 +0200 you wrote:

Hello,

The main goal of this series is to allow dump_cpumask to handle higher numbers of CPUs (patch 3). While doing so I had the opportunity to make the function a bit simpler, which is done in patches 1-2.

None of those is net material IMO.

[...]

Here is the summary with links:
- [net-next,1/3] net: sysctl: remove always-true condition

    https://git.kernel.org/netdev/net-next/c/d631094e4d20
  - [net-next,2/3] net: sysctl: do not reserve an extra char in dump_cpumask temporary buffer
    https://git.kernel.org/netdev/net-next/c/a8cc8fa14541
  - [net-next,3/3] net: sysctl: allow dump_cpumask to handle higher numbers of CPUs
    https://git.kernel.org/netdev/net-next/c/124afe773b1a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html