From patchwork Wed Sep 20 04:09:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13392074 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 3F949CE79AD for ; Wed, 20 Sep 2023 04:10:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C87806B0102; Wed, 20 Sep 2023 00:10:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C37FA6B0103; Wed, 20 Sep 2023 00:10:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD95B6B0104; Wed, 20 Sep 2023 00:10:03 -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 9DF886B0102 for ; Wed, 20 Sep 2023 00:10:03 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 638FF1402A7 for ; Wed, 20 Sep 2023 04:10:03 +0000 (UTC) X-FDA: 81255647886.10.6B3DB6A Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf30.hostedemail.com (Postfix) with ESMTP id BCD1080023 for ; Wed, 20 Sep 2023 04:10:01 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=V9vQj1mC; dmarc=none; spf=none (imf30.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695183001; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DnLTTShxPnnBRb70yIADtwL9X0JQ48GmAQfSWT/vi2I=; b=feeITUCqmz2qQ4RdlN9az0F3x50WXLzb5RNGWU6tlBuH3MuZNiL8auIlAasjLyOGcs8ewi our49ettPoq7MRKgJFXXSScJ2D4A/uglWZ/O79ur2rnP32D47iPtrQBtqe33m8ExI0tfGn hvxvQyFMlwJ09rCwOGRUkmApN56Ye6s= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=V9vQj1mC; dmarc=none; spf=none (imf30.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695183001; a=rsa-sha256; cv=none; b=efRy3OvmBHO8+0tJo6uFAJXPv087fmdITjlRtGbPxeLqRqNnCAz6T2R5C/5JaMMSK2mK20 v1D7D+xSMVHlUw3Z1Biwjob8rYr3GdVEvUz0m2VJCYxEP3gC3xixMQT8mA/t1PXW2uTa/i TMHB0Ra2gTgbpWbreMOdpGSgGT7VWUs= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=DnLTTShxPnnBRb70yIADtwL9X0JQ48GmAQfSWT/vi2I=; b=V9vQj1mCoPG5z/+CApE8v2jvGM oCmhjx9jmNof6h/Q9LE1l/qd/tGyK4SwYHeD5JXwNGAJzkbGcGKJiMmdfnOIbBxBSib1o4NogbSqE RueIf8xo/CGjVxRcmrIt5Lr/+v7at+3g7TGV3ocBS/KpA0gsw7RStablIA5Pp1h5mxbGoPdIycMfm bvjRyIe2+XBKfhA+2XveMFbFw4uAtKwh1THjPCi8B0d+orxi825+fzu0A9ffSOWsXUdO2fhPZ+uqR kxC6V50zfrXnmoT8kkoaH1drG4GybAos1A1DQQ0joAiyXPQtZ0trjXUSUUGotmXBDLL+h/XlabXR1 dubXJ2zg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qioXH-003dQt-UY; Wed, 20 Sep 2023 04:09:59 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, Yin Fengwei , Dave Hansen , David Hildenbrand , Thomas Gleixner , syzbot+55cc72f8cc3a549119df@syzkaller.appspotmail.com Subject: [PATCH 2/2] mm: Abstract moving to the next PFN Date: Wed, 20 Sep 2023 05:09:58 +0100 Message-Id: <20230920040958.866520-1-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230920035336.854212-1-willy@infradead.org> References: <20230920035336.854212-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: BCD1080023 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 3zsfkwgh4j6okmyrybdhjj7hibw6fpqu X-HE-Tag: 1695183001-70482 X-HE-Meta: U2FsdGVkX198qh+TRU12AeYn2iZUOytdwl8NrTgVbQt/wZdiDh+03WWYau50I7rkkdo9NLT6ZsqzBV37uOQDxdbxcb/rfZRszldEOq64+IqPrFAcUtRMnJ4XRrBfLE66YEAi34OiQNnIWAqsoThq6EKmooW+VE1UsmxLPGxTAmVQxaizYmxSDOGphXXo8W7GnIp/HrB5+UZq4GaER8b8QPnIfOmTcJBzhJ4ELsHQFt4GRUqsDgTW7iq9IigirIVVCpkOe7AuCz5u+Nqnjm67tLbDg+vK4CprtmCnxLpnWUyMrqrUl9DyccoEM0oYuh9cp4vE24qZpyICulrR3MDacKSjQROuZUbhntUUuMrNPcGsO963OBdwhY/bqQZLdBTAc+xTBIFCW5XkJbI7TAz5ZqCOrFP+JFASB1JGacSU04yDBATGpLMLQsBKRSkL0AxbVk+hJfHaVXeHIz052+yTBzterywhyf24GivTPwTfKdsIEQVBtmr+UTe3e/fN5Fp7dkmobHRLNvwrJ0iqQDhcN1425742ewpvO5NZFHCy7rkw1slm6KQaELpu3NQGbIGoUxM8fGGkuet5N10qB7fSqwxZ+2jiLk4Li18A1FRsX/tlmbgd0sI3UIM3Nu6Shyz8NpxucKsrOZh5cZYotqnT/rGDr6lng2LlUqLSVVrBcyhSpcLEaWnfP1jPiFypUuTYzIR2s/sMe1x2Lj96e2RlXasYdIqlNFw02Y+fd9ipUKI+Fo+lpq2gsL+doySMpw+sjbCt+cAL4nUCSvoG5fgFvdx70VadQdIs3gwvNMACB72Ht9rMmBZ4C4Xt1RGDBZj7/It3BsBNS4ZCbi4zDjC+soBwY/UNczoSiC/Ptxf/N0q3bSzig92g30EHGUkBIng7jeU+pFcm+R3rRvKaGN1T9acd+dDnQAcKhK93xxCZ3BpNGIkcaOAI1V4ey1CTyhuVIuE7RNY3IOX+NaZivks ifAHpfBJ Hl4txqi8BtTxcpe6v9oWMlaA+Gy/HPQe6k8tbTkIutXl3SopGyLoi9aH35uhP6YXn7n5Jo8YKJAsxjciRalNgpFiscyZD9FRRQmOcC/1RULEvtXjib3oKDD9fu7aYkABWTgZgIIWvOfj3aQOuDUc+viQRw6m5NMVuvV5n3mBgbJSCAYuCkkAEM9rEVTe2+NqUTj3CRcz4BSSxNp0f1UGjfmWEz7U8EGcDAno/L7k8BUfF1HYKr4WolqcJfaqLfC/0y+dGDLO/i8JvImUphQSYsLchPcQ9fTAy/b2Hoc6asy5XHSJF0KKaWiJGyfZi/Fyb7SPEb8XLrpBzNAkKQPiv8RyLkg== 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: In order to fix the L1TF vulnerability, x86 can invert the PTE bits for PROT_NONE VMAs, which means we cannot move from one PTE to the next by adding 1 to the PFN field of the PTE. Abstract advancing the PTE to the next PFN through a pte_next_pfn() function/macro. Signed-off-by: Matthew Wilcox (Oracle) Fixes: bcc6cc832573 ("mm: add default definition of set_ptes()") Reported-by: syzbot+55cc72f8cc3a549119df@syzkaller.appspotmail.com Reviewed-by: Yin Fengwei --- arch/x86/include/asm/pgtable.h | 8 ++++++++ include/linux/pgtable.h | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index d6ad98ca1288..e02b179ec659 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -955,6 +955,14 @@ static inline int pte_same(pte_t a, pte_t b) return a.pte == b.pte; } +static inline pte_t pte_next_pfn(pte_t pte) +{ + if (__pte_needs_invert(pte_val(pte))) + return __pte(pte_val(pte) - (1UL << PFN_PTE_SHIFT)); + return __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); +} +#define pte_next_pfn pte_next_pfn + static inline int pte_present(pte_t a) { return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE); diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 1fba072b3dac..af7639c3b0a3 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -206,6 +206,14 @@ static inline int pmd_young(pmd_t pmd) #endif #ifndef set_ptes + +#ifndef pte_next_pfn +static inline pte_t pte_next_pfn(pte_t pte) +{ + return __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); +} +#endif + /** * set_ptes - Map consecutive pages to a contiguous range of addresses. * @mm: Address space to map the pages into. @@ -231,7 +239,7 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr, if (--nr == 0) break; ptep++; - pte = __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); + pte = pte_next_pfn(pte); } arch_leave_lazy_mmu_mode(); }