From patchwork Fri Dec 15 03:07:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "D. Wythe" X-Patchwork-Id: 179022 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9010171dys; Thu, 14 Dec 2023 19:13:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IHNJiHHTSR8Zk5L/c9S33shpzX3pXrpqglppoBLESe7oa/0X8Zynoe+5NVRAGF89FZzjzC8 X-Received: by 2002:a05:6512:2316:b0:50b:f30b:5499 with SMTP id o22-20020a056512231600b0050bf30b5499mr7415914lfu.80.1702610035703; Thu, 14 Dec 2023 19:13:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702610035; cv=none; d=google.com; s=arc-20160816; b=q578l/eVLKLbU4ivTMrXXzUZZ8AcODFVJGc+QIaaYi+inLpfKWnj2HuhHwxzsiYb1N zi3rjo7vQBGIUhJls/zKrp7MKKatNZVTk3yvRTj6uppfIsv6qa1RuprkqbDbDUKK3vmE kW3OUWnWVOPxW2Zd+zynXgX4fTSJ2/QvYBGeLbBFJI3vEdfTO1xHZ2Vzy/KxsM2xGLwB WsYoGz/W0h38NAXYJBWZkEs7yuFgzRCUyNkh4t2jQ+fNduhUzDQjd4jrxB9y3vZF5V7O kVSNF/O8aCkc9tRc2/KzAJzEiZ2SXwf2JtK+I5dhiU6XoRuMKhyVOtVFz/E7wbjxb7+3 0b/Q== 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-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-395-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id e24-20020a170906749800b00a2318cd2f57si177128ejl.863.2023.12.14.19.13.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 19:13:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-395-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-395-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-395-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 510A41F2169E for ; Fri, 15 Dec 2023 03:13:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3FC295C89; Fri, 15 Dec 2023 03:13:28 +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: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785167059360166641 X-GMAIL-MSGID: 1785316020398568182 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;