diff mbox series

smb/server: use sock_create_kern() in create_socket()

Message ID 20250324065155.665290-1-chenxiaosong@chenxiaosong.com (mailing list archive)
State New, archived
Headers show
Series smb/server: use sock_create_kern() in create_socket() | expand

Commit Message

ChenXiaoSong March 24, 2025, 6:51 a.m. UTC
From: ChenXiaoSong <chenxiaosong@kylinos.cn>

The socket resides in kernel space, so use sock_create_kern()
instead of sock_create().

Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
---
 fs/smb/server/transport_tcp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

David Laight March 24, 2025, 9:07 p.m. UTC | #1
On Mon, 24 Mar 2025 06:51:55 +0000
chenxiaosong@chenxiaosong.com wrote:

> From: ChenXiaoSong <chenxiaosong@kylinos.cn>
> 
> The socket resides in kernel space, so use sock_create_kern()
> instead of sock_create().

As in the other patches you need to worry about whether the socket
holds a reference to the network namespace.

	David

> 
> Signed-off-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
> ---
>  fs/smb/server/transport_tcp.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/smb/server/transport_tcp.c b/fs/smb/server/transport_tcp.c
> index 7f38a3c3f5bd..e5f46a91c3fc 100644
> --- a/fs/smb/server/transport_tcp.c
> +++ b/fs/smb/server/transport_tcp.c
> @@ -429,12 +429,13 @@ static int create_socket(struct interface *iface)
>  	struct socket *ksmbd_socket;
>  	bool ipv4 = false;
>  
> -	ret = sock_create(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &ksmbd_socket);
> +	ret = sock_create_kern(current->nsproxy->net_ns, PF_INET6, SOCK_STREAM,
> +			       IPPROTO_TCP, &ksmbd_socket);
>  	if (ret) {
>  		if (ret != -EAFNOSUPPORT)
>  			pr_err("Can't create socket for ipv6, fallback to ipv4: %d\n", ret);
> -		ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP,
> -				  &ksmbd_socket);
> +		ret = sock_create_kern(current->nsproxy->net_ns, PF_INET,
> +				       SOCK_STREAM, IPPROTO_TCP, &ksmbd_socket);
>  		if (ret) {
>  			pr_err("Can't create socket for ipv4: %d\n", ret);
>  			goto out_clear;
Kuniyuki Iwashima March 24, 2025, 10:11 p.m. UTC | #2
From: David Laight <david.laight.linux@gmail.com>
Date: Mon, 24 Mar 2025 21:07:47 +0000
> On Mon, 24 Mar 2025 06:51:55 +0000
> chenxiaosong@chenxiaosong.com wrote:
> 
> > From: ChenXiaoSong <chenxiaosong@kylinos.cn>
> > 
> > The socket resides in kernel space, so use sock_create_kern()
> > instead of sock_create().
> 
> As in the other patches you need to worry about whether the socket
> holds a reference to the network namespace.

Right, and if you don't see any real issue, I recommend leaving
it as is for now because we have seen many refcount issues for
kernel TCP sockets.

And I totally forgot to respin my series after holiday to clean
up such callers.
https://lore.kernel.org/netdev/20241213092152.14057-1-kuniyu@amazon.com/

I'll repost v4 after the merge window with some modification

  1. be less invasive as suggested by Paolo
  2. use sk_net_refcnt_upgrade()
diff mbox series

Patch

diff --git a/fs/smb/server/transport_tcp.c b/fs/smb/server/transport_tcp.c
index 7f38a3c3f5bd..e5f46a91c3fc 100644
--- a/fs/smb/server/transport_tcp.c
+++ b/fs/smb/server/transport_tcp.c
@@ -429,12 +429,13 @@  static int create_socket(struct interface *iface)
 	struct socket *ksmbd_socket;
 	bool ipv4 = false;
 
-	ret = sock_create(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &ksmbd_socket);
+	ret = sock_create_kern(current->nsproxy->net_ns, PF_INET6, SOCK_STREAM,
+			       IPPROTO_TCP, &ksmbd_socket);
 	if (ret) {
 		if (ret != -EAFNOSUPPORT)
 			pr_err("Can't create socket for ipv6, fallback to ipv4: %d\n", ret);
-		ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP,
-				  &ksmbd_socket);
+		ret = sock_create_kern(current->nsproxy->net_ns, PF_INET,
+				       SOCK_STREAM, IPPROTO_TCP, &ksmbd_socket);
 		if (ret) {
 			pr_err("Can't create socket for ipv4: %d\n", ret);
 			goto out_clear;