sctp: Avoid redundant initialisation in sctp_accept() and sctp_do_peeloff().
Subsystems (4)
Kuniyuki Iwashima
maintainerv2 cover sctp: Avoid redundant initialisation in sctp_accept() and sctp_do_peeloff().
When sctp_accept() and sctp_do_peeloff() allocates a new socket, somehow sk_alloc() is used, and the new socket goes through full initialisation, but most of the fields are overwritten later.
1)
sctp_accept()
|- sctp_v[46]_create_accept_sk() | |- sk_alloc() | |- sock_init_data() | |- sctp_copy_sock() | `- newsk->sk_prot->init() / sctp_init_sock() |
`- sctp_sock_migrate()
`- sctp_copy_descendant(newsk, oldsk)
sock_init_data() initialises struct sock, but many fields are overwritten by sctp_copy_sock(), which inherits fields of struct sock and inet_sock from the parent socket.
sctp_init_sock() fully initialises struct sctp_sock, but later sctp_copy_descendant() inherits most fields from the parent's struct sctp_sock by memcpy().
2)
sctp_do_peeloff()
|- sock_create() | | | ... | |- sk_alloc() | |- sock_init_data() | ... | `- newsk->sk_prot->init() / sctp_init_sock() | |- sctp_copy_sock()
`- sctp_sock_migrate()
`- sctp_copy_descendant(newsk, oldsk)
sock_create() creates a brand new socket, but sctp_copy_sock() and sctp_sock_migrate() overwrite most of the fields.
So, sk_alloc(), sock_init_data(), sctp_copy_sock(), and sctp_copy_descendant() can be replaced with a single function like sk_clone_lock().
This series does the conversion and removes TODO comment added by commit 4a997d49d92ad ("tcp: Save lock_sock() for memcg in inet_csk_accept().").
Tested accept() and SCTP_SOCKOPT_PEELOFF and both work properly.
socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP) = 3
bind(3, {sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
listen(3, -1) = 0
getsockname(3, {sa_family=AF_INET, sin_port=htons(49460), sin_addr=inet_addr("127.0.0.1")}, [16]) = 0 socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP) = 4
connect(4, {sa_family=AF_INET, sin_port=htons(49460), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
accept(3, NULL, NULL) = 5
...
socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP) = 3
bind(3, {sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("127.0.0.1")}, 16) = 0
listen(3, -1) = 0
getsockname(3, {sa_family=AF_INET, sin_port=htons(48240), sin_addr=inet_addr("127.0.0.1")}, [16]) = 0 socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP) = 4
connect(4, {sa_family=AF_INET, sin_port=htons(48240), sin_addr=inet_addr("127.0.0.1")}, 16) = 0 getsockopt(3, SOL_SCTP, SCTP_SOCKOPT_PEELOFF, "*\0\0\0\5\0\0\0", [8]) = 5
Changes:
v2:
* Patch 7: Export __inet_accept()
v1: https://lore.kernel.org/netdev/[email protected]/
Kuniyuki Iwashima (8):
sctp: Defer SCTP_DBG_OBJCNT_DEC() to sctp_destroy_sock().
sctp: Don't copy sk_sndbuf and sk_rcvbuf in sctp_sock_migrate().
sctp: Don't call sk->sk_prot->init() in sctp_v[46]_create_accept_sk().
net: Add sk_clone().
sctp: Use sk_clone() in sctp_accept().
sctp: Remove sctp_pf.create_accept_sk().
sctp: Use sctp_clone_sock() in sctp_do_peeloff().
sctp: Remove sctp_copy_sock() and sctp_copy_descendant().
include/net/inet_sock.h | 8 -- include/net/sctp/sctp.h | 3 +- include/net/sctp/structs.h | 3 - include/net/sock.h | 7 +- net/core/sock.c | 21 ++-- net/ipv4/af_inet.c | 5 +- net/sctp/ipv6.c | 51 --------- net/sctp/protocol.c | 33 ------ net/sctp/socket.c | 209 +++++++++++++++++-------------------- 9 files changed, 116 insertions(+), 224 deletions(-)
Signature · 1 line
2.51.1.814.gb8fa24458f-goog
Kuniyuki Iwashima
maintainerPATCH v2 1/8 sctp: Defer SCTP_DBG_OBJCNT_DEC() to sctp_destroy_sock().
SCTP_DBG_OBJCNT_INC() is called only when sctp_init_sock() returns 0 after successfully allocating sctp_sk(sk)->ep.
OTOH, SCTP_DBG_OBJCNT_DEC() is called in sctp_close().
The code seems to expect that the socket is always exposed to userspace once SCTP_DBG_OBJCNT_INC() is incremented, but there is a path where the assumption is not true.
In sctp_accept(), sctp_sock_migrate() could fail after sctp_init_sock().
Then, sk_common_release() does not call inet_release() nor sctp_close(). Instead, it calls sk->sk_prot->destroy().
Let's move SCTP_DBG_OBJCNT_DEC() from sctp_close() to sctp_destroy_sock().
--- net/sctp/socket.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
net/sctp/socket.c +3 -2
@@ -1553,8 +1553,6 @@ static void sctp_close(struct sock *sk, long timeout)spin_unlock_bh (&net->sctp.addr_wq_lock);sock_put (sk); - -SCTP_DBG_OBJCNT_DEC (sock); }/* Handle EPIPE error. */
@@ -5109,9 +5107,12 @@ static void sctp_destroy_sock(struct sock *sk) sp->do_auto_asconf =0 ;list_del (&sp->auto_asconf_list); } +sctp_endpoint_free (sp->ep); +sk_sockets_allocated_dec (sk);sock_prot_inuse_add (sock_net (sk), sk->sk_prot,-1 ); +SCTP_DBG_OBJCNT_DEC (sock); }static void sctp_destruct_sock (struct sock *sk)
Signature · 1 line
2.51.1.814.gb8fa24458f-goog
Kuniyuki Iwashima
maintainerPATCH v2 2/8 sctp: Don't copy sk_sndbuf and sk_rcvbuf in sctp_sock_migrate().
In reply to Kuniyuki Iwashimasctp_sock_migrate() is called from 2 places.
1) sctp_accept() calls sp->pf->create_accept_sk() before sctp_sock_migrate(), and sp->pf->create_accept_sk() calls sctp_copy_sock().
2) sctp_do_peeloff() also calls sctp_copy_sock() before sctp_sock_migrate().
sctp_copy_sock() copies sk_sndbuf and sk_rcvbuf from the parent socket.
Let's not copy the two fields in sctp_sock_migrate().
--- net/sctp/socket.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
net/sctp/socket.c +2 -5
@@ -9523,12 +9523,9 @@ static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,struct sctp_bind_hashbucket *head;int err; -/* Migrate socket buffer sizes and all the socket level options to the -* new socket. +/* Migrate all the socket level options to the new socket. +* Brute force copy old sctp opt. */ - newsk->sk_sndbuf = oldsk->sk_sndbuf; - newsk->sk_rcvbuf = oldsk->sk_rcvbuf; -/* Brute force copy old sctp opt. */ sctp_copy_descendant (newsk, oldsk);/* Restore the ep value that was overwritten with the above structure
Signature · 1 line
2.51.1.814.gb8fa24458f-goog
Kuniyuki Iwashima
maintainerPATCH v2 3/8 sctp: Don't call sk->sk_prot->init() in sctp_v[46]_create_accept_sk().
In reply to Kuniyuki Iwashimasctp_accept() calls sctp_v[46]_create_accept_sk() to allocate a new socket and calls sctp_sock_migrate() to copy fields from the parent socket to the new socket.
sctp_v[46]_create_accept_sk() calls sctp_init_sock() to initialise sctp_sock, but most fields are overwritten by sctp_copy_descendant() called from sctp_sock_migrate().
Things done in sctp_init_sock() but not in sctp_sock_migrate() are the following:
1. Copy sk->sk_gso
2. Copy sk->sk_destruct (sctp_v6_init_sock())
3. Allocate sctp_sock.ep
4. Initialise sctp_sock.pd_lobby
5. Count sk_sockets_allocated_inc(), sock_prot_inuse_add(), and SCTP_DBG_OBJCNT_INC()
Let's do these in sctp_copy_sock() and sctp_sock_migrate() and avoid calling sk->sk_prot->init() in sctp_v[46]_create_accept_sk().
Note that sk->sk_destruct is already copied in sctp_copy_sock().
--- net/sctp/ipv6.c | 8 +------- net/sctp/protocol.c | 8 +------- net/sctp/socket.c | 27 ++++++++++++++++++++++----- 3 files changed, 24 insertions(+), 19 deletions(-)
net/sctp/ipv6.c +1 -7
@@ -789,7 +789,7 @@ static struct sock *sctp_v6_create_accept_sk(struct sock *sk, newsk =sk_alloc (sock_net (sk),PF_INET6 ,GFP_KERNEL , sk->sk_prot, kern);if (!newsk) - goto out; +return NULL ;sock_init_data (NULL , newsk);
@@ -818,12 +818,6 @@ static struct sock *sctp_v6_create_accept_sk(struct sock *sk, newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr; -if (newsk->sk_prot->init (newsk)) { -sk_common_release (newsk); - newsk =NULL ; - } - -out:return newsk; }
net/sctp/protocol.c +1 -7
@@ -590,7 +590,7 @@ static struct sock *sctp_v4_create_accept_sk(struct sock *sk,struct inet_sock *newinet;if (!newsk) - goto out; +return NULL ;sock_init_data (NULL , newsk);
@@ -603,12 +603,6 @@ static struct sock *sctp_v4_create_accept_sk(struct sock *sk, newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; -if (newsk->sk_prot->init (newsk)) { -sk_common_release (newsk); - newsk =NULL ; - } - -out:return newsk; }
net/sctp/socket.c +22 -5
@@ -4851,7 +4851,7 @@ static int sctp_disconnect(struct sock *sk, int flags)*/ static struct sock *sctp_accept (struct sock *sk,struct proto_accept_arg *arg) { -struct sctp_sock *sp; +struct sctp_sock *sp, *newsp;struct sctp_endpoint *ep;struct sock *newsk =NULL ;struct sctp_association *asoc;
@@ -4891,19 +4891,35 @@ static struct sock *sctp_accept(struct sock *sk, struct proto_accept_arg *arg) goto out; } + newsp =sctp_sk (newsk); + newsp->ep =sctp_endpoint_new (newsk,GFP_KERNEL ); +if (!newsp->ep) { + error = -ENOMEM ; + goto out_release; + } + +skb_queue_head_init (&newsp->pd_lobby); + +sk_sockets_allocated_inc (newsk); +sock_prot_inuse_add (sock_net (sk), newsk->sk_prot,1 ); +SCTP_DBG_OBJCNT_INC (sock); +/* Populate the fields of the newsk from the oldsk and migrate the * asoc to the newsk. */ error =sctp_sock_migrate (sk, newsk, asoc,SCTP_SOCKET_TCP ); -if (error) { -sk_common_release (newsk); - newsk =NULL ; - } +if (error) + goto out_release; out:release_sock (sk); arg->err = error;return newsk; + +out_release: +sk_common_release (newsk); + newsk =NULL ; + goto out; }/* The SCTP ioctl handler. */
@@ -9469,6 +9485,7 @@ void sctp_copy_sock(struct sock *newsk, struct sock *sk, newsk->sk_rcvtimeo =READ_ONCE (sk->sk_rcvtimeo); newsk->sk_sndtimeo =READ_ONCE (sk->sk_sndtimeo); newsk->sk_rxhash = sk->sk_rxhash; + newsk->sk_gso_type = sk->sk_gso_type; newinet =inet_sk (newsk);
Signature · 1 line
2.51.1.814.gb8fa24458f-goog
Kuniyuki Iwashima
maintainerPATCH v2 4/8 net: Add sk_clone().
In reply to Kuniyuki Iwashimasctp_accept() will use sk_clone_lock(), but it will be called with the parent socket locked, and sctp_migrate() acquires the child lock later.
Let's add no lock version of sk_clone_lock().
Note that lockdep complains if we simply use bh_lock_sock_nested().
--- include/net/sock.h | 7 ++++++- net/core/sock.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-)
include/net/sock.h +6 -1
@@ -1822,7 +1822,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,void sk_free (struct sock *sk);void sk_net_refcnt_upgrade (struct sock *sk);void sk_destruct (struct sock *sk); -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority); +struct sock *sk_clone (const struct sock *sk,const gfp_t priority,bool lock); + +static inline struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +{ +return sk_clone (sk, priority, true); +}struct sk_buff *sock_wmalloc (struct sock *sk,unsigned long size,int force,gfp_t priority);
net/core/sock.c +14 -7
@@ -2462,13 +2462,16 @@ static void sk_init_common(struct sock *sk) }/** -* sk_clone_lock - clone a socket, and lock its clone -* @sk: the socket to clone -* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* sk_clone - clone a socket +* @sk: the socket to clone +* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* @lock: if true, lock the cloned sk * -* Caller must unlock socket even in error path (bh_unlock_sock(newsk)) +* If @lock is true, the clone is locked by bh_lock_sock(), and +* caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority, +bool lock) {struct proto *prot =READ_ONCE (sk->sk_prot);struct sk_filter *filter;
@@ -2497,9 +2500,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)__netns_tracker_alloc (sock_net (newsk), &newsk->ns_tracker, false, priority); } +sk_node_init (&newsk->sk_node);sock_lock_init (newsk); -bh_lock_sock (newsk); + +if (lock) +bh_lock_sock (newsk); + newsk->sk_backlog.head = newsk->sk_backlog.tail =NULL ; newsk->sk_backlog.len =0 ;
@@ -2595,7 +2602,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) newsk =NULL ; goto out; } -EXPORT_SYMBOL_GPL (sk_clone_lock); +EXPORT_SYMBOL_GPL (sk_clone);static u32 sk_dst_gso_max_size (struct sock *sk,const struct net_device *dev) {
Signature · 1 line
2.51.1.814.gb8fa24458f-goog
Xin Long
maintainerRe: [PATCH v2 net-next 4/8] net: Add sk_clone().
Quoted 61 lines · Kuniyuki Iwashima
sctp_accept()will usesk_clone_lock(), but it will be called with the parent socket locked, andsctp_migrate()acquires the child lock later.Let's add no lock version of
sk_clone_lock().Note that lockdep complains if we simply use
bh_lock_sock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- include/net/sock.h | 7 ++++++- net/core/sock.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-)include/net/sock.h +6
-1@@ -1822,7 +1822,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,void sk_free (struct sock *sk);void sk_net_refcnt_upgrade (struct sock *sk);void sk_destruct (struct sock *sk); -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority); +struct sock *sk_clone (const struct sock *sk,const gfp_t priority,bool lock); + +static inline struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +{ +return sk_clone (sk, priority, true); +}struct sk_buff *sock_wmalloc (struct sock *sk,unsigned long size,int force,gfp_t priority);net/core/sock.c +8
-5@@ -2462,13 +2462,16 @@ static void sk_init_common(struct sock *sk) }/** -* sk_clone_lock - clone a socket, and lock its clone -* @sk: the socket to clone -* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* sk_clone - clone a socket +* @sk: the socket to clone +* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* @lock: if true, lock the cloned sk * -* Caller must unlock socket even in error path (bh_unlock_sock(newsk)) +* If @lock is true, the clone is locked by bh_lock_sock(), and +* caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority, +bool lock) {struct proto *prot =READ_ONCE (sk->sk_prot);struct sk_filter *filter;@@ -2497,9 +2500,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) __netns_tracker_alloc(sock_net(newsk), &newsk->ns_tracker,
net/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 2509
Quoted 10 lines
net/core/sock.c +5
-1false, priority); } +sk_node_init (&newsk->sk_node);sock_lock_init (newsk); -bh_lock_sock (newsk); + +if (lock) +bh_lock_sock (newsk); +
does it really need bh_lock_sock() that early, if not, maybe we can move it out of sk_clone_lock(), and names sk_clone_lock() back to sk_clone()?
Quoted 15 lines
net/core/sock.c +1
-2newsk->sk_backlog.head = newsk->sk_backlog.tail =NULL ; newsk->sk_backlog.len =0 ;@@ -2595,7 +2602,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) newsk =NULL ; goto out; } -EXPORT_SYMBOL_GPL (sk_clone_lock); +EXPORT_SYMBOL_GPL (sk_clone);static u32 sk_dst_gso_max_size (struct sock *sk,const struct net_device *dev) { --2.51.1.814.gb8fa24458f-goog
Kuniyuki Iwashima
maintainerRe: [PATCH v2 net-next 4/8] net: Add sk_clone().
Quoted 67 lines · Xin Long
Quoted 65 lines · Kuniyuki Iwashima
sctp_accept()will usesk_clone_lock(), but it will be called with the parent socket locked, andsctp_migrate()acquires the child lock later.Let's add no lock version of
sk_clone_lock().Note that lockdep complains if we simply use
bh_lock_sock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- include/net/sock.h | 7 ++++++- net/core/sock.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-)include/net/sock.h +6
-1@@ -1822,7 +1822,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,void sk_free (struct sock *sk);void sk_net_refcnt_upgrade (struct sock *sk);void sk_destruct (struct sock *sk); -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority); +struct sock *sk_clone (const struct sock *sk,const gfp_t priority,bool lock); + +static inline struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +{ +return sk_clone (sk, priority, true); +}struct sk_buff *sock_wmalloc (struct sock *sk,unsigned long size,int force,gfp_t priority);net/core/sock.c +9
-5@@ -2462,13 +2462,16 @@ static void sk_init_common(struct sock *sk) }/** -* sk_clone_lock - clone a socket, and lock its clone -* @sk: the socket to clone -* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* sk_clone - clone a socket +* @sk: the socket to clone +* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* @lock: if true, lock the cloned sk * -* Caller must unlock socket even in error path (bh_unlock_sock(newsk)) +* If @lock is true, the clone is locked by bh_lock_sock(), and +* caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority, +bool lock) {struct proto *prot =READ_ONCE (sk->sk_prot);struct sk_filter *filter;@@ -2497,9 +2500,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)__netns_tracker_alloc (sock_net (newsk), &newsk->ns_tracker, false, priority); } +sk_node_init (&newsk->sk_node);
Quoted 8 lines
net/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 2509
Quoted 6 lines
net/core/sock.c +4
-1sock_lock_init (newsk); -bh_lock_sock (newsk); + +if (lock) +bh_lock_sock (newsk); +does it really need
bh_lock_sock()that early, if not, maybe we can move it out ofsk_clone_lock(), and namessk_clone_lock()back tosk_clone()?
I think sk_clone_lock() and leaf functions do not have lockdep_sock_is_held(), and probably the closest one is security_inet_csk_clone() which requires lock_sock() for bpf_setsockopt(), this can be easily adjusted though. (see bpf_lsm_locked_sockopt_hooks)
Only concern would be moving bh_lock_sock() there will introduce one cache line miss.
Xin Long
maintainerRe: [PATCH v2 net-next 4/8] net: Add sk_clone().
Quoted 78 lines · Kuniyuki Iwashima
Quoted 67 lines · Xin Long
Quoted 65 lines · Kuniyuki Iwashima
sctp_accept()will usesk_clone_lock(), but it will be called with the parent socket locked, andsctp_migrate()acquires the child lock later.Let's add no lock version of
sk_clone_lock().Note that lockdep complains if we simply use
bh_lock_sock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- include/net/sock.h | 7 ++++++- net/core/sock.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-)include/net/sock.h +6
-1@@ -1822,7 +1822,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,void sk_free (struct sock *sk);void sk_net_refcnt_upgrade (struct sock *sk);void sk_destruct (struct sock *sk); -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority); +struct sock *sk_clone (const struct sock *sk,const gfp_t priority,bool lock); + +static inline struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +{ +return sk_clone (sk, priority, true); +}struct sk_buff *sock_wmalloc (struct sock *sk,unsigned long size,int force,gfp_t priority);net/core/sock.c +9
-5@@ -2462,13 +2462,16 @@ static void sk_init_common(struct sock *sk) }/** -* sk_clone_lock - clone a socket, and lock its clone -* @sk: the socket to clone -* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* sk_clone - clone a socket +* @sk: the socket to clone +* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* @lock: if true, lock the cloned sk * -* Caller must unlock socket even in error path (bh_unlock_sock(newsk)) +* If @lock is true, the clone is locked by bh_lock_sock(), and +* caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority, +bool lock) {struct proto *prot =READ_ONCE (sk->sk_prot);struct sk_filter *filter;@@ -2497,9 +2500,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)__netns_tracker_alloc (sock_net (newsk), &newsk->ns_tracker, false, priority); } +sk_node_init (&newsk->sk_node);Quoted 8 lines
net/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 2509
Quoted 6 lines
net/core/sock.c +4
-1sock_lock_init (newsk); -bh_lock_sock (newsk); + +if (lock) +bh_lock_sock (newsk); +does it really need
bh_lock_sock()that early, if not, maybe we can move it out ofsk_clone_lock(), and namessk_clone_lock()back tosk_clone()?
Quoted 6 lines
I think
sk_clone_lock()and leaf functions do not havelockdep_sock_is_held(), and probably the closest one issecurity_inet_csk_clone()which requireslock_sock()forbpf_setsockopt(), this can be easily adjusted though. (seebpf_lsm_locked_sockopt_hooks)
Right.
Quoted 2 lines
Only concern would be moving
bh_lock_sock()there will introduce one cache line miss.
I think it’s negligible, and it’s not even on the data path, though others may have different opinions.
Kuniyuki Iwashima
maintainerRe: [PATCH v2 net-next 4/8] net: Add sk_clone().
Quoted 86 lines · Xin Long
Quoted 78 lines · Kuniyuki Iwashima
Quoted 67 lines · Xin Long
Quoted 65 lines · Kuniyuki Iwashima
sctp_accept()will usesk_clone_lock(), but it will be called with the parent socket locked, andsctp_migrate()acquires the child lock later.Let's add no lock version of
sk_clone_lock().Note that lockdep complains if we simply use
bh_lock_sock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- include/net/sock.h | 7 ++++++- net/core/sock.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-)include/net/sock.h +6
-1@@ -1822,7 +1822,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,void sk_free (struct sock *sk);void sk_net_refcnt_upgrade (struct sock *sk);void sk_destruct (struct sock *sk); -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority); +struct sock *sk_clone (const struct sock *sk,const gfp_t priority,bool lock); + +static inline struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +{ +return sk_clone (sk, priority, true); +}struct sk_buff *sock_wmalloc (struct sock *sk,unsigned long size,int force,gfp_t priority);net/core/sock.c +9
-5@@ -2462,13 +2462,16 @@ static void sk_init_common(struct sock *sk) }/** -* sk_clone_lock - clone a socket, and lock its clone -* @sk: the socket to clone -* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* sk_clone - clone a socket +* @sk: the socket to clone +* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* @lock: if true, lock the cloned sk * -* Caller must unlock socket even in error path (bh_unlock_sock(newsk)) +* If @lock is true, the clone is locked by bh_lock_sock(), and +* caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority, +bool lock) {struct proto *prot =READ_ONCE (sk->sk_prot);struct sk_filter *filter;@@ -2497,9 +2500,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)__netns_tracker_alloc (sock_net (newsk), &newsk->ns_tracker, false, priority); } +sk_node_init (&newsk->sk_node);Quoted 8 lines
net/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 2509
Quoted 6 lines
net/core/sock.c +4
-1sock_lock_init (newsk); -bh_lock_sock (newsk); + +if (lock) +bh_lock_sock (newsk); +does it really need
bh_lock_sock()that early, if not, maybe we can move it out ofsk_clone_lock(), and namessk_clone_lock()back tosk_clone()?Quoted 6 lines
I think
sk_clone_lock()and leaf functions do not havelockdep_sock_is_held(), and probably the closest one issecurity_inet_csk_clone()which requireslock_sock()forbpf_setsockopt(), this can be easily adjusted though. (seebpf_lsm_locked_sockopt_hooks)
Quoted 6 lines
Right.
Quoted 2 lines
Only concern would be moving
bh_lock_sock()there will introduce one cache line miss.I think it’s negligible, and it’s not even on the data path, though others may have different opinions.
For SCTP, yes, but I'd avoid it for TCP.
Xin Long
maintainerRe: [PATCH v2 net-next 4/8] net: Add sk_clone().
Quoted 88 lines · Kuniyuki Iwashima
Quoted 86 lines · Xin Long
Quoted 84 lines · Kuniyuki Iwashima
Quoted 67 lines · Xin Long
Quoted 65 lines · Kuniyuki Iwashima
sctp_accept()will usesk_clone_lock(), but it will be called with the parent socket locked, andsctp_migrate()acquires the child lock later.Let's add no lock version of
sk_clone_lock().Note that lockdep complains if we simply use
bh_lock_sock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- include/net/sock.h | 7 ++++++- net/core/sock.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-)include/net/sock.h +6
-1@@ -1822,7 +1822,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,void sk_free (struct sock *sk);void sk_net_refcnt_upgrade (struct sock *sk);void sk_destruct (struct sock *sk); -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority); +struct sock *sk_clone (const struct sock *sk,const gfp_t priority,bool lock); + +static inline struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +{ +return sk_clone (sk, priority, true); +}struct sk_buff *sock_wmalloc (struct sock *sk,unsigned long size,int force,gfp_t priority);net/core/sock.c +9
-5@@ -2462,13 +2462,16 @@ static void sk_init_common(struct sock *sk) }/** -* sk_clone_lock - clone a socket, and lock its clone -* @sk: the socket to clone -* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* sk_clone - clone a socket +* @sk: the socket to clone +* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* @lock: if true, lock the cloned sk * -* Caller must unlock socket even in error path (bh_unlock_sock(newsk)) +* If @lock is true, the clone is locked by bh_lock_sock(), and +* caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority, +bool lock) {struct proto *prot =READ_ONCE (sk->sk_prot);struct sk_filter *filter;@@ -2497,9 +2500,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)__netns_tracker_alloc (sock_net (newsk), &newsk->ns_tracker, false, priority); } +sk_node_init (&newsk->sk_node);Quoted 8 lines
net/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 2509
Quoted 6 lines
net/core/sock.c +4
-1sock_lock_init (newsk); -bh_lock_sock (newsk); + +if (lock) +bh_lock_sock (newsk); +does it really need
bh_lock_sock()that early, if not, maybe we can move it out ofsk_clone_lock(), and namessk_clone_lock()back tosk_clone()?I think
sk_clone_lock()and leaf functions do not havelockdep_sock_is_held(), and probably the closest one issecurity_inet_csk_clone()which requireslock_sock()forbpf_setsockopt(), this can be easily adjusted though. (seebpf_lsm_locked_sockopt_hooks)
Quoted 8 lines
Quoted 6 lines
Right.
Quoted 2 lines
Only concern would be moving
bh_lock_sock()there will introduce one cache line miss.I think it’s negligible, and it’s not even on the data path, though others may have different opinions.
For SCTP, yes, but I'd avoid it for TCP.
Okay, not a problem, just doesn't look common to pass such a parameter.
There is also bh_unlock_sock(newsk) in the free path in sk_clone(), does it also need a 'if (lock)' check?
Kuniyuki Iwashima
maintainerRe: [PATCH v2 net-next 4/8] net: Add sk_clone().
From: Xin Long <[email protected]>
Date: Thu, 23 Oct 2025 15:55:57 -0400
Quoted 91 lines · Kuniyuki Iwashima
Quoted 90 lines · Xin Long
Quoted 88 lines · Kuniyuki Iwashima
Quoted 78 lines · Xin Long
Quoted 67 lines · Kuniyuki Iwashima
Quoted 65 lines
sctp_accept()will usesk_clone_lock(), but it will be called with the parent socket locked, andsctp_migrate()acquires the child lock later.Let's add no lock version of
sk_clone_lock().Note that lockdep complains if we simply use
bh_lock_sock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- include/net/sock.h | 7 ++++++- net/core/sock.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-)include/net/sock.h +6
-1@@ -1822,7 +1822,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,void sk_free (struct sock *sk);void sk_net_refcnt_upgrade (struct sock *sk);void sk_destruct (struct sock *sk); -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority); +struct sock *sk_clone (const struct sock *sk,const gfp_t priority,bool lock); + +static inline struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +{ +return sk_clone (sk, priority, true); +}struct sk_buff *sock_wmalloc (struct sock *sk,unsigned long size,int force,gfp_t priority);net/core/sock.c +9
-5@@ -2462,13 +2462,16 @@ static void sk_init_common(struct sock *sk) }/** -* sk_clone_lock - clone a socket, and lock its clone -* @sk: the socket to clone -* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* sk_clone - clone a socket +* @sk: the socket to clone +* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* @lock: if true, lock the cloned sk * -* Caller must unlock socket even in error path (bh_unlock_sock(newsk)) +* If @lock is true, the clone is locked by bh_lock_sock(), and +* caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority, +bool lock) {struct proto *prot =READ_ONCE (sk->sk_prot);struct sk_filter *filter;@@ -2497,9 +2500,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)__netns_tracker_alloc (sock_net (newsk), &newsk->ns_tracker, false, priority); } +sk_node_init (&newsk->sk_node);Quoted 8 lines
net/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 2509
Quoted 6 lines
net/core/sock.c +4
-1sock_lock_init (newsk); -bh_lock_sock (newsk); + +if (lock) +bh_lock_sock (newsk); +does it really need
bh_lock_sock()that early, if not, maybe we can move it out ofsk_clone_lock(), and namessk_clone_lock()back tosk_clone()?Quoted 6 lines
I think
sk_clone_lock()and leaf functions do not havelockdep_sock_is_held(), and probably the closest one issecurity_inet_csk_clone()which requireslock_sock()forbpf_setsockopt(), this can be easily adjusted though. (seebpf_lsm_locked_sockopt_hooks)Right.
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Only concern would be moving
bh_lock_sock()there will introduce one cache line miss.I think it’s negligible, and it’s not even on the data path, though others may have different opinions.
For SCTP, yes, but I'd avoid it for TCP.
Okay, not a problem, just doesn't look common to pass such a parameter.
Another option would be add a check like this ?
---8<---
include/net/sock.h +6 -0
@@ -2904,6 +2904,12 @@ static inline bool sk_is_inet(const struct sock *sk)return family ==AF_INET || family ==AF_INET6 ; } +static inline bool sk_is_sctp (const struct sock *sk) +{ +return IS_ENABLED (CONFIG_SCTP ) && + sk->sk_protocol ==IPPROTO_SCTP ; +} +static inline bool sk_is_tcp (const struct sock *sk) { return sk_is_inet(sk) &&
net/core/sock.c +4 -3
@@ -2470,10 +2470,10 @@ static void sk_init_common(struct sock *sk)* If @lock is true, the clone is locked by bh_lock_sock(), and * caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone (const struct sock *sk,const gfp_t priority, -bool lock) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority) {struct proto *prot =READ_ONCE (sk->sk_prot); +bool lock = !sk_is_sctp (sk);struct sk_filter *filter;bool is_charged = true;struct sock *newsk;
@@ -2597,7 +2597,8 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,* destructor and make plain sk_free() */ newsk->sk_destruct =NULL ; -bh_unlock_sock (newsk); +if (lock) +bh_unlock_sock (newsk);sk_free (newsk); newsk =NULL ; goto out;
---8<---
Quoted 3 lines
There is also
bh_unlock_sock(newsk)in the free path insk_clone(), does it also need a 'if (lock)' check?
Good catch. Will fix it in v3.
Thanks!
Xin Long
maintainerRe: [PATCH v2 net-next 4/8] net: Add sk_clone().
Quoted 110 lines · Kuniyuki Iwashima
From: Xin Long <[email protected]>
Date: Thu, 23 Oct 2025 15:55:57 -0400Quoted 91 lines · Kuniyuki Iwashima
Quoted 90 lines · Xin Long
Quoted 88 lines · Kuniyuki Iwashima
Quoted 78 lines · Xin Long
Quoted 67 lines · Kuniyuki Iwashima
Quoted 65 lines
sctp_accept()will usesk_clone_lock(), but it will be called with the parent socket locked, andsctp_migrate()acquires the child lock later.Let's add no lock version of
sk_clone_lock().Note that lockdep complains if we simply use
bh_lock_sock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- include/net/sock.h | 7 ++++++- net/core/sock.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-)include/net/sock.h +6
-1@@ -1822,7 +1822,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,void sk_free (struct sock *sk);void sk_net_refcnt_upgrade (struct sock *sk);void sk_destruct (struct sock *sk); -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority); +struct sock *sk_clone (const struct sock *sk,const gfp_t priority,bool lock); + +static inline struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +{ +return sk_clone (sk, priority, true); +}struct sk_buff *sock_wmalloc (struct sock *sk,unsigned long size,int force,gfp_t priority);net/core/sock.c +9
-5@@ -2462,13 +2462,16 @@ static void sk_init_common(struct sock *sk) }/** -* sk_clone_lock - clone a socket, and lock its clone -* @sk: the socket to clone -* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* sk_clone - clone a socket +* @sk: the socket to clone +* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* @lock: if true, lock the cloned sk * -* Caller must unlock socket even in error path (bh_unlock_sock(newsk)) +* If @lock is true, the clone is locked by bh_lock_sock(), and +* caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority, +bool lock) {struct proto *prot =READ_ONCE (sk->sk_prot);struct sk_filter *filter;@@ -2497,9 +2500,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)__netns_tracker_alloc (sock_net (newsk), &newsk->ns_tracker, false, priority); } +sk_node_init (&newsk->sk_node);Quoted 8 lines
net/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 2509
Quoted 6 lines
net/core/sock.c +4
-1sock_lock_init (newsk); -bh_lock_sock (newsk); + +if (lock) +bh_lock_sock (newsk); +does it really need
bh_lock_sock()that early, if not, maybe we can move it out ofsk_clone_lock(), and namessk_clone_lock()back tosk_clone()?Quoted 6 lines
I think
sk_clone_lock()and leaf functions do not havelockdep_sock_is_held(), and probably the closest one issecurity_inet_csk_clone()which requireslock_sock()forbpf_setsockopt(), this can be easily adjusted though. (seebpf_lsm_locked_sockopt_hooks)Right.
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Only concern would be moving
bh_lock_sock()there will introduce one cache line miss.I think it’s negligible, and it’s not even on the data path, though others may have different opinions.
For SCTP, yes, but I'd avoid it for TCP.
Okay, not a problem, just doesn't look common to pass such a parameter.
Another option would be add a check like this ?
---8<---
include/net/sock.h +0
-0
include/net/sock.h static inline bool sk_is_inet(const struct sock *sk) · line 2912
Quoted 9 lines
include/net/sock.h +6
-0@@ -2904,6 +2904,12 @@ static inline bool sk_is_inet(const struct sock *sk)return family ==AF_INET || family ==AF_INET6 ; } +static inline bool sk_is_sctp (const struct sock *sk) +{ +return IS_ENABLED (CONFIG_SCTP ) && + sk->sk_protocol ==IPPROTO_SCTP ; +} +
Oh, better not, I'm actually planning to use sk_clone() in quic_accept() :D
https://github.com/lxin/quic/blob/main/modules/net/quic/socket.c#L1421
Quoted 40 lines
include/net/sock.h +0
-0static inline bool sk_is_tcp (const struct sock *sk) { return sk_is_inet(sk) &&net/core/sock.c +4
-4@@ -2470,10 +2470,10 @@ static void sk_init_common(struct sock *sk)* If @lock is true, the clone is locked by bh_lock_sock(), and * caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone (const struct sock *sk,const gfp_t priority, -bool lock) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority) {struct proto *prot =READ_ONCE (sk->sk_prot); +bool lock = !sk_is_sctp (sk);struct sk_filter *filter;bool is_charged = true;struct sock *newsk;@@ -2597,7 +2597,8 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,* destructor and make plain sk_free() */ newsk->sk_destruct =NULL ; -bh_unlock_sock (newsk); +if (lock) +bh_unlock_sock (newsk);sk_free (newsk); newsk =NULL ; goto out; ---8<---Quoted 5 lines
There is also
bh_unlock_sock(newsk)in the free path insk_clone(), does it also need a 'if (lock)' check?Good catch. Will fix it in v3.
Thanks!
Kuniyuki Iwashima
maintainerRe: [PATCH v2 net-next 4/8] net: Add sk_clone().
Quoted 123 lines · Xin Long
Quoted 110 lines · Kuniyuki Iwashima
From: Xin Long <[email protected]>
Date: Thu, 23 Oct 2025 15:55:57 -0400Quoted 91 lines · Kuniyuki Iwashima
Quoted 90 lines · Xin Long
Quoted 88 lines · Kuniyuki Iwashima
Quoted 78 lines · Xin Long
Quoted 67 lines · Kuniyuki Iwashima
Quoted 65 lines
sctp_accept()will usesk_clone_lock(), but it will be called with the parent socket locked, andsctp_migrate()acquires the child lock later.Let's add no lock version of
sk_clone_lock().Note that lockdep complains if we simply use
bh_lock_sock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- include/net/sock.h | 7 ++++++- net/core/sock.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-)include/net/sock.h +6
-1@@ -1822,7 +1822,12 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,void sk_free (struct sock *sk);void sk_net_refcnt_upgrade (struct sock *sk);void sk_destruct (struct sock *sk); -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority); +struct sock *sk_clone (const struct sock *sk,const gfp_t priority,bool lock); + +static inline struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +{ +return sk_clone (sk, priority, true); +}struct sk_buff *sock_wmalloc (struct sock *sk,unsigned long size,int force,gfp_t priority);net/core/sock.c +9
-5@@ -2462,13 +2462,16 @@ static void sk_init_common(struct sock *sk) }/** -* sk_clone_lock - clone a socket, and lock its clone -* @sk: the socket to clone -* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* sk_clone - clone a socket +* @sk: the socket to clone +* @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc) +* @lock: if true, lock the cloned sk * -* Caller must unlock socket even in error path (bh_unlock_sock(newsk)) +* If @lock is true, the clone is locked by bh_lock_sock(), and +* caller must unlock socket even in error path by bh_unlock_sock(). */ -struct sock *sk_clone_lock (const struct sock *sk,const gfp_t priority) +struct sock *sk_clone (const struct sock *sk,const gfp_t priority, +bool lock) {struct proto *prot =READ_ONCE (sk->sk_prot);struct sk_filter *filter;@@ -2497,9 +2500,13 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)__netns_tracker_alloc (sock_net (newsk), &newsk->ns_tracker, false, priority); } +sk_node_init (&newsk->sk_node);Quoted 8 lines
net/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 2509
Quoted 6 lines
net/core/sock.c +4
-1sock_lock_init (newsk); -bh_lock_sock (newsk); + +if (lock) +bh_lock_sock (newsk); +does it really need
bh_lock_sock()that early, if not, maybe we can move it out ofsk_clone_lock(), and namessk_clone_lock()back tosk_clone()?Quoted 6 lines
I think
sk_clone_lock()and leaf functions do not havelockdep_sock_is_held(), and probably the closest one issecurity_inet_csk_clone()which requireslock_sock()forbpf_setsockopt(), this can be easily adjusted though. (seebpf_lsm_locked_sockopt_hooks)Right.
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Only concern would be moving
bh_lock_sock()there will introduce one cache line miss.I think it’s negligible, and it’s not even on the data path, though others may have different opinions.
For SCTP, yes, but I'd avoid it for TCP.
Okay, not a problem, just doesn't look common to pass such a parameter.
Another option would be add a check like this ?
---8<---
include/net/sock.h +0
-0include/net/sock.h static inline bool sk_is_inet(const struct sock *sk) · line 2912
Quoted 9 lines
include/net/sock.h +6
-0@@ -2904,6 +2904,12 @@ static inline bool sk_is_inet(const struct sock *sk)return family ==AF_INET || family ==AF_INET6 ; } +static inline bool sk_is_sctp (const struct sock *sk) +{ +return IS_ENABLED (CONFIG_SCTP ) && + sk->sk_protocol ==IPPROTO_SCTP ; +} +Oh, better not, I'm actually planning to use
sk_clone()inquic_accept():D
Okay, then I'll keep the current form.
Kuniyuki Iwashima
maintainerPATCH v2 5/8 sctp: Use sk_clone() in sctp_accept().
In reply to Kuniyuki Iwashimasctp_accept() calls sctp_v[46]_create_accept_sk() to allocate a new socket and calls sctp_sock_migrate() to copy fields from the parent socket to the new socket.
sctp_v4_create_accept_sk() allocates sk by sk_alloc(), initialises it by sock_init_data(), and copy a bunch of fields from the parent socekt by sctp_copy_sock().
sctp_sock_migrate() calls sctp_copy_descendant() to copy most fields in sctp_sock from the parent socket by memcpy().
These can be simply replaced by sk_clone().
Let's consolidate sctp_v[46]_create_accept_sk() to sctp_clone_sock() with sk_clone().
We will reuse sctp_clone_sock() for sctp_do_peeloff() and then remove sctp_copy_descendant().
Note that sock_reset_flag(newsk, SOCK_ZAPPED) is not copied to sctp_clone_sock() as sctp does not use SOCK_ZAPPED at all.
--- net/ipv4/af_inet.c | 4 +- net/sctp/socket.c | 113 ++++++++++++++++++++++++++++++--------------- 2 files changed, 77 insertions(+), 40 deletions(-)
net/ipv4/af_inet.c +1 -3
@@ -755,9 +755,7 @@ EXPORT_SYMBOL(inet_stream_connect);void __inet_accept (struct socket *sock,struct socket *newsock,struct sock *newsk) { -/* TODO: use sk_clone_lock() in SCTP and remove protocol checks */ -if (mem_cgroup_sockets_enabled && - (!IS_ENABLED (CONFIG_IP_SCTP ) ||sk_is_tcp (newsk))) { +if (mem_cgroup_sockets_enabled) {gfp_t gfp =GFP_KERNEL | __GFP_NOFAIL;mem_cgroup_sk_alloc (newsk);
net/sctp/socket.c +76 -37
143 lines not on this page · raw message
Signature · 1 line
2.51.1.814.gb8fa24458f-goog
Kuniyuki Iwashima
maintainerPATCH v2 6/8 sctp: Remove sctp_pf.create_accept_sk().
In reply to Kuniyuki Iwashimasctp_v[46]_create_accept_sk() are no longer used.
Let's remove sctp_pf.create_accept_sk().
--- include/net/sctp/structs.h | 3 --- net/sctp/ipv6.c | 45 -------------------------------------- net/sctp/protocol.c | 27 ----------------------- 3 files changed, 75 deletions(-)
include/net/sctp/structs.h +0 -3
@@ -497,9 +497,6 @@ struct sctp_pf {int (*bind_verify) (struct sctp_sock *,union sctp_addr *);int (*send_verify) (struct sctp_sock *,union sctp_addr *);int (*supported_addrs)(const struct sctp_sock *,__be16 *); -struct sock *(*create_accept_sk) (struct sock *sk, -struct sctp_association *asoc, -bool kern);int (*addr_to_user)(struct sctp_sock *sk,union sctp_addr *addr);void (*to_sk_saddr)(union sctp_addr *,struct sock *sk);void (*to_sk_daddr)(union sctp_addr *,struct sock *sk);
net/sctp/ipv6.c +0 -45
57 lines not on this page · raw message
net/sctp/protocol.c +0 -27
@@ -580,32 +580,6 @@ static int sctp_v4_is_ce(const struct sk_buff *skb)return INET_ECN_is_ce (ip_hdr (skb)->tos); } -/* Create and initialize a new sk for the socket returned by accept(). */ -static struct sock *sctp_v4_create_accept_sk (struct sock *sk, -struct sctp_association *asoc, -bool kern) -{ -struct sock *newsk =sk_alloc (sock_net (sk),PF_INET ,GFP_KERNEL , - sk->sk_prot, kern); -struct inet_sock *newinet; - -if (!newsk) -return NULL ; - -sock_init_data (NULL , newsk); - -sctp_copy_sock (newsk, sk, asoc); -sock_reset_flag (newsk,SOCK_ZAPPED ); - -sctp_v4_copy_ip_options (sk, newsk); - - newinet =inet_sk (newsk); - - newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; - -return newsk; -} -static int sctp_v4_addr_to_user (struct sctp_sock *sp,union sctp_addr *addr) {/* No address mapping for V4 sockets */
@@ -1113,7 +1087,6 @@ static struct sctp_pf sctp_pf_inet = { .bind_verify = sctp_inet_bind_verify, .send_verify = sctp_inet_send_verify, .supported_addrs = sctp_inet_supported_addrs, - .create_accept_sk = sctp_v4_create_accept_sk, .addr_to_user = sctp_v4_addr_to_user, .to_sk_saddr = sctp_v4_to_sk_saddr, .to_sk_daddr = sctp_v4_to_sk_daddr,
Signature · 1 line
2.51.1.814.gb8fa24458f-goog
Kuniyuki Iwashima
maintainerPATCH v2 7/8 sctp: Use sctp_clone_sock() in sctp_do_peeloff().
In reply to Kuniyuki Iwashimasctp_do_peeloff() calls sock_create() to allocate and initialise struct sock, inet_sock, and sctp_sock, but later sctp_copy_sock() and sctp_sock_migrate() overwrite most fields.
What sctp_do_peeloff() does is more like accept().
Let's use sock_create_lite() and sctp_clone_sock().
---
v2: Export __inet_accept()
--- net/ipv4/af_inet.c | 1 + net/sctp/socket.c | 36 +++++++++++++++--------------------- 2 files changed, 16 insertions(+), 21 deletions(-)
net/ipv4/af_inet.c +1 -0
@@ -788,6 +788,7 @@ void __inet_accept(struct socket *sock, struct socket *newsock, struct sock *new newsock->state =SS_CONNECTED ; } +EXPORT_SYMBOL_GPL (__inet_accept);/* * Accept a pending connection. The TCP layer now gives BSD semantics.
net/sctp/socket.c +15 -21
@@ -5671,11 +5671,11 @@ static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optv/* Helper routine to branch off an association to a new socket. */ static int sctp_do_peeloff (struct sock *sk,sctp_assoc_t id, -struct socket **sockp) +struct socket **sockp) {struct sctp_association *asoc =sctp_id2assoc (sk, id); -struct sctp_sock *sp =sctp_sk (sk);struct socket *sock; +struct sock *newsk;int err =0 ;/* Do not peel off from one netns to another one. */
@@ -5691,30 +5691,24 @@ static int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id,if (!sctp_style (sk,UDP ))return -EINVAL ; -/* Create a new socket. */ - err =sock_create (sk->sk_family,SOCK_SEQPACKET ,IPPROTO_SCTP , &sock); -if (err <0 ) + err =sock_create_lite (sk->sk_family,SOCK_SEQPACKET ,IPPROTO_SCTP , &sock); +if (err)return err; -sctp_copy_sock (sock->sk, sk, asoc); - -/* Make peeled-off sockets more like 1-1 accepted sockets. -* Set the daddr and initialize id to something more random and also -* copy over any ip options. -*/ - sp->pf->to_sk_daddr (&asoc->peer.primary_addr, sock->sk); - sp->pf->copy_ip_options (sk, sock->sk); - -/* Populate the fields of the newsk from the oldsk and migrate the -* asoc to the newsk. -*/ - err =sctp_sock_migrate (sk, sock->sk, asoc, -SCTP_SOCKET_UDP_HIGH_BANDWIDTH ); -if (err) { + newsk =sctp_clone_sock (sk, asoc,SCTP_SOCKET_UDP_HIGH_BANDWIDTH ); +if (IS_ERR (newsk)) {sock_release (sock); - sock =NULL ; + *sockp =NULL ; +return PTR_ERR (newsk); } +lock_sock_nested (newsk,SINGLE_DEPTH_NESTING ); +__inet_accept (sk->sk_socket, sock, newsk); +release_sock (newsk); + + sock->ops = sk->sk_socket->ops; +__module_get (sock->ops->owner); + *sockp = sock;return err;
Signature · 1 line
2.51.1.814.gb8fa24458f-goog
Kuniyuki Iwashima
maintainerPATCH v2 8/8 sctp: Remove sctp_copy_sock() and sctp_copy_descendant().
In reply to Kuniyuki IwashimaNow, sctp_accept() and sctp_do_peeloff() use sk_clone(), and we no longer need sctp_copy_sock() and sctp_copy_descendant().
Let's remove them.
--- include/net/inet_sock.h | 8 ----- include/net/sctp/sctp.h | 3 +- net/sctp/socket.c | 71 ----------------------------------------- 3 files changed, 1 insertion(+), 81 deletions(-)
include/net/inet_sock.h +0 -8
@@ -355,14 +355,6 @@ static inline struct sock *skb_to_full_sk(const struct sk_buff *skb)#define inet_sk (ptr) container_of_const(ptr, struct inet_sock, sk) -static inline void __inet_sk_copy_descendant (struct sock *sk_to, -const struct sock *sk_from, -const int ancestor_size) -{ -memcpy (inet_sk (sk_to) +1 ,inet_sk (sk_from) +1 , - sk_from->sk_prot->obj_size - ancestor_size); -} -int inet_sk_rebuild_header (struct sock *sk);/**
include/net/sctp/sctp.h +1 -2
@@ -94,8 +94,7 @@ void sctp_data_ready(struct sock *sk);__poll_t sctp_poll (struct file *file,struct socket *sock,poll_table *wait);void sctp_sock_rfree (struct sk_buff *skb); -void sctp_copy_sock (struct sock *newsk,struct sock *sk, -struct sctp_association *asoc); +extern struct percpu_counter sctp_sockets_allocated;int sctp_asconf_mgmt (struct sctp_sock *,struct sctp_sockaddr_entry *);struct sk_buff *sctp_skb_recv_datagram (struct sock *,int ,int *);
net/sctp/socket.c +0 -71
83 lines not on this page · raw message
Signature · 1 line
2.51.1.814.gb8fa24458f-goog