From patchwork Thu Dec 7 21:08:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Vernet X-Patchwork-Id: 175379 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5064745vqy; Thu, 7 Dec 2023 13:09:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IGwTet5/j4s2H5K8udm0z4N/KU34iwXqVzAzGzIUFQC4EfPklMduwp9l+XPst8nVbLWvmk/ X-Received: by 2002:a17:90b:3781:b0:28a:93d:8158 with SMTP id mz1-20020a17090b378100b0028a093d8158mr1174804pjb.48.1701983349697; Thu, 07 Dec 2023 13:09:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701983349; cv=none; d=google.com; s=arc-20160816; b=BSUESMWRQgzKDQZcZgT1j5hUY8YyZujCjyauvWvwfFU2u6Qx8bYe3d6HEh3JlvQVEH WJiExIdAD00vxWUNB6ULYKGa4foy2ifmkOeFEl6jz6mEjhLPDBnISZwWZrhgezytwBkQ vjhcXNB29EFPqHsT0gRl+tPbSwIhNyTYWjiuOBdMei7pRJoHQhF4LtCkUgDI5gOaz/fK lpE3j87fSP+md++/bSp907gWCChvBCSg9CCKuBgnFSv8zvGNRkzBq+YD3GRMUmZSdbQh +RK66V5M5/avt/IywJBr3InbIXcaAerGlzM7Qq64TWIwB9sxcMV5yUMB/sM4W9z529I9 8PRw== 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=/mJOqg3+xAvM9f6KK768X58MK+6lOJSs4ZpbCqNq8kc=; fh=fRduVGGcbGNLOU4DUo/BPZE9kW2AdZyQZjfnRZNvspU=; b=lOOedBM7+tA2W24Fftgd4ghT7/nmp434R7qYsMhKUmrVJwt/+auUl9fw7cRtlLNPba gGEqh9AVx6O5tOriKOouEHT62JE0EN5TiJMhMU8c/KARvB0in9dEfFPNAY8HSiB7XO9X tNkZHpQ75hf6ccNTXX8nXGEsUUgZaxnH4pmOMfd41sr+MuPSiZ/QBoqgwfcS6aN0B0Ny 2iMMpXYVGtMbwTdc3l4fPfg/v9HRDADS+3I74ygnMrRVVBqC9nMs0keg6PcIEo8uYO7G ZQb5df0PzYWdBUppOgQLwQx5htu8M10RH9Uv3nfZJaBEJGOGXqE7asLIu8o5R9HOxY5P 8OGw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id e11-20020a17090301cb00b001d0b6d1d463si343902plh.351.2023.12.07.13.09.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 13:09:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 67FE580CF551; Thu, 7 Dec 2023 13:09:08 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443959AbjLGVI4 (ORCPT + 99 others); Thu, 7 Dec 2023 16:08:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235229AbjLGVIv (ORCPT ); Thu, 7 Dec 2023 16:08:51 -0500 Received: from mail-io1-f47.google.com (mail-io1-f47.google.com [209.85.166.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3F5A1719; Thu, 7 Dec 2023 13:08:57 -0800 (PST) Received: by mail-io1-f47.google.com with SMTP id ca18e2360f4ac-7b37405f64aso44411539f.2; Thu, 07 Dec 2023 13:08:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701983337; x=1702588137; 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=/mJOqg3+xAvM9f6KK768X58MK+6lOJSs4ZpbCqNq8kc=; b=bOfTXr150LjyXg/4wfdc/W2o6Ie9+NcNwwh6royulFcBM6akVz0kEjm2gukDoFWdJE Q76g7l/sYBWIEedjuN6ayeXCMekjr6E/KvvCYzhUoQthfLCWGWsGHolqhhnZnb5/Cuvw R13dF1RcIVNIN/pzZD0nsAhJ3Iu90u2ia7OereI8p+abhVQEoLw5dyL/6+QwRMXIT9kw Ccp7d5agY9buk4RClVLJSlZitEo+inErmiIINzyw+I52+MOHLwGzoeuEwxG9KVmiX+Ob ZtvGXnKAtL8HkkLs9d/jAAWZJQcGkAPzS73or5D+gQVeY5h7QQvpFphmUuxKFkdTyM3T btrw== X-Gm-Message-State: AOJu0YxtkIpsPfFoxapJKogv/G0FeiaM2XWCqYvDgxpx5VgWZMQcX1dE ugdUbu8ootfviNgvo82K+/QwTmo2tMUY1LFA X-Received: by 2002:a6b:f80f:0:b0:7b6:e765:1d17 with SMTP id o15-20020a6bf80f000000b007b6e7651d17mr2661506ioh.40.1701983336753; Thu, 07 Dec 2023 13:08:56 -0800 (PST) Received: from localhost (c-24-1-27-177.hsd1.il.comcast.net. [24.1.27.177]) by smtp.gmail.com with ESMTPSA id f12-20020a02cacc000000b0042b530d29c3sm119386jap.164.2023.12.07.13.08.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 13:08:56 -0800 (PST) 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, yonghong.song@linux.dev, 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/2] bpf: Add bpf_cpumask_weight() kfunc Date: Thu, 7 Dec 2023 15:08:42 -0600 Message-ID: <20231207210843.168466-2-void@manifault.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231207210843.168466-1-void@manifault.com> References: <20231207210843.168466-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_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 07 Dec 2023 13:09:08 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784658892794366991 X-GMAIL-MSGID: 1784658892794366991 It can be useful to query how many bits are set in a cpumask. For example, if you want to perform special logic for the last remaining core that's set in a mask. Let's therefore add a new bpf_cpumask_weight() kfunc which checks how many bits are set in a mask. Signed-off-by: David Vernet Acked-by: Yonghong Song --- Documentation/bpf/cpumasks.rst | 2 +- kernel/bpf/cpumask.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Documentation/bpf/cpumasks.rst b/Documentation/bpf/cpumasks.rst index a22b6ad105fb..b5d47a04da5d 100644 --- a/Documentation/bpf/cpumasks.rst +++ b/Documentation/bpf/cpumasks.rst @@ -352,7 +352,7 @@ can be used to query the contents of cpumasks. .. kernel-doc:: kernel/bpf/cpumask.c :identifiers: bpf_cpumask_first bpf_cpumask_first_zero bpf_cpumask_first_and - bpf_cpumask_test_cpu + bpf_cpumask_test_cpu bpf_cpumask_weight .. kernel-doc:: kernel/bpf/cpumask.c :identifiers: bpf_cpumask_equal bpf_cpumask_intersects bpf_cpumask_subset diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c index e01c741e54e7..7499b7d8c06f 100644 --- a/kernel/bpf/cpumask.c +++ b/kernel/bpf/cpumask.c @@ -405,6 +405,17 @@ __bpf_kfunc u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, return cpumask_any_and_distribute(src1, src2); } +/** + * bpf_cpumask_weight() - Return the number of bits in @cpumask. + * @cpumask: The cpumask being queried. + * + * Count the number of set bits in the given cpumask. + */ +__bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) +{ + return cpumask_weight(cpumask); +} + __bpf_kfunc_end_defs(); BTF_SET8_START(cpumask_kfunc_btf_ids) @@ -432,6 +443,7 @@ BTF_ID_FLAGS(func, bpf_cpumask_full, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_copy, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_any_distribute, KF_RCU) BTF_ID_FLAGS(func, bpf_cpumask_any_and_distribute, KF_RCU) +BTF_ID_FLAGS(func, bpf_cpumask_weight, KF_RCU) BTF_SET8_END(cpumask_kfunc_btf_ids) static const struct btf_kfunc_id_set cpumask_kfunc_set = {