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 7598AD2A555 for ; Wed, 16 Oct 2024 19:59:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F19636B0083; Wed, 16 Oct 2024 15:59:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC9516B0088; Wed, 16 Oct 2024 15:59:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D90DA6B0089; Wed, 16 Oct 2024 15:59:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B41486B0083 for ; Wed, 16 Oct 2024 15:59:55 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9B53CA08C4 for ; Wed, 16 Oct 2024 19:59:36 +0000 (UTC) X-FDA: 82680530688.09.BF8CD54 Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) by imf30.hostedemail.com (Postfix) with ESMTP id 7E2B280006 for ; Wed, 16 Oct 2024 19:59:36 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0YmJoAtK; spf=pass (imf30.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.48 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729108745; 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=y+lPf3FNOmE8pMU/2Iycp1XnYxIydARXrVQNA1uL0No=; b=1+521ZjJmCX+hcQ9wABYWPT6KeK/KFCnBLilwa2QyXnEkjB8Pna1NDadgMJSz401Da/t1t onETZkX9J/8dG4vho3PJUzGyl1qbHwvTXtlN8yfyXhqE4GSjLLTDVYcLA23OurDW76X0/f 6PhcmUKN4NiYJiomouEsDnnvxA0WJMo= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0YmJoAtK; spf=pass (imf30.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.48 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729108745; a=rsa-sha256; cv=none; b=JFxTfwCyTB37GVFHavvKnq9N8kbUckTGQsnxj6Z8Tn7AdFwu1E0eWsG3CpO3mI3ndm/Sha nnNkBEYDDyEhSDjctPAcfFktnhqaYNL32mtp8nD31Uuc6iDvw5ejtZUo++aTHZXbojvWoP LJyPtOvkheEO3rrHX1pdoHSQnATETIc= Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-a99eb8b607aso18062766b.2 for ; Wed, 16 Oct 2024 12:59:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1729108792; x=1729713592; 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=y+lPf3FNOmE8pMU/2Iycp1XnYxIydARXrVQNA1uL0No=; b=0YmJoAtKr59GWyuNRubpccmsWp59D8Es2Jpo919/hACuuOjA07glmL8lcNWrF8WOl6 hKpbwg3Do8V10y9Agy06Dx3VMLV1RbXEilMgCG5jhFbCkSMB5jsJ1dnw4Zbp2Hzn6pJ7 lo7fcfazPs/altziiR62WkznHw+yI5Uq8elUirtwTyrBBdFZmWS9X2DzVN+l79o06BP/ g7DRH3EeN2nBCEoLRhAkKdRTOgcw87mNWzA+8LvlfYRw3gsL2fMic5cLqGoP+ar2lWP5 wxlhjZjAITUcQ4Ok6ACfBQRUigJtn+KP+JF7MwCN0lSwJcicOzUp7OizD1tWIo/yYRwB 8+jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729108792; x=1729713592; 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=y+lPf3FNOmE8pMU/2Iycp1XnYxIydARXrVQNA1uL0No=; b=CxJp3mA3qO43S5JGh5HgErmea1JF4Q0sxN2jW+VG1R0xSW10zwwnol4lxR+0nLQTdG M6IVAK7iSAtxQMdxB2OnxaZ+GT3pye7lGBOocbIEp6JJG4uEEc9ESIlYn0ZOpM+VQd3F 1C1N0Rsk1MgfFdUDKtu96nxUSWLhd/lQjWn72rRqGFAir3jby2Aj6N1CB3mKI8PWC2bL XBYDvUXRpIhSlPnOC1i6BMDlWc8jyP86cJYf9pBNMqZT5bBOsF9xRLBFb8Ln5yCHds76 2qGnff3U6Bni7KVilUvLNzGDIVm5LfOXyyvhSzKcJMGBs0QIOof0latDZzOL4981LDOI QOhg== X-Forwarded-Encrypted: i=1; AJvYcCWj18DFCUirbNBVGztcC7Yj/q7bNkKdU+L45eJPwi8YVXad5nX5Gw3iY7ORQZmSi+9N5xDiisF0PQ==@kvack.org X-Gm-Message-State: AOJu0YzuM9qPBv4B+0eT0qUHmYP6sLO7+n1edhv+Aa5Yi+4oL76eps0+ vT+cmDuUlnAsbBwlzwQQujEt0cjtxFpEzJUOhsGh1bvRG/YEYQYgMwV0Js4xpOIr7EvjKvqBAhu ANaEM5wqy3EJScs1FnK2g0JGiG3T5vU6PorLW X-Google-Smtp-Source: AGHT+IHeHGSJGST3y7pnbjWTe7yANkgiRQ1Q+ewN/4YgOGby4P6amBbfCGy33IqgOgmGsE0A8OhBdeVemB3UzdU3xTM= X-Received: by 2002:a17:907:9815:b0:a99:5d4c:7177 with SMTP id a640c23a62f3a-a9a34c80793mr404655566b.6.1729108791587; Wed, 16 Oct 2024 12:59:51 -0700 (PDT) MIME-Version: 1.0 References: <20241014235631.1229438-1-andrii@kernel.org> <2rweiiittlxcio6kknwy45wez742mlgjnfdg3tq3xdkmyoq5nn@g7bfoqy4vdwt> In-Reply-To: <2rweiiittlxcio6kknwy45wez742mlgjnfdg3tq3xdkmyoq5nn@g7bfoqy4vdwt> From: Yosry Ahmed Date: Wed, 16 Oct 2024 12:59:13 -0700 Message-ID: Subject: Re: [PATCH bpf] lib/buildid: handle memfd_secret() files in build_id_parse() To: Shakeel Butt Cc: Andrii Nakryiko , bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, martin.lau@kernel.org, linux-mm@kvack.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, Yi Lai , pbonzini@redhat.com, seanjc@google.com, tabba@google.com, david@redhat.com, jackmanb@google.com, jannh@google.com, rppt@kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Stat-Signature: 9toq9h4di9kwyq9nhahzaqq7h3fbo39j X-Rspamd-Queue-Id: 7E2B280006 X-Rspamd-Server: rspam11 X-HE-Tag: 1729108776-571176 X-HE-Meta: U2FsdGVkX1+viNfyj5EtIPWlWqMKi99IvLJ3+EYbD+qgSdyaMqtmeQbYI/ZIjrAAV+78H3sg8oQj/uSxcxP0n3kRviTz+E+TjxdiVwloF6dbdEoYumjpvd2d+SfwKnhOSucX0Q3jIq/HUzCwC455u2tX1GM1G0wHQTVGWBY48cCKxByIaKdrHyfRS4UDg7xCv087sHdJJuT1HHtVHIZEroGFBuKIk3mp1jr5UfqJyM9/JtF20PmY64WCEBT+1sZI7Cp/bV0WWflRih3xpDAJf1Q7HZr89kpxdREXlutT+PFZ9uAv51IKMeira60+z1aGi/po/r+lCpxw4XRmmOvotEMWdSu8d3LHgyoKCSvKQ3fzYeEvYVdPaom8sb/zhGm88RbD/LzT6hvsJKbqbbCkKFOOQah9DJo0w0lvfK4UA7F63YV6erhprXkQOZpP2C5noJLAbeNmSqiQIuYFTSIMkQ23BTCBsUGZ1Zi9i8iMb2Ut5HdVjo7ABk+J8Hxv3rtKzIwn1n6Ur0MmO9y5dfX/NXWsBxdlVGetRPni2hhMgyE2/HPltw/ZuBHNGS6RkiDj2S5FpRQcD71p2n8GMq3XIqZaRfvqgO0lcw/2hYF8Z2ttsyUNlQKA5PHR7DJEV/+EGWId/Zu/zIzgObHn/phZJdOBlXBYiPzxAkhnTxfqBaUtPuDisQE8PD+wdUYtQK/uZW8W8FH8PEqKG/0BBzqogbU3BPk4BZAPtkXLvOopwmoWU102DgQh3vy7GmYFxSjIUJlZ5yuaFZdkE7m94dP4FMSZKWYHRyzx8tA8UjHG1iOJMuYnH/sumu8bNHu5aAXrWVcSUdcy1G5kNNFSGwjiUb1RBpqQ+mSIAnEtASE62/v5Hn2hcBevYGLlVraWhqwuUnHIPiKYaQsBiGuT+M7qIu6BniK4hbw2YTjhsSnTZpx906vp/h3W8kQoWlcEl0QBD2cIUrH/3tCwFarosgn yuLP7gye Jf4eOKJBcn5fXeky6wxJZYidSG2/AK64aL80HxUBu53k4LObY64kLmiUCLs8ZSE6dKn/dJ7uZCtZxr4hKrJE+zz6854IfowDWO/sQTRoZww8fATo8KZANrom6ygHshj+DQt1NP3uRAviRd6g6qQwL+PDmtDf8cZJ/XXlkYAE4eEVaSVErt/AS2Ah75OCfuKPFCuVYqv6ny8QSZOl6uoZm9rV1wIt401rgVLsh/QIqBfIhiSVNdLuYvGm3reUnH7Knn32MFiR/ZWi1grwMxuRa57QXXCAtckVTmrzg 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 Wed, Oct 16, 2024 at 11:39=E2=80=AFAM Shakeel Butt wrote: > > Ccing couple more folks who are doing similar work (ASI, guest_memfd) > > Folks, what is the generic way to check if a given mapping has folios > unmapped from kernel address space? I suppose you mean specifically if a folio is not mapped in the direct map, because a folio can also be mapped in other regions of the kernel address space (e.g. vmalloc). >From my perspective of working on ASI on the x86 side, I think lookup_address() is the right API to use. It returns a PTE and you can check if it is present. Based on that, I would say that the generic way is perhaps kernel_page_present(), which does the above on x86, not sure about other architectures. It seems like kernel_page_present() always returns true with !CONFIG_ARCH_HAS_SET_DIRECT_MAP, which assumes that unmapping folios from the direct map uses set_direct_map_*(). For secretmem, it seems like set_direct_map_*() is indeed the method used to unmap folios. I am not sure if the same stands for guest_memfd, but I don't see why not. ASI does not use set_direct_map_*(), but it doesn't matter in this context, read below if you care about the reasoning. ASI does not unmap folios from the direct map in the kernel address space, but it creates a new "restricted" address space that has the folios unmapped from the direct map by default. However, I don't think this is relevant here. IIUC, the purpose of this patch is to check if the folio is accessible by the kernel, which should be true even in the ASI restricted address space, because ASI will just transparently switch to the unrestricted kernel address space where the folio is mapped if needed. I hope this helps. > > On Mon, Oct 14, 2024 at 04:56:31PM GMT, Andrii Nakryiko wrote: > > From memfd_secret(2) manpage: > > > > The memory areas backing the file created with memfd_secret(2) are > > visible only to the processes that have access to the file descriptor= . > > The memory region is removed from the kernel page tables and only the > > page tables of the processes holding the file descriptor map the > > corresponding physical memory. (Thus, the pages in the region can't b= e > > accessed by the kernel itself, so that, for example, pointers to the > > region can't be passed to system calls.) > > > > We need to handle this special case gracefully in build ID fetching > > code. Return -EACCESS whenever secretmem file is passed to build_id_par= se() > > family of APIs. Original report and repro can be found in [0]. > > > > [0] https://lore.kernel.org/bpf/ZwyG8Uro%2FSyTXAni@ly-workstation/ > > > > Reported-by: Yi Lai > > Suggested-by: Shakeel Butt > > Fixes: de3ec364c3c3 ("lib/buildid: add single folio-based file reader a= bstraction") > > Signed-off-by: Andrii Nakryiko > > --- > > lib/buildid.c | 5 +++++ > > 1 file changed, 5 insertions(+) > > > > diff --git a/lib/buildid.c b/lib/buildid.c > > index 290641d92ac1..f0e6facf61c5 100644 > > --- a/lib/buildid.c > > +++ b/lib/buildid.c > > @@ -5,6 +5,7 @@ > > #include > > #include > > #include > > +#include > > > > #define BUILD_ID 3 > > > > @@ -64,6 +65,10 @@ static int freader_get_folio(struct freader *r, loff= _t file_off) > > > > freader_put_folio(r); > > > > + /* reject secretmem folios created with memfd_secret() */ > > + if (secretmem_mapping(r->file->f_mapping)) > > + return -EACCES; > > + > > r->folio =3D filemap_get_folio(r->file->f_mapping, file_off >> PA= GE_SHIFT); > > > > /* if sleeping is allowed, wait for the page, if necessary */ > > -- > > 2.43.5 > >