From patchwork Thu Mar 16 05:40:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70580 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp301778wrt; Wed, 15 Mar 2023 22:42:50 -0700 (PDT) X-Google-Smtp-Source: AK7set/FhW9l/Bt/Id0C2cFMMLTakopSyEWYO7c7crqGOIFpjzGpIUu/ITkxXcLczXFH/eVmKwzZ X-Received: by 2002:a17:902:d2d0:b0:19e:7880:1c2 with SMTP id n16-20020a170902d2d000b0019e788001c2mr2616045plc.47.1678945369682; Wed, 15 Mar 2023 22:42:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678945369; cv=none; d=google.com; s=arc-20160816; b=wqyabvf4ejAUZTz0bZdWHar+JN5So1eZo2RpmSVZHrnnJSqP46YW8b6bstUBL4evHw DrOKUbmILguqweurASoH0M6u/yBr09YqvuFX7cti1BFJsaDbtXL0LvdWCK0vUfi0k19Q xi8DdQKGQFyZl7YEFYckdi8r2nT0pIRmy8EoSIokQJEk139VzceItwh/xEpJwjunfoGI dUAAYKuobRfAoeIjECIr30QJiQONQtvurjKbBtzdXMyE4drt78yfrdrw9iS/X5a3HbU4 tVuCvCsbJd/+ruETk3ptl9x6m+gcWnizHr5lAv+w7pFosdpZIhiXiwYVYt6Ie1DoUFWX IyNw== 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=LqdX1QaRwIooZT8c9LoVfwKnRXDO5nO+/Pj89wdMEuk=; b=JUPEmqtYCRQqKXJFAYQf33hAfhp4h7IJpz/v23Cubm/UYPvHJoiPR6Ox3ZYRW/Mqjs SeCXaXYhsAMlJJdX9Nh2LCADoloAwY0MNTQNS3IQZbhOgHquWZ5gTx9Wp5+4MJ6JVw7m LEb1S2rr1VLNP/N83evf4i2YsjRcEI3YtS/JfM02ZEct4MrcdCo9IphlnBsMsXEkSHy/ VAn0Z/UK7tEpvq6/v4RFk9y11jidb3nI/yLZuX9PdyQ8cxrNeSCU1Xkv3AKQZUyr8MIM m9EkUKstCKAMWf4U/dAg2wDIMN54smAGfYhKtam0C4oT7SOYRqQ9mpVMCrhud0UM7CR/ mVOw== 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 u18-20020a170902a61200b0019e8ffc177asi7018598plq.367.2023.03.15.22.42.34; Wed, 15 Mar 2023 22:42:49 -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 S229801AbjCPFkm (ORCPT + 99 others); Thu, 16 Mar 2023 01:40:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229785AbjCPFki (ORCPT ); Thu, 16 Mar 2023 01:40:38 -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 E9F335A1B1; Wed, 15 Mar 2023 22:40:37 -0700 (PDT) Received: by mail-qv1-f44.google.com with SMTP id x8so555597qvr.9; Wed, 15 Mar 2023 22:40:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678945237; 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=LqdX1QaRwIooZT8c9LoVfwKnRXDO5nO+/Pj89wdMEuk=; b=uhG3Z31MYUExDONsAkQsL4axFQe4FDkcwSnde04edN1C7CH8k9GE2xK67BRLBMTY17 W8afpqQBw7FbOh1b4e9QamcBYhbgG4Xit2cNX2hPWqnr8xwhgcWS4VXhKTDT30EN7AEa AJXEJZV+PlsY/5fpSeaeKmlxA9IeJCXEa3boQx/xNlUbyxAiOP3CCpCZILL58QZT7gh3 2SwTFtYi9gr5+FVVTPc2lkCJ2TNFCb53GKB2MJiu+neWHdvxI/BHSDUIcbrjiZa4p5SZ 149Y+StTEcZICRr/iWMSFeMsY8Hslo9ZH2QwZyJSalBi3ZStgjkXlVW4jnRxdRAOzdne jotA== X-Gm-Message-State: AO0yUKXlGLapgcBSSUJtJKXndqmvNnrODN2Zhp31ujtpQOKtYAZ7JLtm imKCSfSqf6/b7th/sAUXTiYx2rO0BS4tUY0r X-Received: by 2002:a05:6214:1c81:b0:577:4ea5:243f with SMTP id ib1-20020a0562141c8100b005774ea5243fmr29565976qvb.7.1678945236627; Wed, 15 Mar 2023 22:40:36 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:7f57]) by smtp.gmail.com with ESMTPSA id r8-20020ae9d608000000b0073b7f2a0bcbsm5199468qkk.36.2023.03.15.22.40.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 22:40:36 -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@fb.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 v2 1/5] bpf: Free struct bpf_cpumask in call_rcu handler Date: Thu, 16 Mar 2023 00:40:24 -0500 Message-Id: <20230316054028.88924-2-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316054028.88924-1-void@manifault.com> References: <20230316054028.88924-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?1760501819967167160?= X-GMAIL-MSGID: =?utf-8?q?1760501819967167160?= 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 | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c index b6587ec40f1b..98eea62b6b7b 100644 --- a/kernel/bpf/cpumask.c +++ b/kernel/bpf/cpumask.c @@ -9,6 +9,7 @@ /** * struct bpf_cpumask - refcounted BPF cpumask wrapper structure * @cpumask: The actual cpumask embedded in the struct. + * @rcu: The RCU head used to free the cpumask with RCU safety. * @usage: Object reference counter. When the refcount goes to 0, the * memory is released back to the BPF allocator, which provides * RCU safety. @@ -24,6 +25,7 @@ */ struct bpf_cpumask { cpumask_t cpumask; + struct rcu_head rcu; refcount_t usage; }; @@ -108,6 +110,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 +133,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 05:40:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70578 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp301683wrt; Wed, 15 Mar 2023 22:42:29 -0700 (PDT) X-Google-Smtp-Source: AK7set8Un7p9wZXZmCPcz6m73xfU9Ttb55/ENL7j8YgwMaYuKFyvmp4fOhzvHLrnOqsVpP94aVwD X-Received: by 2002:a05:6a20:4408:b0:cd:3069:28cd with SMTP id ce8-20020a056a20440800b000cd306928cdmr2538075pzb.39.1678945348990; Wed, 15 Mar 2023 22:42:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678945348; cv=none; d=google.com; s=arc-20160816; b=j+VU4OHcn22BtM7nbEsKdJ5kOnePxBZVXC89pJDZ4C4/0XeG7w+TXntyBk14fTdAqd xqX2Rjf4TQdDJnlLCXXb9UcTmRGPSnqyeZ5XEFtDXV6qW7Exv3MZiEqyUOoKMIlVLnNP W5pbcKI/3EpCv5rRX5BkmRnV0d+ar2D7KE2ndJivG+6KGa8xQdOVLorGymeXsQcYO4Ui bcnSDKqw8uwO8nAU/SiUmX+QrwjfXFQi2MYibx3GvAfYeZbA6DDwmbLvzLAiG55VuNg0 +mT8WBjesMhreODFcnD6H/KOvnOpCHcNuhNfjm/ilAerdj74bQVuUuVV+WmCOC0KExGY vvoA== 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=/+xUrg9q1AAptwCu8YnIT5AXN2cGpwLJEELj3y+obxs=; b=qavboHL5N+GxQWX7jVioZN4DMgHF/LjNuCaIzLeWCvBa5tHyHUs7u3Sch+siKt5iYF oGxdmHv7oJJ/MX8un2LhugFk1KuGFKXnWcqkl7ZEp6KDsoM7aoETjfHkbw/qr6d4TNlW gVu8EcVoOB1ivXzKM+2FRWbtWA3e2BJrl4+j6zrRPD9lIxTeT5fjtg4ARqy5HraZit6U kEsE/yZqHg3N610VMRrZadSNBPGTv1CqqS1LXmhc4ZZSnIiQtrE0UPDy7I3MskyCuco9 0t9mKKrGF92kYvk6zu6dDceC/a2NZjfI9p0AYb82yfn2JXf3XrxrxIoO3ryb50P4eGID 0PUQ== 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 h9-20020a631209000000b00502f4be647fsi6792540pgl.641.2023.03.15.22.42.14; Wed, 15 Mar 2023 22:42: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; 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 S229832AbjCPFkq (ORCPT + 99 others); Thu, 16 Mar 2023 01:40:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229487AbjCPFkk (ORCPT ); Thu, 16 Mar 2023 01:40:40 -0400 Received: from mail-qt1-f177.google.com (mail-qt1-f177.google.com [209.85.160.177]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 530965CC1A; Wed, 15 Mar 2023 22:40:39 -0700 (PDT) Received: by mail-qt1-f177.google.com with SMTP id c19so610265qtn.13; Wed, 15 Mar 2023 22:40:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678945238; 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=/+xUrg9q1AAptwCu8YnIT5AXN2cGpwLJEELj3y+obxs=; b=Ec5zskyA+CcxxKTcQLOE4iemb6Ht/F4YGEjYmARn3IJU9zMTopdvLtEQOFRc+dkxCM iVUw3qJimroFMgDMUJeswElwKP0HIcXQ3DXHKlGpD6IlpYAyv8VrMuDJKbHHQ+Hc9XFV Tcs69aCIaDSPQ7vuM6e2c0ewBJDZWp/f0CMk3ZvtPU85LeiVnPa4rD8ZCRGl8stTdkQa Pi6o3s0ZPOvOdoX396/WCIDa0QFnE9pneGmXcQH/ZpV/30i6O05VueCVDMNbga09pjuH jo5/n1c6d1/ZjsnhUqdE6gYmRT9tXBD3MRehJoStgyvz0oaSutOYafK4nl8XoTLAgUqt P8MA== X-Gm-Message-State: AO0yUKU5qcyhjm06mOYTHYHxVFpYXsiJRjS6wXmP9K+GC+8oO7lYdL/v i1Nqs7QlL0nd+ZV2zeiI0mV/dYLN9+U97wYd X-Received: by 2002:a05:622a:1049:b0:3d4:f850:2e9 with SMTP id f9-20020a05622a104900b003d4f85002e9mr4349342qte.31.1678945237984; Wed, 15 Mar 2023 22:40:37 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:7f57]) by smtp.gmail.com with ESMTPSA id d63-20020a37b442000000b0073b575f3603sm5153630qkf.101.2023.03.15.22.40.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 22:40:37 -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@fb.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 v2 2/5] bpf: Mark struct bpf_cpumask as rcu protected Date: Thu, 16 Mar 2023 00:40:25 -0500 Message-Id: <20230316054028.88924-3-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316054028.88924-1-void@manifault.com> References: <20230316054028.88924-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?1760501797962102877?= X-GMAIL-MSGID: =?utf-8?q?1760501797962102877?= 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, so 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 we can just treat the type as RCU safe by default and do our own if check. 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 05:40:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70582 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp305520wrt; Wed, 15 Mar 2023 22:57:18 -0700 (PDT) X-Google-Smtp-Source: AK7set/X1iZ1iXguCNbJYZdOZ7EYFWfLJrOcf8XgLI2i2UiCR8BsnAZVr8oduUh+JAC/feZoCxKO X-Received: by 2002:a17:90b:4d10:b0:23a:87d1:9586 with SMTP id mw16-20020a17090b4d1000b0023a87d19586mr2534548pjb.23.1678946238487; Wed, 15 Mar 2023 22:57:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678946238; cv=none; d=google.com; s=arc-20160816; b=PXohxCTiBgANfaPfaKIs9r0sAuzGm3Ukqix0Im9jPCv/VcS5tDFWkFFErPCI28y1S4 x8DpH+bq9I4qwCjR91daPdPpJgp05GgqRy6lUOtKLhUjoGNd69n66vPTIrAZWrmJxACo Ka00hoDQslBoXqzUc79JOUVR6KlVq3J8CON1QsxzI8GpO9PlJmc6mFsicJkNovdUhOfe 80YB93RFO/BBzOxIKq5j9xvyI0Oar4wj95R3dm5nIuSyuHj4aQ2Afs8+3r08Sj5Lb9Nt uZYx6XLB93GC+WmnpqyD0WVbTeyNGGePu46iRN2OQyPPt5J5in6+3jsxsjDHePKkr5Dy XjQw== 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=jTdz4dme7xeRrGw9wABoX4tFcTWv6ZXJy/b5IbwMhLCIzR/ng76NES3XTxEoqWBaCi VUKKpjZQfCVyuATLxY9xmYGiHVdsY6qxr6XhXdPAgtwBIQ0uyZg8huOIN/Yw0pX1NZuh mp3n7GHi5+SxgDhlimFZpvH/U/YVY3Tqm0LTMPhQ5I+qgvKNwKdq6HJTppBURTuWfyNy uZbSZiYLrbcEtxm9dcQ+XsPUGMJJavAIR4VaaVGynEfMqPMGnVtiSJBcqtvs+DNQDAlD SZBGqUd6vXZDm5ywokYYoqrJ37mlKp6H0zqSlZEv1V+CwIbiHPf3vbRImPHxEq/cTON1 qcpA== 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 q2-20020a17090a938200b0023b4483a784si3502397pjo.161.2023.03.15.22.57.02; Wed, 15 Mar 2023 22:57:18 -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 S229834AbjCPFkv (ORCPT + 99 others); Thu, 16 Mar 2023 01:40:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229802AbjCPFkm (ORCPT ); Thu, 16 Mar 2023 01:40:42 -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 1CAA75F21D; Wed, 15 Mar 2023 22:40:40 -0700 (PDT) Received: by mail-qt1-f175.google.com with SMTP id t9so630315qtx.8; Wed, 15 Mar 2023 22:40:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678945239; 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=J1aGYm4XpzzJNzoci0JgQFAA5Yb5hgQwylVi/6HvBAnpI72YsuZj3CumC2gNQm7cwu qDlCeZi4h7DgNg9vNEY+5Fm9A3bxJGAK3WcKjeWOmwzCACg/Kmj5VWzuddxLwZDKf8zZ 885sIOorIvcz8K1Bp7zoCtI+ziyP5C1zu0zrqQOJlML1mSDDXzhVbnAZrXf6WLNdinVa 5Ubu7oWm1v5n44Y2SXIeDlsunluOFEzeFBtSpTVuwLawJoIz5kgBPjIzgqY4gGkg+MPC xkn9zJlYfWeXMyT2gH3kpp9NLApSuvJWPLAFpVQLDBHbRlnbo4EI+Fd0VS53rvAWTI/B Mgog== X-Gm-Message-State: AO0yUKU1BwDviIYwGrE1RerHzSLnW24zIJl3Jk362HmGNNCGq9LitiG+ D/AmzUMZ/Kppe0mLu9/c4ODDr66Ivzqi1JGD X-Received: by 2002:a05:622a:174c:b0:3bf:db29:b793 with SMTP id l12-20020a05622a174c00b003bfdb29b793mr4696811qtk.20.1678945239228; Wed, 15 Mar 2023 22:40:39 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:7f57]) by smtp.gmail.com with ESMTPSA id d192-20020ae9efc9000000b00745df1fac2bsm3876535qkg.76.2023.03.15.22.40.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 22:40:38 -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@fb.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 v2 3/5] bpf/selftests: Test using global cpumask kptr with RCU Date: Thu, 16 Mar 2023 00:40:26 -0500 Message-Id: <20230316054028.88924-4-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316054028.88924-1-void@manifault.com> References: <20230316054028.88924-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?1760502731278836685?= X-GMAIL-MSGID: =?utf-8?q?1760502731278836685?= 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 05:40:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70579 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp301728wrt; Wed, 15 Mar 2023 22:42:40 -0700 (PDT) X-Google-Smtp-Source: AK7set+rm4hGYdc8E75PmVR0PUls1muvJ7rF5SbHA/fMj2Vldsh6ZifUCVhM9t/0AsAkzpAihi3j X-Received: by 2002:a17:90b:3b4d:b0:233:ee50:d28b with SMTP id ot13-20020a17090b3b4d00b00233ee50d28bmr2587050pjb.16.1678945359767; Wed, 15 Mar 2023 22:42:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678945359; cv=none; d=google.com; s=arc-20160816; b=WXSbUkZ3J4nEeqGL3X6lrNPHsOa8nRWZ1C/wsrnpBRJZ2T/PAjrv8fsgaH0S/JSBA8 1UeCU8B17IZHBoIVFqY41iqVOgtgXLkEIPa2TiyF17RTRcudpC8xNcUMWJDO7UzjD+L/ kbJSXQOyGSlsjWFpfmqAurba2rcSs5WRFakiENMgX72AzvfW8wr5aXnSW2LRf4XHUeMb nRgmiqWSfuf+EfwhcBpLRB5KE1mO5PIAbU3iWCBo837ypZwVfEECb88zzAzsd+OrirAB TBYyH1Ze4yMQdh0cczgagaUz6WFrrUSQqDx6j2Q0F1Y9TCPT1QeMRnvEuD2oTTIgemRw 4IVg== 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=RteOw1hdEmSHA2iP0mOq4L/bB/BhHMRYsIEBS0nodsM=; b=FO8PcEefM0MUuxZqETkWg152fQ2uXqXqJdA4dsL2q3CFOJAenjdQwPWPIXmVK3ekN+ yMDe98cVWvaKlyO0IXZU+PDJN8VrCdSt43Yh44Gk3mKa8PdWzO5vB7tzTu8QI+cpsvx+ dgXGKP32MW2RlnZbI+hkjD6WHIXgbeHFb9gM9C0EwC0UHib+6eLTHg0bffde1AeDPGea DihD2txBYOY64L51slTqenHa3ogiawxXKLLizpDyWTYBKjKGQAOlS7lACQ5KpWtzscBz tADzonJKpKzzP1ATktZ77rU5CsQsw+nshLy26IFEm9gLLtXHXfCDRE+p4jTT9v3Pmztv eOUw== 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 j23-20020a635517000000b004fc298179b2si7025312pgb.123.2023.03.15.22.42.25; Wed, 15 Mar 2023 22:42: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 S229873AbjCPFlA (ORCPT + 99 others); Thu, 16 Mar 2023 01:41:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229790AbjCPFkp (ORCPT ); Thu, 16 Mar 2023 01:40:45 -0400 Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC54A64846; Wed, 15 Mar 2023 22:40:41 -0700 (PDT) Received: by mail-qt1-f169.google.com with SMTP id t9so630340qtx.8; Wed, 15 Mar 2023 22:40:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678945241; 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=RteOw1hdEmSHA2iP0mOq4L/bB/BhHMRYsIEBS0nodsM=; b=1oUAuqU5wqWAOonwwbDrd283sMY11AmyamBjp51VQdiIuU59HbQrtrIFVQhgusxKXr WwffojSBl2r9nC8QbPGJ/AJJj/jmbs5M8uuycOwDEKnIADD6v0QCAJLUj8wjgWffra1S CeH9RNuTTcelW929ARI+iAel7kSQ47NRhqsHKTS3Zg7tr7LuoZP95tqJHRjYhTkzqVRm nZghyLbiQo9knrsmzIftNT3mciUBCkKmrylHHjuT2Hf/c9ETw3cl/MxuEHgHPsGgk0bi ilgKEvcIP1nI2faYp7/iosqi7/Nw3rPXUzqMh4Q+pG1FB5O+aTrYvT+qL0usYygR4XBY vPAA== X-Gm-Message-State: AO0yUKU+akJ9+JpLxqOrTH9sLCg8s6Y8fljjPbXdNzO1mnndZCM8VWb0 3XhR2QVWOLzcVBEHqJYtqZbhhBiEWo6nzqId X-Received: by 2002:a05:622a:156:b0:3bf:ce2f:576c with SMTP id v22-20020a05622a015600b003bfce2f576cmr4033320qtw.66.1678945240627; Wed, 15 Mar 2023 22:40:40 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:7f57]) by smtp.gmail.com with ESMTPSA id j5-20020a37b905000000b00745bca3c8a0sm5145783qkf.43.2023.03.15.22.40.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 22:40:40 -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@fb.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 v2 4/5] bpf: Remove bpf_cpumask_kptr_get() kfunc Date: Thu, 16 Mar 2023 00:40:27 -0500 Message-Id: <20230316054028.88924-5-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316054028.88924-1-void@manifault.com> References: <20230316054028.88924-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?1760501809318404092?= X-GMAIL-MSGID: =?utf-8?q?1760501809318404092?= 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 (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, as well as its selftests and documentation. 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 98eea62b6b7b..db9da2194c1a 100644 --- a/kernel/bpf/cpumask.c +++ b/kernel/bpf/cpumask.c @@ -82,34 +82,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; @@ -435,7 +407,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 05:40:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 70581 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp303713wrt; Wed, 15 Mar 2023 22:49:51 -0700 (PDT) X-Google-Smtp-Source: AK7set8LNPI8ftTcBaSjwOCzVvrOYkmX1zt6b7o2IdITY7WhrozZ+3qinEnmDXakoSQx5pBnLoDn X-Received: by 2002:a17:90b:1bc2:b0:23d:32f2:1a43 with SMTP id oa2-20020a17090b1bc200b0023d32f21a43mr2637924pjb.4.1678945791284; Wed, 15 Mar 2023 22:49:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678945791; cv=none; d=google.com; s=arc-20160816; b=Zm4WTk6VohmK93CjcNa+GrCcWuO/sg8Lsg3FSNLGyE4kgYnfqrk3IpkclC0LvrINgu ioC/zGkA7NaxMt8a540N9Q1pb7JNoSvx42c3Vfk3ZJ1yjJyPlP/jHTT8INqzZypfSmPo 3q89VswteXpFksB0Wgpu0jaDG/mAYbAXuG60Z040qyWgiKEAums3u1gCgImBIu95U+ro Y2TAufrhNw+QeLj/VlyQ7tttSKsR4lKKFNtca3sXxLoTDPm4o20/GER4DSDYULb4R3Cw kVtOCBtmzzZIzPWi3TOzjBDWeDHLO0/3OmcOUQl8sC4u5yWzM0m3+HfTU06BAvBOrbDF Pazw== 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=COVuWFTqsFDzmP+IfTDCNYujNeKhFKra6i1npZy/ZDSC683Ktav88bB/vFDIqf8b5O Xa+Bar4Sukw54ItMzKCfLO2qO/KAjlGU/gKBibXdzZ84amXYMj6b36QNeOjAP4/DagED n1zHE8MRWfv+4OIbolz1JEowIKAsgIZxcXwzpnW4SgRoCAWX4FZ0TZm5+FOIUKAXkfT9 JGfcRxf5ljChQ9DwIDI1SURn3xVvpZQGra9vPhCzsNbEo+n76OFHOktCRtt6BTzpTMeb BSDirhtrdsG2CIP7Eb+rj/f0yz5rhB74vrGb8QZU4GKtVIJ/Q8RkUiXYE1ZZDSZanu+k FYag== 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 c36-20020a630d24000000b004fd2171917asi6808508pgl.350.2023.03.15.22.49.34; Wed, 15 Mar 2023 22:49:51 -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 S229820AbjCPFlE (ORCPT + 99 others); Thu, 16 Mar 2023 01:41:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229823AbjCPFkp (ORCPT ); Thu, 16 Mar 2023 01:40:45 -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 640BD6A41F; Wed, 15 Mar 2023 22:40:43 -0700 (PDT) Received: by mail-qt1-f179.google.com with SMTP id l13so651333qtv.3; Wed, 15 Mar 2023 22:40:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678945242; 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=UPe4MoEo5xB90E7RYxvwYRMo9UawZ2jOG35bbNgk6IW3u5h+eHIewAzFJ9VWTsIZtK 2ZdGdOcfEO4jzcCFLxPuZYLebUFQONfaNIifNYKk7p/8XVOmTA/zk/rsvxEAlCEmNxXO RD2AIF9soECGakncnUjj+zVOfYyGzF4VytBqcFXXVmK7kORMg3wqR1ENn+PvYbTtuxEi rwnJL6oQlMJNUH5JilYC0WrSDamZbn7+1gaC/MlroAcE4vMyWPa5pWIjtzQ7uWVUUVPL tPc8wJtLrEmAfj3gDYm2MtTf4p7EklNrcsiGafw+8/i+O5WOjkfAOMdh1fMA4pPy95Q6 U/fA== X-Gm-Message-State: AO0yUKXqh4tvvM0Rj1e6tcynDrv3/EdCHEU+4S8Xyf2/h8OkRu+VZfFb 1LUyrPkev6sJqz5e4CqTEPh5Xc/yaON/0MwC X-Received: by 2002:ac8:58c2:0:b0:3bf:b6ba:1c1f with SMTP id u2-20020ac858c2000000b003bfb6ba1c1fmr4747926qta.10.1678945242097; Wed, 15 Mar 2023 22:40:42 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:7f57]) by smtp.gmail.com with ESMTPSA id q8-20020a374308000000b0074589d41342sm5152303qka.17.2023.03.15.22.40.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 22:40:41 -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@fb.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 v2 5/5] bpf,docs: Remove bpf_cpumask_kptr_get() from documentation Date: Thu, 16 Mar 2023 00:40:28 -0500 Message-Id: <20230316054028.88924-6-void@manifault.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230316054028.88924-1-void@manifault.com> References: <20230316054028.88924-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?1760502261955878213?= X-GMAIL-MSGID: =?utf-8?q?1760502261955878213?= 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; }