From patchwork Thu Mar 16 01:41:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70538 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp230302wrt; Wed, 15 Mar 2023 18:48:05 -0700 (PDT) X-Google-Smtp-Source: AK7set+uhu3C51YNl8J9uTqTdiz/zxTJ1V1oAL4aWplP45rJrB7DHeGk7wcs7qBhDw6Wf83o6tnz X-Received: by 2002:a17:903:41cb:b0:1a0:69e6:bb32 with SMTP id u11-20020a17090341cb00b001a069e6bb32mr1664428ple.54.1678931285127; Wed, 15 Mar 2023 18:48:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678931285; cv=none; d=google.com; s=arc-20160816; b=A1d94wmDnEvEtNL8Ha6qaQD0iDkMDRalVPa5DDKZOeD8Fdt8MC6f5Sfq5RqJjIYkVu Ut88ALjRkBXEcNbMmRmsniEbiarmmQuwRmQSV712gp0MBkhB81y+P1MBXNasgJvRmlop 1nWAg8maNJwYxCdUWRPKDxHtFQJrlZjeLMsbHjsU9fBJG3EX3OouP934bUrJuYTh+onv 4sMlE9ex/uGcLXlPysd1wX1/3r5n/DASjoALxMp48FpPvmmxOVqHSu/RMYIYCTHJ4Pxr U+Xbfcvk8EAhIpEM5QWPGaiijFw4FbaQw+5N9S0CSDvlC7SoDz60m5B7OuCHX2hWcMdi G2eQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=RmYJO+iIXCovawotEcIt4TeAe1rA/wKRkg1b4WWjtqo=; b=I8X3m9KTqntU+4C+q2Aha3oNu/ZP1tWz7FYRlpzDZETVdTOimJNrZZJo8s7FlkaSJu 4Y++bJWd2ICvgM9euMfAOtLHJIw6ZMTtvmNE1kR32SIWfiRdrtN9f3IKuMRD5wDVh4wH li6y79LlOCiIf8npli8hHAArCrefQHuy7QWIaAX0Xypz+Ei6HHyHvAM94f+n7987aEqu d7J+ZRG8PDYW30JrYPIAPCkhygXUmbeGCdDwUr47nur5nc0XzsyKYHhklftZrEmvI48k vjFxCsZXqpJQSK2XIEtUt+rVarqVT/YDjlGA7n5/2SFiNHk9KkCxHxGo5qXLooFHx7kV wAKQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lk15-20020a17090308cf00b0019cf1bde932si6137369plb.35.2023.03.15.18.47.52; Wed, 15 Mar 2023 18:48:05 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229813AbjCPBla (ORCPT + 99 others); Wed, 15 Mar 2023 21:41:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229704AbjCPBl2 (ORCPT ); Wed, 15 Mar 2023 21:41:28 -0400 Received: from mail-qt1-f171.google.com (mail-qt1-f171.google.com [209.85.160.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2436BAB080; Wed, 15 Mar 2023 18:41:27 -0700 (PDT) Received: by mail-qt1-f171.google.com with SMTP id t9so267390qtx.8; Wed, 15 Mar 2023 18:41:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678930886; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RmYJO+iIXCovawotEcIt4TeAe1rA/wKRkg1b4WWjtqo=; b=k02aeUMyEIDSiaISUUew1Pm80Sr21sa1WkgpEnh8QfN6tmURi5iJJIAHnCyRU9nPy7 f32sLb5SNjPSiWJzTjnxGcgLecazi/YgI4jfCDFxZ9u48DChaXf4TDw1nfE2TrWoYFj/ GGiL+bmGOUQqotvIh0lnH6TGdFCCGm0OsjcS707t29zWCkq059CTMwiGmIqh9DitoGBK 3e6M6MxaT0NMdrUubnAoltd4YDf0Pk+uaJAmtxO0bl8ke2Vg2dxFW98zPLJr64gbhLQi 9IuFRZuYWFAsb7EVfGzNs4FGxBTDOEzNoMvcc3gj7ZH/gWq9TyAKXHLRfjPi9Y0GsdgZ wRIQ== X-Gm-Message-State: AO0yUKWMrfU0Rm9/m2cs7sdZX/nG6YfwVH1T/+3cceOeEgFhS5SO+1IO 4HVVi9W21uq2wsWRNZcU6dKRN9UN5941VYl/ X-Received: by 2002:a05:622a:1449:b0:3b8:6c8e:4f85 with SMTP id v9-20020a05622a144900b003b86c8e4f85mr3085144qtx.43.1678930885853; Wed, 15 Mar 2023 18:41:25 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:5c58]) by smtp.gmail.com with ESMTPSA id f33-20020a05622a1a2100b003bfb5fd72a7sm5048670qtb.86.2023.03.15.18.41.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 18:41:25 -0700 (PDT) From: David Vernet To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@meta.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH bpf-next 1/5] bpf: Free struct bpf_cpumask in call_rcu handler Date: Wed, 15 Mar 2023 20:41:18 -0500 Message-Id: <20230316014122.678082-2-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316014122.678082-1-void@manifault.com> References: <20230316014122.678082-1-void@manifault.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760487051607242293?= X-GMAIL-MSGID: =?utf-8?q?1760487051607242293?= The struct bpf_cpumask type uses the bpf_mem_cache_{alloc,free}() APIs to allocate and free its cpumasks. The bpf_mem allocator may currently immediately reuse some memory when its freed, without waiting for an RCU read cycle to elapse. We want to be able to treat struct bpf_cpumask objects as completely RCU safe. This is necessary for two reasons: 1. bpf_cpumask_kptr_get() currently does an RCU-protected refcnt_inc_not_zero(). This of course assumes that the underlying memory is not reused, and is therefore unsafe in its current form. 2. We want to be able to get rid of bpf_cpumask_kptr_get() entirely, and intead use the superior kptr RCU semantics now afforded by the verifier. This patch fixes (1), and enables (2), by making struct bpf_cpumask RCU safe. A subsequent patch will update the verifier to allow struct bpf_cpumask * pointers to be passed to KF_RCU kfuncs, and then a latter patch will remove bpf_cpumask_kptr_get(). Fixes: 516f4d3397c9 ("bpf: Enable cpumasks to be queried and used as kptrs") Signed-off-by: David Vernet --- kernel/bpf/cpumask.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c index b6587ec40f1b..9ab462c5848a 100644 --- a/kernel/bpf/cpumask.c +++ b/kernel/bpf/cpumask.c @@ -24,6 +24,7 @@ */ struct bpf_cpumask { cpumask_t cpumask; + struct rcu_head rcu; refcount_t usage; }; @@ -108,6 +109,16 @@ __bpf_kfunc struct bpf_cpumask *bpf_cpumask_kptr_get(struct bpf_cpumask **cpumas return cpumask; } +static void cpumask_free_cb(struct rcu_head *head) +{ + struct bpf_cpumask *cpumask; + + cpumask = container_of(head, struct bpf_cpumask, rcu); + migrate_disable(); + bpf_mem_cache_free(&bpf_cpumask_ma, cpumask); + migrate_enable(); +} + /** * bpf_cpumask_release() - Release a previously acquired BPF cpumask. * @cpumask: The cpumask being released. @@ -121,11 +132,8 @@ __bpf_kfunc void bpf_cpumask_release(struct bpf_cpumask *cpumask) if (!cpumask) return; - if (refcount_dec_and_test(&cpumask->usage)) { - migrate_disable(); - bpf_mem_cache_free(&bpf_cpumask_ma, cpumask); - migrate_enable(); - } + if (refcount_dec_and_test(&cpumask->usage)) + call_rcu(&cpumask->rcu, cpumask_free_cb); } /** From patchwork Thu Mar 16 01:41:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70537 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp230171wrt; Wed, 15 Mar 2023 18:47:39 -0700 (PDT) X-Google-Smtp-Source: AK7set/+ZEHt08IISQMVJI/kxuyQYsKbakiNMEo7mX+3aNWbqO4sNs6ArHupIYThkT4vJJYUrzNy X-Received: by 2002:a17:903:120b:b0:19e:6989:bcac with SMTP id l11-20020a170903120b00b0019e6989bcacmr1402310plh.15.1678931259399; Wed, 15 Mar 2023 18:47:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678931259; cv=none; d=google.com; s=arc-20160816; b=i83H3I7qMzTzE081aYAgyKZ6IYHvBTJlieJ/kNnSjB8UhOHVaQDwAUibBlEFbzDohG TmHcsDxJMS9LKPxOZA5I9BCaspSJ7ImjioMO42dk9NqOt3uNz/HpOF4PKDNtWzkqBh/6 i2BgT9kUjlMW1U0dAx0b/9O2rem84rEDZSiubzcZY9haZElXLt7K8FN0sNDsrg0lU1Qj fHU1zusud9MjTrH5y/2krl6SQZQe3BqHf7mOwLel3iKXCGKgnaFEKlutbazBZ8ngAoPq JhvgdJLg7Lur20Gu4iuAFPwfK+aOIeaSkG0H1FQCYBX19R8VikmcnrValAzqner6zo3X mleQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=3Mw+fd0bH4Z7LLAT9myck2MFtZEkV/SC3orkjhIOzHc=; b=hFpuNSYsI9//ZduP02mVNoVaqmg++X3s7KbimBgKV8fnWBFd8EreqPPC7D83BCWUS7 ocreFU3EWyLuDwTo4y64NI2eKZydXILBv55o7iz9YhLJwbD0SwgWCsLHI9SFPaSX/hWf vXvwlfC7qtvVvlapS3y6pzshM2osQxk99+FPwHIB0iGL8Au0F8maJOlCTjEI4SnpojgE 56c/1zcY+2DqIl3roO+4b5jJtuT04pOOuUT6B8uHJBjGMXQoeuadcR8vnZ+ojQUzN+a5 EQV55Pk1dsBX/0Lj/3B7y6vuqW9rqUAFCyB3GWaDBwxia4n5F7jobCI1dG8WrimfqzX5 4q+Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e18-20020a17090301d200b001a0767b58d4si4377971plh.81.2023.03.15.18.47.26; Wed, 15 Mar 2023 18:47:39 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229829AbjCPBlc (ORCPT + 99 others); Wed, 15 Mar 2023 21:41:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229808AbjCPBl3 (ORCPT ); Wed, 15 Mar 2023 21:41:29 -0400 Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E58AAB080; Wed, 15 Mar 2023 18:41:28 -0700 (PDT) Received: by mail-qv1-f44.google.com with SMTP id m6so360888qvq.0; Wed, 15 Mar 2023 18:41:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678930887; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3Mw+fd0bH4Z7LLAT9myck2MFtZEkV/SC3orkjhIOzHc=; b=tJPoVaVle+g1LVzEL/kjV0dMc3kcdagkVf1HUpdHYyEBwAbNUAFbjWg6myvthWvDu2 XhZegT5DCt5+iPl90BPS1WGMN6d4ojRNQwXtyOIQMna1eiwJUUnP644/1uYqOJSIZqmm 139e65Hmttnnx5vTLBnLJ2oiLt07bxYOIFnsYueqm0ZXOWxUKIzHSIlxnEuKbj5aV1ra FWV5RqIw1TdjWxFMcjZ6Sj1xDX2C6flhS42MEWIvoGwbNWip7J3Ovxtdbs6OdTgyMAc9 BoFxHC8M0jtMol9zgBLwpjb2TJVYX5VULvIfeIX6cOH/0wO0tz3+uJEu12YTTncKBLXP fp8A== X-Gm-Message-State: AO0yUKWWK2IzQ9NcwBiQWZ/O8YP9j5eBMy3RCYcpyb4cuBsIHQg5qQWe PJ6o39h5qI9Zz07uaeIcgPkVBj1s1CjGglhi X-Received: by 2002:a05:6214:238e:b0:572:5a16:51c0 with SMTP id fw14-20020a056214238e00b005725a1651c0mr28574433qvb.16.1678930887158; Wed, 15 Mar 2023 18:41:27 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:5c58]) by smtp.gmail.com with ESMTPSA id u19-20020a37ab13000000b00725d8d6983asm3390746qke.61.2023.03.15.18.41.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 18:41:26 -0700 (PDT) From: David Vernet To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@meta.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH bpf-next 2/5] bpf: Mark struct bpf_cpumask as RCU protected Date: Wed, 15 Mar 2023 20:41:19 -0500 Message-Id: <20230316014122.678082-3-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316014122.678082-1-void@manifault.com> References: <20230316014122.678082-1-void@manifault.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760487023940947783?= X-GMAIL-MSGID: =?utf-8?q?1760487023940947783?= struct bpf_cpumask is a BPF-wrapper around the struct cpumask type which can be instantiated by a BPF program, and then queried as a cpumask in similar fashion to normal kernel code. The previous patch in this series makes the type fully RCU safe, allowing the type can be included in the rcu_protected_type BTF ID list. A subsequent patch will remove bpf_cpumask_kptr_get(), as it's no longer useful now that the verifier allows us to use bpf_rcu_read_{un}lock() to get an RCU-safe pointer. Signed-off-by: David Vernet --- kernel/bpf/verifier.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 60793f793ca6..15b5c5c729f9 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -4599,6 +4599,7 @@ static bool in_rcu_cs(struct bpf_verifier_env *env) BTF_SET_START(rcu_protected_types) BTF_ID(struct, prog_test_ref_kfunc) BTF_ID(struct, cgroup) +BTF_ID(struct, bpf_cpumask) BTF_SET_END(rcu_protected_types) static bool rcu_protected_object(const struct btf *btf, u32 btf_id) From patchwork Thu Mar 16 01:41:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70539 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp232220wrt; Wed, 15 Mar 2023 18:55:10 -0700 (PDT) X-Google-Smtp-Source: AK7set9h1lHW8d5Gz2UnjP2HeiczWA/DkK74FyPejI8vZqNK41q+IIUYmZHkZFG6GWBv9uD85dsZ X-Received: by 2002:a17:902:ea0f:b0:19e:8267:9590 with SMTP id s15-20020a170902ea0f00b0019e82679590mr1765433plg.41.1678931710018; Wed, 15 Mar 2023 18:55:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678931710; cv=none; d=google.com; s=arc-20160816; b=c9y16RBAdzFrNyuHPI35nRe/OO8jz4atTG7J+cWYFA+d6X76GyHzZTzyQ0tNigz/7R XrnzD4h6Yo7wpPYpo8onxcSxaBIGMajBg05/H6tWUrp3f3g1fiMw0piM379PUOHcJ6TR 6TraK9YNnFk7Jp6X98KdzkmL2g2SuNmj5RCVO2dHyJ1AGAjhqqqXETYWqG7jkiwYW2r0 4MfCqobi8I5mGwfeSAZRGD/NFcKHU7fSPbzuTW4rM9OQEPn7UJ55tURrWkUwV6Lf1Z5k L0zn+XBGjQF9SYKiGHMHil1OrHHp3Pyqgq6SVJdjjiwgDH2CEw1zyjfTTnUehga1KFs4 y9cA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=fa2fhlw1TqFXS4nJVOwTl5oF6HLkgTrJ78c4jwZcDqU=; b=IzH1HtJVqput8CKpwXoYRB5EY40naBdVZ0uYREVSHuYq1TUu6PB5UMWfOKcXO/6jRO YIN+PZ7kGs2m5BjUtD/XzHjT6CKxb+71pqB0W38xc03i2meO3YY2ifKe7AXw2s3PbPih s6uUbg4v9gVxONRnTcT3ohORdO4Fp4zZN8db//ulm5mIqNeJIhK/CC6l7R4C/7e/LUhF QZ/pQUguM/ldvSDUuL3T4pB75Wdgqw0Cn7CdW6IalHreTupm7BG4Icktl/V21j5rK9fk uoudzK2WeAX7qqbVFjYIoeR98s7jpVF+h3KFUjwRDRqLRfYNILZbfmPNO7Oeshb7um2t 6DLw== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i12-20020a17090332cc00b001a076e98649si4353625plr.447.2023.03.15.18.54.55; Wed, 15 Mar 2023 18:55:09 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229878AbjCPBll (ORCPT + 99 others); Wed, 15 Mar 2023 21:41:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229820AbjCPBlb (ORCPT ); Wed, 15 Mar 2023 21:41:31 -0400 Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC6BFB1B2F; Wed, 15 Mar 2023 18:41:29 -0700 (PDT) Received: by mail-qt1-f175.google.com with SMTP id ek9so259091qtb.10; Wed, 15 Mar 2023 18:41:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678930888; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fa2fhlw1TqFXS4nJVOwTl5oF6HLkgTrJ78c4jwZcDqU=; b=SzKE/cSzA/munZmVu4G36Q7oF4WUbhcHGvG4QXyfBkNeOd4RbRJD41z6yE+AcGIjIz SNzgc80Ij8UO8md+5C8+euyGK4PcCBQaZJvmtOvYHb3OxS8wxekiXnr0RC34LOxmqF/a iSKZ2rglH/QeFlyAKVJRt6CTpca6J/MKRH84866J7d/qv5y9/6LztLMLX3IPHIIRVKkF lG394j6u+6DfZjD51BJGShQqb/zyLfi/KJxeHvk7TRYB+52n6MhcZGOhcJABILXMHxlk NI1OxcE4D7PcYF5WqdhGSHjGf4VGDAw2HcVYCwELZQKeC7hnSVwQ71xcSM7BkgmUkths EWxg== X-Gm-Message-State: AO0yUKW5jXHZACopqUWNBWXwIP79SUwNyRw0yGXcvsZoWfzFODq+BRka LDtlpKYTc51aYbCt0b+sSZu5AjnpN/Qs3BnO X-Received: by 2002:ac8:7fca:0:b0:3bf:d051:8f5f with SMTP id b10-20020ac87fca000000b003bfd0518f5fmr3833330qtk.32.1678930888603; Wed, 15 Mar 2023 18:41:28 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:5c58]) by smtp.gmail.com with ESMTPSA id t73-20020a37aa4c000000b007456a9bccc3sm4944000qke.7.2023.03.15.18.41.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 18:41:28 -0700 (PDT) From: David Vernet To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@meta.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH bpf-next 3/5] bpf/selftests: Test using global cpumask kptr with RCU Date: Wed, 15 Mar 2023 20:41:20 -0500 Message-Id: <20230316014122.678082-4-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316014122.678082-1-void@manifault.com> References: <20230316014122.678082-1-void@manifault.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760487497128091526?= X-GMAIL-MSGID: =?utf-8?q?1760487497128091526?= Now that struct bpf_cpumask * is considered an RCU-safe type according to the verifier, we should add tests that validate its common usages. This patch adds those tests to the cpumask test suite. A subsequent changes will remove bpf_cpumask_kptr_get(), and will adjust the selftest and BPF documentation accordingly. Signed-off-by: David Vernet --- .../selftests/bpf/prog_tests/cpumask.c | 1 + .../selftests/bpf/progs/cpumask_common.h | 6 ++ .../selftests/bpf/progs/cpumask_failure.c | 62 +++++++++++++++++++ .../selftests/bpf/progs/cpumask_success.c | 33 ++++++++++ 4 files changed, 102 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/cpumask.c b/tools/testing/selftests/bpf/prog_tests/cpumask.c index 5fbe457c4ebe..6c0fe23498c7 100644 --- a/tools/testing/selftests/bpf/prog_tests/cpumask.c +++ b/tools/testing/selftests/bpf/prog_tests/cpumask.c @@ -17,6 +17,7 @@ static const char * const cpumask_success_testcases[] = { "test_insert_leave", "test_insert_remove_release", "test_insert_kptr_get_release", + "test_global_mask_rcu", }; static void verify_success(const char *prog_name) diff --git a/tools/testing/selftests/bpf/progs/cpumask_common.h b/tools/testing/selftests/bpf/progs/cpumask_common.h index 65e5496ca1b2..7623782fbd62 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_common.h +++ b/tools/testing/selftests/bpf/progs/cpumask_common.h @@ -9,6 +9,9 @@ int err; +#define private(name) SEC(".bss." #name) __hidden __attribute__((aligned(8))) +private(MASK) static struct bpf_cpumask __kptr * global_mask; + struct __cpumask_map_value { struct bpf_cpumask __kptr * cpumask; }; @@ -51,6 +54,9 @@ void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym u32 bpf_cpumask_any(const struct cpumask *src) __ksym; u32 bpf_cpumask_any_and(const struct cpumask *src1, const struct cpumask *src2) __ksym; +void bpf_rcu_read_lock(void) __ksym; +void bpf_rcu_read_unlock(void) __ksym; + static inline const struct cpumask *cast(struct bpf_cpumask *cpumask) { return (const struct cpumask *)cpumask; diff --git a/tools/testing/selftests/bpf/progs/cpumask_failure.c b/tools/testing/selftests/bpf/progs/cpumask_failure.c index cfe83f0ef9e2..9f726d55f747 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_failure.c +++ b/tools/testing/selftests/bpf/progs/cpumask_failure.c @@ -127,3 +127,65 @@ int BPF_PROG(test_cpumask_null, struct task_struct *task, u64 clone_flags) return 0; } + +SEC("tp_btf/task_newtask") +__failure __msg("R2 must be a rcu pointer") +int BPF_PROG(test_global_mask_out_of_rcu, struct task_struct *task, u64 clone_flags) +{ + struct bpf_cpumask *local, *prev; + + local = create_cpumask(); + if (!local) + return 0; + + prev = bpf_kptr_xchg(&global_mask, local); + if (prev) { + bpf_cpumask_release(prev); + err = 3; + return 0; + } + + bpf_rcu_read_lock(); + local = global_mask; + if (!local) { + err = 4; + bpf_rcu_read_unlock(); + return 0; + } + + bpf_rcu_read_unlock(); + + /* RCU region is exited before calling KF_RCU kfunc. */ + + bpf_cpumask_test_cpu(0, (const struct cpumask *)local); + + return 0; +} + +SEC("tp_btf/task_newtask") +__failure __msg("NULL pointer passed to trusted arg1") +int BPF_PROG(test_global_mask_no_null_check, struct task_struct *task, u64 clone_flags) +{ + struct bpf_cpumask *local, *prev; + + local = create_cpumask(); + if (!local) + return 0; + + prev = bpf_kptr_xchg(&global_mask, local); + if (prev) { + bpf_cpumask_release(prev); + err = 3; + return 0; + } + + bpf_rcu_read_lock(); + local = global_mask; + + /* No NULL check is performed on global cpumask kptr. */ + bpf_cpumask_test_cpu(0, (const struct cpumask *)local); + + bpf_rcu_read_unlock(); + + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c index 97ed08c4ff03..fe928ff72a06 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_success.c +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c @@ -423,3 +423,36 @@ int BPF_PROG(test_insert_kptr_get_release, struct task_struct *task, u64 clone_f return 0; } + +SEC("tp_btf/task_newtask") +int BPF_PROG(test_global_mask_rcu, struct task_struct *task, u64 clone_flags) +{ + struct bpf_cpumask *local, *prev; + + if (!is_test_task()) + return 0; + + local = create_cpumask(); + if (!local) + return 0; + + prev = bpf_kptr_xchg(&global_mask, local); + if (prev) { + bpf_cpumask_release(prev); + err = 3; + return 0; + } + + bpf_rcu_read_lock(); + local = global_mask; + if (!local) { + err = 4; + bpf_rcu_read_unlock(); + return 0; + } + + bpf_cpumask_test_cpu(0, (const struct cpumask *)local); + bpf_rcu_read_unlock(); + + return 0; +} From patchwork Thu Mar 16 01:41:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70541 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp232586wrt; Wed, 15 Mar 2023 18:56:31 -0700 (PDT) X-Google-Smtp-Source: AK7set+QnmmpWtkQVH0su+KkXz/s8aMm11W/wANq0ARr1t2p7hAyuMLCXs48LGNv7sBswU+uKEwT X-Received: by 2002:a17:90a:191e:b0:23d:3fbe:2f7 with SMTP id 30-20020a17090a191e00b0023d3fbe02f7mr1905587pjg.20.1678931791307; Wed, 15 Mar 2023 18:56:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678931791; cv=none; d=google.com; s=arc-20160816; b=U/jCU/HAk9kSBPkQ8RFTdCgBVHS8BpdZOY1W+aavL+9bIIaJDSt9By2tag+io3Dpmh 8b4MZeD5J5nfHFRxDF/Y5mb4+KhQLaKFfPxIpd1arfBo0iZ3wn/xaZgEUoOmA2pbAbIa p+2VAw6m8avfnEUDwZO4qYHarSJyWNvz+LZ1gUH7GN4J3pV53jyFmIlkF6O5y65/vbey ZHXlaoVeyP5OISlhbbFAxhbWn+p50Udu+I2Q2ckalJgW3bJA83uGUmRLQeN4sR9HJjaA 3ZrJMfCOVqjzPyCwcZ5efeDmeqpBqeIiOcONKcHTmmuonZZbNcwmZ9DuajgF5gjRK/Z0 JKsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=x5WS6KKdTFofb6bEzbdOSRo28ZlNPT8Pg4JD2YjEh/o=; b=naYrv69+ttN4xXdobnR3BfrrRKei8fI94azFvvYEGuaDHMuE/5n6JKr7fWnD6X7Ggr j3A8rRYpPmZbVpYn8z/r8ysQn+P/QO3BAzq0pVi8EKoyoWi8JwsFTwoAPZIC7GyeCNpC BLaQxs12Knuu7UArsNPtRidC7SnL3VdETEfljpNEOgzxjoRCVw9x2T5xfk7dnMgtr/3K HuLedRFsjXFnqHgekPnaZndGRHaNaqlvXLAWxTPKMC4OttKm//qvDfbbujE+y3GwhDJD frngXPApLRaXZR7P0ZRGenkpwOeSM2rQ0VjHvQ0U2cUZwPGM/EjnojKQpt7SklbMJmSp +lOw== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x5-20020a17090abc8500b00233ca49c5a7si261396pjr.17.2023.03.15.18.56.16; Wed, 15 Mar 2023 18:56:31 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229868AbjCPBlo (ORCPT + 99 others); Wed, 15 Mar 2023 21:41:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229459AbjCPBlg (ORCPT ); Wed, 15 Mar 2023 21:41:36 -0400 Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F4207B7D89; Wed, 15 Mar 2023 18:41:31 -0700 (PDT) Received: by mail-qt1-f179.google.com with SMTP id c18so277329qte.5; Wed, 15 Mar 2023 18:41:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678930890; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=x5WS6KKdTFofb6bEzbdOSRo28ZlNPT8Pg4JD2YjEh/o=; b=dxZM2Ru5wkdwrm70UKwkWC/QWBoKkFZk9jt0ESDRPwF9OrgK05bI7qcInPmSabuwMr msfT/qZbSIFEoewzJTF4pIeM/qCsUlO7+8PIPh2LKrexM9iEyyw5b6JMNINPFfB8r9cu KGT0SvQNQFEoZrRBBbBsVcIJyW2XTjy6pA1VaiMpFh+96eonDdIwcaFcsKCJlEZZkPk+ ycfIpLojSN3599IMeIq0M/4FqNG1fhKJwk03TZTR976IXDwk6u04kydSSWJp1dXduOxI l9Hwch57+80rA4qqP/1+dfUqK/4I/AfCKu5OGeHWezFYDMsK1WCE3Cbr/qL1V0nxyOHJ R+Dw== X-Gm-Message-State: AO0yUKUXvq7SprnoQQPFAazWJbCWtohdtFEHmgXHZ0AzpItwaXrLMUIb ZqX6+Nnp02YmhucKygc2jhtjKbwECToyWgWX X-Received: by 2002:ac8:7d02:0:b0:3bf:daae:7f2a with SMTP id g2-20020ac87d02000000b003bfdaae7f2amr3221891qtb.68.1678930889901; Wed, 15 Mar 2023 18:41:29 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:5c58]) by smtp.gmail.com with ESMTPSA id c1-20020ac853c1000000b003bfc1f49ad1sm4938505qtq.87.2023.03.15.18.41.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 18:41:29 -0700 (PDT) From: David Vernet To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@meta.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH bpf-next 4/5] bpf: Remove bpf_cpumask_kptr_get() kfunc Date: Wed, 15 Mar 2023 20:41:21 -0500 Message-Id: <20230316014122.678082-5-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316014122.678082-1-void@manifault.com> References: <20230316014122.678082-1-void@manifault.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760487581559565351?= X-GMAIL-MSGID: =?utf-8?q?1760487581559565351?= Now that struct bpf_cpumask is RCU safe, there's no need for this kfunc. Rather than doing the following: private(MASK) static struct bpf_cpumask __kptr *global; int BPF_PROG(prog, s32 cpu, ...) { struct bpf_cpumask *cpumask; bpf_rcu_read_lock(); cpumask = bpf_cpumask_kptr_get(&global); if (!cpumask) { bpf_rcu_read_unlock(); return -1; } bpf_cpumask_setall(cpumask); ... bpf_cpumask_release(cpumask); bpf_rcu_read_unlock(); } Programs can instead simply do this (assume same global cpumask): int BPF_PROG(prog, ...) { struct bpf_cpumask *cpumask; bpf_rcu_read_lock(); cpumask = global; if (!cpumask) { bpf_rcu_read_unlock(); return -1; } bpf_cpumask_setall(cpumask); ... bpf_rcu_read_unlock(); } In other words, no extra atomic acquire / release, and less boilerplate code. This patch removes both the kfunc, and its selftests. A subsequent patch will update the BPF documentation to use the new method for accessing cpumasks with RCU protection. Signed-off-by: David Vernet --- kernel/bpf/cpumask.c | 29 ------------------ .../selftests/bpf/prog_tests/cpumask.c | 1 - .../selftests/bpf/progs/cpumask_common.h | 1 - .../selftests/bpf/progs/cpumask_failure.c | 24 --------------- .../selftests/bpf/progs/cpumask_success.c | 30 ------------------- 5 files changed, 85 deletions(-) diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c index 9ab462c5848a..5329d07114e9 100644 --- a/kernel/bpf/cpumask.c +++ b/kernel/bpf/cpumask.c @@ -81,34 +81,6 @@ __bpf_kfunc struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask) return cpumask; } -/** - * bpf_cpumask_kptr_get() - Attempt to acquire a reference to a BPF cpumask - * stored in a map. - * @cpumaskp: A pointer to a BPF cpumask map value. - * - * Attempts to acquire a reference to a BPF cpumask stored in a map value. The - * cpumask returned by this function must either be embedded in a map as a - * kptr, or freed with bpf_cpumask_release(). This function may return NULL if - * no BPF cpumask was found in the specified map value. - */ -__bpf_kfunc struct bpf_cpumask *bpf_cpumask_kptr_get(struct bpf_cpumask **cpumaskp) -{ - struct bpf_cpumask *cpumask; - - /* The BPF memory allocator frees memory backing its caches in an RCU - * callback. Thus, we can safely use RCU to ensure that the cpumask is - * safe to read. - */ - rcu_read_lock(); - - cpumask = READ_ONCE(*cpumaskp); - if (cpumask && !refcount_inc_not_zero(&cpumask->usage)) - cpumask = NULL; - - rcu_read_unlock(); - return cpumask; -} - static void cpumask_free_cb(struct rcu_head *head) { struct bpf_cpumask *cpumask; @@ -434,7 +406,6 @@ BTF_SET8_START(cpumask_kfunc_btf_ids) BTF_ID_FLAGS(func, bpf_cpumask_create, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_cpumask_release, KF_RELEASE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_cpumask_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS) -BTF_ID_FLAGS(func, bpf_cpumask_kptr_get, KF_ACQUIRE | KF_KPTR_GET | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_cpumask_first, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_first_zero, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_set_cpu, KF_RCU) diff --git a/tools/testing/selftests/bpf/prog_tests/cpumask.c b/tools/testing/selftests/bpf/prog_tests/cpumask.c index 6c0fe23498c7..cdf4acc18e4c 100644 --- a/tools/testing/selftests/bpf/prog_tests/cpumask.c +++ b/tools/testing/selftests/bpf/prog_tests/cpumask.c @@ -16,7 +16,6 @@ static const char * const cpumask_success_testcases[] = { "test_copy_any_anyand", "test_insert_leave", "test_insert_remove_release", - "test_insert_kptr_get_release", "test_global_mask_rcu", }; diff --git a/tools/testing/selftests/bpf/progs/cpumask_common.h b/tools/testing/selftests/bpf/progs/cpumask_common.h index 7623782fbd62..0c5b785a93e4 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_common.h +++ b/tools/testing/selftests/bpf/progs/cpumask_common.h @@ -26,7 +26,6 @@ struct array_map { struct bpf_cpumask *bpf_cpumask_create(void) __ksym; void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym; struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask) __ksym; -struct bpf_cpumask *bpf_cpumask_kptr_get(struct bpf_cpumask **cpumask) __ksym; u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym; u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym; void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; diff --git a/tools/testing/selftests/bpf/progs/cpumask_failure.c b/tools/testing/selftests/bpf/progs/cpumask_failure.c index 9f726d55f747..db4f94e72b61 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_failure.c +++ b/tools/testing/selftests/bpf/progs/cpumask_failure.c @@ -94,30 +94,6 @@ int BPF_PROG(test_insert_remove_no_release, struct task_struct *task, u64 clone_ return 0; } -SEC("tp_btf/task_newtask") -__failure __msg("Unreleased reference") -int BPF_PROG(test_kptr_get_no_release, struct task_struct *task, u64 clone_flags) -{ - struct bpf_cpumask *cpumask; - struct __cpumask_map_value *v; - - cpumask = create_cpumask(); - if (!cpumask) - return 0; - - if (cpumask_map_insert(cpumask)) - return 0; - - v = cpumask_map_value_lookup(); - if (!v) - return 0; - - cpumask = bpf_cpumask_kptr_get(&v->cpumask); - - /* cpumask is never released. */ - return 0; -} - SEC("tp_btf/task_newtask") __failure __msg("NULL pointer passed to trusted arg0") int BPF_PROG(test_cpumask_null, struct task_struct *task, u64 clone_flags) diff --git a/tools/testing/selftests/bpf/progs/cpumask_success.c b/tools/testing/selftests/bpf/progs/cpumask_success.c index fe928ff72a06..2fcdd7f68ac7 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_success.c +++ b/tools/testing/selftests/bpf/progs/cpumask_success.c @@ -394,36 +394,6 @@ int BPF_PROG(test_insert_remove_release, struct task_struct *task, u64 clone_fla return 0; } -SEC("tp_btf/task_newtask") -int BPF_PROG(test_insert_kptr_get_release, struct task_struct *task, u64 clone_flags) -{ - struct bpf_cpumask *cpumask; - struct __cpumask_map_value *v; - - cpumask = create_cpumask(); - if (!cpumask) - return 0; - - if (cpumask_map_insert(cpumask)) { - err = 3; - return 0; - } - - v = cpumask_map_value_lookup(); - if (!v) { - err = 4; - return 0; - } - - cpumask = bpf_cpumask_kptr_get(&v->cpumask); - if (cpumask) - bpf_cpumask_release(cpumask); - else - err = 5; - - return 0; -} - SEC("tp_btf/task_newtask") int BPF_PROG(test_global_mask_rcu, struct task_struct *task, u64 clone_flags) { From patchwork Thu Mar 16 01:41:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70540 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp232429wrt; Wed, 15 Mar 2023 18:55:56 -0700 (PDT) X-Google-Smtp-Source: AK7set+33WCuA2075fPAD9haG8d0XhbRF1uXFap/CkYKhGGYwvEnsO3/lm5DtC2jEejasjdg7RCD X-Received: by 2002:a17:903:2310:b0:1a0:7584:f468 with SMTP id d16-20020a170903231000b001a07584f468mr1925788plh.0.1678931756623; Wed, 15 Mar 2023 18:55:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678931756; cv=none; d=google.com; s=arc-20160816; b=VTsKrjFK4kUFhiFNzyRcvKx72siYLCfdagnhzStMN+NCfaml5DrWpztTRMKQpcR62r VEYUJM72oi2+l1PCuflFK4LbpMVnlc3XBtEwJPDjvfD3FRs+lcM5802v6qw/BQhm737d OXSIbhScehxWs3vo91DkAf9U6mqmcCW5PJs+gtZmRdb1chSAsWQLfA7UmZZ5EsdJhyiI 5ocoJbebqFUqwrYkf++nL6iNr/ZDBUl7e0v+Arh5LaB/RZEQPWodp0j6dYCDYhAWq62w f0+One4taba0KPsrk8/V4yqjpPjPm0yyFRJQg+ALlkUXQ3tyRIFNDaeFHkCaLIXMUw8b EPqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=fujln5B4PJVc7FU0F2EjRjzV5VR7c7BJfqwtEXGQG30=; b=psOrQFGXERVdivOgfMpnSnOcqqiW4fJ64TN7ptJfuBefTmQWZEQHtquge5Xb0BAAAi 3v7roFEJHnZCcGQZEd33ftIof7u6cDB+qizptjDBwCHE5peP+uZO7afxBIMZegzMrDPT 8VeX+L1Lc5pQlYXNrvnmNkSWpTkcq6SL+meOXd8puqJ6F0LwtBHuTwNp7O1iJDWs3aQW DHkFsCZgSZA5m+tHO+A7WDJAYxw120Oh8W2QAcr6q8pDfHR54l749YNjYmbMTScM0Vm7 tSnvnyDJwEya7Aw/V3fmuzahZkvmG/U8gZayMSUwGAx2ufD1vCpRxNrSZ3kEsIYwqlQk S2ew== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id la7-20020a170902fa0700b0019cd640c3f4si6837263plb.206.2023.03.15.18.55.42; Wed, 15 Mar 2023 18:55:56 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229916AbjCPBlr (ORCPT + 99 others); Wed, 15 Mar 2023 21:41:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229844AbjCPBlk (ORCPT ); Wed, 15 Mar 2023 21:41:40 -0400 Received: from mail-qt1-f172.google.com (mail-qt1-f172.google.com [209.85.160.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDACEB860D; Wed, 15 Mar 2023 18:41:32 -0700 (PDT) Received: by mail-qt1-f172.google.com with SMTP id l13so285742qtv.3; Wed, 15 Mar 2023 18:41:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678930891; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fujln5B4PJVc7FU0F2EjRjzV5VR7c7BJfqwtEXGQG30=; b=fb9D9hLFXdrVtLcdhi5BPw8DzBQmDCcbu0PPRLD8eaUxDGZWBoM91Zo+X0RZwpvNLr C+X+iunwZu0DpWRzI0RA+HEs/zLlC3Lup/YbmPrvZcul1kNsxbBNp63dbgG3+bTM5vPD kGhOzsH7inIQVwfp2N+slHi4yuPVUV2wtlELX9nMPLHJg+Bh1Pmh0aJvU64TsE1nAxE0 4LdDajy9Z9fCSkRQBWawFIMPGmWJzSytUtSP1t/vYJtCsZ4WQS0HIWB4Is7KCQLr4Gwm w5ssmj2KcymOhS1PzRE4uu1LrWXhSnEAQLd1v93m9Jmg7AT68beEEhDPKz9JmTuuwRZb O/+w== X-Gm-Message-State: AO0yUKXj/PXGh556SY4JB384n7SbCHTJesYkQkTMSWGWlLiQp4s4gRUb 6qxvylzL1sHtchtcCOuGjKawAiCRqahB4d62 X-Received: by 2002:a05:622a:174c:b0:3bf:d0c7:12df with SMTP id l12-20020a05622a174c00b003bfd0c712dfmr3506016qtk.63.1678930891324; Wed, 15 Mar 2023 18:41:31 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:5c58]) by smtp.gmail.com with ESMTPSA id x24-20020ac86b58000000b003bfa2c512e6sm4735074qts.20.2023.03.15.18.41.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 18:41:30 -0700 (PDT) From: David Vernet To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@meta.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH bpf-next 5/5] bpf,docs: Remove bpf_cpumask_kptr_get() from documentation Date: Wed, 15 Mar 2023 20:41:22 -0500 Message-Id: <20230316014122.678082-6-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316014122.678082-1-void@manifault.com> References: <20230316014122.678082-1-void@manifault.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760487545818690062?= X-GMAIL-MSGID: =?utf-8?q?1760487545818690062?= Now that the kfunc no longer exists, we can remove it and instead describe how RCU can be used to get a struct bpf_cpumask from a map value. This patch updates the BPF documentation accordingly. Signed-off-by: David Vernet --- Documentation/bpf/cpumasks.rst | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/Documentation/bpf/cpumasks.rst b/Documentation/bpf/cpumasks.rst index 75344cd230e5..41efd8874eeb 100644 --- a/Documentation/bpf/cpumasks.rst +++ b/Documentation/bpf/cpumasks.rst @@ -117,12 +117,7 @@ For example: As mentioned and illustrated above, these ``struct bpf_cpumask *`` objects can also be stored in a map and used as kptrs. If a ``struct bpf_cpumask *`` is in a map, the reference can be removed from the map with bpf_kptr_xchg(), or -opportunistically acquired with bpf_cpumask_kptr_get(): - -.. kernel-doc:: kernel/bpf/cpumask.c - :identifiers: bpf_cpumask_kptr_get - -Here is an example of a ``struct bpf_cpumask *`` being retrieved from a map: +opportunistically acquired using RCU: .. code-block:: c @@ -144,7 +139,7 @@ Here is an example of a ``struct bpf_cpumask *`` being retrieved from a map: /** * A simple example tracepoint program showing how a * struct bpf_cpumask * kptr that is stored in a map can - * be acquired using the bpf_cpumask_kptr_get() kfunc. + * be passed to kfuncs using RCU protection. */ SEC("tp_btf/cgroup_mkdir") int BPF_PROG(cgrp_ancestor_example, struct cgroup *cgrp, const char *path) @@ -158,26 +153,21 @@ Here is an example of a ``struct bpf_cpumask *`` being retrieved from a map: if (!v) return -ENOENT; + bpf_rcu_read_lock(); /* Acquire a reference to the bpf_cpumask * kptr that's already stored in the map. */ - kptr = bpf_cpumask_kptr_get(&v->cpumask); - if (!kptr) + kptr = v->cpumask; + if (!kptr) { /* If no bpf_cpumask was present in the map, it's because * we're racing with another CPU that removed it with * bpf_kptr_xchg() between the bpf_map_lookup_elem() - * above, and our call to bpf_cpumask_kptr_get(). - * bpf_cpumask_kptr_get() internally safely handles this - * race, and will return NULL if the cpumask is no longer - * present in the map by the time we invoke the kfunc. + * above, and our load of the pointer from the map. */ + bpf_rcu_read_unlock(); return -EBUSY; + } - /* Free the reference we just took above. Note that the - * original struct bpf_cpumask * kptr is still in the map. It will - * be freed either at a later time if another context deletes - * it from the map, or automatically by the BPF subsystem if - * it's still present when the map is destroyed. - */ - bpf_cpumask_release(kptr); + bpf_cpumask_setall(kptr); + bpf_rcu_read_unlock(); return 0; }