netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jamal Hadi Salim <jhs@mojatatu.com>
To: Eric Dumazet <edumazet@google.com>
Cc: renmingshuai <renmingshuai@huawei.com>,
	xiyou.wangcong@gmail.com, jiri@resnulli.us,  davem@davemloft.net,
	vladbu@nvidia.com, netdev@vger.kernel.org,  yanan@huawei.com,
	liaichun@huawei.com, caowangbao@huawei.com,
	 Eric Dumazet <eric.dumazet@gmail.com>,
	Jakub Kicinski <kuba@kernel.org>,
	 Paolo Abeni <pabeni@redhat.com>,
	Victor Nogueira <victor@mojatatu.com>,
	 Pedro Tammela <pctammela@mojatatu.com>
Subject: Re: [PATCH] net/sched: Forbid assigning mirred action to a filter attached to the egress
Date: Mon, 18 Mar 2024 13:36:38 -0400	[thread overview]
Message-ID: <CAM0EoMkqhmDtpg09ktnkxjAtddvXzwQo4Qh2-LX2r8iqrECogw@mail.gmail.com> (raw)
In-Reply-To: <CANn89iL_hfoWTqr+KaKZoO8fKoZdd-xcY040NeSb-WL7pHMLGQ@mail.gmail.com>

On Mon, Mar 18, 2024 at 11:46 AM Eric Dumazet <edumazet@google.com> wrote:
>
> On Mon, Mar 18, 2024 at 3:27 PM Jamal Hadi Salim <jhs@mojatatu.com> wrote:
> >
> > On Sun, Mar 17, 2024 at 12:10 PM Jamal Hadi Salim <jhs@mojatatu.com> wrote:
> > >
> > > On Thu, Mar 14, 2024 at 1:14 PM Jamal Hadi Salim <jhs@mojatatu.com> wrote:
> > > >
> > > > On Thu, Mar 14, 2024 at 7:18 AM renmingshuai <renmingshuai@huawei.com> wrote:
> > > > >
> > > > > As we all know the mirred action is used to mirroring or redirecting the
> > > > > packet it receives. Howerver, add mirred action to a filter attached to
> > > > > a egress qdisc might cause a deadlock. To reproduce the problem, perform
> > > > > the following steps:
> > > > > (1)tc qdisc add dev eth0 root handle 1: htb default 30 \n
> > > > > (2)tc filter add dev eth2 protocol ip prio 2 flower verbose \
> > > > >      action police rate 100mbit burst 12m conform-exceed jump 1 \
> > > > >      / pipe mirred egress redirect dev eth2 action drop
> > > > >
> > > >
> > > > I think you meant both to be the same device eth0 or eth2?
> > > >
> > > > > The stack is show as below:
> > > > > [28848.883915]  _raw_spin_lock+0x1e/0x30
> > > > > [28848.884367]  __dev_queue_xmit+0x160/0x850
> > > > > [28848.884851]  ? 0xffffffffc031906a
> > > > > [28848.885279]  tcf_mirred_act+0x3ab/0x596 [act_mirred]
> > > > > [28848.885863]  tcf_action_exec.part.0+0x88/0x130
> > > > > [28848.886401]  fl_classify+0x1ca/0x1e0 [cls_flower]
> > > > > [28848.886970]  ? dequeue_entity+0x145/0x9e0
> > > > > [28848.887464]  ? newidle_balance+0x23f/0x2f0
> > > > > [28848.887973]  ? nft_lookup_eval+0x57/0x170 [nf_tables]
> > > > > [28848.888566]  ? nft_do_chain+0xef/0x430 [nf_tables]
> > > > > [28848.889137]  ? __flush_work.isra.0+0x35/0x80
> > > > > [28848.889657]  ? nf_ct_get_tuple+0x1cf/0x210 [nf_conntrack]
> > > > > [28848.890293]  ? do_select+0x637/0x870
> > > > > [28848.890735]  tcf_classify+0x52/0xf0
> > > > > [28848.891177]  htb_classify+0x9d/0x1c0 [sch_htb]
> > > > > [28848.891722]  htb_enqueue+0x3a/0x1c0 [sch_htb]
> > > > > [28848.892251]  __dev_queue_xmit+0x2d8/0x850
> > > > > [28848.892738]  ? nf_hook_slow+0x3c/0xb0
> > > > > [28848.893198]  ip_finish_output2+0x272/0x580
> > > > > [28848.893692]  __ip_queue_xmit+0x193/0x420
> > > > > [28848.894179]  __tcp_transmit_skb+0x8cc/0x970
> > > > >
> > > > > In this case, the process has hold the qdisc spin lock in __dev_queue_xmit
> > > > > before the egress packets are mirred, and it will attempt to obtain the
> > > > > spin lock again after packets are mirred, which cause a deadlock.
> > > > >
> > > > > Fix the issue by forbidding assigning mirred action to a filter attached
> > > > > to the egress.
> > > > >
> > > > > Signed-off-by: Mingshuai Ren <renmingshuai@huawei.com>
> > > > > ---
> > > > >  net/sched/act_mirred.c                        |  4 +++
> > > > >  .../tc-testing/tc-tests/actions/mirred.json   | 32 +++++++++++++++++++
> > > > >  2 files changed, 36 insertions(+)
> > > > >
> > > > > diff --git a/net/sched/act_mirred.c b/net/sched/act_mirred.c
> > > > > index 5b3814365924..fc96705285fb 100644
> > > > > --- a/net/sched/act_mirred.c
> > > > > +++ b/net/sched/act_mirred.c
> > > > > @@ -120,6 +120,10 @@ static int tcf_mirred_init(struct net *net, struct nlattr *nla,
> > > > >                 NL_SET_ERR_MSG_MOD(extack, "Mirred requires attributes to be passed");
> > > > >                 return -EINVAL;
> > > > >         }
> > > > > +       if (tp->chain->block->q->parent != TC_H_INGRESS) {
> > > > > +               NL_SET_ERR_MSG_MOD(extack, "Mirred can only be assigned to the filter attached to ingress");
> > > > > +               return -EINVAL;
> > > > > +       }
> > > >
> > > > Sorry, this is too restrictive as Jiri said. We'll try to reproduce. I
> > > > am almost certain this used to work in the old days.
> > >
> > > Ok, i looked at old notes - it did work at "some point" pre-tdc.
> > > Conclusion is things broke around this time frame:
> > > https://lore.kernel.org/netdev/1431679850-31896-1-git-send-email-fw@strlen.de/
> > > https://lore.kernel.org/netdev/1465095748.2968.45.camel@edumazet-glaptop3.roam.corp.google.com/
> > >
> > > Looking further into it.
> >
> > This is what we came up with. Eric, please take a look...
> >
> > cheers,
> > jamal
> >
> >
> > --- a/net/core/dev.c
> > +++ b/net/core/dev.c
> > @@ -3789,7 +3789,14 @@ static inline int __dev_xmit_skb(struct sk_buff
> > *skb, struct Qdisc *q,
> >         if (unlikely(contended))
> >                 spin_lock(&q->busylock);
> >
> > +       if (dev_recursion_level()) {
>
> I am not sure what your intent is, but this seems wrong to me.
>

There is a deadlock if you reenter the same device which has a qdisc
attached to it more than once.
Essentially entering __dev_xmit_skb() we grab the root qdisc lock then
run some action which requires it to grab the root qdisc lock (again).
This is easy to show with mirred (although i am wondering if syzkaller
may have produced this at some point)..
$TC qdisc add dev $DEV root handle 1: htb default 1
$TC filter add dev $DEV protocol ip u32 match ip protocol 1 0xff
action mirred egress mirror dev $DEV

Above example is essentially egress $DEV-> egress $DEV in both cases
"egress $DEV" grabs the root qdisc lock. You could also create another
example with egress($DEV1->$DEV2->back to $DEV1).

> Some valid setup use :
>
> A bonding device, with HTB qdisc (or other qdisc)
>   (This also could be a tunnel device with a qdisc)
>
> -> one or multiple physical NIC, wth FQ or other qdisc.
>
> Packets would be dropped here when we try to reach the physical device.
>

If you have an example handy please send it. I am trying to imagine
how those would have worked if they have to reenter the root qdisc of
the same dev multiple times..

cheers,
jamal


> > +               rc = NET_XMIT_DROP;
> > +               __qdisc_drop(skb, &to_free);
> > +               goto free;
> > +       }
> > +
> >         spin_lock(root_lock);
> > +       dev_xmit_recursion_inc();
> >         if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
> >                 __qdisc_drop(skb, &to_free);
> >                 rc = NET_XMIT_DROP;
> > @@ -3824,8 +3831,10 @@ static inline int __dev_xmit_skb(struct sk_buff
> > *skb, struct Qdisc *q,
> >                         qdisc_run_end(q);
> >                 }
> >         }
> > +       dev_xmit_recursion_dec();
> >         spin_unlock(root_lock);
> >         if (unlikely(to_free))
> > +free:
> >                 kfree_skb_list_reason(to_free,
> >                                       tcf_get_drop_reason(to_free));
> >         if (unlikely(contended))

  reply	other threads:[~2024-03-18 17:36 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-14 11:17 [PATCH] net/sched: Forbid assigning mirred action to a filter attached to the egress renmingshuai
2024-03-14 11:43 ` Jiri Pirko
2024-03-14 14:04   ` renmingshuai
2024-03-14 14:47     ` Pedro Tammela
2024-03-14 17:14 ` Jamal Hadi Salim
2024-03-15  1:56   ` renmingshuai
2024-03-15 22:34     ` Jamal Hadi Salim
2024-03-17 16:10   ` Jamal Hadi Salim
2024-03-18 14:27     ` Jamal Hadi Salim
2024-03-18 15:46       ` Eric Dumazet
2024-03-18 17:36         ` Jamal Hadi Salim [this message]
2024-03-18 19:11           ` Eric Dumazet
2024-03-18 22:05             ` Jamal Hadi Salim
2024-03-19  9:38               ` Eric Dumazet
2024-03-19 20:54                 ` Jamal Hadi Salim
2024-03-20 16:46                   ` Jamal Hadi Salim
2024-03-20 16:57                   ` Eric Dumazet
2024-03-20 17:31                     ` Jamal Hadi Salim
2024-03-20 17:50                       ` Jamal Hadi Salim
2024-03-20 18:13                         ` Eric Dumazet
2024-03-20 18:25                           ` Eric Dumazet
2024-03-20 19:34                             ` Jamal Hadi Salim
2024-03-24 15:27                               ` Jamal Hadi Salim
2024-03-26 23:18                                 ` Jamal Hadi Salim

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=CAM0EoMkqhmDtpg09ktnkxjAtddvXzwQo4Qh2-LX2r8iqrECogw@mail.gmail.com \
    --to=jhs@mojatatu.com \
    --cc=caowangbao@huawei.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=eric.dumazet@gmail.com \
    --cc=jiri@resnulli.us \
    --cc=kuba@kernel.org \
    --cc=liaichun@huawei.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pctammela@mojatatu.com \
    --cc=renmingshuai@huawei.com \
    --cc=victor@mojatatu.com \
    --cc=vladbu@nvidia.com \
    --cc=xiyou.wangcong@gmail.com \
    --cc=yanan@huawei.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).