Skip to contentSkip to conversation

af_unix: Do not call kmemdup() for init_net's sysctl table.

PATCH Applied Kuniyuki Iwashima 4 participants 5 messages Jun 27, 2022 – Jul 8, 2022

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

PATCH v3 1/1 af_unix: Do not call kmemdup() for init_net's sysctl table.

While setting up init_net's sysctl table, we need not duplicate the global table and can use it directly as ipv4_sysctl_init_net() does.

Unlike IPv4, AF_UNIX does not have a huge sysctl table for now, so it cannot be a problem, but this patch makes code consistent.

Fixes 1597fbc0faf8 ("[UNIX]: Make the unix sysctl tables per-namespace")

---
v3:
* Update changelog
* Fix a bug when we unload unix.ko

v2: https://lore.kernel.org/all/[email protected]/
* Fix NULL comparison style by checkpatch.pl

v1: https://lore.kernel.org/all/[email protected]/

---
 net/unix/sysctl_net_unix.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
net/unix/sysctl_net_unix.c +13 -6
index 01d44e2598e2..3f1fdffd6092 100644
@@ -26,11 +26,16 @@ int __net_init unix_sysctl_register(struct net *net)
 {
 	struct ctl_table *table;
 
-	table = kmemdup(unix_table, sizeof(unix_table), GFP_KERNEL);
-	if (table == NULL)
-		goto err_alloc;
+	if (net_eq(net, &init_net)) {
+		table = unix_table;
+	} else {
+		table = kmemdup(unix_table, sizeof(unix_table), GFP_KERNEL);
+		if (!table)
+			goto err_alloc;
+
+		table[0].data = &net->unx.sysctl_max_dgram_qlen;
+	}
 
-	table[0].data = &net->unx.sysctl_max_dgram_qlen;
 	net->unx.ctl = register_net_sysctl(net, "net/unix", table);
 	if (net->unx.ctl == NULL)
 		goto err_reg;
@@ -38,7 +43,8 @@ int __net_init unix_sysctl_register(struct net *net)
 	return 0;
 
 err_reg:
-	kfree(table);
+	if (net_eq(net, &init_net))
+		kfree(table);
 err_alloc:
 	return -ENOMEM;
 }
@@ -49,5 +55,6 @@ void unix_sysctl_unregister(struct net *net)
 
 	table = net->unx.ctl->ctl_table_arg;
 	unregister_net_sysctl_table(net->unx.ctl);
-	kfree(table);
+	if (!net_eq(net, &init_net))
+		kfree(table);
 }
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 5 lines · On Mon, 27 Jun 2022 16:36:27 -0700 Kuniyuki Iwashima wrote:

While setting up init_net's sysctl table, we need not duplicate the global table and can use it directly as ipv4_sysctl_init_net() does.

Unlike IPv4, AF_UNIX does not have a huge sysctl table for now, so it cannot be a problem, but this patch makes code consistent.

Thanks for the extra info. It sounds like an optimization, tho. We save one table's worth of memory. Any objections to applying this to net-next?

Quoted 3 lines
Fixes 1597fbc0faf8 ("[UNIX]: Make the unix sysctl tables per-namespace")
#2 lore raw
From:   Jakub Kicinski <[email protected]>
Date:   Tue, 28 Jun 2022 20:51:25 -0700
Quoted 10 lines · On Mon, 27 Jun 2022 16:36:27 -0700 Kuniyuki Iwashima wrote:
Quoted 5 lines

While setting up init_net's sysctl table, we need not duplicate the global table and can use it directly as ipv4_sysctl_init_net() does.

Unlike IPv4, AF_UNIX does not have a huge sysctl table for now, so it cannot be a problem, but this patch makes code consistent.

Thanks for the extra info. It sounds like an optimization, tho. We save one table's worth of memory. Any objections to applying this to net-next?

I'm fine with net-next.

Thank you,
Kuniyuki

Quoted 3 lines
Quoted 3 lines
Fixes 1597fbc0faf8 ("[UNIX]: Make the unix sysctl tables per-namespace")
#3 lore raw
In reply to Kuniyuki Iwashima
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Mon, 27 Jun 2022 16:36:27 -0700 you wrote:

While setting up init_net's sysctl table, we need not duplicate the global table and can use it directly as ipv4_sysctl_init_net() does.

Unlike IPv4, AF_UNIX does not have a huge sysctl table for now, so it cannot be a problem, but this patch makes code consistent.

Fixes 1597fbc0faf8 ("[UNIX]: Make the unix sysctl tables per-namespace")

[...]

Here is the summary with links:
- [v3,net] af_unix: Do not call kmemdup() for init_net's sysctl table.

    https://git.kernel.org/netdev/net-next/c/849d5aa3a1d8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#4 lore raw
In reply to Kuniyuki Iwashima
Quoted 48 lines · Kuniyuki Iwashima

While setting up init_net's sysctl table, we need not duplicate the global table and can use it directly as ipv4_sysctl_init_net() does.

Unlike IPv4, AF_UNIX does not have a huge sysctl table for now, so it cannot be a problem, but this patch makes code consistent.

Fixes 1597fbc0faf8 ("[UNIX]: Make the unix sysctl tables per-namespace")

---
v3:
* Update changelog
* Fix a bug when we unload unix.ko

v2: https://lore.kernel.org/all/[email protected]/
* Fix NULL comparison style by checkpatch.pl

v1: https://lore.kernel.org/all/[email protected]/

---
  net/unix/sysctl_net_unix.c | 19 +++++++++++++------
  1 file changed, 13 insertions(+), 6 deletions(-)
net/unix/sysctl_net_unix.c +9 -4
index 01d44e2598e2..3f1fdffd6092 100644
@@ -26,11 +26,16 @@ int __net_init unix_sysctl_register(struct net *net)
  {
  	struct ctl_table *table;
 
-	table = kmemdup(unix_table, sizeof(unix_table), GFP_KERNEL);
-	if (table == NULL)
-		goto err_alloc;
+	if (net_eq(net, &init_net)) {
+		table = unix_table;
+	} else {
+		table = kmemdup(unix_table, sizeof(unix_table), GFP_KERNEL);
+		if (!table)
+			goto err_alloc;
+
+		table[0].data = &net->unx.sysctl_max_dgram_qlen;
+	}
 
-	table[0].data = &net->unx.sysctl_max_dgram_qlen;
  	net->unx.ctl = register_net_sysctl(net, "net/unix", table);
  	if (net->unx.ctl == NULL)
  		goto err_reg;

net/unix/sysctl_net_unix.c int __net_init unix_sysctl_register(struct net *net) · line 47

Quoted 6 lines
net/unix/sysctl_net_unix.c +2 -1
@@ -38,7 +43,8 @@ int __net_init unix_sysctl_register(struct net *net)
  	return 0;
 
  err_reg:
-	kfree(table);
+	if (net_eq(net, &init_net))
+		kfree(table);

Inverted test, I guess :/

I will send a fix.

Quoted 11 lines
net/unix/sysctl_net_unix.c +2 -1
  err_alloc:
  	return -ENOMEM;
  }
@@ -49,5 +55,6 @@ void unix_sysctl_unregister(struct net *net)
 
  	table = net->unx.ctl->ctl_table_arg;
  	unregister_net_sysctl_table(net->unx.ctl);
-	kfree(table);
+	if (!net_eq(net, &init_net))
+		kfree(table);
  }