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 E1D76C47DD9 for ; Sun, 24 Mar 2024 10:30:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED5816B0082; Sun, 24 Mar 2024 06:30:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E85B76B0083; Sun, 24 Mar 2024 06:30:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4CBD6B0085; Sun, 24 Mar 2024 06:30:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C201B6B0082 for ; Sun, 24 Mar 2024 06:30:55 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 380AC1C03DA for ; Sun, 24 Mar 2024 10:30:55 +0000 (UTC) X-FDA: 81931564470.04.4013704 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 883DBA002A for ; Sun, 24 Mar 2024 10:30:52 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Y5Soixto; spf=pass (imf25.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711276252; a=rsa-sha256; cv=none; b=DE+EsJsNht/d2ZOCR+D19EaA1d4PFl0AdIWzgSGNUMCy0ZorWwAW2eq7j4z7ATg4taOmKb go9iMZUWCvXqPrOUOlR6+pMsKzfYAMKRWCVOj0L/fb5IQ6egHdA7BBSLv5lo0D1dhXncVB aCDjYUZ8lSygOqrhyOwztIw0kITArpQ= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Y5Soixto; spf=pass (imf25.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711276252; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=700x+sZihPBn26Dm/v5tD9fkmelNi2GL5OcZmwLtGRo=; b=5F+14oXiTUkmxqO3+9quXOiKm6Gh1miX+Agg2vVK9FHhKLakk4rdPQn+TEhxyZ7OI/JkWn Y5dfv8n3E8y37DoYPsMhJTyjTNNXjNxsrBGOOJj8CuKlrPBOeYR1RiFZu8MtnhvdE+w8vo xx2VmbpE+0Zpz9qO2zQWdU6qOHSZess= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 602A160B7B; Sun, 24 Mar 2024 10:30:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 211E3C433F1; Sun, 24 Mar 2024 10:30:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711276251; bh=i/KBVM/hVsILZHtcXZzSHp2NdrbsYCYNR/zIoJ3kBaA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Y5SoixtoQOXDGlmn7h4qZyREMJ6mWoeIaoniDbaB8DYpPY4lP+Fk8FhR3xwxK1gz9 AM/JCXIvzOlhNa5wokIeF6+ZUWRYzPOvRYLye6bcgeNex82WYQCdM0mkSvmmmulpeV THXt3BFrRP2WCMTz0ZPuCrEKPSW6KRibMppHqcY663WUZURlspcm5IiTZESzoAvUKw VEMbUJukG4OZ5KnKIb4aQGbCwqGbOFfKBCaRQMeft13CXgUXLJHLDOWtKxvA52sQmr OinKzNAeqGJ5VJwisWLebE4hjcZGDXK+8ygilZGv3N0ihFBq0TVZCouP+4OQOn1IfL nOFBi5g8gWsKA== Date: Sun, 24 Mar 2024 12:29:39 +0200 From: Mike Rapoport To: David Hildenbrand Cc: Miklos Szeredi , xingwei lee , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, samsun1006219@gmail.com, syzkaller-bugs@googlegroups.com, linux-mm Subject: Re: BUG: unable to handle kernel paging request in fuse_copy_do Message-ID: References: <620f68b0-4fe0-4e3e-856a-dedb4bcdf3a7@redhat.com> <463612f2-5590-4fb3-8273-0d64c3fd3684@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 883DBA002A X-Stat-Signature: xomb99i7p14o4aesiwss1e9e7s319moo X-Rspam-User: X-HE-Tag: 1711276252-409666 X-HE-Meta: U2FsdGVkX18pIjAXXPxjo+hsBcyaqf8lNrrgHT8AU+g/aDRBwSmXgNNeRMAm7GQ7i5zO6RWpY+itaG21zrYgS7VfJrbmc+KQEpnVeLeoskepioAMxj+shwd9t1SIokmLft4YlM8tIocEfBjZKT+DQu7MMnlkWqq+S3jFDDBNrs2fv0b98vhzyXLCyzWphq2D+8QHholKRoIOkLHBuc9bE3BbyTkHGz99B2QEqh+YTt03N4OfyX+Dplwup79tpoOWzHGKIrsqSb3VjJls+ble/5RkuEWCvHFv2R2vbJx47Q5gvAZAGq5I4x57FWcelH2xs4lyweFitPuGY4Z1wkg6W+fwCGKU6d8qzL5+TVDdCBFNUCuAjGa1liD6H74Q3c3oBX7IzlYNlCrBOcvXvHnxxBA2hXkt+kcYRPDKVuq8n+H1Cmiahz28hjcuEBgM/trsrD5avyNtg6Ed0VIjkpJjfDLfGkF1MXteYyovKHNLbsdozZzlkSnPMJWvFhqWs/omTX0FBIN9OzuWkQoe97aupVcoFMyTmN4iNL//gPBQtqg6omvKvxVjPOaKWlinXbL4PmVHgQdmu+12hmRnP2xRVnOEC/onh5jALEp4BQXHytje0DiAImQGrtzwrCneyLFh3d2SA10/NpCwLRCCREErF84itKtDfuvdPR1nSNwSU7bhtSmzn0k33V4kAi8y1HClJLDUtRaiZw0Kkj5Qj74StPDhpc2iAfeOqz32MKFJfLOnzr8JA79fO4FAMXJiGTxiye0dBUaeZ5PcaClCoA+OqY4WHPUnS6eI6jnRsdfdejGYdi03Op4rzPL2mI9B6+7VHqGqEL2P3ZYN5LsuRYjO6fq3kVAxMl/0+VK3S0MdceRHONAn+JWfdge94cl2fQ/KTH98IKiUBYgnMwpsfw/L9W5U52NB74kTz50azDDEJu24mVQ096ScTotg0/9h1MQmIKHrvnqQXwPanfd+P1k iWB1Uh0T CcetW+/Irjm6AYvojfxozNQn6cbQED7zFs+TCrXDiAPobVy9gyZR3qH0EdFBK87fgiwVwDab6HRqnfZuvmeyG9XqJnZAc/mK0MgSMV3kzIlyX30bCWRR/CV6VJtGUiABCnpaP3yWBS4wHMX+H7XkR9ZTFACfWV5BmcS0yzZCT11J/drhocPCYsjYEHlDHJExhe0o4lM1ZT56hG0YNlaMz4TEZ2pOFOmgZidLFevjT7haCxAtp7CfUThiFASsNjV2TyUW2ji8QHQ8tZSfIH4Gr+HGFR2InIf9hrOtsnj2gdlVnVqcGbzr82bYhlWcRYp1WjevF/XxmsmNJmgL6WcUHvDwmsw== 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, Mar 22, 2024 at 10:56:08PM +0100, David Hildenbrand wrote: > On 22.03.24 22:37, David Hildenbrand wrote: > > On 22.03.24 22:33, David Hildenbrand wrote: > > > On 22.03.24 22:18, David Hildenbrand wrote: > > > > On 22.03.24 22:13, Miklos Szeredi wrote: > > > > > On Fri, 22 Mar 2024 at 22:08, David Hildenbrand wrote: > > > > > > > > > > > > On 22.03.24 20:46, Miklos Szeredi wrote: > > > > > > > On Fri, 22 Mar 2024 at 16:41, David Hildenbrand wrote: > > > > > > > > > > > > > > > But at least the vmsplice() just seems to work. Which is weird, because > > > > > > > > GUP-fast should not apply (page not faulted in?) > > > > > > > > > > > > > > But it is faulted in, and that indeed seems to be the root cause. > > > > > > > > > > > > secretmem mmap() won't populate the page tables. So it's not faulted in yet. > > > > > > > > > > > > When we GUP via vmsplice, GUP-fast should not find it in the page tables > > > > > > and fallback to slow GUP. > > > > > > > > > > > > There, we seem to pass check_vma_flags(), trigger faultin_page() to > > > > > > fault it in, and then find it via follow_page_mask(). > > > > > > > > > > > > ... and I wonder how we manage to skip check_vma_flags(), or otherwise > > > > > > managed to GUP it. > > > > > > > > > > > > vmsplice() should, in theory, never succeed here. > > > > > > > > > > > > Weird :/ > > > > > > > > > > > > > Improved repro: > > > > > > > > > > > > > > #define _GNU_SOURCE > > > > > > > > > > > > > > #include > > > > > > > #include > > > > > > > #include > > > > > > > #include > > > > > > > #include > > > > > > > #include > > > > > > > > > > > > > > int main(void) > > > > > > > { > > > > > > > int fd1, fd2; > > > > > > > int pip[2]; > > > > > > > struct iovec iov; > > > > > > > char *addr; > > > > > > > int ret; > > > > > > > > > > > > > > fd1 = syscall(__NR_memfd_secret, 0); > > > > > > > addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0); > > > > > > > ftruncate(fd1, 7); > > > > > > > addr[0] = 1; /* fault in page */ > > > > > > > > > > Here the page is faulted in and GUP-fast will find it. It's not in > > > > > the kernel page table, but it is in the user page table, which is what > > > > > matter for GUP. > > > > > > > > Trust me, I know the GUP code very well :P > > > > > > > > gup_pte_range -- GUP fast -- contains: > > > > > > > > if (unlikely(folio_is_secretmem(folio))) { > > > > gup_put_folio(folio, 1, flags); > > > > goto pte_unmap; > > > > } > > > > > > > > So we "should" be rejecting any secretmem folios and fallback to GUP slow. > > > > > > > > > > > > ... we don't check the same in gup_huge_pmd(), but we shouldn't ever see > > > > THP in secretmem code. > > > > > > > > > > Ehm: > > > > > > [ 29.441405] Secretmem fault: PFN: 1096177 > > > [ 29.442092] GUP-fast: PFN: 1096177 > > > > > > > > > ... is folio_is_secretmem() broken? > > > > > > ... is it something "obvious" like: > > > > > > diff --git a/include/linux/secretmem.h b/include/linux/secretmem.h > > > index 35f3a4a8ceb1e..6996f1f53f147 100644 > > > --- a/include/linux/secretmem.h > > > +++ b/include/linux/secretmem.h > > > @@ -16,7 +16,7 @@ static inline bool folio_is_secretmem(struct folio *folio) > > > * We know that secretmem pages are not compound and LRU so we can > > > * save a couple of cycles here. > > > */ > > > - if (folio_test_large(folio) || !folio_test_lru(folio)) > > > + if (folio_test_large(folio) || folio_test_lru(folio)) > > > return false; > > > mapping = (struct address_space *) > > > > ... yes, that does the trick! > > > > Proper patch (I might send out again on Monday "officially"). There are > other improvements we want to do to folio_is_secretmem() in the light of > folio_fast_pin_allowed(), that I wanted to do a while ago. I might send > a patch for that as well now that I'm at it. The most robust but a bit slower solution is to make folio_is_secretmem() call folio_mapping() rather than open code the check. What improvements did you have in mind? > From 85558a46d9f249f26bd77dd3b18d14f248464845 Mon Sep 17 00:00:00 2001 > From: David Hildenbrand > Date: Fri, 22 Mar 2024 22:45:36 +0100 > Subject: [PATCH] mm/secretmem: fix GUP-fast succeeding on secretmem folios > > folio_is_secretmem() states that secretmem folios cannot be LRU folios: > so we may only exit early if we find an LRU folio. Yet, we exit early if > we find a folio that is not a secretmem folio. > > Consequently, folio_is_secretmem() fails to detect secretmem folios and, > therefore, we can succeed in grabbing a secretmem folio during GUP-fast, > crashing the kernel when we later try reading/writing to the folio, because > the folio has been unmapped from the directmap. > > Reported-by: xingwei lee > Reported-by: yue sun > Debugged-by: Miklos Szeredi > Fixes: 1507f51255c9 ("mm: introduce memfd_secret system call to create "secret" memory areas") > Cc: > Signed-off-by: David Hildenbrand Reviewed-by: Mike Rapoport (IBM) > --- > include/linux/secretmem.h | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/include/linux/secretmem.h b/include/linux/secretmem.h > index 35f3a4a8ceb1..6996f1f53f14 100644 > --- a/include/linux/secretmem.h > +++ b/include/linux/secretmem.h > @@ -16,7 +16,7 @@ static inline bool folio_is_secretmem(struct folio *folio) > * We know that secretmem pages are not compound and LRU so we can > * save a couple of cycles here. > */ > - if (folio_test_large(folio) || !folio_test_lru(folio)) > + if (folio_test_large(folio) || folio_test_lru(folio)) > return false; > mapping = (struct address_space *) > -- > 2.43.2 > > > -- > Cheers, > > David / dhildenb > -- Sincerely yours, Mike.