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 3D563ECAAD4 for ; Fri, 26 Aug 2022 14:25:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F1316B0074; Fri, 26 Aug 2022 10:25:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A0366B0075; Fri, 26 Aug 2022 10:25:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36818940007; Fri, 26 Aug 2022 10:25:15 -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 21B386B0074 for ; Fri, 26 Aug 2022 10:25:15 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EE9B11A07C5 for ; Fri, 26 Aug 2022 14:25:14 +0000 (UTC) X-FDA: 79841966148.16.9CBD86D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf16.hostedemail.com (Postfix) with ESMTP id 1BB5918001A for ; Fri, 26 Aug 2022 14:25:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661523912; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Y4bjLXg/JtmRKJW/PsE4ZvXOvyRGRLjuYGGABsuG5Dk=; b=Z451xlg1Pf8bDWVs+8BIICvLF5fupHFlMFjjsQ0/YGuaKpMmEyjvW2cGjsM8Mfxa2C1RoU j9RoI5X5qpNbueigMQTLixYxdlH6IIPWDT5kUNmM26J9RH0yJjocyoaPsA181EZPxWTt7R FC5SZgPfuRrkULPE7rSyNf7jMAAe0as= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-473-Gr8y9si6PoSSC_Shcjf-eA-1; Fri, 26 Aug 2022 10:25:11 -0400 X-MC-Unique: Gr8y9si6PoSSC_Shcjf-eA-1 Received: by mail-qk1-f197.google.com with SMTP id x22-20020a05620a259600b006b552a69231so1337839qko.18 for ; Fri, 26 Aug 2022 07:25:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc; bh=Y4bjLXg/JtmRKJW/PsE4ZvXOvyRGRLjuYGGABsuG5Dk=; b=CiFlo93sSTo7gZ5jrUtI036gC5+m7JodnkEuaLG40dRclMLixHL+weM/Pj1druR0z/ ojPMZbrvpDlyHG+L2p2oiCAG51NBaWpdO36j0tCERNInHKuVIwM+lvtYQC84s88xKqCU ZklS8zJAVLlpy51HM5Iy2vNIE2M9asuFA1/AbiA7iayKj5QpUgtrG+3EpFTHWobXQ8hy k6loECv4+09UDlKu4rBJgWSQooGo2E1Cc2iyJ4TQkXE6uepxptmaMj2lQzdvA1wAX/XV trpvmo3wrvt9/1tAS1kMziGksk0ZnVXf8nm1MV9rDDXACI3hRQ+7Jbx/kcRP+vu6MOfa QWVg== X-Gm-Message-State: ACgBeo1iqckol8fMxbGznV+HtTlLlPvDGM9WoN8NpVFO79WZTjRnLULy MyyWRwHm/jG3IYh7rm+tNKg5bKiuCecwkoby28oMRYXp6Wm/OeFWx8HJpHmp/m4rT0x6RhQ8PL+ sD8jY9J02LEw= X-Received: by 2002:a05:622a:6027:b0:343:5691:93de with SMTP id he39-20020a05622a602700b00343569193demr8069012qtb.165.1661523910500; Fri, 26 Aug 2022 07:25:10 -0700 (PDT) X-Google-Smtp-Source: AA6agR7+t+rG++BFNW0uDP/2t8VMm7035UgpMd2ct1n9bL/iw1fkQYla9mH099oUJxPeUBOcLBE22Q== X-Received: by 2002:a05:622a:6027:b0:343:5691:93de with SMTP id he39-20020a05622a602700b00343569193demr8068988qtb.165.1661523910244; Fri, 26 Aug 2022 07:25:10 -0700 (PDT) Received: from xz-m1.local (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id x19-20020a05620a449300b006b629f86244sm2033590qkp.50.2022.08.26.07.25.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Aug 2022 07:25:09 -0700 (PDT) Date: Fri, 26 Aug 2022 10:25:08 -0400 From: Peter Xu To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Huang Ying , stable@vger.kernel.org, Yu Zhao Subject: Re: [PATCH] mm/mprotect: Only reference swap pfn page if type match Message-ID: References: <20220823221138.45602-1-peterx@redhat.com> <411d7b8c-f914-875e-b397-856e6a894366@redhat.com> MIME-Version: 1.0 In-Reply-To: <411d7b8c-f914-875e-b397-856e6a894366@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661523913; a=rsa-sha256; cv=none; b=F630OERjdh3BgAbM1y4RsRYUB2PX9lT8UckIHpQS3z36bjUMItI4OOdPI8TiC2HKP1G5WT l9LXfhsagDD6JmsQn5oWdjBlhJkFVXNIGNddW8+b1OqDVoYEzjQsCHGwa8RNeOxVmFdcxp +chMKMWGPO0eADtbak/9P+PJc7YMZIM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Z451xlg1; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661523913; 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=Y4bjLXg/JtmRKJW/PsE4ZvXOvyRGRLjuYGGABsuG5Dk=; b=2Go2ksvnpRwKWVGk2u8XJajNbDa6BTS54mtSPl1mtiD8b1AT2eQ9v84Yxr/ChewzVjAU6k sHd0/l1ZuQt+8D5bor2c7YVejJFy90kUBV9izx5ae0PAVujd18FIozBhZEtNBSa0E5at7v J1Y5FwdAerak1s8OerOk96GYmHbr/cg= Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Z451xlg1; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ufrzpac5szq8ukczwojmnb3s11pjauu8 X-Rspamd-Queue-Id: 1BB5918001A X-HE-Tag: 1661523912-686122 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: On Fri, Aug 26, 2022 at 12:49:37PM +0200, David Hildenbrand wrote: > On 24.08.22 00:11, Peter Xu wrote: > > Yu Zhao reported a bug after the commit "mm/swap: Add swp_offset_pfn() to > > fetch PFN from swap entry" added a check in swp_offset_pfn() for swap type [1]: > > > > kernel BUG at include/linux/swapops.h:117! > > CPU: 46 PID: 5245 Comm: EventManager_De Tainted: G S O L 6.0.0-dbg-DEV #2 > > RIP: 0010:pfn_swap_entry_to_page+0x72/0xf0 > > Code: c6 48 8b 36 48 83 fe ff 74 53 48 01 d1 48 83 c1 08 48 8b 09 f6 > > c1 01 75 7b 66 90 48 89 c1 48 8b 09 f6 c1 01 74 74 5d c3 eb 9e <0f> 0b > > 48 ba ff ff ff ff 03 00 00 00 eb ae a9 ff 0f 00 00 75 13 48 > > RSP: 0018:ffffa59e73fabb80 EFLAGS: 00010282 > > RAX: 00000000ffffffe8 RBX: 0c00000000000000 RCX: ffffcd5440000000 > > RDX: 1ffffffffff7a80a RSI: 0000000000000000 RDI: 0c0000000000042b > > RBP: ffffa59e73fabb80 R08: ffff9965ca6e8bb8 R09: 0000000000000000 > > R10: ffffffffa5a2f62d R11: 0000030b372e9fff R12: ffff997b79db5738 > > R13: 000000000000042b R14: 0c0000000000042b R15: 1ffffffffff7a80a > > FS: 00007f549d1bb700(0000) GS:ffff99d3cf680000(0000) knlGS:0000000000000000 > > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > > CR2: 0000440d035b3180 CR3: 0000002243176004 CR4: 00000000003706e0 > > DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > > DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > > Call Trace: > > > > change_pte_range+0x36e/0x880 > > change_p4d_range+0x2e8/0x670 > > change_protection_range+0x14e/0x2c0 > > mprotect_fixup+0x1ee/0x330 > > do_mprotect_pkey+0x34c/0x440 > > __x64_sys_mprotect+0x1d/0x30 > > > > It triggers because pfn_swap_entry_to_page() could be called upon e.g. a > > genuine swap entry. > > > > Fix it by only calling it when it's a write migration entry where the page* > > is used. > > > > [1] https://lore.kernel.org/lkml/CAOUHufaVC2Za-p8m0aiHw6YkheDcrO-C3wRGixwDS32VTS+k1w@mail.gmail.com/ > > > > Fixes: 6c287605fd56 ("mm: remember exclusively mapped anonymous pages with PG_anon_exclusive") > > Cc: David Hildenbrand > > Cc: > > Reported-by: Yu Zhao > > Signed-off-by: Peter Xu > > --- > > mm/mprotect.c | 3 ++- > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > diff --git a/mm/mprotect.c b/mm/mprotect.c > > index f2b9b1da9083..4549f5945ebe 100644 > > --- a/mm/mprotect.c > > +++ b/mm/mprotect.c > > @@ -203,10 +203,11 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, > > pages++; > > } else if (is_swap_pte(oldpte)) { > > swp_entry_t entry = pte_to_swp_entry(oldpte); > > - struct page *page = pfn_swap_entry_to_page(entry); > > pte_t newpte; > > > > if (is_writable_migration_entry(entry)) { > > + struct page *page = pfn_swap_entry_to_page(entry); > > + > > /* > > * A protection check is difficult so > > * just be safe and disable write > > > Stumbling over the THP code, I was wondering if we also want to adjust change_huge_pmd() > and hugetlb_change_protection. There are no actual swap entries, so I assume we're fine. > > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index 482c1826e723..466364e7fc5f 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -1798,10 +1798,10 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, > #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION > if (is_swap_pmd(*pmd)) { > swp_entry_t entry = pmd_to_swp_entry(*pmd); > - struct page *page = pfn_swap_entry_to_page(entry); > > VM_BUG_ON(!is_pmd_migration_entry(*pmd)); > if (is_writable_migration_entry(entry)) { > + struct page *page = pfn_swap_entry_to_page(entry); > pmd_t newpmd; > /* > * A protection check is difficult so > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 2480ba627aa5..559465fae5cd 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -6370,9 +6370,9 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, > } > if (unlikely(is_hugetlb_entry_migration(pte))) { > swp_entry_t entry = pte_to_swp_entry(pte); > - struct page *page = pfn_swap_entry_to_page(entry); > > if (!is_readable_migration_entry(entry)) { > + struct page *page = pfn_swap_entry_to_page(entry); > pte_t newpte; > > if (PageAnon(page)) > > > @Peter, what's your thought? IMHO they're not needed? The rule is simple in my mind: we should only pass in a pfn-typed swap entry into pfn_swap_entry_to_page() (or the new swp_offset_pfn()), or it's a violation of the API. In these two cases they do not violate the API and they're always safe because they're guaranteed to be pfn swap entries when calling. Thanks, -- Peter Xu