[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAF=yD-+X1Tke22BV0ZrWJOw0FXr2WpNLsaCtGt6+-p2hSAnCRA@mail.gmail.com>
Date: Fri, 15 Sep 2017 16:01:55 -0400
From: Willem de Bruijn <willemdebruijn.kernel@...il.com>
To: Cong Wang <xiyou.wangcong@...il.com>
Cc: Willem de Bruijn <willemb@...gle.com>,
Linux Kernel Network Developers <netdev@...r.kernel.org>,
David Miller <davem@...emloft.net>,
nixiaoming <nixiaoming@...wei.com>
Subject: Re: [PATCH net] packet: hold bind lock when rebinding to fanout hook
>> diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
>> index c26172995511..d288f52c53f7 100644
>> --- a/net/packet/af_packet.c
>> +++ b/net/packet/af_packet.c
>> @@ -1684,10 +1684,6 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
>>
>> mutex_lock(&fanout_mutex);
>>
>> - err = -EINVAL;
>> - if (!po->running)
>> - goto out;
>> -
>> err = -EALREADY;
>> if (po->fanout)
>> goto out;
>> @@ -1749,7 +1745,10 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
>> list_add(&match->list, &fanout_list);
>> }
>> err = -EINVAL;
>> - if (match->type == type &&
>> +
>> + spin_lock(&po->bind_lock);
>> + if (po->running &&
>
>
> As you move the po->running check later after setting po->rollover, I wonder
> if po->rollover possibly depends on po>running on other path?
The rollover code does not explicitly check po->running, if that is
what you are concerned about.
A newly allocated po->rollover structure will also only be accessed
from the datapath once the socket is added to a fanout group. Both
before and after this patch, that happens well after the po->running
check.
If the socket already had a po->rollover, then it must already have
had a po->fanout, too, so fanout_add does not reach this code.
>> + match->type == type &&
>> match->prot_hook.type == po->prot_hook.type &&
>> match->prot_hook.dev == po->prot_hook.dev) {
>> err = -ENOSPC;
>> @@ -1761,6 +1760,13 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
>> err = 0;
>> }
>> }
>> + spin_unlock(&po->bind_lock);
>> +
>> + if (err && !refcount_read(&match->sk_ref)) {
>> + list_del(&match->list);
>> + kfree(match);
>> + }
>
> This looks correct but still seems odd, it smells you don't use refcnt in an
> expected way.
It tests whether the object has no references, in which case it must have
been newly allocated and the fanout join operation must have failed.
I don't see an obviously simpler patch.
The entire code could perhaps be restructured eventually. There is,
for instance, no reason to test match->{type, prot_hook.type, prot_hook.dev}
when having just allocated the structure. Nor to test whether sk_ref exceeds
PACKET_FANOUT_MAX. Conversely, this sk_ref == 0 test only makes sense
when having taken the (!match) branch earlier.
But a refactor is out of scope for a bug fix.
Powered by blists - more mailing lists