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 5C814CF8852 for ; Fri, 4 Oct 2024 23:45:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3F086B00BE; Fri, 4 Oct 2024 19:44:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEC958D0001; Fri, 4 Oct 2024 19:44:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B4EF6B010A; Fri, 4 Oct 2024 19:44:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7C6508D0001 for ; Fri, 4 Oct 2024 19:44:59 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2902A8076C for ; Fri, 4 Oct 2024 23:44:59 +0000 (UTC) X-FDA: 82637552718.21.9F73D3C Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by imf01.hostedemail.com (Postfix) with ESMTP id 4FDD74000E for ; Fri, 4 Oct 2024 23:44:57 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dXPz087i; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728085474; a=rsa-sha256; cv=none; b=pbpmIQ5AeZR93QYnVcEkNPpdElM8Hf8dS7cYyc2IZFiqv2LummQkrVG6lKpr5TbNHi6N9Q +IPzZbWZ0emxcdHjmDBe4or+ViIMKT71RguXqcQx085kK58VNYjH2rSWelmORtMVbwT8W1 GkjgGn1gSZY/funSyqJ4yMtkk/rCQJQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dXPz087i; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf01.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728085474; 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=/ES3vmRb4BxIBJ2wMtB9RFOQn+fn7LZl8p42EnlOXCo=; b=mSmYZX/OPqDkwCSyOJTaKpwNalUkCcaZNmwyyaG8o+OnDp9ePMn3k4Y8HcW71jMNUxXbge urRHcziQ8IvuyEbfGf/drQ8Fk3CL0BF+EsAEJUG7IqOTEOtejXUmwHkAj29weBy1AZeMUU 01B5Nso9P3YiTLeK+4OT5F3MIOaP33k= Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-37cdb42b29dso1739608f8f.0 for ; Fri, 04 Oct 2024 16:44:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728085496; x=1728690296; 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=/ES3vmRb4BxIBJ2wMtB9RFOQn+fn7LZl8p42EnlOXCo=; b=dXPz087iNemL9/q/khcHLqatrkcxJDWfJoFF6SQFq1QrS+zMTac79MpYgH0cDehcss V+VlvhyB/gRceT+himLlIPu/1lEb4UXFIfRBbYYsAzGRpm7hicN3iauSnaflqIbUg/ru hf4E3JBvzy5hT+6mK2YuiD1BFU7d7yP8UZC21w1Ie873TQ1NkdCJBWLUZh2z4JYO1ADr OBZ833khBFyd6iPWVzH8Bf/H30Q7GB3KG/p9P2QRE2EpMJuLb19uhYDmUKXpPupkYCKy FtckZNJE3I/y8dcEKiyiMkQhcmirdutrF/l6hjn9xWz6cmerAtARzGClBOQq0WDFwuv5 8ZjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728085496; x=1728690296; 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=/ES3vmRb4BxIBJ2wMtB9RFOQn+fn7LZl8p42EnlOXCo=; b=oBslIUGVQ+Iuppk6nezMICBecZidMM5Fa12wXeHfSopSACOoGK+LYbXTEjrEtZuyvl Te2SDlJ2YQ/ociS/bm1wTdfL0///JE+tL5GEBvW7m9ptgYn6f/URwMmxV6yxme+LZMCM 5mpN3syWuPrW825v0HHI7ilU/Jb4ILUsGzJ9kjETmuncxxORYrjqw9Su0b+52pd8adOH SlvP7pj5KFAM3D4NLe+fSkE44yT9mFOiiLu80UGQGeAP+iTtKUaBvRVt0Yfrf/No2Z1A NFWBEwCjynYINJ2EwIImekDtR5Ph87TFiM4SJrmrojKJNXbLOJh/+mcW7A94u9dtVyn1 tnFQ== X-Forwarded-Encrypted: i=1; AJvYcCVtCI2rZw08pp77M1T3zmR22UHMayFFXh1xIITBlOgWNeGfIgeiGI/jjqEzGm88Ss2UZZu/sSr2yQ==@kvack.org X-Gm-Message-State: AOJu0Yy5ILqoY1vgFu3q1QFWr9U2yCWMCCiobPZOHeH1MPwnJV/OFpnQ JHeYxjOBWDJ/37CBQPJwYsCgyRqPCy47mbXo3N6+ydy/3xOyNCAjwv9GzkipTYPNCfQ1IfykVb/ VUV4A+pL67S6TjvRAaBsAfWBcGgs= X-Google-Smtp-Source: AGHT+IHfxt6TLvINC8Y20uR6Lt8dMWMnQaEXox3ax49DYOVcNCUuRWrJCEwfOwbZxxLH3Kp2UDT4HdCSB2NR0AdUjz0= X-Received: by 2002:a05:6000:1acc:b0:37c:d179:2f73 with SMTP id ffacd0b85a97d-37d0e6f8efamr3505932f8f.13.1728085495517; Fri, 04 Oct 2024 16:44:55 -0700 (PDT) MIME-Version: 1.0 References: <20241002180956.1781008-1-namhyung@kernel.org> <20241002180956.1781008-3-namhyung@kernel.org> In-Reply-To: From: Alexei Starovoitov Date: Fri, 4 Oct 2024 16:44:44 -0700 Message-ID: Subject: Re: [PATCH v4 bpf-next 2/3] mm/bpf: Add bpf_get_kmem_cache() kfunc To: Song Liu Cc: Namhyung Kim , Roman Gushchin , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , LKML , bpf , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Vlastimil Babka , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm , Arnaldo Carvalho de Melo , Kees Cook Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Stat-Signature: 3ixg5agysirek8y1phpmmbaz8fd4gbd8 X-Rspamd-Queue-Id: 4FDD74000E X-Rspamd-Server: rspam02 X-HE-Tag: 1728085497-702294 X-HE-Meta: U2FsdGVkX19Y+MbwNxufDHFNkvY94lihnsfS6hi+odTrwYBnEbLv02wGZFyWchsnUi1XNQ/Rgjtwq1d5BTJ5K0j5wZX3Kuo0N9mdhbrtk3pewnjSLzAoG0Pk9CaCBdTanHfUo9oDOB6ccYRzvTr62GcFr5cmO5M6jxsFzt0XC2CDiuLKibU6XvekMXsildyVxp1L6dMjzufObi+3Pj4D1ghySrNteCArzQ5FsNIX9ZT1KVkjXg0ciGcN567U1bUM5t2phI+zbjMPZVLW40kv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tJf0rIgMFZBO7SGAcVczCI0K7tR49bQeD5+SA4TdWGSi5Yr34TbCwBfB1EBS5wJhz/NIEWVq/1QDYpuBYxJsMkeWwgA9noWEFYGduqJeEIoTGFFW0ewzWK2Jsjf1Is= 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 Fri, Oct 4, 2024 at 3:57=E2=80=AFPM Song Liu wrote: > > On Fri, Oct 4, 2024 at 2:58=E2=80=AFPM Namhyung Kim = wrote: > > > > On Fri, Oct 04, 2024 at 02:36:30PM -0700, Song Liu wrote: > > > On Fri, Oct 4, 2024 at 2:25=E2=80=AFPM Roman Gushchin wrote: > > > > > > > > On Fri, Oct 04, 2024 at 01:10:58PM -0700, Song Liu wrote: > > > > > On Wed, Oct 2, 2024 at 11:10=E2=80=AFAM Namhyung Kim wrote: > > > > > > > > > > > > The bpf_get_kmem_cache() is to get a slab cache information fro= m a > > > > > > virtual address like virt_to_cache(). If the address is a poin= ter > > > > > > to a slab object, it'd return a valid kmem_cache pointer, other= wise > > > > > > NULL is returned. > > > > > > > > > > > > It doesn't grab a reference count of the kmem_cache so the call= er is > > > > > > responsible to manage the access. The intended use case for no= w is to > > > > > > symbolize locks in slab objects from the lock contention tracep= oints. > > > > > > > > > > > > Suggested-by: Vlastimil Babka > > > > > > Acked-by: Roman Gushchin (mm/*) > > > > > > Acked-by: Vlastimil Babka #mm/slab > > > > > > Signed-off-by: Namhyung Kim > > > > > > --- > > > > > > kernel/bpf/helpers.c | 1 + > > > > > > mm/slab_common.c | 19 +++++++++++++++++++ > > > > > > 2 files changed, 20 insertions(+) > > > > > > > > > > > > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > > > > > > index 4053f279ed4cc7ab..3709fb14288105c6 100644 > > > > > > --- a/kernel/bpf/helpers.c > > > > > > +++ b/kernel/bpf/helpers.c > > > > > > @@ -3090,6 +3090,7 @@ BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_= ITER_NEW) > > > > > > BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_N= ULL) > > > > > > BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY) > > > > > > BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE) > > > > > > +BTF_ID_FLAGS(func, bpf_get_kmem_cache, KF_RET_NULL) > > > > > > BTF_KFUNCS_END(common_btf_ids) > > > > > > > > > > > > static const struct btf_kfunc_id_set common_kfunc_set =3D { > > > > > > diff --git a/mm/slab_common.c b/mm/slab_common.c > > > > > > index 7443244656150325..5484e1cd812f698e 100644 > > > > > > --- a/mm/slab_common.c > > > > > > +++ b/mm/slab_common.c > > > > > > @@ -1322,6 +1322,25 @@ size_t ksize(const void *objp) > > > > > > } > > > > > > EXPORT_SYMBOL(ksize); > > > > > > > > > > > > +#ifdef CONFIG_BPF_SYSCALL > > > > > > +#include > > > > > > + > > > > > > +__bpf_kfunc_start_defs(); > > > > > > + > > > > > > +__bpf_kfunc struct kmem_cache *bpf_get_kmem_cache(u64 addr) > > > > > > +{ > > > > > > + struct slab *slab; > > > > > > + > > > > > > + if (!virt_addr_valid(addr)) > > > > > > + return NULL; > > > > > > + > > > > > > + slab =3D virt_to_slab((void *)(long)addr); > > > > > > + return slab ? slab->slab_cache : NULL; > > > > > > +} > > > > > > > > > > Do we need to hold a refcount to the slab_cache? Given > > > > > we make this kfunc available everywhere, including > > > > > sleepable contexts, I think it is necessary. > > > > > > > > It's a really good question. > > > > > > > > If the callee somehow owns the slab object, as in the example > > > > provided in the series (current task), it's not necessarily. > > > > > > > > If a user can pass a random address, you're right, we need to > > > > grab the slab_cache's refcnt. But then we also can't guarantee > > > > that the object still belongs to the same slab_cache, the > > > > function becomes racy by the definition. > > > > > > To be safe, we can limit the kfunc to sleepable context only. Then > > > we can lock slab_mutex for virt_to_slab, and hold a refcount > > > to slab_cache. We will need a KF_RELEASE kfunc to release > > > the refcount later. > > > > Then it needs to call kmem_cache_destroy() for release which contains > > rcu_barrier. :( > > > > > > > > IIUC, this limitation (sleepable context only) shouldn't be a problem > > > for perf use case? > > > > No, it would be called from the lock contention path including > > spinlocks. :( > > > > Can we limit it to non-sleepable ctx and not to pass arbtrary address > > somehow (or not to save the result pointer)? > > I hacked something like the following. It is not ideal, because we are > taking spinlock_t pointer instead of void pointer. To use this with void > 'pointer, we will need some verifier changes. > > Thanks, > Song > > > diff --git i/kernel/bpf/helpers.c w/kernel/bpf/helpers.c > index 3709fb142881..7311a26ecb01 100644 > --- i/kernel/bpf/helpers.c > +++ w/kernel/bpf/helpers.c > @@ -3090,7 +3090,7 @@ BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW) > BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL) > BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY) > BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE) > -BTF_ID_FLAGS(func, bpf_get_kmem_cache, KF_RET_NULL) > +BTF_ID_FLAGS(func, bpf_get_kmem_cache, KF_RET_NULL | KF_TRUSTED_ARGS > | KF_RCU_PROTECTED) I don't think KF_TRUSTED_ARGS approach would fit here. Namhyung's use case is tracing. The 'addr' will be some potentially arbitrary address from somewhere. The chance to see a trusted pointer is probably very low in such a tracing use case. The verifier change can mainly be the following: diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 7d9b38ffd220..e09eb108e956 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12834,6 +12834,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, regs[BPF_REG_0].type =3D PTR_TO_BTF_ID; regs[BPF_REG_0].btf_id =3D ptr_type_id; + if (meta.func_id =3D=3D special_kfunc_list[KF_get_kmem_cache]) + regs[BPF_REG_0].type |=3D PTR_UNTRUSTED; + if (is_iter_next_kfunc(&meta)) { struct bpf_reg_state *cur_iter; The returned 'struct kmem_cache *' won't be refcnt-ed (acquired). It will be readonly via ptr_to_btf_id logic. s->flags; s->size; s->offset; access will be allowed but the verifier will sanitize them with an inlined version of probe_read_kernel. Even KF_RET_NULL can be dropped.