From patchwork Tue Oct 15 11:12:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13836208 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 0B0E2CFC288 for ; Tue, 15 Oct 2024 11:12:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AE5D6B0082; Tue, 15 Oct 2024 07:12:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 75F2B6B0085; Tue, 15 Oct 2024 07:12:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6267D6B0088; Tue, 15 Oct 2024 07:12:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 42E026B0082 for ; Tue, 15 Oct 2024 07:12:46 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7B6D24154B for ; Tue, 15 Oct 2024 11:12:40 +0000 (UTC) X-FDA: 82675573680.23.2082B6D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf09.hostedemail.com (Postfix) with ESMTP id C24F214001A for ; Tue, 15 Oct 2024 11:12:38 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="S3U2a/4B"; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728990717; 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:references:dkim-signature; bh=FHbFI2SXndzro2xjf3G5uBQtpAQZ1sA34BP2wLkHiAs=; b=oXXPI0Mj8t0plDVXiwedd0vDuHhq4cwrioAJwkDhBeoEQynj8mbSakKtCnOULkQxPeCDkY uheAoMlM9wDfm8kAmg98eaU9HmAn84E9ymbrPDCU7Qdy5U+4yFtBnjhfKLMGqFtmcE2uj9 lZjT0IEO7sIGwXCJx7dYcKZJ+TRMu5M= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="S3U2a/4B"; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728990717; a=rsa-sha256; cv=none; b=s8LJ+BPMvglEGlJlFjc1XEGtKn4MwzGIC6MFPktTKF/0FAIDHxTBLuzTTepFNqgB2V2aAD t6ouBB1DOyCl3DWWiAaC1yL0kK9u9xbTfTh6nLqh1eBz8mFhixwyvliRYwE/KNSmZ1gkBm +Csgz/LSkic4YcpI3i/RbrXhWmebtXQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728990763; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=FHbFI2SXndzro2xjf3G5uBQtpAQZ1sA34BP2wLkHiAs=; b=S3U2a/4B9qTdDYqbZvSHx1bAJPA5LRQ2CGYt7Aw07z7+RbhQJ52sMjzxRlaC5zfPlvYFGB JMKsJOSHf4KhYzKpmcUAkuGqdQGJjJWQJ1dcrxythze87Zi2yeIxzTYSQ+G9ApIcHpiibm 5G4oBJ0WzfO+PysPgwENFInf7pwsjvc= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-467-KqxHEe-KOGCPKqDu-MxQYw-1; Tue, 15 Oct 2024 07:12:42 -0400 X-MC-Unique: KqxHEe-KOGCPKqDu-MxQYw-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BD22B194511A; Tue, 15 Oct 2024 11:12:40 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.22.32.43]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 230863000198; Tue, 15 Oct 2024 11:12:37 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , syzbot+7d917f67c05066cec295@syzkaller.appspotmail.com, Andrew Morton , Jann Horn Subject: [PATCH v1] mm/pagewalk: fix usage of pmd_leaf()/pud_leaf() without present check Date: Tue, 15 Oct 2024 13:12:36 +0200 Message-ID: <20241015111236.1290921-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Rspam-User: X-Stat-Signature: 8chfhk187p9j3kydftffud13yxjkpxsn X-Rspamd-Queue-Id: C24F214001A X-Rspamd-Server: rspam11 X-HE-Tag: 1728990758-753234 X-HE-Meta: U2FsdGVkX1/hZY6w6PjR0DQ26XZfzG7u6foCtKLPDE7g8xCVz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OaxmzTGH6m1h4/mCyiNKdY+jzFkUPh2OMWMNSGZ5TQMcqxlSif+80+RjBBTv3tITxq7qecyIQxTAOj9un2d8Tm6omTpUltox80jGDsDnXv8jn/Mu1Ud5CdWxS5TJK0Spok6MoKQMb1IY0UlAaV8u+ECnb8xOQw5jrK3UGwsIEd/3hlK24ZS2hR1tRcS6YgLOsqdvCfssrdO2c2zycZIY/ciPzVuQLB+W2ui5oyS2eWhHuMRG7qvf5TF+xW9VB3TG+PBRJHcZBDU+6n7MTt9Wed6K4r9oWAf/U6YRIAigsJwUZNxOK2D3Rj5wfaAE6nG82r4EJJXoTt/k8fFn0iUfMmSDjON/g9P0sylhSRt2ih33myPx8oS6rozAbjmEClyxD1LTYWf3XacJMFqF/9UjyVJ6nvNBklgrYFu9FdIR/hsa8YHbUnRlbO+z35bpiJsXOrcMLwG8Ju3ni16YmLcV8X9UcImYIx8p6PO5M/MlDik1WMHaO2fFmg== 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: pmd_leaf()/pud_leaf() only implies a pmd_present()/pud_present() check on some architectures. We really should check for pmd_present()/pud_present() first. This should explain the report we got on ppc64 (which has CONFIG_PGTABLE_HAS_HUGE_LEAVES set in the config) that triggered: VM_WARN_ON_ONCE(pmd_leaf(pmdp_get_lockless(pmdp))); Likely we had a PMD migration entry for which pmd_leaf() did not trigger. We raced with restoring the PMD migration entry, and suddenly saw a pmd_leaf(). In this case, pte_offset_map_lock() saved us from more trouble, because it rechecks the PMD value, but we would not have processed the migration entry -- which is not too bad because the only user of FW_MIGRATION is KSM for unsharing, and KSM only applies to small folios. Further, we shouldn't re-read the PMD/PUD value for our warning, the primary purpose of the VM_WARN_ON_ONCE() is to find spurious use of pmd_leaf()/pud_leaf() without CONFIG_PGTABLE_HAS_HUGE_LEAVES. As a side note, we are currently not implementing FW_MIGRATION support for PUD migration entries, which likely should exist due to hugetlb. Add a TODO so this won't fall through the cracks if more FW_MIGRATION users get added. Fixes: aa39ca6940f1 ("mm/pagewalk: introduce folio_walk_start() + folio_walk_end()") Reported-by: syzbot+7d917f67c05066cec295@syzkaller.appspotmail.com Closes: https://lkml.kernel.org/r/670d3248.050a0220.3e960.0064.GAE@google.com Cc: Andrew Morton Cc: Jann Horn Signed-off-by: David Hildenbrand Acked-by: Kirill A. Shutemov Acked-by: Qi Zheng --- mm/pagewalk.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mm/pagewalk.c b/mm/pagewalk.c index 461ea3bbd8d9..5f9f01532e67 100644 --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -744,7 +744,8 @@ struct folio *folio_walk_start(struct folio_walk *fw, pud = pudp_get(pudp); if (pud_none(pud)) goto not_found; - if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) && pud_leaf(pud)) { + if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) && + (!pud_present(pud) || pud_leaf(pud))) { ptl = pud_lock(vma->vm_mm, pudp); pud = pudp_get(pudp); @@ -753,6 +754,10 @@ struct folio *folio_walk_start(struct folio_walk *fw, fw->pudp = pudp; fw->pud = pud; + /* + * TODO: FW_MIGRATION support for PUD migration entries + * once there are relevant users. + */ if (!pud_present(pud) || pud_devmap(pud) || pud_special(pud)) { spin_unlock(ptl); goto not_found; @@ -769,12 +774,13 @@ struct folio *folio_walk_start(struct folio_walk *fw, } pmd_table: - VM_WARN_ON_ONCE(pud_leaf(*pudp)); + VM_WARN_ON_ONCE(!pud_present(pud) || pud_leaf(pud)); pmdp = pmd_offset(pudp, addr); pmd = pmdp_get_lockless(pmdp); if (pmd_none(pmd)) goto not_found; - if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) && pmd_leaf(pmd)) { + if (IS_ENABLED(CONFIG_PGTABLE_HAS_HUGE_LEAVES) && + (!pmd_present(pmd) || pmd_leaf(pmd))) { ptl = pmd_lock(vma->vm_mm, pmdp); pmd = pmdp_get(pmdp); @@ -786,7 +792,7 @@ struct folio *folio_walk_start(struct folio_walk *fw, if (pmd_none(pmd)) { spin_unlock(ptl); goto not_found; - } else if (!pmd_leaf(pmd)) { + } else if (pmd_present(pmd) && !pmd_leaf(pmd)) { spin_unlock(ptl); goto pte_table; } else if (pmd_present(pmd)) { @@ -812,7 +818,7 @@ struct folio *folio_walk_start(struct folio_walk *fw, } pte_table: - VM_WARN_ON_ONCE(pmd_leaf(pmdp_get_lockless(pmdp))); + VM_WARN_ON_ONCE(!pmd_present(pmd) || pmd_leaf(pmd)); ptep = pte_offset_map_lock(vma->vm_mm, pmdp, addr, &ptl); if (!ptep) goto not_found;