Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp565489rwp; Wed, 12 Jul 2023 19:03:28 -0700 (PDT) X-Google-Smtp-Source: APBJJlH6TFjaQiM+YjJQ6jGcWukmZnHTp6W5PjpXNHJY54rbvqZrfZgnRbH0zCb4tOQXAK4LOhd5 X-Received: by 2002:a05:6808:180b:b0:3a3:efec:2adf with SMTP id bh11-20020a056808180b00b003a3efec2adfmr160512oib.5.1689213808222; Wed, 12 Jul 2023 19:03:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689213808; cv=none; d=google.com; s=arc-20160816; b=h7lJ/1r0OhkpklIJWNCAS+k2Czf9+i6j1GDgiJNPijpDw9IuoCYjVMSCpWBOVQgM4g qu/BqSCCSezjBnlcY6hJ6H6dldU8FpsnJ3KJEyH6CC7gstJ32MrssSjnet+M0oQ+oV7g 0htLhzPuxm7BINSSX5pW04CwQnvA4pUg+DEcFdQHWVzX9WthMYp+mZMagHR3xzCUsGos jb+lh/9QvDzjQuIHnBPsaUIeCL+d6dDrptEyZaxBFJQ07F+k82rkLGfxGe/lb/OccMRB S69Fq/Qro6pU8knylQt5G7CzQs79dyxyhfSi5w2Y0xlza47JaHB1p4d87q6Y31UALxs0 WznA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=jxHIfdjUy2D+e36IlhY5UhNMqIoRQ40rBpziU8T3N10=; fh=K7fFINMq11bAhCWodUjGaYM0JHLPPKkK9/6n5KyG/Rw=; b=t+DAhhlui0ziC5fwxUO1iAW4vEUBgJnLfiYvBoJaaNWrM3BkneqGAIcqjy4NSN6/L9 FoGIFEDXkHfVLgAxPr9YuInW+mwXkylR/MPvLAMy6TzhcM9NvKoAWqmy/FZILkyxMchs ULfBc6MqHxZaYUMR7iJ5I+54OGTjrz9sw5lPBWufKxSx0yZeCygWqFZcTXn8fdYq0rCN s8rQuYf9iCTr3BMMQCgBy3RQPN8oJ3oe2dCxACB/nOkVfY2gMn+m6Cmx4lwroBY0BZnK ZXeP0rQjMCzJcLncyOTzHRXvujwt8AYzuubb+jGTnsOtSS/vgNDQYe7A6TJ7lCdDjDyq TKXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=psdAc+8F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s9-20020a6550c9000000b0055adb191739si4154449pgp.62.2023.07.12.19.03.16; Wed, 12 Jul 2023 19:03:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=psdAc+8F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231489AbjGMB0a (ORCPT + 99 others); Wed, 12 Jul 2023 21:26:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229502AbjGMB03 (ORCPT ); Wed, 12 Jul 2023 21:26:29 -0400 Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05AF21FDE; Wed, 12 Jul 2023 18:26:27 -0700 (PDT) Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2b70404a5a0so1172891fa.2; Wed, 12 Jul 2023 18:26:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689211585; x=1691803585; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=jxHIfdjUy2D+e36IlhY5UhNMqIoRQ40rBpziU8T3N10=; b=psdAc+8FN7dzzOo3MgPLwTCd3NAklAdi3rikz2jT3jc/p3me/4+OxL5Ksyd2+SZ2Qr Tr+5Vrz4vW28HUgdOo2frgGI2Aer7ABUIIt3AudPpk3UsWCye0gduL23mBOxZS65FWA+ Js1aJQ/f89qX0rez6vlbHtfnCs3BbV32GOP6hIenAoD04wzSDA91sQ6RImD+N8iMF4iQ U0rYSIHOU5KZGIV883j1CWFWWS4t4Cf/feC8X+OFtCNSdQ4l+JCYw2EbfkhLMgwyoMQP WIs3Z08NzjmchL12enT1YArAE5Q1MD7zXq70V+HFpFERxtk+Rl8uKsyVIcYSe3I1bhAQ vBvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689211585; x=1691803585; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jxHIfdjUy2D+e36IlhY5UhNMqIoRQ40rBpziU8T3N10=; b=c6Z+Xx+9mhQT+yYL+jCciN4JUB0V0BKb9+5cMiYII9XPDf8x0fprkZcjYVtn6GENpk fSlOJmaooX10jutonsN8Nj7L0bxR3ctNa5pm1K25xOXwKn6z80eAcBqs83JcPhdd5F/G LPZyRjOUJI2CBEMb54Ae75WR+YWFjF4CX0dBkSSb3Lh/QUqHbVSbZ57I1XwFfx+sjllk 7H60pB20P9HNkFxRceqgs6ffY1QbCO2K1Zy2PO32jefquUVw2dxY7RLCLAPXGw24CoXV JjoHWPeaBL6q5PnaBMpdI3r0r7CdVHBuUtTV6hQIi9Km2y/M95+Mo1YJPpIXuemTiw8D PR0A== X-Gm-Message-State: ABy/qLZN9zBWQLi1gRi5Lg/2emXHZ+DBBIBE9oEYFElCx6n3ToD+XCqM 99fFEY/jom/cJzWtgSQIHf3gUflMbv4KC+9Q3eg= X-Received: by 2002:a2e:8456:0:b0:2b6:df00:b371 with SMTP id u22-20020a2e8456000000b002b6df00b371mr43638ljh.6.1689211584879; Wed, 12 Jul 2023 18:26:24 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Alexei Starovoitov Date: Wed, 12 Jul 2023 18:26:13 -0700 Message-ID: Subject: Re: [PATCH bpf-next v4 2/6] netfilter: bpf: Support BPF_F_NETFILTER_IP_DEFRAG in netfilter link To: Daniel Xu Cc: Andrii Nakryiko , Alexei Starovoitov , Florian Westphal , "David S. Miller" , Pablo Neira Ayuso , Paolo Abeni , Daniel Borkmann , Eric Dumazet , Jakub Kicinski , Jozsef Kadlecsik , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf , LKML , netfilter-devel , coreteam@netfilter.org, Network Development , David Ahern Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 12, 2023 at 6:22=E2=80=AFPM Daniel Xu wrote: > > Hi Alexei, > > On Wed, Jul 12, 2023 at 05:43:49PM -0700, Alexei Starovoitov wrote: > > On Wed, Jul 12, 2023 at 4:44=E2=80=AFPM Daniel Xu wrote= : > > > +#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) > > > + case NFPROTO_IPV6: > > > + rcu_read_lock(); > > > + v6_hook =3D rcu_dereference(nf_defrag_v6_hook); > > > + if (!v6_hook) { > > > + rcu_read_unlock(); > > > + err =3D request_module("nf_defrag_ipv6"); > > > + if (err) > > > + return err < 0 ? err : -EINVAL; > > > + > > > + rcu_read_lock(); > > > + v6_hook =3D rcu_dereference(nf_defrag_v6_hook= ); > > > + if (!v6_hook) { > > > + WARN_ONCE(1, "nf_defrag_ipv6_hooks ba= d registration"); > > > + err =3D -ENOENT; > > > + goto out_v6; > > > + } > > > + } > > > + > > > + err =3D v6_hook->enable(link->net); > > > > I was about to apply, but luckily caught this issue in my local test: > > > > [ 18.462448] BUG: sleeping function called from invalid context at > > kernel/locking/mutex.c:283 > > [ 18.463238] in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: > > 2042, name: test_progs > > [ 18.463927] preempt_count: 0, expected: 0 > > [ 18.464249] RCU nest depth: 1, expected: 0 > > [ 18.464631] CPU: 15 PID: 2042 Comm: test_progs Tainted: G > > O 6.4.0-04319-g6f6ec4fa00dc #4896 > > [ 18.465480] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), > > BIOS rel-1.12.0-59-gc9ba5276e321-prebuilt.qemu.org 04/01/2014 > > [ 18.466531] Call Trace: > > [ 18.466767] > > [ 18.466975] dump_stack_lvl+0x32/0x40 > > [ 18.467325] __might_resched+0x129/0x180 > > [ 18.467691] mutex_lock+0x1a/0x40 > > [ 18.468057] nf_defrag_ipv4_enable+0x16/0x70 > > [ 18.468467] bpf_nf_link_attach+0x141/0x300 > > [ 18.468856] __sys_bpf+0x133e/0x26d0 > > > > You cannot call mutex under rcu_read_lock. > > Whoops, my bad. I think this patch should fix it: > > ``` > From 7e8927c44452db07ddd7cf0e30bb49215fc044ed Mon Sep 17 00:00:00 2001 > Message-ID: <7e8927c44452db07ddd7cf0e30bb49215fc044ed.1689211250.git.dxu@= dxuuu.xyz> > From: Daniel Xu > Date: Wed, 12 Jul 2023 19:17:35 -0600 > Subject: [PATCH] netfilter: bpf: Don't hold rcu_read_lock during > enable/disable > > ->enable()/->disable() takes a mutex which can sleep. You can't sleep > during RCU read side critical section. > > Our refcnt on the module will protect us from ->enable()/->disable() > from going away while we call it. > > Signed-off-by: Daniel Xu > --- > net/netfilter/nf_bpf_link.c | 10 ++++++++-- > 1 file changed, 8 insertions(+), 2 deletions(-) > > diff --git a/net/netfilter/nf_bpf_link.c b/net/netfilter/nf_bpf_link.c > index 77ffbf26ba3d..79704cc596aa 100644 > --- a/net/netfilter/nf_bpf_link.c > +++ b/net/netfilter/nf_bpf_link.c > @@ -60,9 +60,12 @@ static int bpf_nf_enable_defrag(struct bpf_nf_link *li= nk) > goto out_v4; > } > > + rcu_read_unlock(); > err =3D v4_hook->enable(link->net); > if (err) > module_put(v4_hook->owner); > + > + return err; > out_v4: > rcu_read_unlock(); > return err; > @@ -92,9 +95,12 @@ static int bpf_nf_enable_defrag(struct bpf_nf_link *li= nk) > goto out_v6; > } > > + rcu_read_unlock(); > err =3D v6_hook->enable(link->net); > if (err) > module_put(v6_hook->owner); > + > + return err; > out_v6: > rcu_read_unlock(); > return err; > @@ -114,11 +120,11 @@ static void bpf_nf_disable_defrag(struct bpf_nf_lin= k *link) > case NFPROTO_IPV4: > rcu_read_lock(); > v4_hook =3D rcu_dereference(nf_defrag_v4_hook); > + rcu_read_unlock(); > if (v4_hook) { > v4_hook->disable(link->net); > module_put(v4_hook->owner); > } > - rcu_read_unlock(); > > break; > #endif > @@ -126,11 +132,11 @@ static void bpf_nf_disable_defrag(struct bpf_nf_lin= k *link) > case NFPROTO_IPV6: > rcu_read_lock(); > v6_hook =3D rcu_dereference(nf_defrag_v6_hook); > + rcu_read_unlock(); No. v6_hook is gone as soon as you unlock it.