From patchwork Thu Oct 24 07:48:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 13848478 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 297BAD0BB6C for ; Thu, 24 Oct 2024 07:48:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10F8D6B0085; Thu, 24 Oct 2024 03:48:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0EA576B0092; Thu, 24 Oct 2024 03:48:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDB2F6B0085; Thu, 24 Oct 2024 03:48:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D29F56B0085 for ; Thu, 24 Oct 2024 03:48:20 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B7AFE8110F for ; Thu, 24 Oct 2024 07:48:04 +0000 (UTC) X-FDA: 82707717792.28.AC43E13 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf28.hostedemail.com (Postfix) with ESMTP id AB03EC0009 for ; Thu, 24 Oct 2024 07:48:00 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mA6rk5G3; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf28.hostedemail.com: domain of namhyung@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=namhyung@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729756060; a=rsa-sha256; cv=none; b=k/auN6kpLAGBuuPnR34OOxryy7sZMqB4g4FIhpE1QdTJhmKy29VSrWSIfH1yoFMkb4WBy/ YjV8nCrvE9k12KG8ObSVtBUUaFWpRM7/0RzQfu8oLpXZDstt366Z9Byov+sHgra1DGRXFF 4WHu7jcBExscXQsrOmHnz+5XhziOfu0= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mA6rk5G3; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf28.hostedemail.com: domain of namhyung@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=namhyung@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729756060; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QqU1J4hg6zlVF29WqRYnQMgDNkLYuNp3dUjF9Blxrcs=; b=myPwNxSuk2Fkc0rG7Gts+6035SBZup0/Vb56MnyU7cPRX+jZELuW0heDGBLOD7BCgzVI0E LORDVOMtFb/qCoBuL98a/2c5bHCpa5bYiRh7jXF3EZFjFS6nvSr5HzqnjkneurQN/KYdXE EB5Qoxz8OjJunjukZzcl9yDY1YCWLPg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 27990A451BE; Thu, 24 Oct 2024 07:48:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E0E6C4AF11; Thu, 24 Oct 2024 07:48:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729756098; bh=KdTfSCDjkkqD9bhaV9lSbcv8lSsiidIcVwYLU1/0JAQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mA6rk5G3BGxp468AygJtj6VOjozKki9/lKuCqmUT91hD8jmQD8QvPqfluHvvGz4dV zLCIMhwYXsTgxeWQeb4bKbxOOn2DFu2BcEMkg3T4VDDPup3/UapJeX8li+Kd0Ac3kl EnCYJ2XNHaYUBwQ8FAf5HthmVf59uCYX/qelabnWho6I+6qroQjPsSgJwDz/p0tMZ+ sUJ7SuBGuoUSZXwIPXBn7Rx5FAFoLCXiLC57Md2STUL0ZCyL6Rh/63iICV3Y7/Utuv 9o4uMc1+xbCsaML7WJi0srwZXiLhfloZqnX8YByDhKOBeab1IoYlEucCeew/Y41bUo 3I3lNoFUrnC6Q== From: Namhyung Kim To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , LKML , bpf@vger.kernel.org, Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, Arnaldo Carvalho de Melo , Kees Cook Subject: [PATCH v2 bpf-next 2/2] selftests/bpf: Add a test for open coded kmem_cache iter Date: Thu, 24 Oct 2024 00:48:15 -0700 Message-ID: <20241024074815.1255066-2-namhyung@kernel.org> X-Mailer: git-send-email 2.47.0.105.g07ac214952-goog In-Reply-To: <20241024074815.1255066-1-namhyung@kernel.org> References: <20241024074815.1255066-1-namhyung@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: w8m7urkucgedr43q9chn66zxo78394ss X-Rspamd-Queue-Id: AB03EC0009 X-Rspamd-Server: rspam02 X-HE-Tag: 1729756080-410872 X-HE-Meta: U2FsdGVkX1/U0qYzRcweZlB23egGl7qvXsau1R7Le9ubHgYTyE4BZuayQKd5P1T4B7Z6B8LqJsWqHAs+qejsZo3pGFHnEk/+VqOxuLZiaSodbDqnHbVU+b5c0OJ3m416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S08l3pVI137I0rZ2QDVX5ers8VqUMygpaUWA0Ga0qlQnoZah0EtyblBUJsF+e3zrsPm/FV5wayqMD5oNTcCy9+myvBn0lbifPOfy48mnODburtOM5dyuAnULIXG2YfRxMeBz/w7D8kSKWgCkXW+XXApC6F0AtIWT02s= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The new subtest is attached to sleepable fentry of syncfs() syscall. It iterates the kmem_cache using bpf_for_each loop and count the number of entries. Finally it checks it with the number of entries from the regular iterator. $ ./vmtest.sh -- ./test_progs -t kmem_cache_iter ... #130/1 kmem_cache_iter/check_task_struct:OK #130/2 kmem_cache_iter/check_slabinfo:OK #130/3 kmem_cache_iter/open_coded_iter:OK #130 kmem_cache_iter:OK Summary: 1/3 PASSED, 0 SKIPPED, 0 FAILED Also simplify the code by using attach routine of the skeleton. Signed-off-by: Namhyung Kim --- v2) * remove unnecessary detach (Martin) * check pid in syncfs to prevent surprise (Martin) * remove unnecessary local variable (Andrii) .../testing/selftests/bpf/bpf_experimental.h | 6 ++++ .../bpf/prog_tests/kmem_cache_iter.c | 28 +++++++++++-------- .../selftests/bpf/progs/kmem_cache_iter.c | 28 +++++++++++++++++++ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index b0668f29f7b394eb..cd8ecd39c3f3c68d 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -582,4 +582,10 @@ extern int bpf_wq_set_callback_impl(struct bpf_wq *wq, unsigned int flags__k, void *aux__ign) __ksym; #define bpf_wq_set_callback(timer, cb, flags) \ bpf_wq_set_callback_impl(timer, cb, flags, NULL) + +struct bpf_iter_kmem_cache; +extern int bpf_iter_kmem_cache_new(struct bpf_iter_kmem_cache *it) __weak __ksym; +extern struct kmem_cache *bpf_iter_kmem_cache_next(struct bpf_iter_kmem_cache *it) __weak __ksym; +extern void bpf_iter_kmem_cache_destroy(struct bpf_iter_kmem_cache *it) __weak __ksym; + #endif diff --git a/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c b/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c index 848d8fc9171fae45..778b55bc1f912b98 100644 --- a/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c @@ -68,12 +68,20 @@ static void subtest_kmem_cache_iter_check_slabinfo(struct kmem_cache_iter *skel) fclose(fp); } +static void subtest_kmem_cache_iter_open_coded(struct kmem_cache_iter *skel) +{ + skel->bss->tgid = getpid(); + + /* To trigger the open coded iterator attached to the syscall */ + syncfs(0); + + /* It should be same as we've seen from the explicit iterator */ + ASSERT_EQ(skel->bss->open_coded_seen, skel->bss->kmem_cache_seen, "open_code_seen_eq"); +} + void test_kmem_cache_iter(void) { - DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); struct kmem_cache_iter *skel = NULL; - union bpf_iter_link_info linfo = {}; - struct bpf_link *link; char buf[256]; int iter_fd; @@ -81,16 +89,12 @@ void test_kmem_cache_iter(void) if (!ASSERT_OK_PTR(skel, "kmem_cache_iter__open_and_load")) return; - opts.link_info = &linfo; - opts.link_info_len = sizeof(linfo); - - link = bpf_program__attach_iter(skel->progs.slab_info_collector, &opts); - if (!ASSERT_OK_PTR(link, "attach_iter")) + if (!ASSERT_OK(kmem_cache_iter__attach(skel), "skel_attach")) goto destroy; - iter_fd = bpf_iter_create(bpf_link__fd(link)); + iter_fd = bpf_iter_create(bpf_link__fd(skel->links.slab_info_collector)); if (!ASSERT_GE(iter_fd, 0, "iter_create")) - goto free_link; + goto destroy; memset(buf, 0, sizeof(buf)); while (read(iter_fd, buf, sizeof(buf) > 0)) { @@ -105,11 +109,11 @@ void test_kmem_cache_iter(void) subtest_kmem_cache_iter_check_task_struct(skel); if (test__start_subtest("check_slabinfo")) subtest_kmem_cache_iter_check_slabinfo(skel); + if (test__start_subtest("open_coded_iter")) + subtest_kmem_cache_iter_open_coded(skel); close(iter_fd); -free_link: - bpf_link__destroy(link); destroy: kmem_cache_iter__destroy(skel); } diff --git a/tools/testing/selftests/bpf/progs/kmem_cache_iter.c b/tools/testing/selftests/bpf/progs/kmem_cache_iter.c index 72c9dafecd98406b..e62807caa7593604 100644 --- a/tools/testing/selftests/bpf/progs/kmem_cache_iter.c +++ b/tools/testing/selftests/bpf/progs/kmem_cache_iter.c @@ -2,6 +2,8 @@ /* Copyright (c) 2024 Google */ #include "bpf_iter.h" +#include "bpf_experimental.h" +#include "bpf_misc.h" #include #include @@ -30,9 +32,12 @@ struct { extern struct kmem_cache *bpf_get_kmem_cache(u64 addr) __ksym; +unsigned int tgid; + /* Result, will be checked by userspace */ int task_struct_found; int kmem_cache_seen; +int open_coded_seen; SEC("iter/kmem_cache") int slab_info_collector(struct bpf_iter__kmem_cache *ctx) @@ -85,3 +90,26 @@ int BPF_PROG(check_task_struct) task_struct_found = -2; return 0; } + +SEC("fentry.s/" SYS_PREFIX "sys_syncfs") +int open_coded_iter(const void *ctx) +{ + struct kmem_cache *s; + + if (tgid != bpf_get_current_pid_tgid() >> 32) + return 0; + + bpf_for_each(kmem_cache, s) { + struct kmem_cache_result *r; + + r = bpf_map_lookup_elem(&slab_result, &open_coded_seen); + if (!r) + break; + + open_coded_seen++; + + if (r->obj_size != s->size) + break; + } + return 0; +}