From mboxrd@z Thu Jan 1 00:00:00 1970 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 D25EBCF6491 for ; Mon, 30 Sep 2024 03:25:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2A10C80011; Sun, 29 Sep 2024 23:25:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 251C480009; Sun, 29 Sep 2024 23:25:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F26280011; Sun, 29 Sep 2024 23:25:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E4B3080009 for ; Sun, 29 Sep 2024 23:25:08 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 789C7407BC for ; Mon, 30 Sep 2024 03:25:08 +0000 (UTC) X-FDA: 82619963496.01.11BE362 Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) by imf29.hostedemail.com (Postfix) with ESMTP id 86F95120014 for ; Mon, 30 Sep 2024 03:25:06 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Hu6m/V8l"; spf=pass (imf29.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.167.46 as permitted sender) smtp.mailfrom=42.hyeyoo@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727666642; a=rsa-sha256; cv=none; b=3gj4/0G4ahfjnT72cj1rEj1BL2ODnjqME+Dte86mRixfWGEQ4aIhrgyyPawKrib8YTIhKi 01pcv7Z/2eTmkf5zaJpxW0Wv+4qUL78gZQtsfYSZEWAddbb7DUCiI5epYFRBqFijcXEc5I tNqn6QiKG5QNVGPue1Ji3/WB2xMeWN4= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Hu6m/V8l"; spf=pass (imf29.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.167.46 as permitted sender) smtp.mailfrom=42.hyeyoo@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727666642; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xptrEtzFtFOhGhhDu38fIaDiqDAME1wxUs6nvetbUTk=; b=3e+8UQs9cMkqwBMN7WLehNKY+16WorOUhKg3CBMBWxETJIIq8E5VkeoxQaXS45dYLKCOw1 RiWW37nXtmzjaX/HCdcrb2P8d4CG33U3O/JoDEmPNKtDBlM+5mXVcCB3XxWqAq0bVBX724 demtt5SNI8EkzqTmP+KiactTavsy2WA= Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-53988c54ec8so2488156e87.0 for ; Sun, 29 Sep 2024 20:25:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727666704; x=1728271504; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=xptrEtzFtFOhGhhDu38fIaDiqDAME1wxUs6nvetbUTk=; b=Hu6m/V8l597LOqobodoPKGvOGhpwDs1hItvQ+uEa9II98PHQCEC+OfxcACCctcd/bZ LVxZexb518JXWE6xChujCmHjFtvMtS8dPbNwsmoC8R72HjLO5VPXxrjNkNHiAmRlbBJk 9VqT5ZvpEcKhi7mGXFjiBfycagEjOPG8bOfI+2lILixPfsKDV9sqHHT/K9Q46NeHxECo HgEw+4Z+kOkcvk+9QV48GIxQuMdWQgSiexu9a+TuxqmrjH7snX27kfKcuIXp5fwYaNVT 55XJsXqxZ5nRaSyTqUUhcEoJQPylt43IBAdd3EppdFHDfGa10ipFxIv0nllhqrYS4n7I 4mnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727666704; x=1728271504; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xptrEtzFtFOhGhhDu38fIaDiqDAME1wxUs6nvetbUTk=; b=ZCt+grimddjgeyA8yHUcocOpryhqbm4ix1r6Cd0SSXzz5c2niVjU6jRjba1d+3HU2D RA6cXaP2HqVTgyvL+mMWTpxIKHrqd7xDOqMUjVr0QTWWVH/BRHHKofMgrLSfmpbrKxsz 4zZfRTePgfViN2bkiL89yxoVv0wb2e7iBM+5N/qXwDiL4w2LEGr837cX2p1yfmR/GYPJ DDLutGsIi5OQDmQcUZXrPKFEVxYqli2G3wXyyhbA3qMJGhr1x40NYxLQETJMQ4KOFqum 6fLQos20bGtxP0bV4W9uRglNmK3NUjYIJNShQevlDp6dym1E+1t4d3ZO8RVyW8E2GQNV nNcg== X-Forwarded-Encrypted: i=1; AJvYcCWtPw9itIVoHjYaCZ2M8f3VucB5kCMZyJugHBz1JJv1LnDBGFBYXnj+J+6F/e5LKfs0c7YrMj8s8A==@kvack.org X-Gm-Message-State: AOJu0YwOZ507X8KBQjr3CHsgAlrXAXtAr9dBm+4KqwR9QXA9AGcsJnmm tjDwlK2GvFUnR1t3MbSzxkGoZfF3BKRH5AD+UtsD2trVJczAi51JYIsFLjN8iSXuMvnWPkxW9Uu OE1kKywX7nVhz60VPsa5+juEXrpY= X-Google-Smtp-Source: AGHT+IELe+2NOBUdZEI+ghESbtr3V11lcC/22EdAwVAOqSICs5O2qdB8ektR+NGrKCr9D9/eubpAvuLCvbIyLZsdyYU= X-Received: by 2002:a05:6512:1047:b0:537:a855:7d6f with SMTP id 2adb3069b0e04-5389fc4bc4fmr4472211e87.34.1727666704290; Sun, 29 Sep 2024 20:25:04 -0700 (PDT) MIME-Version: 1.0 References: <20240927184133.968283-1-namhyung@kernel.org> <20240927184133.968283-4-namhyung@kernel.org> In-Reply-To: From: Hyeonggon Yoo <42.hyeyoo@gmail.com> Date: Mon, 30 Sep 2024 12:24:52 +0900 Message-ID: Subject: Re: [RFC/PATCH bpf-next 3/3] selftests/bpf: Add a test for kmem_cache_iter To: Namhyung Kim Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , 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 , linux-mm@kvack.org, Arnaldo Carvalho de Melo Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: 8t57qaccnfidbi5ithpbf139j1nc78ts X-Rspamd-Queue-Id: 86F95120014 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1727666706-341591 X-HE-Meta: U2FsdGVkX1/zeLVKvIWQftVidlxEnOS9Uqg2MkCyI9+/sKRfVqBi7t7o2Qj9cDrDyFD5NTU5evvlkunkHJM53/RZxkZiO7QIlL8Tmr1r3vEa6PIwrP+QeNr4ou4r2Z43Em2TwODTv85RpbpCHoZr3j1cfDCVhXm4gqQAy8RYaXxC27XUgvZIbfo6MiEUExtf5EjSPrO5rpKgJvxEtET6H8s32xdGBj1gx2OI73gweCPbvt8sOvCzbQs1zR2fdwvGItScg/EtsWWAZbHGTI1NgdPSf1Gmzgfh962ChRvp32+o3kJpEVQyAmpgoiW7watyh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ovRfk30ZskW0efOZYX3NYntXT/QVjuVSOXJCyOMt+sR4dMd0C2pqOhlcgz/lgZedlFGY1FMlm02n9OWsSQu3VW4rxXGYeA== 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: On Mon, Sep 30, 2024 at 11:18=E2=80=AFAM Namhyung Kim = wrote: > > Hello Hyeonggon, > > On Sun, Sep 29, 2024 at 11:27:25PM +0900, Hyeonggon Yoo wrote: > > On Sun, Sep 29, 2024 at 3:13=E2=80=AFPM Namhyung Kim wrote: > > > > > > On Fri, Sep 27, 2024 at 11:41:33AM -0700, Namhyung Kim wrote: > > > > The test traverses all slab caches using the kmem_cache_iter and ch= eck > > > > if current task's pointer is from "task_struct" slab cache. > > > > > > > > Signed-off-by: Namhyung Kim > > > > --- > > > > .../bpf/prog_tests/kmem_cache_iter.c | 64 +++++++++++++++= +++ > > > > tools/testing/selftests/bpf/progs/bpf_iter.h | 7 ++ > > > > .../selftests/bpf/progs/kmem_cache_iter.c | 66 +++++++++++++++= ++++ > > > > 3 files changed, 137 insertions(+) > > > > create mode 100644 tools/testing/selftests/bpf/prog_tests/kmem_cac= he_iter.c > > > > create mode 100644 tools/testing/selftests/bpf/progs/kmem_cache_it= er.c > > > > > > > > diff --git a/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter= .c b/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c > > > > new file mode 100644 > > > > index 0000000000000000..814bcc453e9f3ccd > > > > --- /dev/null > > > > +++ b/tools/testing/selftests/bpf/prog_tests/kmem_cache_iter.c > > > > @@ -0,0 +1,64 @@ > > > > +// SPDX-License-Identifier: GPL-2.0 > > > > +/* Copyright (c) 2024 Google */ > > > > + > > > > +#include > > > > +#include > > > > +#include > > > > +#include "kmem_cache_iter.skel.h" > > > > + > > > > +static void test_kmem_cache_iter_check_task(struct kmem_cache_iter= *skel) > > > > +{ > > > > + LIBBPF_OPTS(bpf_test_run_opts, opts, > > > > + .flags =3D BPF_F_TEST_RUN_ON_CPU, > > > > + ); > > > > + int prog_fd =3D bpf_program__fd(skel->progs.check_task_struct= ); > > > > + > > > > + /* get task_struct and check it if's from a slab cache */ > > > > + bpf_prog_test_run_opts(prog_fd, &opts); > > > > + > > > > + /* the BPF program should set 'found' variable */ > > > > + ASSERT_EQ(skel->bss->found, 1, "found task_struct"); > > > > > > Hmm.. I'm seeing a failure with found being -1, which means ... > > > > > > > +} > > > > + > > > > +void test_kmem_cache_iter(void) > > > > +{ > > > > + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); > > > > + struct kmem_cache_iter *skel =3D NULL; > > > > + union bpf_iter_link_info linfo =3D {}; > > > > + struct bpf_link *link; > > > > + char buf[1024]; > > > > + int iter_fd; > > > > + > > > > + skel =3D kmem_cache_iter__open_and_load(); > > > > + if (!ASSERT_OK_PTR(skel, "kmem_cache_iter__open_and_load")) > > > > + return; > > > > + > > > > + opts.link_info =3D &linfo; > > > > + opts.link_info_len =3D sizeof(linfo); > > > > + > > > > + link =3D bpf_program__attach_iter(skel->progs.slab_info_colle= ctor, &opts); > > > > + if (!ASSERT_OK_PTR(link, "attach_iter")) > > > > + goto destroy; > > > > + > > > > + iter_fd =3D bpf_iter_create(bpf_link__fd(link)); > > > > + if (!ASSERT_GE(iter_fd, 0, "iter_create")) > > > > + goto free_link; > > > > + > > > > + memset(buf, 0, sizeof(buf)); > > > > + while (read(iter_fd, buf, sizeof(buf) > 0)) { > > > > + /* read out all contents */ > > > > + printf("%s", buf); > > > > + } > > > > + > > > > + /* next reads should return 0 */ > > > > + ASSERT_EQ(read(iter_fd, buf, sizeof(buf)), 0, "read"); > > > > + > > > > + test_kmem_cache_iter_check_task(skel); > > > > + > > > > + close(iter_fd); > > > > + > > > > +free_link: > > > > + bpf_link__destroy(link); > > > > +destroy: > > > > + kmem_cache_iter__destroy(skel); > > > > +} > > > > diff --git a/tools/testing/selftests/bpf/progs/bpf_iter.h b/tools/t= esting/selftests/bpf/progs/bpf_iter.h > > > > index c41ee80533ca219a..3305dc3a74b32481 100644 > > > > --- a/tools/testing/selftests/bpf/progs/bpf_iter.h > > > > +++ b/tools/testing/selftests/bpf/progs/bpf_iter.h > > > > @@ -24,6 +24,7 @@ > > > > #define BTF_F_PTR_RAW BTF_F_PTR_RAW___not_used > > > > #define BTF_F_ZERO BTF_F_ZERO___not_used > > > > #define bpf_iter__ksym bpf_iter__ksym___not_used > > > > +#define bpf_iter__kmem_cache bpf_iter__kmem_cache___not_used > > > > #include "vmlinux.h" > > > > #undef bpf_iter_meta > > > > #undef bpf_iter__bpf_map > > > > @@ -48,6 +49,7 @@ > > > > #undef BTF_F_PTR_RAW > > > > #undef BTF_F_ZERO > > > > #undef bpf_iter__ksym > > > > +#undef bpf_iter__kmem_cache > > > > > > > > struct bpf_iter_meta { > > > > struct seq_file *seq; > > > > @@ -165,3 +167,8 @@ struct bpf_iter__ksym { > > > > struct bpf_iter_meta *meta; > > > > struct kallsym_iter *ksym; > > > > }; > > > > + > > > > +struct bpf_iter__kmem_cache { > > > > + struct bpf_iter_meta *meta; > > > > + struct kmem_cache *s; > > > > +} __attribute__((preserve_access_index)); > > > > diff --git a/tools/testing/selftests/bpf/progs/kmem_cache_iter.c b/= tools/testing/selftests/bpf/progs/kmem_cache_iter.c > > > > new file mode 100644 > > > > index 0000000000000000..3f6ec15a1bf6344c > > > > --- /dev/null > > > > +++ b/tools/testing/selftests/bpf/progs/kmem_cache_iter.c > > > > @@ -0,0 +1,66 @@ > > > > +// SPDX-License-Identifier: GPL-2.0 > > > > +/* Copyright (c) 2024 Google */ > > > > + > > > > +#include "bpf_iter.h" > > > > +#include > > > > +#include > > > > + > > > > +char _license[] SEC("license") =3D "GPL"; > > > > + > > > > +#define SLAB_NAME_MAX 256 > > > > + > > > > +struct { > > > > + __uint(type, BPF_MAP_TYPE_HASH); > > > > + __uint(key_size, sizeof(void *)); > > > > + __uint(value_size, SLAB_NAME_MAX); > > > > + __uint(max_entries, 1024); > > > > +} slab_hash SEC(".maps"); > > > > + > > > > +extern struct kmem_cache *bpf_get_kmem_cache(__u64 addr) __ksym; > > > > + > > > > +/* result, will be checked by userspace */ > > > > +int found; > > > > + > > > > +SEC("iter/kmem_cache") > > > > +int slab_info_collector(struct bpf_iter__kmem_cache *ctx) > > > > +{ > > > > + struct seq_file *seq =3D ctx->meta->seq; > > > > + struct kmem_cache *s =3D ctx->s; > > > > + > > > > + if (s) { > > > > + char name[SLAB_NAME_MAX]; > > > > + > > > > + /* > > > > + * To make sure if the slab_iter implements the seq i= nterface > > > > + * properly and it's also useful for debugging. > > > > + */ > > > > + BPF_SEQ_PRINTF(seq, "%s: %u\n", s->name, s->object_si= ze); > > > > + > > > > + bpf_probe_read_kernel_str(name, sizeof(name), s->name= ); > > > > + bpf_map_update_elem(&slab_hash, &s, name, BPF_NOEXIST= ); > > > > + } > > > > + > > > > + return 0; > > > > +} > > > > + > > > > +SEC("raw_tp/bpf_test_finish") > > > > +int BPF_PROG(check_task_struct) > > > > +{ > > > > + __u64 curr =3D bpf_get_current_task(); > > > > + struct kmem_cache *s; > > > > + char *name; > > > > + > > > > + s =3D bpf_get_kmem_cache(curr); > > > > + if (s =3D=3D NULL) { > > > > + found =3D -1; > > > > + return 0; > > > > > > ... it cannot find a kmem_cache for the current task. This program i= s > > > run by bpf_prog_test_run_opts() with BPF_F_TEST_RUN_ON_CPU. So I thi= nk > > > the curr should point a task_struct in a slab cache. > > > > > > Am I missing something? > > > > Hi Namhyung, > > > > Out of curiosity I've been investigating this issue on my machine and > > running some experiments. > > Thanks a lot for looking at this! > > > > > When the test fails, calling dump_page() for the page the task_struct > > belongs to, > > shows that the page does not have the PGTY_slab flag set which is why > > virt_to_slab(current) returns NULL. > > > > Does the test always fails on your environment? On my machine, the > > test passed sometimes but failed some times. > > I'm using vmtest.sh but it succeeded mostly. I thought I couldn't > reproduce it locally, but I also see the failure sometimes. I'll take a > deeper look. > > > > > Maybe sometimes the value returned by 'current' macro belongs to a > > slab, but sometimes it does not. > > But that doesn't really make sense to me as IIUC task_struct > > descriptors are allocated from slab. > > AFAIK the notable exception is the init_task which lives in the kernel > data. I'm not sure the if the test is running by PID 1. I checked that the test is running under PID 0 (swapper) when it fails and non-0 PID when it succeeds. This makes sense as the task_struct for PID 0 should be in the kernel image area, not in a slab. Phew, fortunately, it's not a bug! :) Any plans on how to adjust the test program? Best, Hyeonggon