net: correct zerocopy refcnt with newly allocated UDP or RAW uarg
Subsystems (2)
Quoted 10 lines · Willem de Bruijn
Quoted 5 lines · Miaohe Lin
The var
extra_urefis introduced to pass the initial reference taken insock_zerocopy_allocto the first generated skb. But now we may fail to pass the initial reference with newly allocated UDP or RAW uarg when the skb is zcopied.
extra_urefis true if there is no previous skb to append to or there is a previous skb, but that does not have zerocopy data associated yet (because the previous call(s) did not setMSG_ZEROCOPY).In other words, when first (allocating and) associating a zerocopy struct with the skb.
Many thanks for your explaination. The var extra_uref plays the role as you say. I just borrowed the description of var extra_uref from previous commit log here.
Quoted 6 lines
Review
Quoted 5 lines
(quoted diff) +3
-1- extra_uref = !skb_zcopy(skb); /* only ref on new uarg */ + /* Only ref on newly allocated uarg. */ + if (!skb_zcopy(skb) || (sk->sk_type != SOCK_STREAM && skb_zcopy(skb) != uarg)) + extra_uref = true;
Quoted 8 lines
SOCK_STREAMdoes not use__ip_append_data.This leaves as new branch
skb_zcopy(skb)&&skb_zcopy(skb)!= uarg.This function can only acquire a uarg through
sock_zerocopy_realloc, which onskb_zcopy(skb)only returns the existing uarg or NULL (for notSOCK_STREAM).So I don't see when that condition can happen.
On skb_zcopy(skb), we returns the existing uarg iff (uarg->id + uarg->len == atomic_read(&sk->sk_zckey)) in sock_zerocopy_realloc. So we may get a newly allocated uarg via sock_zerocopy_alloc(). Though we may not trigger this codepath now, it's still a potential problem that we may missed the right trace to uarg.
Or am I still miss anything? Thanks.
Quoted 25 lines · linmiaohe
Quoted 10 lines · Willem de Bruijn
Quoted 5 lines · Miaohe Lin
The var
extra_urefis introduced to pass the initial reference taken insock_zerocopy_allocto the first generated skb. But now we may fail to pass the initial reference with newly allocated UDP or RAW uarg when the skb is zcopied.
extra_urefis true if there is no previous skb to append to or there is a previous skb, but that does not have zerocopy data associated yet (because the previous call(s) did not setMSG_ZEROCOPY).In other words, when first (allocating and) associating a zerocopy struct with the skb.
Many thanks for your explaination. The var
extra_urefplays the role as you say. I just borrowed the description of varextra_ureffrom previous commit log here.Quoted 10 lines
Review
Quoted 5 lines
(quoted diff) +3
-1- extra_uref = !skb_zcopy(skb); /* only ref on new uarg */ + /* Only ref on newly allocated uarg. */ + if (!skb_zcopy(skb) || (sk->sk_type != SOCK_STREAM && skb_zcopy(skb) != uarg)) + extra_uref = true;
SOCK_STREAMdoes not use__ip_append_data.This leaves as new branch
skb_zcopy(skb)&&skb_zcopy(skb)!= uarg.
Quoted 7 lines
Quoted 4 lines
This function can only acquire a uarg through
sock_zerocopy_realloc, which onskb_zcopy(skb)only returns the existing uarg or NULL (for notSOCK_STREAM).So I don't see when that condition can happen.
On
skb_zcopy(skb), we returns the existing uarg iff (uarg->id+uarg->len==atomic_read(&sk->sk_zckey)) insock_zerocopy_realloc. So we may get a newly allocated uarg viasock_zerocopy_alloc(). Though we may not trigger this codepath now, it's still a potential problem that we may missed the right trace to uarg.
I don't think that can happen.
The question is when this branch is false
next = (u32)atomic_read(&sk->sk_zckey);
if ((u32)(uarg->id + uarg->len) == next) {I cannot come up with a case. I think it might be vestigial. The goal is to ensure to append only a consecutive range of notification IDs. Each notification ID corresponds to a sendmsg invocation with MSG_ZEROCOPY. In both TCP and UDP with corking, data is ordered and access to changes to these fields happen together as a transaction:
/* realloc only when socket is locked (TCP, UDP cork),
* so uarg->len and sk_zckey access is serialized
*/