linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: Yunsheng Lin <linyunsheng@huawei.com>,
	"Peter Zijlstra (Intel)" <peterz@infradead.org>
Cc: <davem@davemloft.net>, <linmiaohe@huawei.com>,
	<martin.varghese@nokia.com>, <pabeni@redhat.com>,
	<pshelar@ovn.org>, <fw@strlen.de>, <gnault@redhat.com>,
	<steffen.klassert@secunet.com>, <kyk.segfault@gmail.com>,
	<viro@zeniv.linux.org.uk>, <vladimir.oltean@nxp.com>,
	<edumazet@google.com>, <saeed@kernel.org>,
	<netdev@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linuxarm@huawei.com>
Subject: Re: [PATCH net-next] net: add in_softirq() debug checking in napi_consume_skb()
Date: Mon, 2 Nov 2020 11:41:10 -0800	[thread overview]
Message-ID: <20201102114110.4a20d461@kicinski-fedora-PC1C0HJN.hsd1.ca.comcast.net> (raw)
In-Reply-To: <5b04ad33-1611-8d7b-8fec-4269c01ecab3@huawei.com>

On Mon, 2 Nov 2020 11:14:32 +0800 Yunsheng Lin wrote:
> On 2020/11/1 6:38, Jakub Kicinski wrote:
> > On Thu, 29 Oct 2020 19:34:48 +0800 Yunsheng Lin wrote:  
> >> The current semantic for napi_consume_skb() is that caller need
> >> to provide non-zero budget when calling from NAPI context, and
> >> breaking this semantic will cause hard to debug problem, because
> >> _kfree_skb_defer() need to run in atomic context in order to push
> >> the skb to the particular cpu' napi_alloc_cache atomically.
> >>
> >> So add a in_softirq() debug checking in napi_consume_skb() to catch
> >> this kind of error.
> >>
> >> Suggested-by: Eric Dumazet <edumazet@google.com>
> >> Signed-off-by: Yunsheng Lin <linyunsheng@huawei.com>  
> >   
> >> diff --git a/net/core/skbuff.c b/net/core/skbuff.c
> >> index 1ba8f01..1834007 100644
> >> --- a/net/core/skbuff.c
> >> +++ b/net/core/skbuff.c
> >> @@ -897,6 +897,10 @@ void napi_consume_skb(struct sk_buff *skb, int budget)
> >>  		return;
> >>  	}
> >>  
> >> +	DEBUG_NET_WARN(!in_softirq(),
> >> +		       "%s is called with non-zero budget outside softirq context.\n",
> >> +		       __func__);  
> > 
> > Can't we use lockdep instead of defining our own knobs?  
> 
> From the first look, using the below seems better than defining our
> own knobs, because there is similar lockdep_assert_in_irq() checking
> already and lockdep_assert_in_*() is NULL-OP when CONFIG_PROVE_LOCKING
> is not defined.
> 
> > 
> > Like this maybe?
> > 
> > diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
> > index f5594879175a..5253a167d00c 100644
> > --- a/include/linux/lockdep.h
> > +++ b/include/linux/lockdep.h
> > @@ -594,6 +594,14 @@ do {                                                                       \
> >                       this_cpu_read(hardirqs_enabled)));                \
> >  } while (0)
> >  
> > +#define lockdep_assert_in_softirq()                                    \
> > +do {                                                                   \
> > +       WARN_ON_ONCE(__lockdep_enabled                  &&              \
> > +                    (softirq_count() == 0              ||              \
> > +                     this_cpu_read(hardirq_context)));                 \  
> 
> Using in_softirq() seems more obvious then using softirq_count()?
> And there is below comment above avoiding the using of in_softirq(), maybe
> that is why you use softirq_count() directly here?
> "softirq_count() == 0" still mean we are not in the SoftIRQ context and
> BH is not disabled. right? Perhap lockdep_assert_in_softirq_or_bh_disabled()
> is more obvious?

Let's add Peter to the recipients to get his opinion.

We have a per-cpu resource which is also accessed from BH (see
_kfree_skb_defer()).

We're trying to come up with the correct lockdep incantation for it.

> /*
>  * Are we doing bottom half or hardware interrupt processing?
>  *
>  * in_irq()       - We're in (hard) IRQ context
>  * in_softirq()   - We have BH disabled, or are processing softirqs
>  * in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled
>  * in_serving_softirq() - We're in softirq context
>  * in_nmi()       - We're in NMI context
>  * in_task()	  - We're in task context
>  *
>  * Note: due to the BH disabled confusion: in_softirq(),in_interrupt() really
>  *       should not be used in new code.
>  */
> 
> 
> Also, is there any particular reason we do the "this_cpu_read(hardirq_context)"
> checking?

Accessing BH resources from a hard IRQ context would be a bug, we may
have interrupted a BH, so AFAIU softirq_count() != 0, but we can nest
calls to _kfree_skb_defer().

  reply	other threads:[~2020-11-02 19:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-29 11:34 [PATCH net-next] net: add in_softirq() debug checking in napi_consume_skb() Yunsheng Lin
2020-10-31 22:38 ` Jakub Kicinski
2020-11-02  3:14   ` Yunsheng Lin
2020-11-02 19:41     ` Jakub Kicinski [this message]
2020-11-18  1:57       ` Yunsheng Lin
2020-11-18 15:43         ` Jakub Kicinski
2020-11-18 15:57           ` Peter Zijlstra
2020-11-18 16:26             ` Jakub Kicinski
2020-11-19  9:19               ` Yunsheng Lin
2020-11-19 11:41                 ` Peter Zijlstra
2020-11-19 12:29                   ` Yunsheng Lin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201102114110.4a20d461@kicinski-fedora-PC1C0HJN.hsd1.ca.comcast.net \
    --to=kuba@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=fw@strlen.de \
    --cc=gnault@redhat.com \
    --cc=kyk.segfault@gmail.com \
    --cc=linmiaohe@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxarm@huawei.com \
    --cc=linyunsheng@huawei.com \
    --cc=martin.varghese@nokia.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pshelar@ovn.org \
    --cc=saeed@kernel.org \
    --cc=steffen.klassert@secunet.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=vladimir.oltean@nxp.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).