Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6417854rdb; Thu, 14 Dec 2023 19:13:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IH5B65xRg7Yn2s7FmzFFGSNNEF8gkicJOkMToUHTzxY7fMSdKe8UuSdpPxL3m5T/YX17KjD X-Received: by 2002:a05:6214:228:b0:67e:bf2e:d2ab with SMTP id j8-20020a056214022800b0067ebf2ed2abmr8582501qvt.120.1702610023027; Thu, 14 Dec 2023 19:13:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702610023; cv=none; d=google.com; s=arc-20160816; b=wp850xT/bBrhJHurHwO3tpktULbNcjp94LAk0guA5nNg0bj7w80x/dmjh50yP0e5nN eBjxaojsUFHO/h5ZVknJjjVeLgatlBKjTncL+BMe3U59Z+55tF9sppIqMoRKgDWwK3QY SAsV7djr40DSPUpiEWwkyZ+OUQs7m9hn7V+3v4qg2uoS4H4kLHTayx8GyarCokyWtUXV 5HsVBqNtkne6Sse4cW8+ux11SYWDEIggTfbolwLxip6lpt1RFESbJCbpJuto1+hS97nl OT7G09CtBr3ZW76mXyQpI2SWb4h7798aC8OeUdJVe+mHHvre54of+z/gX+U+EUDM/Rjr NwnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from; bh=I8xxqUkuonYo5Adb4ZOtC1/OmrI+Y2M73027bwTSvbE=; fh=0j9hEtKEGSulx/lgeQcpDl+4ujiVFr5DCPGOZLIRfAc=; b=oJrA4Do4yf35g7kwKazDsMMQgVeTE887j8Nd3Gv0N4kjWPz50Nv8DqJkYuwSFxzeAk MSz3912HhFdypCjmwodRpBMskhVBGuXW8KUJuYfBbcSum1V82eh4WyBTCNCyx/oSLTA3 UmWZnceMyxkBdAb7qmdM+7X5E8RS41sx/kXyKwDEu9QflOkEWnsMFPHjT1LKd4o0Rcno hZQ1bwCZJxtXgd+oKZjVoS3akd4mUgrc9+yPe4D77urhQiPrmnv80k5XWsJgZy0OHNds qfulMh/0Pk8FMjkY0AhbmtjC1NcKMsf6Q8m3lSJcMdQIk3JDqgW2ZXNcSQi7sAMjfVAw +77g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-395-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-395-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id w11-20020a0cdf8b000000b0067f0708fd8esi3262583qvl.83.2023.12.14.19.13.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 19:13:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-395-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-395-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-395-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id AF78F1C20DB1 for ; Fri, 15 Dec 2023 03:13:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3273446B1; Fri, 15 Dec 2023 03:13:27 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from out199-6.us.a.mail.aliyun.com (out199-6.us.a.mail.aliyun.com [47.90.199.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B94F3CA64; Fri, 15 Dec 2023 03:13:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=alibuda@linux.alibaba.com;NM=1;PH=DS;RN=13;SR=0;TI=SMTPD_---0VyWIqbW_1702609659; Received: from j66a10360.sqa.eu95.tbsite.net(mailfrom:alibuda@linux.alibaba.com fp:SMTPD_---0VyWIqbW_1702609659) by smtp.aliyun-inc.com; Fri, 15 Dec 2023 11:07:40 +0800 From: "D. Wythe" To: pablo@netfilter.org, kadlec@netfilter.org, fw@strlen.de Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, coreteam@netfilter.org, netfilter-devel@vger.kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, ast@kernel.org Subject: [RFC nf-next v1 1/2] netfilter: bpf: support prog update Date: Fri, 15 Dec 2023 11:07:32 +0800 Message-Id: <1702609653-45835-2-git-send-email-alibuda@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1702609653-45835-1-git-send-email-alibuda@linux.alibaba.com> References: <1702609653-45835-1-git-send-email-alibuda@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: From: "D. Wythe" To support the prog update, we need to ensure that the prog seen within the hook is always valid. Considering that hooks are always protected by rcu_read_lock(), which provide us the ability to access the prog under rcu. Signed-off-by: D. Wythe --- net/netfilter/nf_bpf_link.c | 68 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/net/netfilter/nf_bpf_link.c b/net/netfilter/nf_bpf_link.c index e502ec0..e17f256 100644 --- a/net/netfilter/nf_bpf_link.c +++ b/net/netfilter/nf_bpf_link.c @@ -8,17 +8,8 @@ #include #include -static unsigned int nf_hook_run_bpf(void *bpf_prog, struct sk_buff *skb, - const struct nf_hook_state *s) -{ - const struct bpf_prog *prog = bpf_prog; - struct bpf_nf_ctx ctx = { - .state = s, - .skb = skb, - }; - - return bpf_prog_run(prog, &ctx); -} +/* protect link update in parallel */ +static DEFINE_MUTEX(bpf_nf_mutex); struct bpf_nf_link { struct bpf_link link; @@ -26,8 +17,22 @@ struct bpf_nf_link { struct net *net; u32 dead; const struct nf_defrag_hook *defrag_hook; + const struct bpf_prog __rcu *nf_prog; + struct rcu_head head; }; +static unsigned int nf_hook_run_bpf(void *bpf_link, struct sk_buff *skb, + const struct nf_hook_state *s) +{ + const struct bpf_nf_link *link = bpf_link; + struct bpf_nf_ctx ctx = { + .state = s, + .skb = skb, + }; + + return bpf_prog_run(rcu_dereference(link->nf_prog), &ctx); +} + #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) static const struct nf_defrag_hook * get_proto_defrag_hook(struct bpf_nf_link *link, @@ -126,8 +131,7 @@ static void bpf_nf_link_release(struct bpf_link *link) static void bpf_nf_link_dealloc(struct bpf_link *link) { struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link); - - kfree(nf_link); + kfree_rcu(nf_link, head); } static int bpf_nf_link_detach(struct bpf_link *link) @@ -162,7 +166,35 @@ static int bpf_nf_link_fill_link_info(const struct bpf_link *link, static int bpf_nf_link_update(struct bpf_link *link, struct bpf_prog *new_prog, struct bpf_prog *old_prog) { - return -EOPNOTSUPP; + struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link); + int err = 0; + + mutex_lock(&bpf_nf_mutex); + + if (nf_link->dead) { + err = -EPERM; + goto out; + } + + /* target old_prog mismatch */ + if (old_prog && link->prog != old_prog) { + err = -EPERM; + goto out; + } + + old_prog = link->prog; + if (old_prog == new_prog) { + /* don't need update */ + bpf_prog_put(new_prog); + goto out; + } + + rcu_assign_pointer(nf_link->nf_prog, new_prog); + old_prog = xchg(&link->prog, new_prog); + bpf_prog_put(old_prog); +out: + mutex_unlock(&bpf_nf_mutex); + return err; } static const struct bpf_link_ops bpf_nf_link_lops = { @@ -226,7 +258,13 @@ int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) link->hook_ops.hook = nf_hook_run_bpf; link->hook_ops.hook_ops_type = NF_HOOK_OP_BPF; - link->hook_ops.priv = prog; + + rcu_assign_pointer(link->nf_prog, prog); + + /* bpf_nf_link_release & bpf_nf_link_dealloc() can ensures that link remains + * valid at all times within nf_hook_run_bpf(). + */ + link->hook_ops.priv = link; link->hook_ops.pf = attr->link_create.netfilter.pf; link->hook_ops.priority = attr->link_create.netfilter.priority; -- 1.8.3.1