From patchwork Tue Feb 6 08:14:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13546847 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13372129A65 for ; Tue, 6 Feb 2024 08:14:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207295; cv=none; b=gSd5LRKXbDykDtFRfnBlxtcjuo13Dls5xyJ03ajGIUP1VR+w/WMIhYvOoFb1v3++jc2+1u9dhRKIsUgOfmOwpk2qalFKOomDEWEJqjOFoCauCLh9Sc/DSiv2+u4vdw/f58sqFA987tGN8kdsXjfnTMoA+bSjH3xS0si2FYsE258= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207295; c=relaxed/simple; bh=lcbLJ1orewuBSkMenb5BgjRxDmygmGPKl6bxMmSn81w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EUf5hDwOgQX9HI5Ug/F5WoxXhYzgZk7S6t4xVfkBpYzPPkQSfr8iqXKMWavHidvjUhtN/n0SB/Ds/8CjnlGMbNeVGAPtJ2BrTLgB+CALa1Wq8vqEq79cxOAxYoWq6yHnCSdo5GcdFYtfdy46mVnM3LrcV42STx3mm5oVFzLyHAc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JOHjQHk8; arc=none smtp.client-ip=209.85.215.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JOHjQHk8" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-5cf2d73a183so238830a12.1 for ; Tue, 06 Feb 2024 00:14:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707207293; x=1707812093; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fOTZXeoZhfUUL3cXZa8MjKcdeM1tbf5usBwd0h2B3GE=; b=JOHjQHk8BDb2xVYxCJDlTkHbm2607Y7O8eyVGb97Gc7seDvqWu2rH54CGlhNA9DY0c ygqBs+tmwq/9KXyyTXk+gYtnOdu6dkcXl8cA2wAFTf4tKjaHjEYw7iepKc6B2bm1uEM3 7uWfSPYALUWQqETiS/RWmqlMNMPx0g9k5EXymvSAtkJcit3FdUgtajaE+F+sxS9eZTeT EJd7rWyKxBDDnX6+LTSHCOU6K+DN+XXy3uFPK0eB5DqdRcSkBxd9z1a0QII3Iw/8aqsW OHo2NZuStKGKgn8yBwiOWpI14fsegwxkoSUa9wl8Vea3iIEG21CZuxl3MM/p1LZUNXcz YD0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707207293; x=1707812093; 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=fOTZXeoZhfUUL3cXZa8MjKcdeM1tbf5usBwd0h2B3GE=; b=wC7B+cvoT4CqB76V40qMHiuZEbXsFCrmR4Zmne2cEBlP5UUll3Sbi59wP/fUoxxWZX rfJcHal1ooxm9M8yxZpcnMvqrzlu38rXVEiOdfW5uqeH4QuKIjaGzGr06XXw5jTdY1/X lY6mw5G/2VT6ebuWKbaly6QU15NdgRjc9FaTiNomBDZMrHlDJhdQ602xhQLl6n7HM8nC NMDHlCCQLlcpyRNzDU1TBQtlwUifv4QAQ6t4kjszij6fie5+2y3kTAvDzJWTaJbYn7Rk v6EsKCjmcdSC6whUXgiwElG5490N7zntffdWHoDTQjLxiprWg4AadnXD0r2OqRTnZdVp wQzA== X-Gm-Message-State: AOJu0Yw1wfgrGOFh0W8VeWTZrP65MYSR27v9CLW73gIzxkJTQgmK8FzW YAikV8IHBbVdiNxOTTwqtUVmlyVuCnSA6FtAnLGVKCdH9jIFV7Cc X-Google-Smtp-Source: AGHT+IEh2U0ykbLU/g3mVA2Aocy8z6lycqpxFBQsC3hHexq5xoLoJnoA9dtGWodmR5EoslTqROOQJA== X-Received: by 2002:a05:6a20:3c89:b0:19e:858d:c677 with SMTP id b9-20020a056a203c8900b0019e858dc677mr975193pzj.31.1707207293231; Tue, 06 Feb 2024 00:14:53 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVjM6bphcvtEAe8WJX+xkWZx2xhx6czmIpK6UUSDBLaSlRCvEV7PsWU//a4mre+DilOFztuCypGt4vM3dKlDhHLH04le1q+7Psc1kwvpaYBo/4JkCUNSQp1x+n19ocfPHiUvqgsmnGkMPgABZ/R2pQO+PuGx0sK25ZpfKm7uaUErZZ7vHbqFFpnLdfEYbhpeoSBB0UW4edDp9zKvPz6I4vSEZyFTELgIXECHR9hU0rmosFWtUu4aW3osGnY6TmOAHAc1jeQpGDT6TpCv2AfQiJyuFzKrft7+Qw4isfKP3wEqLl0GSp2AFUrhJz5MTltE5Mr4G2pYo3tHtToZWDtLIZfwSrS+08CuZcvNqMVPyIEwPEk+mTiUkXTzL2eJFcTqT+ppsPrIT0couENKG+XnrV3cA7dMPsTaCR6ZI4mB+QvBClqqyCoFg== Received: from localhost.localdomain ([39.144.105.129]) by smtp.gmail.com with ESMTPSA id 3-20020a630c43000000b005d7c02994c4sm1381660pgm.60.2024.02.06.00.14.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Feb 2024 00:14:52 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, void@manifault.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH v6 bpf-next 1/5] bpf: Add bpf_iter_cpumask kfuncs Date: Tue, 6 Feb 2024 16:14:12 +0800 Message-Id: <20240206081416.26242-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240206081416.26242-1-laoar.shao@gmail.com> References: <20240206081416.26242-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add three new kfuncs for bpf_iter_cpumask. - bpf_iter_cpumask_new KF_RCU is defined because the cpumask must be a RCU trusted pointer such as task->cpus_ptr. - bpf_iter_cpumask_next - bpf_iter_cpumask_destroy These new kfuncs facilitate the iteration of percpu data, such as runqueues, psi_cgroup_cpu, and more. Signed-off-by: Yafang Shao --- kernel/bpf/cpumask.c | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/kernel/bpf/cpumask.c b/kernel/bpf/cpumask.c index dad0fb1c8e87..ed6078cfa40e 100644 --- a/kernel/bpf/cpumask.c +++ b/kernel/bpf/cpumask.c @@ -422,6 +422,82 @@ __bpf_kfunc u32 bpf_cpumask_weight(const struct cpumask *cpumask) return cpumask_weight(cpumask); } +struct bpf_iter_cpumask { + __u64 __opaque[2]; +} __aligned(8); + +struct bpf_iter_cpumask_kern { + struct cpumask *mask; + int cpu; +} __aligned(8); + +/** + * bpf_iter_cpumask_new() - Initialize a new CPU mask iterator for a given CPU mask + * @it: The new bpf_iter_cpumask to be created. + * @mask: The cpumask to be iterated over. + * + * This function initializes a new bpf_iter_cpumask structure for iterating over + * the specified CPU mask. It assigns the provided cpumask to the newly created + * bpf_iter_cpumask @it for subsequent iteration operations. + * + * On success, 0 is returned. On failure, ERR is returned. + */ +__bpf_kfunc int bpf_iter_cpumask_new(struct bpf_iter_cpumask *it, const struct cpumask *mask) +{ + struct bpf_iter_cpumask_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_cpumask_kern) > sizeof(struct bpf_iter_cpumask)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_cpumask_kern) != + __alignof__(struct bpf_iter_cpumask)); + + kit->mask = bpf_mem_alloc(&bpf_global_ma, cpumask_size()); + if (!kit->mask) + return -ENOMEM; + + cpumask_copy(kit->mask, mask); + kit->cpu = -1; + return 0; +} + +/** + * bpf_iter_cpumask_next() - Get the next CPU in a bpf_iter_cpumask + * @it: The bpf_iter_cpumask + * + * This function returns a pointer to a number representing the ID of the + * next CPU in CPU mask. It allows sequential access to CPUs within the + * cpumask. If there are no further CPUs available, it returns NULL. + */ +__bpf_kfunc int *bpf_iter_cpumask_next(struct bpf_iter_cpumask *it) +{ + struct bpf_iter_cpumask_kern *kit = (void *)it; + const struct cpumask *mask = kit->mask; + int cpu; + + if (!mask) + return NULL; + cpu = cpumask_next(kit->cpu, mask); + if (cpu >= nr_cpu_ids) + return NULL; + + kit->cpu = cpu; + return &kit->cpu; +} + +/** + * bpf_iter_cpumask_destroy() - Destroy a bpf_iter_cpumask + * @it: The bpf_iter_cpumask to be destroyed. + * + * Destroy the resource associated with the bpf_iter_cpumask. + */ +__bpf_kfunc void bpf_iter_cpumask_destroy(struct bpf_iter_cpumask *it) +{ + struct bpf_iter_cpumask_kern *kit = (void *)it; + + if (!kit->mask) + return; + bpf_mem_free(&bpf_global_ma, kit->mask); +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(cpumask_kfunc_btf_ids) @@ -450,6 +526,9 @@ 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_ID_FLAGS(func, bpf_iter_cpumask_new, KF_ITER_NEW | KF_RCU) +BTF_ID_FLAGS(func, bpf_iter_cpumask_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_cpumask_destroy, KF_ITER_DESTROY) BTF_KFUNCS_END(cpumask_kfunc_btf_ids) static const struct btf_kfunc_id_set cpumask_kfunc_set = { From patchwork Tue Feb 6 08:14:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13546848 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oo1-f51.google.com (mail-oo1-f51.google.com [209.85.161.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35B7C129A65 for ; Tue, 6 Feb 2024 08:15:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207308; cv=none; b=YFYsojANf246Vtm9HYm608F45RV89tb+6+BLvJ8I6iPMWMUtSpuNy4di+/u/3e5PDm44SEJ/bk3isS2PPwW//3Y0vXM1ZFkrl/wND91fRmh5Ql6helYQQdDIe7dd7jWbo/6A/TFVBeDoyVCwei29vprwQyFuR/uodje85HFSUgE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207308; c=relaxed/simple; bh=N2+OfDASSUkYC4v52Y0rIqaWTwox1VG+qGu5cFGHnN0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DKE4r2vnAjrLCKkHXuq5vZ/NQa0Mw6hUr5JFNfowJp81/q88Kuhvf65sI5rNxU5stBI7+5RUNvRRyareTHwWK2MWW+orhYLd2J5MVHCLiOkCewbedSt27yhXRyQYzaOqNX32pl1h1pSpZXCvfhWOZqeXTYTu7F+GLE2kCftwCvc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aan7Sb2f; arc=none smtp.client-ip=209.85.161.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aan7Sb2f" Received: by mail-oo1-f51.google.com with SMTP id 006d021491bc7-5961a2726aaso3105368eaf.0 for ; Tue, 06 Feb 2024 00:15:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707207306; x=1707812106; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iE0q5tBiZvTz92JBreV2ZnEqPmGuVdzUjDYXOxGofSw=; b=aan7Sb2feA3nLudhNGHtO9CurkuayYVAgquFxbqBAwzB9bBS28A2TSi8gtSPEvKf9n yK+WZdLx6EqF4stRFTSPbL6g6XvFxoMdtTCSNRYiQDMrr3o8wsPD3IUE4kjw8I3j4ZmO PeasGiFybrddH00ksXzeQ/7cGvZBfXtBAxk71IgHarKlDAIyo0BBo4YwcpKm0C/Fbuf4 6nR8v+cu5QNDcW9kY3x7nKqnfbbChl+BlepCPNQAhzZ6ISrqy7O3IGGQTBtckfAEMqIW cDjDvLKw9ImtIbdzcXdEvaXNKYITW4j0jKxw/leJORKmK8EuDiJ+jhtqAUpgO5U1oyrc gORw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707207306; x=1707812106; 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=iE0q5tBiZvTz92JBreV2ZnEqPmGuVdzUjDYXOxGofSw=; b=wy2h529QLQiF+Y23/ymnjQSPsH1nB4/qMg6PyilTEKmNxvzNxOKU+z2Kc+EQwTo0r3 yUH+5EMqhJ/9LOocxYPiuVstRAC5iq0rpskestWWRcurk2xr7hcZwI+/R6DheeUUAz7i 1htSYtbj8MiMu9HyGx3tEd7UaXpPATby6tPtUzJ6c/rgHs6kCwbOexw+vqRoEd6g5UCT Bm/rjPaQEM3JhKUG3xI3bAFMp6neiWviTCFWSwOkLqNMVeerMMkPmSKrWRY5PS5Se80C dedx8qp5o/KZuYWWmth5t9qna3t2F9XJ2YmkS2C6W6tGno1qfTCdAIvD3qtC1+pYe/zM JlaQ== X-Gm-Message-State: AOJu0YwdOK6YDkLuAdUfELoT6JxnA5pVcqqJZV5zYDK/bfQOQbhMXRC6 bMDWiHMWZbH3ibl67FNN3RXNnnV7hk0ag0pdyM840Sjvu3WSqY6p X-Google-Smtp-Source: AGHT+IFDHS2TMqmI4fRgXhv1ncvNpqWR7f0pNyN5K76Rs8hqvczo2qJ7wJh6IW+Z4sqPtUy4uSVnCg== X-Received: by 2002:a05:6870:e30f:b0:219:4426:53ca with SMTP id z15-20020a056870e30f00b00219442653camr1763556oad.4.1707207306093; Tue, 06 Feb 2024 00:15:06 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCXixA90J/n5xdB7Ldmg8k/tzBeVu4WMKpsL6by2O/XGj1Z5Tain3D/Bjc+C2faslMtcNJim0ciNYCdCRE9kwyc6Vo5doCrepJUEPhh42zLWrn4l4z2Esgy9VjM3pTxXPb8SX8KCg202wMYqXC/hwEzBgYf1kFD/6QnjwqPHKB8cTlIp/vLC3Lyc2V34Vpz4wuCELG9p510Lb8iAfCkYGfXf5071SOj3HkVvkNwFCAZAFEszTSD6YU133+SyHGMX/yVXYIJNb72wq8zGLk6yvZVJ+TQxQESNHRh7UPjrowcUkoWfXKnaM9OOIdv2sSIJYkZZNfhDggFK9mTh8rN65D9PQ7wjoJKEC6baU2YR0vn9C5s+VmqYf4S31ryBVIuD5JlWbj2m2Ijoedr0rRnaBgMy82ow6DYbg2WoybXfa5PXRd51pkz05A== Received: from localhost.localdomain ([39.144.105.129]) by smtp.gmail.com with ESMTPSA id 3-20020a630c43000000b005d7c02994c4sm1381660pgm.60.2024.02.06.00.14.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Feb 2024 00:15:05 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, void@manifault.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH v6 bpf-next 2/5] bpf, docs: Add document for cpumask iter Date: Tue, 6 Feb 2024 16:14:13 +0800 Message-Id: <20240206081416.26242-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240206081416.26242-1-laoar.shao@gmail.com> References: <20240206081416.26242-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds the document for the newly added cpumask iterator kfuncs. Signed-off-by: Yafang Shao Acked-by: Andrii Nakryiko --- Documentation/bpf/cpumasks.rst | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/Documentation/bpf/cpumasks.rst b/Documentation/bpf/cpumasks.rst index b5d47a04da5d..5cedd719874c 100644 --- a/Documentation/bpf/cpumasks.rst +++ b/Documentation/bpf/cpumasks.rst @@ -372,6 +372,66 @@ used. .. _tools/testing/selftests/bpf/progs/cpumask_success.c: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/cpumask_success.c +3.3 cpumask iterator +-------------------- + +The cpumask iterator facilitates the iteration of per-CPU data, including +runqueues, system_group_pcpu, and other such structures. To leverage the cpumask +iterator, one can employ the bpf_for_each() macro. + +Here's an example illustrating how to determine the number of running tasks on +each CPU. + +.. code-block:: c + + /** + * Here's an example demonstrating the functionality of the cpumask iterator. + * We retrieve the cpumask associated with the specified pid, iterate through + * its elements, and ultimately expose per-CPU data to userspace through a + * seq file. + */ + const struct rq runqueues __ksym __weak; + u32 target_pid; + + SEC("iter/cgroup") + int BPF_PROG(cpu_cgroup, struct bpf_iter_meta *meta, struct cgroup *cgrp) + { + u32 nr_running = 0, nr_cpus = 0, nr_null_rq = 0; + struct task_struct *p; + struct rq *rq; + int *cpu; + + /* epilogue */ + if (cgrp == NULL) + return 0; + + p = bpf_task_from_pid(target_pid); + if (!p) + return 1; + + BPF_SEQ_PRINTF(meta->seq, "%4s %s\n", "CPU", "nr_running"); + bpf_for_each(cpumask, cpu, p->cpus_ptr) { + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, *cpu); + if (!rq) { + nr_null_rq += 1; + continue; + } + nr_cpus += 1; + + if (!rq->nr_running) + continue; + + nr_running += rq->nr_running; + BPF_SEQ_PRINTF(meta->seq, "%4u %u\n", *cpu, rq->nr_running); + } + BPF_SEQ_PRINTF(meta->seq, "Summary: nr_cpus %u, nr_running %u, nr_null_rq %u\n", + nr_cpus, nr_running, nr_null_rq); + + bpf_task_release(p); + return 0; + } + +---- 4. Adding BPF cpumask kfuncs ============================ From patchwork Tue Feb 6 08:14:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13546849 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41BAF12AACA for ; Tue, 6 Feb 2024 08:15:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207322; cv=none; b=s7yIfJ9kWDrUzD+TNwLncW1L7SB7nmHggNO+jKwx4FqT+J9YB9V7G3D78I8jgYjLPPWL/PmiMJzEV0QYtoAB7wfkxJgnejraUADBzLlMFeHCyhSrKPfclzzU3YXpMtfLksbUDZ0CpjaNnXsF8tcBJpFH0NpA/FBX2aFJ9RzPGDs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207322; c=relaxed/simple; bh=yTw3iMbpiHkoMOAEjLkvxKwb0/tYyK2no14G78laVzE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qHG3EsxHlou5fwo3n8FzyDWDRnuQYSvNWcjy71Ny6Z3X2NO4FdsAr6T3bhwxQ2fx7AX/ZXVANuq/Khq6Njvc4UVA033nBVy8fP3ij/evfPM8jOjpUm2ePNpa5utOUT+BmkPYEw7/YOI8KEXGgOJXL/ZtTKb7SFAFl1McaeXPwpI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WUspYL0O; arc=none smtp.client-ip=209.85.161.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WUspYL0O" Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-595d24ad466so3304237eaf.0 for ; Tue, 06 Feb 2024 00:15:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707207320; x=1707812120; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+y3dhJcZNIfgObUe6ZQKYjQoS0Kyd1B/l8QOAn5+S30=; b=WUspYL0OPkT39JaLCEXX0MMO6DEvv8d3VvO95fE1ZrgHLVbPg0oAouOM+Yp/Ug8pmY pogNcV5VC9CmP5sFh0My/na5ErSZder8T7F4j4l+V/t/qr+5w7ogpTpP6Cx1AhFpf17Z x4eyjKXdo5hYs7de8A0qDJUBAZrXSkELDIY35GBEdTumSETi2MJzknsIjqcZ/vxqmtTi WCBHWMYFu/FFv8At/QNJLC4jmbLh+0POjJWBMh68K8Fj5ima7LpIbNBQHkL9dujlr/Li u4H7xE+UK8q/dUza9VgNq2SE6oM92dE1C67xgn7vewct2YTffr5nQKwqzjrTXckZ9pot 6VaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707207320; x=1707812120; 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=+y3dhJcZNIfgObUe6ZQKYjQoS0Kyd1B/l8QOAn5+S30=; b=qdHl4NJBGMt+qdqJXuccA2bIyx+a2RDoszrvof9QucXGXcMrS0DYOKjciqwjHl0Xrj uQKQ4aqcxoDlEj8SGEmE1S/s3t/+gw/Jqd+FdxuS9GQahBPhzJ1NXA7mcjLodHbbTvEa F+YAdJS/jTCEzFtbEgtI2HG4SLviuLWHNiDdOCoMNRLYo1JtM+MNOTbhUDoGptRdURaM 7MPrwwZxFkh9VK2KcsAhRcbBUcn6YXiIkzn55tUjYGh32lzi89XUdYr5S8cKuWRYNQ9S ytf/LmeB7BmpFYwlQyx2nSvrDCGvotoL3QjWuQ6mkC4xpS6MciKLgL+CJhywJfL0leoc X3kA== X-Gm-Message-State: AOJu0Yyc82EKC+eLagw+whbxzRD3Xu3558qUa1KVLbsl6qk/oMN14UZL +x4BGwYWayZsSzmKss6uClDnLSfpCW3HyYct7amkmg4BmU7H5tAD X-Google-Smtp-Source: AGHT+IHfXwI4Wyn2EETk/tpzhQdCv5JkPNi51ogk2cMn+LWheLgWxkCz1LKZNR4SCD9TS2HXaIgDrA== X-Received: by 2002:a05:6358:714:b0:178:b97c:f087 with SMTP id e20-20020a056358071400b00178b97cf087mr1669667rwj.15.1707207320200; Tue, 06 Feb 2024 00:15:20 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWaDjS5pEFFxqp6vaxIA69gUTraJWP2/lZDjxZyru8ZpxMoa8bzAZAOgPMOkVNF7NA87JMoLjjqQhL2ETD4KWj1txn6vrhwG9ngUqn2ndrGSll6C/agWmztsISEe7fKQtOGKLgTPCNp/xuj+rvqI9NdXbYwnFUkPPmnAwX5BeHlKUTxMlnU6iLV/YWQjjLDRQ4tkfFi2TU5Ryl5Q1LOy5YaKUqTm102inX1JmpCU8Bua73ZNmHlB9pfbvO5wF3IBJOLClZ/o74vDt3/G/QMXGs14Ab4bQo0HHWiVsO1CPXfvEUxOWyqRh+m+REfsr+NNAZlhgWgNscy4291vxHeEsE5oNOVxghOb83NI5OpoP0th2Z1DfG6V5BzjyiSRJ8hZlUSXmjLQqBxsFGfjLe58VZ117xKfjT2ySsRn1SWs//hovKOnbC09Q== Received: from localhost.localdomain ([39.144.105.129]) by smtp.gmail.com with ESMTPSA id 3-20020a630c43000000b005d7c02994c4sm1381660pgm.60.2024.02.06.00.15.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Feb 2024 00:15:19 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, void@manifault.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH v6 bpf-next 3/5] selftests/bpf: Fix error checking for cpumask_success__load() Date: Tue, 6 Feb 2024 16:14:14 +0800 Message-Id: <20240206081416.26242-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240206081416.26242-1-laoar.shao@gmail.com> References: <20240206081416.26242-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net We should verify the return value of cpumask_success__load(). Signed-off-by: Yafang Shao Acked-by: Andrii Nakryiko Cc: David Vernet --- tools/testing/selftests/bpf/prog_tests/cpumask.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/cpumask.c b/tools/testing/selftests/bpf/prog_tests/cpumask.c index c2e886399e3c..ecf89df78109 100644 --- a/tools/testing/selftests/bpf/prog_tests/cpumask.c +++ b/tools/testing/selftests/bpf/prog_tests/cpumask.c @@ -27,7 +27,7 @@ static void verify_success(const char *prog_name) struct bpf_program *prog; struct bpf_link *link = NULL; pid_t child_pid; - int status; + int status, err; skel = cpumask_success__open(); if (!ASSERT_OK_PTR(skel, "cpumask_success__open")) @@ -36,8 +36,8 @@ static void verify_success(const char *prog_name) skel->bss->pid = getpid(); skel->bss->nr_cpus = libbpf_num_possible_cpus(); - cpumask_success__load(skel); - if (!ASSERT_OK_PTR(skel, "cpumask_success__load")) + err = cpumask_success__load(skel); + if (!ASSERT_OK(err, "cpumask_success__load")) goto cleanup; prog = bpf_object__find_program_by_name(skel->obj, prog_name); From patchwork Tue Feb 6 08:14:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13546850 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EFDD12AAD6 for ; Tue, 6 Feb 2024 08:15:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207337; cv=none; b=bnmyg4be6oyArcF7sTzFYzRxE4Ua3mdb176Mr+Rg6oVMbLzmwrRH6P8ItsrMAcMgsfG9aBPlURQusOPEsAh5XZjwexln4mGympT3on4kfWlZVb3OWk2YIivgn220DWAgY3INnfkFfewhkd1444FE+z6FJUUt0O5h50PQQ3g9Xr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207337; c=relaxed/simple; bh=p5YFwt0lznO99LnlPO06tkVy3LwCT7E4VX2YIZDl7io=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cSeC9w8DMctHNXtJErmiBAsWqIK8BmVEHAcjIKz7rrEo/YtSNUor0jhWdFGxWT5cCuLhKCTMLdU+Gh5OC4AZ7bvzZQI7RfNep+zHNH7dWu+b8Am1w6dGN2yd6JbIHTvMGt3P3Cm/AZsrEUDT42i3oY++9iJH+dF6CrnH8ZpCrg0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BHyU+/AV; arc=none smtp.client-ip=209.85.160.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BHyU+/AV" Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-2191dc7079aso2045774fac.0 for ; Tue, 06 Feb 2024 00:15:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707207335; x=1707812135; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QL9q0Nn+JwZ83eKfz6uVxGQth1YMygFDcsBkZ2eKrxg=; b=BHyU+/AVD4OYOspQ60auP3xOpRa5+4G8N34SM+WEoLIDLUY+NrQpYimpSfvitw5xhh YwUwoCP9s85XOm4TfQWrYITYwYhlHkRYC4s++/F99xVpSPZqLzG5VWc9PqaI0SRHlQ59 aF6uwqTLYKWKsN2Vp5WMdJkHAG/Vpa4oG4jsRDdIiBztmJZAgUmwMUTPaJRq9GhM5TEa c9KHHEZQI7+Hz4OCzpxPa5vQNKc1Ptut3Z/bzY3ji2HKs1qvy8QD+cQCgN4c1MalCsnk RxXSPOZIujmGLFwoqOgknSMsSvH+wWc3tFPgX99e6XKD/7nmf3+JBOOtUIiePnjsIAJ6 ZaUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707207335; x=1707812135; 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=QL9q0Nn+JwZ83eKfz6uVxGQth1YMygFDcsBkZ2eKrxg=; b=QSld6BdBxYYpD1x6y0Yi1SNNiyLsxmj98KxFkpypGFblvGm6YHcew4F4Z8Y9Cuyayn vhG13pfVvS+kj+O7SNpHBcBAWZx6LE0lb1+NrD5Nmr4Yxj99HEIpVLW1jjd9f7gQIjm3 Ar4m0qru7+dqamaTlJTP/H5Ab2sCxuOTzn/SRnmM7iy5byfgdWSKOxnDmpwQHBbab4jb 8dgWXRwbozQwXoFFREb706Gbc2i58ycBYINghLPujS+Zfncwzlaqkq0gpQL+o1uGcnYg esiZzAgw1AERc/zuw/kdv48zY3PdCIieeKkNefdvQG9g3UKzjnGWMORuLc0rVdO9dmnw WPTw== X-Gm-Message-State: AOJu0YydxKg15aaMisIIabBwjQNlZHHSjPi/8+qYJ9vFzh/D7McVTr86 /liH6Gsn67gwbmtEPaliBkRRebXLBP+DPlRoGjccQnYMp18OmnNflrzaaw6pFfNcNBNH X-Google-Smtp-Source: AGHT+IEMS9uSc3To3FVB7L0i/D8fiaMIl8NGdBvSdD35j7TIc/83xDoOVxGlR55ML+vCi3+dyyotig== X-Received: by 2002:a05:6870:7e0b:b0:214:fcc4:e384 with SMTP id wx11-20020a0568707e0b00b00214fcc4e384mr1884751oab.15.1707207335000; Tue, 06 Feb 2024 00:15:35 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCU/mRHgyoiWYApAsfbCpTHDeesost37V0bsorPA+a4q1pIJdUsfd1FKgPOpk8Jb6clmqyXm1qw8p/RdJl0PBv7iyqS1DUL65uEZdGC1NvjHx7s2uKoi97/0qBINJ8oTE9RxlffPHKEda/wjbYv3HkMK4mA0OnUYWAHrLqSRd3duwSDuid9c83DLLhd6c54cZy3DsQDpidLKhcVUILRFu30Z/2PT5T73B99W6iKOgbGpC/TdXCUgA3JtUHalt21VW5D1oI8ocrn9zou8KPhJd4ywC2QHqEeAXQt1CnXwxq1ZtcSWm+OH8asJkJDMgZSYRI46/Fos098QLKII4FGjOU0yu+6JeS3b+aanaypc/e3zKPGmQEewq0TVB8Kb9q8tfzycL4wRGz+UlP4zmQYhHRlfC++aAzP2wpZe012CGvjcEKvj6imnup1YsrGUCqOE1SCyn++fwW9l7SFr8XZDVu3NTs6Jo9a4rw2NEPtJ/r0ucg== Received: from localhost.localdomain ([39.144.105.129]) by smtp.gmail.com with ESMTPSA id 3-20020a630c43000000b005d7c02994c4sm1381660pgm.60.2024.02.06.00.15.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Feb 2024 00:15:34 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, void@manifault.com Cc: bpf@vger.kernel.org, Yafang Shao , Andrii Nakryiko , Daniel Xu Subject: [PATCH v6 bpf-next 4/5] selftests/bpf: Mark cpumask kfunc declarations as __weak Date: Tue, 6 Feb 2024 16:14:15 +0800 Message-Id: <20240206081416.26242-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240206081416.26242-1-laoar.shao@gmail.com> References: <20240206081416.26242-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net After the series "Annotate kfuncs in .BTF_ids section"[0], kfuncs can be generated from bpftool. Let's mark the existing cpumask kfunc declarations __weak so they don't conflict with definitions that will eventually come from vmlinux.h. [0]. https://lore.kernel.org/all/cover.1706491398.git.dxu@dxuuu.xyz Suggested-by: Andrii Nakryiko Signed-off-by: Yafang Shao Cc: Daniel Xu Acked-by: Daniel Xu --- .../selftests/bpf/progs/cpumask_common.h | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/tools/testing/selftests/bpf/progs/cpumask_common.h b/tools/testing/selftests/bpf/progs/cpumask_common.h index 0cd4aebb97cf..c705d8112a35 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_common.h +++ b/tools/testing/selftests/bpf/progs/cpumask_common.h @@ -23,41 +23,42 @@ struct array_map { __uint(max_entries, 1); } __cpumask_map SEC(".maps"); -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; -u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym; -u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym; +struct bpf_cpumask *bpf_cpumask_create(void) __ksym __weak; +void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym __weak; +struct bpf_cpumask *bpf_cpumask_acquire(struct bpf_cpumask *cpumask) __ksym __weak; +u32 bpf_cpumask_first(const struct cpumask *cpumask) __ksym __weak; +u32 bpf_cpumask_first_zero(const struct cpumask *cpumask) __ksym __weak; u32 bpf_cpumask_first_and(const struct cpumask *src1, - const struct cpumask *src2) __ksym; -void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; -void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; -bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym; -bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; -bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym; -void bpf_cpumask_setall(struct bpf_cpumask *cpumask) __ksym; -void bpf_cpumask_clear(struct bpf_cpumask *cpumask) __ksym; + const struct cpumask *src2) __ksym __weak; +void bpf_cpumask_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak; +void bpf_cpumask_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak; +bool bpf_cpumask_test_cpu(u32 cpu, const struct cpumask *cpumask) __ksym __weak; +bool bpf_cpumask_test_and_set_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak; +bool bpf_cpumask_test_and_clear_cpu(u32 cpu, struct bpf_cpumask *cpumask) __ksym __weak; +void bpf_cpumask_setall(struct bpf_cpumask *cpumask) __ksym __weak; +void bpf_cpumask_clear(struct bpf_cpumask *cpumask) __ksym __weak; bool bpf_cpumask_and(struct bpf_cpumask *cpumask, const struct cpumask *src1, - const struct cpumask *src2) __ksym; + const struct cpumask *src2) __ksym __weak; void bpf_cpumask_or(struct bpf_cpumask *cpumask, const struct cpumask *src1, - const struct cpumask *src2) __ksym; + const struct cpumask *src2) __ksym __weak; void bpf_cpumask_xor(struct bpf_cpumask *cpumask, const struct cpumask *src1, - const struct cpumask *src2) __ksym; -bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2) __ksym; -bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2) __ksym; -bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2) __ksym; -bool bpf_cpumask_empty(const struct cpumask *cpumask) __ksym; -bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym; -void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym; -u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym; -u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, const struct cpumask *src2) __ksym; -u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym; - -void bpf_rcu_read_lock(void) __ksym; -void bpf_rcu_read_unlock(void) __ksym; + const struct cpumask *src2) __ksym __weak; +bool bpf_cpumask_equal(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak; +bool bpf_cpumask_intersects(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak; +bool bpf_cpumask_subset(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak; +bool bpf_cpumask_empty(const struct cpumask *cpumask) __ksym __weak; +bool bpf_cpumask_full(const struct cpumask *cpumask) __ksym __weak; +void bpf_cpumask_copy(struct bpf_cpumask *dst, const struct cpumask *src) __ksym __weak; +u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym __weak; +u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, + const struct cpumask *src2) __ksym __weak; +u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym __weak; + +void bpf_rcu_read_lock(void) __ksym __weak; +void bpf_rcu_read_unlock(void) __ksym __weak; static inline const struct cpumask *cast(struct bpf_cpumask *cpumask) { From patchwork Tue Feb 6 08:14:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13546851 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D80427C099 for ; Tue, 6 Feb 2024 08:15:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207350; cv=none; b=iuJ1imV8nlXDBAnTaadM5dFKXpMa1C0xyL5n3lUyPasX4WBkAL14mIjZEg/ea3XeU7TBt2WAdrwY8KJxH/u5bDQ10gFWfRWnuub2ww1KyYGQ3xh8EBpgcaxmejvCZahWMT+XpA5oylb53/uTp6t/aqvZmopXYE6JTQw0GdqDp3s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707207350; c=relaxed/simple; bh=8bflKmxMgoB7fH0nbK5bg1yqTEuUsx80TZVYDp+j3eQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VfQWF71izfP09+o5nVH5GGH9Ob1zlm0XRw06RuHRxL20Z2AvMOI9UqG97MUc2KESOExRMNRs7zVzLH4yS4jo60Atas5WexnMnll3SLsRjwmoeKjdEWLhRLbExNsSYI1Fzy+q+oCOVfYCZUFPfGjag4HBKUJ2BZJcBJyrlShH03w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=F/cuD3il; arc=none smtp.client-ip=209.85.210.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F/cuD3il" Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-6e117eec348so331778a34.2 for ; Tue, 06 Feb 2024 00:15:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707207348; x=1707812148; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=93sJpduZ+PfWWK0FIrMlFr/mZh1WpgsGF0yQ12B38Ng=; b=F/cuD3ildR74MovfksmZ3YJMJKNyr1BO6T1Si6JvPfey3dSpuMCQ6FYh1TtO6sEEbG 4g0Bf8V0U0YTgRw+ff3PwApNY7QDf385M5ygNXu+pNQMduqkZJ4HrFv1fAhHSc2GWJ75 dIEJuCubv+nQnuAgzPB6L2BFpqSjsn1xsRJJkwR5fBQvQnX715rEXYPFyOPqJp5qHkoq ZCB1XU+7orUP8Tm1KLu6vg/FXXPx3cVfGF5Yo0zpekH44s5/49CG8cQOkoDo6LGy/PI6 wG0O11SBZfetewAhEnXUEtSS603OGb41emQIh+6yGL4PO28LJYW88iBenCrk/sVEYuJy wZEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707207348; x=1707812148; 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=93sJpduZ+PfWWK0FIrMlFr/mZh1WpgsGF0yQ12B38Ng=; b=kVcxEkkoWqkS6dBr6/jpJaoP6JEGoZn4LUHGNS3T46lFywkek/9ga7mTuRxQJC/3Lw bOLlFcZTvk1Ac7j7EQEZ2P0LYQoUue9wU59dI2ZO5oTd5Sxl/VsXVssg67T5IGdt9X/7 uM67X4CYaZgga1VA1vJRWrzP44GI1ULqs3h1VoOl/ZXM5RrGhUJ3PuE6Ds4IMThrOvSU qkkh82kCE/i70BxJ2EhMTiVLa78IYeYrsEtAQw97aVeWlhHEyTUs9okqxCvvB6JqSMvk G+c8kMRsNA+HQOj6bMuupoJbKMbk/dss5kZ9G+iWoKMLhbbWfVzhSIeXEX/ixM8dkWM0 yBRg== X-Gm-Message-State: AOJu0YzBX0He2+ULe98hmyWpQe8BImkw6QNY6A+6Gxj8qsCmI3r/tGZl J9NBt9f+ShaWQ5Z+9ZPMAsexiH/iYfW/3KnjO71T2dxc7mIeDnae X-Google-Smtp-Source: AGHT+IEctNCCVoUWVJo1qGVmF9GzWnD6WYyrP5zdnUH7cZuOqzjMswUEJchc3uhBfRzTf6VfpY3Fng== X-Received: by 2002:a05:6830:185:b0:6de:6ad0:d34c with SMTP id q5-20020a056830018500b006de6ad0d34cmr2250118ota.9.1707207347667; Tue, 06 Feb 2024 00:15:47 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUF46L5VKx3mfnoMA9j94lBOUOjslGSAW+AGXZdSExLYZiQbqA6OJnQa6O/HghDsl7YvNMYUIiirofXhF1TOvorT3RCKY0jCB3h5zgJ1cWsTXiNF4KDCNpJF7IRPOlqaqjjuAwgPczUqWOpOkI68S0I2kRRSSy04uHgT0C08OYJ18NT4HNAdGjOUnszs0C5rYvAlr65r7btvySIFbZPe0PlttPibVUsFj2Ei+xI3BaEJ8mpvbA+HBd06uj8xiypSL3Hnnb3qxH2lYERJHQbPEuysGu6xkkGwulLGzqCUhQgDKTEoUicotDUT65Gty2JGzwb6YHhkHFli/sP5r0wO6CHhu9S9lu5YPOI0/ObdWAJYUwEXNCi8D1ggqfXWcogdei6vXx6uEtY1qqLmUbrR5aM8/epJVQSZ04c513f9e9GKkSpI+TIcA== Received: from localhost.localdomain ([39.144.105.129]) by smtp.gmail.com with ESMTPSA id 3-20020a630c43000000b005d7c02994c4sm1381660pgm.60.2024.02.06.00.15.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Feb 2024 00:15:47 -0800 (PST) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, void@manifault.com Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH v6 bpf-next 5/5] selftests/bpf: Add selftests for cpumask iter Date: Tue, 6 Feb 2024 16:14:16 +0800 Message-Id: <20240206081416.26242-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240206081416.26242-1-laoar.shao@gmail.com> References: <20240206081416.26242-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add selftests for the newly added cpumask iter. - cpumask_iter_success - The number of CPUs should be expected when iterating over the cpumask - percpu data extracted from the percpu struct should be expected - It can work in both non-sleepable and sleepable prog - RCU lock is only required by bpf_iter_cpumask_new() - It is fine without calling bpf_iter_cpumask_next() - cpumask_iter_failure - RCU lock is required in sleepable prog - The cpumask to be iterated over can't be NULL - bpf_iter_cpumask_destroy() is required after calling bpf_iter_cpumask_new() - bpf_iter_cpumask_destroy() can only destroy an initialized iter - bpf_iter_cpumask_next() must use an initialized iter The result as follows, #64/37 cpumask/test_cpumask_iter:OK #64/38 cpumask/test_cpumask_iter_sleepable:OK #64/39 cpumask/test_cpumask_iter_sleepable:OK #64/40 cpumask/test_cpumask_iter_next_no_rcu:OK #64/41 cpumask/test_cpumask_iter_no_next:OK #64/42 cpumask/test_cpumask_iter:OK #64/43 cpumask/test_cpumask_iter_no_rcu:OK #64/44 cpumask/test_cpumask_iter_no_destroy:OK #64/45 cpumask/test_cpumask_iter_null_pointer:OK #64/46 cpumask/test_cpumask_iter_next_uninit:OK #64/47 cpumask/test_cpumask_iter_destroy_uninit:OK #64 cpumask:OK Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/prog_tests/cpumask.c | 152 ++++++++++++++++++ .../selftests/bpf/progs/cpumask_common.h | 3 + .../bpf/progs/cpumask_iter_failure.c | 99 ++++++++++++ .../bpf/progs/cpumask_iter_success.c | 126 +++++++++++++++ 5 files changed, 381 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_failure.c create mode 100644 tools/testing/selftests/bpf/progs/cpumask_iter_success.c diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config index 01f241ea2c67..dd4b0935e35f 100644 --- a/tools/testing/selftests/bpf/config +++ b/tools/testing/selftests/bpf/config @@ -78,6 +78,7 @@ CONFIG_NF_CONNTRACK_MARK=y CONFIG_NF_DEFRAG_IPV4=y CONFIG_NF_DEFRAG_IPV6=y CONFIG_NF_NAT=y +CONFIG_PSI=y CONFIG_RC_CORE=y CONFIG_SECURITY=y CONFIG_SECURITYFS=y diff --git a/tools/testing/selftests/bpf/prog_tests/cpumask.c b/tools/testing/selftests/bpf/prog_tests/cpumask.c index ecf89df78109..78423745689c 100644 --- a/tools/testing/selftests/bpf/prog_tests/cpumask.c +++ b/tools/testing/selftests/bpf/prog_tests/cpumask.c @@ -1,9 +1,15 @@ // SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ +#define _GNU_SOURCE +#include + #include #include "cpumask_failure.skel.h" #include "cpumask_success.skel.h" +#include "cpumask_iter_success.skel.h" +#include "cpumask_iter_failure.skel.h" +#include "cgroup_helpers.h" static const char * const cpumask_success_testcases[] = { "test_alloc_free_cpumask", @@ -61,6 +67,142 @@ static void verify_success(const char *prog_name) cpumask_success__destroy(skel); } +static const char * const cpumask_iter_success_testcases[] = { + "test_cpumask_iter", + "test_cpumask_iter_sleepable", +}; + +static int read_percpu_data(struct bpf_link *link, int nr_cpu_exp, int nr_running_exp) +{ + int iter_fd, len, item, nr_running, psi_running, nr_cpus, err = -1; + char buf[128]; + size_t left; + char *p; + + iter_fd = bpf_iter_create(bpf_link__fd(link)); + if (!ASSERT_GE(iter_fd, 0, "iter_fd")) + return -1; + + memset(buf, 0, sizeof(buf)); + left = ARRAY_SIZE(buf); + p = buf; + while ((len = read(iter_fd, p, left)) > 0) { + p += len; + left -= len; + } + + item = sscanf(buf, "nr_running %u nr_cpus %u psi_running %u\n", + &nr_running, &nr_cpus, &psi_running); + if (!ASSERT_EQ(item, 3, "seq_format")) + goto out; + if (!ASSERT_EQ(nr_cpus, nr_cpu_exp, "nr_cpus")) + goto out; + if (!ASSERT_GE(nr_running, nr_running_exp, "nr_running")) + goto out; + if (!ASSERT_GE(psi_running, nr_running_exp, "psi_running")) + goto out; + + err = 0; +out: + close(iter_fd); + return err; +} + +static void verify_iter_success(const char *prog_name) +{ + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); + int cgrp_fd, nr_cpus, err, i, chosen = 0; + struct cpumask_iter_success *skel; + union bpf_iter_link_info linfo; + struct bpf_program *prog; + struct bpf_link *link; + cpu_set_t set; + + if (setup_cgroup_environment()) + return; + + /* Utilize the cgroup iter */ + cgrp_fd = get_root_cgroup(); + if (!ASSERT_GE(cgrp_fd, 0, "create_cgrp")) + goto cleanup; + + skel = cpumask_iter_success__open(); + if (!ASSERT_OK_PTR(skel, "cpumask_iter_success__open")) + goto close_fd; + + skel->bss->pid = getpid(); + nr_cpus = libbpf_num_possible_cpus(); + + err = cpumask_iter_success__load(skel); + if (!ASSERT_OK(err, "cpumask_iter_success__load")) + goto destroy; + + prog = bpf_object__find_program_by_name(skel->obj, prog_name); + if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) + goto destroy; + + memset(&linfo, 0, sizeof(linfo)); + linfo.cgroup.cgroup_fd = cgrp_fd; + linfo.cgroup.order = BPF_CGROUP_ITER_SELF_ONLY; + opts.link_info = &linfo; + opts.link_info_len = sizeof(linfo); + link = bpf_program__attach_iter(prog, &opts); + if (!ASSERT_OK_PTR(link, "bpf_program__attach")) + goto destroy; + + /* Case 1): Enable all possible CPUs */ + CPU_ZERO(&set); + for (i = 0; i < nr_cpus; i++) + CPU_SET(i, &set); + err = sched_setaffinity(skel->bss->pid, sizeof(set), &set); + if (!ASSERT_OK(err, "setaffinity_all_cpus")) + goto free_link; + err = read_percpu_data(link, nr_cpus, 1); + if (!ASSERT_OK(err, "read_percpu_data")) + goto free_link; + if (!ASSERT_OK(skel->bss->err, "null_rq")) + goto free_link; + + /* Case 2): CPU0 only */ + CPU_ZERO(&set); + CPU_SET(0, &set); + err = sched_setaffinity(skel->bss->pid, sizeof(set), &set); + if (!ASSERT_OK(err, "setaffinity_cpu0")) + goto free_link; + err = read_percpu_data(link, 1, 1); + if (!ASSERT_OK(err, "read_percpu_data")) + goto free_link; + if (!ASSERT_OK(skel->bss->err, "null_rq_psi")) + goto free_link; + + /* Case 3): Partial CPUs */ + CPU_ZERO(&set); + for (i = 0; i < nr_cpus; i++) { + if (i < 4 && i & 0x1) + continue; + if (i > 8 && i & 0x2) + continue; + CPU_SET(i, &set); + chosen++; + } + err = sched_setaffinity(skel->bss->pid, sizeof(set), &set); + if (!ASSERT_OK(err, "setaffinity_partial_cpus")) + goto free_link; + err = read_percpu_data(link, chosen, 1); + if (!ASSERT_OK(err, "read_percpu_data")) + goto free_link; + ASSERT_OK(skel->bss->err, "null_rq_psi"); + +free_link: + bpf_link__destroy(link); +destroy: + cpumask_iter_success__destroy(skel); +close_fd: + close(cgrp_fd); +cleanup: + cleanup_cgroup_environment(); +} + void test_cpumask(void) { int i; @@ -74,4 +216,14 @@ void test_cpumask(void) RUN_TESTS(cpumask_success); RUN_TESTS(cpumask_failure); + + for (i = 0; i < ARRAY_SIZE(cpumask_iter_success_testcases); i++) { + if (!test__start_subtest(cpumask_iter_success_testcases[i])) + continue; + + verify_iter_success(cpumask_iter_success_testcases[i]); + } + + RUN_TESTS(cpumask_iter_success); + RUN_TESTS(cpumask_iter_failure); } diff --git a/tools/testing/selftests/bpf/progs/cpumask_common.h b/tools/testing/selftests/bpf/progs/cpumask_common.h index c705d8112a35..fb65943ef130 100644 --- a/tools/testing/selftests/bpf/progs/cpumask_common.h +++ b/tools/testing/selftests/bpf/progs/cpumask_common.h @@ -56,6 +56,9 @@ u32 bpf_cpumask_any_distribute(const struct cpumask *src) __ksym __weak; u32 bpf_cpumask_any_and_distribute(const struct cpumask *src1, const struct cpumask *src2) __ksym __weak; u32 bpf_cpumask_weight(const struct cpumask *cpumask) __ksym __weak; +int bpf_iter_cpumask_new(struct bpf_iter_cpumask *it, const struct cpumask *mask) __ksym __weak; +int *bpf_iter_cpumask_next(struct bpf_iter_cpumask *it) __ksym __weak; +void bpf_iter_cpumask_destroy(struct bpf_iter_cpumask *it) __ksym __weak; void bpf_rcu_read_lock(void) __ksym __weak; void bpf_rcu_read_unlock(void) __ksym __weak; diff --git a/tools/testing/selftests/bpf/progs/cpumask_iter_failure.c b/tools/testing/selftests/bpf/progs/cpumask_iter_failure.c new file mode 100644 index 000000000000..3d304cee0a72 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/cpumask_iter_failure.c @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024 Yafang Shao */ + +#include "vmlinux.h" +#include +#include + +#include "bpf_misc.h" +#include "task_kfunc_common.h" +#include "cpumask_common.h" + +char _license[] SEC("license") = "GPL"; + +SEC("iter.s/cgroup") +__failure __msg("R2 must be a rcu pointer") +int BPF_PROG(test_cpumask_iter_no_rcu, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct task_struct *p; + int *cpu; + + p = bpf_task_from_pid(1); + if (!p) + return 1; + + bpf_for_each(cpumask, cpu, p->cpus_ptr) { + } + bpf_task_release(p); + return 0; +} + +SEC("iter/cgroup") +__failure __msg("Possibly NULL pointer passed to trusted arg1") +int BPF_PROG(test_cpumask_iter_null_pointer, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct cpumask *mask = NULL; + int *cpu; + + bpf_for_each(cpumask, cpu, mask) { + } + return 0; +} + +SEC("iter.s/cgroup") +__failure __msg("Unreleased reference id=3 alloc_insn=10") +int BPF_PROG(test_cpumask_iter_no_destroy, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct bpf_iter_cpumask it; + struct task_struct *p; + + p = bpf_task_from_pid(1); + if (!p) + return 1; + + bpf_rcu_read_lock(); + bpf_iter_cpumask_new(&it, p->cpus_ptr); + bpf_rcu_read_unlock(); + + bpf_iter_cpumask_next(&it); + bpf_task_release(p); + return 0; +} + +SEC("iter/cgroup") +__failure __msg("expected an initialized iter_cpumask as arg #1") +int BPF_PROG(test_cpumask_iter_next_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct bpf_iter_cpumask *it = NULL; + + bpf_iter_cpumask_next(it); + return 0; +} + +SEC("iter/cgroup") +__failure __msg("expected an initialized iter_cpumask as arg #1") +int BPF_PROG(test_cpumask_iter_next_uninit2, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct bpf_iter_cpumask it = {}; + + bpf_iter_cpumask_next(&it); + return 0; +} + +SEC("iter/cgroup") +__failure __msg("expected an initialized iter_cpumask as arg #1") +int BPF_PROG(test_cpumask_iter_destroy_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct bpf_iter_cpumask_kern it = {.cpu = -1}; + struct bpf_cpumask *mask; + + mask = bpf_cpumask_create(); + if (!mask) + return 1; + + bpf_cpumask_setall(mask); + it.mask = &mask->cpumask; + bpf_iter_cpumask_destroy((struct bpf_iter_cpumask *)&it); + bpf_cpumask_release(mask); + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/cpumask_iter_success.c b/tools/testing/selftests/bpf/progs/cpumask_iter_success.c new file mode 100644 index 000000000000..a71db42cc38a --- /dev/null +++ b/tools/testing/selftests/bpf/progs/cpumask_iter_success.c @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024 Yafang Shao */ + +#include "vmlinux.h" +#include +#include + +#include "task_kfunc_common.h" +#include "cpumask_common.h" + +char _license[] SEC("license") = "GPL"; + +extern const struct psi_group_cpu system_group_pcpu __ksym __weak; +extern const struct rq runqueues __ksym __weak; + +int pid; + +static void read_percpu_data(struct bpf_iter_meta *meta, struct cgroup *cgrp, const cpumask_t *mask) +{ + u32 nr_running = 0, psi_nr_running = 0, nr_cpus = 0; + struct psi_group_cpu *groupc; + struct rq *rq; + int *cpu; + + bpf_for_each(cpumask, cpu, mask) { + rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, *cpu); + if (!rq) { + err += 1; + continue; + } + nr_running += rq->nr_running; + nr_cpus += 1; + + groupc = (struct psi_group_cpu *)bpf_per_cpu_ptr(&system_group_pcpu, *cpu); + if (!groupc) { + err += 1; + continue; + } + psi_nr_running += groupc->tasks[NR_RUNNING]; + } + BPF_SEQ_PRINTF(meta->seq, "nr_running %u nr_cpus %u psi_running %u\n", + nr_running, nr_cpus, psi_nr_running); +} + +SEC("iter.s/cgroup") +int BPF_PROG(test_cpumask_iter_sleepable, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct task_struct *p; + + /* epilogue */ + if (!cgrp) + return 0; + + bpf_rcu_read_lock(); + p = bpf_task_from_pid(pid); + if (!p) { + bpf_rcu_read_unlock(); + return 1; + } + + read_percpu_data(meta, cgrp, p->cpus_ptr); + bpf_task_release(p); + bpf_rcu_read_unlock(); + return 0; +} + +SEC("iter/cgroup") +int BPF_PROG(test_cpumask_iter, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct task_struct *p; + + /* epilogue */ + if (!cgrp) + return 0; + + p = bpf_task_from_pid(pid); + if (!p) + return 1; + + read_percpu_data(meta, cgrp, p->cpus_ptr); + bpf_task_release(p); + return 0; +} + +SEC("iter.s/cgroup") +int BPF_PROG(test_cpumask_iter_next_no_rcu, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct bpf_iter_cpumask it; + struct task_struct *p; + + p = bpf_task_from_pid(1); + if (!p) + return 1; + + /* RCU is only required by bpf_iter_cpumask_new(). */ + bpf_rcu_read_lock(); + bpf_iter_cpumask_new(&it, p->cpus_ptr); + bpf_rcu_read_unlock(); + + bpf_iter_cpumask_next(&it); + bpf_iter_cpumask_destroy(&it); + + bpf_task_release(p); + return 0; +} + +SEC("iter.s/cgroup") +int BPF_PROG(test_cpumask_iter_no_next, struct bpf_iter_meta *meta, struct cgroup *cgrp) +{ + struct bpf_iter_cpumask it; + struct task_struct *p; + + p = bpf_task_from_pid(1); + if (!p) + return 1; + + bpf_rcu_read_lock(); + bpf_iter_cpumask_new(&it, p->cpus_ptr); + bpf_rcu_read_unlock(); + + /* It is fine without calling bpf_iter_cpumask_next(). */ + + bpf_iter_cpumask_destroy(&it); + bpf_task_release(p); + return 0; +}