From nobody Fri Dec 19 19:15:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A0E4C7C0B7 for ; Mon, 15 Jul 2024 19:21:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071313; cv=none; b=Nug+F86yI0nJF5X/9ReG8earGuIVVEZDBkFsnvCTU299G4jex0i1le6dZ733/m0YloxC4U6FTEW/LLjTxNb4BEJSvUIw4mgCnv/xr98cngMpwOpQD0ZSVrTDr4mlrXyNQ1E91xdLEZqECwrTCIm7SA1vZBnEckfuPIW5tYqI4No= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071313; c=relaxed/simple; bh=B0IpMvJzke/973HZPlLZKBuCQcswwxmVEGuJbnrmXVU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J+6sal+8yVc9E5vrwZSUO7DeyvDCaQHSxBhbQfecmgIsWXgrgqk5e05fVv5+eWdc1JrfdCCCj5kMGxilUICEJacjDShcbg4ynY2ZW8TTZ9ATfStLkZ2X4A3hs4++VTiJqzYhOS7vhK06ruQz1Qv+CiT5/+nrQYj/ExLGqLYI0qU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=f1dQFmFI; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="f1dQFmFI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721071310; 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: in-reply-to:in-reply-to:references:references; bh=xrFUBbcc9D0ORnx0qoNz81Y3Ewmb+xGo+hBykZs/BiM=; b=f1dQFmFIvE39L4Oz1vCjEab6/3xCsZp91iUrO/L6DzsAITR8+D5/B4cc7oF4aIFl8Ix34c HNMMDhHVDgR1gTHPhOo3RPxFw9TKIWvpc/vuMR59BjmXeuTMNVVOUo2zUlkiYR+Rc8W7xx fxMxe92vWLe12VkyWL2GraL656ZMM38= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-303-40PEkCWSOQeOZ6yJOcCfcw-1; Mon, 15 Jul 2024 15:21:49 -0400 X-MC-Unique: 40PEkCWSOQeOZ6yJOcCfcw-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6b7678caf7dso6080926d6.1 for ; Mon, 15 Jul 2024 12:21:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721071308; x=1721676108; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xrFUBbcc9D0ORnx0qoNz81Y3Ewmb+xGo+hBykZs/BiM=; b=T/EQEP5NPKh2tRIq4+6YBZMC0xko9HTUUSVi3Mz3Q66w2+UYqSKtqjRMTxqSjQzPPb a/fo6E+843TxKmycryPjlrXKTEtzpDb9kqy3/0zSXRznFddNqu+RlrwnRR9VMNrF6Br9 m1loGoIU7oPxKSC0qqUyWlxS0hSk1pP6slnt+1CV6DnQl/IM/glCNFzXG1njTkrisvUj fEzxHNiQ4ArYmRZWnx115h/4cv93QOGfdQ/lP7U5XzonEXLaoog/oPaYsB2rivhW6+pV gn8oUtDbXIsfMLw2DBsWKKgkW+MHkgdVFnCsplv50f12brZ+xyEj7deLaioHwv2z2LOU S1Bg== X-Forwarded-Encrypted: i=1; AJvYcCUVLwXmKvKZPSIdGIAVIYH1HwrAEu+RVgpuphzOeSV7Mi2PGuiam6F8BidXWTqy0pdfWTFNrzkq9Gohb82S/PiqKXoGnzYQzWDEUVgc X-Gm-Message-State: AOJu0YxD0R2vJI8MU9q3lzTwbYuBp+Xvz6hkXYoNQ8N5OSB5X7pWD4eS BY1Yn7S7sR3LhWTpNdvDGi0HMlB6nUchzGw4m3IAw1nXDt18InaVZghUDPR5YH7EcdYAyDMIvT7 N3rdDNa2m7FzgsFpJe2IrTi8JzabDbR3KTu9XIuiMw5qBzTc2B6t4B8Klyo0qyA== X-Received: by 2002:a05:6214:3209:b0:6b7:64a2:3189 with SMTP id 6a1803df08f44-6b77e17f47fmr4877726d6.8.1721071308370; Mon, 15 Jul 2024 12:21:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFchV1JB+E55nHTPY9KJJNPGb9+S7LV2RfQetOl80mLgqA/kra292Zxs5L4jIR6tRho3Ee1yQ== X-Received: by 2002:a05:6214:3209:b0:6b7:64a2:3189 with SMTP id 6a1803df08f44-6b77e17f47fmr4877546d6.8.1721071308060; Mon, 15 Jul 2024 12:21:48 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b761978d30sm24039356d6.31.2024.07.15.12.21.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 12:21:47 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Jiang , Rik van Riel , Dave Hansen , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Matthew Wilcox , Rick P Edgecombe , peterx@redhat.com, Oscar Salvador , Mel Gorman , Andrew Morton , Borislav Petkov , Christophe Leroy , Huang Ying , "Kirill A . Shutemov" , "Aneesh Kumar K . V" , Dan Williams , Thomas Gleixner , Hugh Dickins , x86@kernel.org, Nicholas Piggin , Vlastimil Babka , Ingo Molnar Subject: [PATCH v3 1/8] mm/dax: Dump start address in fault handler Date: Mon, 15 Jul 2024 15:21:35 -0400 Message-ID: <20240715192142.3241557-2-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240715192142.3241557-1-peterx@redhat.com> References: <20240715192142.3241557-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently the dax fault handler dumps the vma range when dynamic debugging enabled. That's mostly not useful. Dump the (aligned) address instead with the order info. Signed-off-by: Peter Xu Acked-by: David Hildenbrand --- drivers/dax/device.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/dax/device.c b/drivers/dax/device.c index eb61598247a9..714174844ca5 100644 --- a/drivers/dax/device.c +++ b/drivers/dax/device.c @@ -235,9 +235,9 @@ static vm_fault_t dev_dax_huge_fault(struct vm_fault *v= mf, unsigned int order) int id; struct dev_dax *dev_dax =3D filp->private_data; =20 - dev_dbg(&dev_dax->dev, "%s: %s (%#lx - %#lx) order:%d\n", current->comm, - (vmf->flags & FAULT_FLAG_WRITE) ? "write" : "read", - vmf->vma->vm_start, vmf->vma->vm_end, order); + dev_dbg(&dev_dax->dev, "%s: op=3D%s addr=3D%#lx order=3D%d\n", current->c= omm, + (vmf->flags & FAULT_FLAG_WRITE) ? "write" : "read", + vmf->address & ~((1UL << (order + PAGE_SHIFT)) - 1), order); =20 id =3D dax_read_lock(); if (order =3D=3D 0) --=20 2.45.0 From nobody Fri Dec 19 19:15:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0783137932 for ; Mon, 15 Jul 2024 19:21:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071317; cv=none; b=TnjMBdqoyU7ka5sra5msicG+in0Ck+ogwcAn7Wr6S3c8MwbpYeH6TzkAcOizq+k8TgIhr1OEh/q4PL2aMOZywhlNOt03TfFfLrgkwXTN6jQq+QTwGt/jMWXyPj7zInRgyKTrUo8IlDwp5VPWg4+/Hgpqkw+7Ka4IzVk3WTMaWWQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071317; c=relaxed/simple; bh=j4dGntuy5ovfbg5GfpZwgNQjpitGGaZVaqagsHPmSn0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Us1sYEECQBq07jajSJOYGEFCWIvFXtZiXkkxXRwQopfUq41NYiBIAu41ha7/TdBo5gS0zrHQYez7inpvLIaYK6H0BAca8zQrp803HURulyfXNEdnROZKg+B86pyV0u+Cy6HPOMC11VR0N24KUhxN0qbauje6o43sDnyy7wuS0Zk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=L3WxHRnB; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="L3WxHRnB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721071313; 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: in-reply-to:in-reply-to:references:references; bh=uDSHiwJLCIYvvzv2DOw1kewa0irBZ6HqHLZp2ORNUK8=; b=L3WxHRnB5gJDLe3EyqWZftF+5pm0P/wi9ynvFParG98cR0+YFDIUbPBdAII47pFzGcA0EM 7RmzjRE+ZNoVea121kWhpxqodj65beVD0pXy+RYshcIzcmbAfWyt4JeokYRyeXv3vfj4dY ZSfPxZFmjyhQt4jU0+x7OLEJDja8I0Y= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-443-xYfgCB_kPjK9rtsdUlqdOA-1; Mon, 15 Jul 2024 15:21:51 -0400 X-MC-Unique: xYfgCB_kPjK9rtsdUlqdOA-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-6b5e4f6e95bso7236506d6.3 for ; Mon, 15 Jul 2024 12:21:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721071311; x=1721676111; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uDSHiwJLCIYvvzv2DOw1kewa0irBZ6HqHLZp2ORNUK8=; b=k0oKtrkzzojHtvtqn/Tnuc9EB4TZB3U4voeqxZVEnJu0zJuNEomb1Mzv0HQs1SgtT7 ywi7e9m4yKN61QnWNFNf6rzhSOLo2vK1Y0OLte3bUHvsgFvyfATu+RSTNI17ILrBAGL8 jxP+sZ1P5rslnHa6kN/pdsusobdZKxISaYQlnIFByTY/iLvy5Z6l+V1oWEmZNz/7A4F8 l2bEj0nHzMkQSIZCP2YJ3iZQFuN9ls9urw+s4zQHvZ7r3tgaD0yWS3sXTmB7PamqSArU DoOOt5DA1tYniDNUjEXRLhDsVluwR/1OKEGXIrSzEU9Sg5TDtmXiqRYHnaFR2QKPzI34 IvpA== X-Forwarded-Encrypted: i=1; AJvYcCXQqYcXVaiKNXBiy4xan08gb+06aUFUbN7eGd0iXYbSiSCdUkGU+MTFi4wKFDp750/fB0cdxfCWFO8G6w2EPRfUK83inntkXQday3C9 X-Gm-Message-State: AOJu0Yw6xFJXY6aZkPLkG8b8jS59+rRnjgqI3x+tfb4sMqVbo33glwUP xBE6F8HwqexZafo1XjU98iQ9DPPRXuH2mKdIzPv+uivxKWmz6kBB2xDI+CImyYXPfETgxLBBZcQ l167EF2dUFEt05SgYi9MAcu/hKy5P2BZmt4qIvtjs5d0QwulDPaxmXLoTqTNS+Q== X-Received: by 2002:a05:6214:2d44:b0:6b5:4bf:e0b2 with SMTP id 6a1803df08f44-6b77df230d0mr4738326d6.6.1721071310696; Mon, 15 Jul 2024 12:21:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHXNZWcX+OGChgQMRYaFRNal2nxEfRlz+I/X26JLEslV+UpQEodY5JY9Fmq0XZviJ4grHRk9w== X-Received: by 2002:a05:6214:2d44:b0:6b5:4bf:e0b2 with SMTP id 6a1803df08f44-6b77df230d0mr4738076d6.6.1721071310396; Mon, 15 Jul 2024 12:21:50 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b761978d30sm24039356d6.31.2024.07.15.12.21.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 12:21:49 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Jiang , Rik van Riel , Dave Hansen , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Matthew Wilcox , Rick P Edgecombe , peterx@redhat.com, Oscar Salvador , Mel Gorman , Andrew Morton , Borislav Petkov , Christophe Leroy , Huang Ying , "Kirill A . Shutemov" , "Aneesh Kumar K . V" , Dan Williams , Thomas Gleixner , Hugh Dickins , x86@kernel.org, Nicholas Piggin , Vlastimil Babka , Ingo Molnar , Alex Thorlton Subject: [PATCH v3 2/8] mm/mprotect: Remove NUMA_HUGE_PTE_UPDATES Date: Mon, 15 Jul 2024 15:21:36 -0400 Message-ID: <20240715192142.3241557-3-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240715192142.3241557-1-peterx@redhat.com> References: <20240715192142.3241557-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In 2013, commit 72403b4a0fbd ("mm: numa: return the number of base pages altered by protection changes") introduced "numa_huge_pte_updates" vmstat entry, trying to capture how many huge ptes (in reality, PMD thps at that time) are marked by NUMA balancing. This patch proposes to remove it for some reasons. Firstly, the name is misleading. We can have more than one way to have a "huge pte" at least nowadays, and that's also the major goal of this patch, where it paves way for PUD handling in change protection code paths. PUDs are coming not only for dax (which has already came and yet broken..), but also for pfnmaps and hugetlb pages. The name will simply stop making sense when PUD will start to be involved in mprotect() world. It'll also make it not reasonable either if we boost the counter for both pmd/puds. In short, current accounting won't be right when PUD comes, so the scheme was only suitable at that point in time where PUD wasn't even possible. Secondly, the accounting was simply not right from the start as long as it was also affected by other call sites besides NUMA. mprotect() is one, while userfaultfd-wp also leverages change protection path to modify pgtables. If it wants to do right it needs to check the caller but it never did; at least mprotect() should be there even in 2013. It gives me the impression that nobody is seriously using this field, and it's also impossible to be serious. We may want to do it right if any NUMA developers would like it to exist, but we should do that with all above resolved, on both considering PUDs, but also on correct accountings. That should be able to be done on top when there's a real need of such. Cc: Huang Ying Cc: Mel Gorman Cc: Alex Thorlton Cc: Rik van Riel Signed-off-by: Peter Xu --- include/linux/vm_event_item.h | 1 - mm/mprotect.c | 8 +------- mm/vmstat.c | 1 - 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h index 747943bc8cc2..2a3797fb6742 100644 --- a/include/linux/vm_event_item.h +++ b/include/linux/vm_event_item.h @@ -59,7 +59,6 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, OOM_KILL, #ifdef CONFIG_NUMA_BALANCING NUMA_PTE_UPDATES, - NUMA_HUGE_PTE_UPDATES, NUMA_HINT_FAULTS, NUMA_HINT_FAULTS_LOCAL, NUMA_PAGE_MIGRATE, diff --git a/mm/mprotect.c b/mm/mprotect.c index 222ab434da54..21172272695e 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -363,7 +363,6 @@ static inline long change_pmd_range(struct mmu_gather *= tlb, pmd_t *pmd; unsigned long next; long pages =3D 0; - unsigned long nr_huge_updates =3D 0; struct mmu_notifier_range range; =20 range.start =3D 0; @@ -411,11 +410,8 @@ static inline long change_pmd_range(struct mmu_gather = *tlb, ret =3D change_huge_pmd(tlb, vma, pmd, addr, newprot, cp_flags); if (ret) { - if (ret =3D=3D HPAGE_PMD_NR) { + if (ret =3D=3D HPAGE_PMD_NR) pages +=3D HPAGE_PMD_NR; - nr_huge_updates++; - } - /* huge pmd was handled */ goto next; } @@ -435,8 +431,6 @@ static inline long change_pmd_range(struct mmu_gather *= tlb, if (range.start) mmu_notifier_invalidate_range_end(&range); =20 - if (nr_huge_updates) - count_vm_numa_events(NUMA_HUGE_PTE_UPDATES, nr_huge_updates); return pages; } =20 diff --git a/mm/vmstat.c b/mm/vmstat.c index 73d791d1caad..53656227f70d 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1313,7 +1313,6 @@ const char * const vmstat_text[] =3D { =20 #ifdef CONFIG_NUMA_BALANCING "numa_pte_updates", - "numa_huge_pte_updates", "numa_hint_faults", "numa_hint_faults_local", "numa_pages_migrated", --=20 2.45.0 From nobody Fri Dec 19 19:15:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D734413A268 for ; Mon, 15 Jul 2024 19:21:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071319; cv=none; b=IH5gWw/bR4ArzPZzLlTQtvxtb6kgJSGaUyb+F0s3QO+45KulvJXuEQJaenJkGAsdU43KUCRobIfN8Vr4ho3vNrxhDvb8ttiFPTgYk+2E7TWvHe8t7PcEjMX8NqJTJn5XWANJR0GE8/hbCEiHQwYsCuUmmxaQbEESIkeKiLT5Qc8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071319; c=relaxed/simple; bh=YCiezBGQi5XZZC1nGB6Xk5gi7omXw4czBvCuVu44aKE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=noWX6JHG/gmXPVpbnQA/L/4oqPI5k8Y6AiQncIOCxT54ha2CZU7+5sx0/DzRM0wiO/TdVbeOX14zeKYcoj1wH9Srm9rMFjYwV1CDLRP96ChAOYqR6OF4vhmLjtUjgccL0Nd4bRj951hJTxgYBEY74jZ2J4jiRasDGDbqEQrzkAY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=VJbB3sxw; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VJbB3sxw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721071314; 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: in-reply-to:in-reply-to:references:references; bh=ZTG40gQ1/IqtnBedMzEchXvuwrXGVRlB32P0aAwFHGI=; b=VJbB3sxwlhGGuBloRtEDjZib0FqgHWBRnV5tM3rIP1eP/60MiYmTdzDn1xrqOp471EKOmN szublzGnpAE+FKeAiHCJMWKeMk+IJIqK9kk6AAaYw4w6SJAQk7t8iqvyMjq2QN1Di1HRp4 IkB6hAOwJx5MRmfQnzu/BEFdb/w6QDM= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-286-Vl4LE_m5MsaiVHFIAZ1mrA-1; Mon, 15 Jul 2024 15:21:53 -0400 X-MC-Unique: Vl4LE_m5MsaiVHFIAZ1mrA-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6b7678caf7dso6081036d6.1 for ; Mon, 15 Jul 2024 12:21:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721071313; x=1721676113; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZTG40gQ1/IqtnBedMzEchXvuwrXGVRlB32P0aAwFHGI=; b=aVVOY3RRrJz5J2NyzOODc4f3F6Cw0w4RdZNUJfPboO4UMfU3qc4cavZBwShT+JgcsC iBfNxWCVCGsWI0LvpklBlIMY3RMrPAkGVENwUYEFCabOcghh1LQqiupEv8Y0JWjQ9E5x YDLqDLiYB8+ESapp1PMD03T0pV1tNw29RmPbk35kgv7pqPPpHTZpz7LZm6hpyEAoMhgh 3JtE0NeyjVD8vzf8/fJrm4QC9QAOsuDrUHy4Kp1aIl96OZvUKyOSOGFUeEb0lfd2R04Y x3meyJoQS9ARSsqI2HHtIZSr6/DZTwmSXQqo85e1G7nNlvMQQVe7mvHS4HXkErDPOu6Z 6eNQ== X-Forwarded-Encrypted: i=1; AJvYcCVlInBcxVhNadRPgNfdZGXgAKMBoyeiQJblltXhua4O8IcD1TCOXa8Aysjs9jtAJbkPhFz9wSCVxFuDOwjeIrS6EznEvqziaRT6+bGX X-Gm-Message-State: AOJu0Yxwho0z7ZtsuUt00oKNjH9tqDWeHJ7z+Rog0x66xJuHrFTX+Mk6 N/+P/xQ2KMDQYNCuTj2uVZ5yceuC17f4340Y0sNEpzVp2sI5FW939a65LWpYdlFmFjNIXWUwizi y1Azcrt1IAZU8N8yMu/sASQ2dFQWcvHvscQXufkEHGsCvB2RNnf0sba3RmwnHmQ== X-Received: by 2002:a05:6214:3d9c:b0:6b7:586c:6db with SMTP id 6a1803df08f44-6b77e1a9bbbmr4344406d6.9.1721071312953; Mon, 15 Jul 2024 12:21:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFuaD7Bvj1JQVrhIrOGjnKODWs3APjSgAvSuKVxjGp/QvXbyCQZIOrqbPCB4cG0KJw/WyvKgQ== X-Received: by 2002:a05:6214:3d9c:b0:6b7:586c:6db with SMTP id 6a1803df08f44-6b77e1a9bbbmr4344156d6.9.1721071312655; Mon, 15 Jul 2024 12:21:52 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b761978d30sm24039356d6.31.2024.07.15.12.21.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 12:21:52 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Jiang , Rik van Riel , Dave Hansen , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Matthew Wilcox , Rick P Edgecombe , peterx@redhat.com, Oscar Salvador , Mel Gorman , Andrew Morton , Borislav Petkov , Christophe Leroy , Huang Ying , "Kirill A . Shutemov" , "Aneesh Kumar K . V" , Dan Williams , Thomas Gleixner , Hugh Dickins , x86@kernel.org, Nicholas Piggin , Vlastimil Babka , Ingo Molnar , kvm@vger.kernel.org, Sean Christopherson , Paolo Bonzini , David Rientjes Subject: [PATCH v3 3/8] mm/mprotect: Push mmu notifier to PUDs Date: Mon, 15 Jul 2024 15:21:37 -0400 Message-ID: <20240715192142.3241557-4-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240715192142.3241557-1-peterx@redhat.com> References: <20240715192142.3241557-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" mprotect() does mmu notifiers in PMD levels. It's there since 2014 of commit a5338093bfb4 ("mm: move mmu notifier call from change_protection to change_pmd_range"). At that time, the issue was that NUMA balancing can be applied on a huge range of VM memory, even if nothing was populated. The notification can be avoided in this case if no valid pmd detected, which includes either THP or a PTE pgtable page. Now to pave way for PUD handling, this isn't enough. We need to generate mmu notifications even on PUD entries properly. mprotect() is currently broken on PUD (e.g., one can easily trigger kernel error with dax 1G mappings already), this is the start to fix it. To fix that, this patch proposes to push such notifications to the PUD layers. There is risk on regressing the problem Rik wanted to resolve before, but I think it shouldn't really happen, and I still chose this solution because of a few reasons: 1) Consider a large VM that should definitely contain more than GBs of memory, it's highly likely that PUDs are also none. In this case there will have no regression. 2) KVM has evolved a lot over the years to get rid of rmap walks, which might be the major cause of the previous soft-lockup. At least TDP MMU already got rid of rmap as long as not nested (which should be the major use case, IIUC), then the TDP MMU pgtable walker will simply see empty VM pgtable (e.g. EPT on x86), the invalidation of a full empty region in most cases could be pretty fast now, comparing to 2014. 3) KVM has explicit code paths now to even give way for mmu notifiers just like this one, e.g. in commit d02c357e5bfa ("KVM: x86/mmu: Retry fault before acquiring mmu_lock if mapping is changing"). It'll also avoid contentions that may also contribute to a soft-lockup. 4) Stick with PMD layer simply don't work when PUD is there... We need one way or another to fix PUD mappings on mprotect(). Pushing it to PUD should be the safest approach as of now, e.g. there's yet no sign of huge P4D coming on any known archs. Cc: kvm@vger.kernel.org Cc: Sean Christopherson Cc: Paolo Bonzini Cc: David Rientjes Cc: Rik van Riel Signed-off-by: Peter Xu --- mm/mprotect.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 21172272695e..2a81060b603d 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -363,9 +363,6 @@ static inline long change_pmd_range(struct mmu_gather *= tlb, pmd_t *pmd; unsigned long next; long pages =3D 0; - struct mmu_notifier_range range; - - range.start =3D 0; =20 pmd =3D pmd_offset(pud, addr); do { @@ -383,14 +380,6 @@ static inline long change_pmd_range(struct mmu_gather = *tlb, if (pmd_none(*pmd)) goto next; =20 - /* invoke the mmu notifier if the pmd is populated */ - if (!range.start) { - mmu_notifier_range_init(&range, - MMU_NOTIFY_PROTECTION_VMA, 0, - vma->vm_mm, addr, end); - mmu_notifier_invalidate_range_start(&range); - } - _pmd =3D pmdp_get_lockless(pmd); if (is_swap_pmd(_pmd) || pmd_trans_huge(_pmd) || pmd_devmap(_pmd)) { if ((next - addr !=3D HPAGE_PMD_SIZE) || @@ -428,9 +417,6 @@ static inline long change_pmd_range(struct mmu_gather *= tlb, cond_resched(); } while (pmd++, addr =3D next, addr !=3D end); =20 - if (range.start) - mmu_notifier_invalidate_range_end(&range); - return pages; } =20 @@ -438,22 +424,36 @@ static inline long change_pud_range(struct mmu_gather= *tlb, struct vm_area_struct *vma, p4d_t *p4d, unsigned long addr, unsigned long end, pgprot_t newprot, unsigned long cp_flags) { + struct mmu_notifier_range range; pud_t *pud; unsigned long next; long pages =3D 0, ret; =20 + range.start =3D 0; + pud =3D pud_offset(p4d, addr); do { next =3D pud_addr_end(addr, end); ret =3D change_prepare(vma, pud, pmd, addr, cp_flags); - if (ret) - return ret; + if (ret) { + pages =3D ret; + break; + } if (pud_none_or_clear_bad(pud)) continue; + if (!range.start) { + mmu_notifier_range_init(&range, + MMU_NOTIFY_PROTECTION_VMA, 0, + vma->vm_mm, addr, end); + mmu_notifier_invalidate_range_start(&range); + } pages +=3D change_pmd_range(tlb, vma, pud, addr, next, newprot, cp_flags); } while (pud++, addr =3D next, addr !=3D end); =20 + if (range.start) + mmu_notifier_invalidate_range_end(&range); + return pages; } =20 --=20 2.45.0 From nobody Fri Dec 19 19:15:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28F8613A86C for ; Mon, 15 Jul 2024 19:21:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071320; cv=none; b=LGfAX/1txIMZGsExBSSq7uTEgCEw908MG2pgsj7/xccJPM37+Arn8fhP3UF1yQ4nAdNFC8u/ASWn4d4P6XLKsyMJhgYsCYhSYCTtnEloh5M1T8y1iwFRJaSURDRLgtnYHKt2pAjY+J4bzTreg3UkLBt3vMxkJiaxCrZZCdSzz/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071320; c=relaxed/simple; bh=lGBhFBkbaz7EzTpAgSm8PUxosOm03SfTjgCaLHkY1V8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pj1HpbZo4S9sx5piB07xLBNv4aKVAys0/Whlzi0Sb78poNYwAsqfXUklPOw87XAmczl08qwyHFnVZEM/704d3sa5T0Gh1gY37Qt4lYAK42sCCS9eJlD6XbJXvujqqwgRaYbp5Gs4p2QBD0/H8mQImHyH5DX3m0LCEIf5x+EMXQk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=hXKiCgr4; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="hXKiCgr4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721071317; 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: in-reply-to:in-reply-to:references:references; bh=DPSgdrK+/9txvlBVeWURx0wHDXuvLt3nB8aMlUhEkV8=; b=hXKiCgr45oTKVwaLOhic2Z1cvFe9Ut6FYnRutv9nP6gX3GGCSYEGOsfODQQM8Z+e6KIjRg DaAHFJ4X3LBuI5YS9S5VXJuiKqlmrZMPMGUcWgE1i3j/PdyEqRlJmjFSTVKLnTs+VOaChv znb3w6vEJ1NQXFjMKyLD6l8XwDrQ5QE= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-288-lkjX_x3-NSW2kAhShxOsPg-1; Mon, 15 Jul 2024 15:21:55 -0400 X-MC-Unique: lkjX_x3-NSW2kAhShxOsPg-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6b60afbf5d1so14885416d6.0 for ; Mon, 15 Jul 2024 12:21:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721071315; x=1721676115; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DPSgdrK+/9txvlBVeWURx0wHDXuvLt3nB8aMlUhEkV8=; b=bmEXw7a57/q6vE63Z9f5HBxNoo+AMTHdGD0xwluGzv4ynBxaSEePo+4BClabMIO9iE 6sguXXJdfv7MjosZF4D5o+OU8j9ChdW64wLEbg1fByK2yeg3bLxOuWURk8tAuRrbrYsD nTdgjeOcq1RPN2J7ZtMOnDS/jF2jsA2CAjtRpWGMhObnLakZhGriNBMA8UOo1FXKNW95 tFkofdgrSMTV2jAR8XBckfC7Vq70bpuWsg54r3JJHo1nEqY+FtFlQmUzC7KEiiWMzXaU Gwl7Kqn/0AgWOK2ul92PR/IXACUvvRtzKO9GLNfycE7TXeU52Uah+9Y9917M/HA2zlVH lthA== X-Forwarded-Encrypted: i=1; AJvYcCVDrF8p/VjbFIcYJPAwglb5nfsrywj7afgrVBk/+mVuAdIrseyfkyxx1i8vMzQnRDKu6cnRbZsLKGXcgztDWeke1l23yJ67mvgrIFwt X-Gm-Message-State: AOJu0YwJSWg+lRIYiqpvJSun7lLaqWB1W6PUkA2oGhzMgwgBDxd2Vcc/ pnRdMbSTEO5Q0B3D2wZngiBscQwpXlCoEECRTrV/d2fZDgIskpr2yuCg3Qv7ha8l1GdP/Mzi93n 5AhePjImjLfAoieSevpSfZ8MztG2auUfV3nHe+lc8Tcnr4LbDZfBgoDZBwOAtSA== X-Received: by 2002:a05:6214:2dc1:b0:6b5:90eb:370f with SMTP id 6a1803df08f44-6b77de8150amr4905696d6.2.1721071314997; Mon, 15 Jul 2024 12:21:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGFc1iq6cjowggxR08A9jWNh2nLNKZtlnYVNF070vv7/tCRn3zBtJYgG+pCX+rmBsWl/qf7Fw== X-Received: by 2002:a05:6214:2dc1:b0:6b5:90eb:370f with SMTP id 6a1803df08f44-6b77de8150amr4905476d6.2.1721071314625; Mon, 15 Jul 2024 12:21:54 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b761978d30sm24039356d6.31.2024.07.15.12.21.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 12:21:54 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Jiang , Rik van Riel , Dave Hansen , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Matthew Wilcox , Rick P Edgecombe , peterx@redhat.com, Oscar Salvador , Mel Gorman , Andrew Morton , Borislav Petkov , Christophe Leroy , Huang Ying , "Kirill A . Shutemov" , "Aneesh Kumar K . V" , Dan Williams , Thomas Gleixner , Hugh Dickins , x86@kernel.org, Nicholas Piggin , Vlastimil Babka , Ingo Molnar Subject: [PATCH v3 4/8] mm/powerpc: Add missing pud helpers Date: Mon, 15 Jul 2024 15:21:38 -0400 Message-ID: <20240715192142.3241557-5-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240715192142.3241557-1-peterx@redhat.com> References: <20240715192142.3241557-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" These new helpers will be needed for pud entry updates soon. Introduce them by referencing the pmd ones. Namely: - pudp_invalidate() - pud_modify() Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: linuxppc-dev@lists.ozlabs.org Cc: Aneesh Kumar K.V Signed-off-by: Peter Xu --- arch/powerpc/include/asm/book3s/64/pgtable.h | 3 +++ arch/powerpc/mm/book3s64/pgtable.c | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/arch/powerpc/include/asm/book3s/64/pgtable.h b/arch/powerpc/in= clude/asm/book3s/64/pgtable.h index 519b1743a0f4..5da92ba68a45 100644 --- a/arch/powerpc/include/asm/book3s/64/pgtable.h +++ b/arch/powerpc/include/asm/book3s/64/pgtable.h @@ -1124,6 +1124,7 @@ extern pmd_t pfn_pmd(unsigned long pfn, pgprot_t pgpr= ot); extern pud_t pfn_pud(unsigned long pfn, pgprot_t pgprot); extern pmd_t mk_pmd(struct page *page, pgprot_t pgprot); extern pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot); +extern pud_t pud_modify(pud_t pud, pgprot_t newprot); extern void set_pmd_at(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp, pmd_t pmd); extern void set_pud_at(struct mm_struct *mm, unsigned long addr, @@ -1384,6 +1385,8 @@ static inline pgtable_t pgtable_trans_huge_withdraw(s= truct mm_struct *mm, #define __HAVE_ARCH_PMDP_INVALIDATE extern pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long add= ress, pmd_t *pmdp); +extern pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long add= ress, + pud_t *pudp); =20 #define pmd_move_must_withdraw pmd_move_must_withdraw struct spinlock; diff --git a/arch/powerpc/mm/book3s64/pgtable.c b/arch/powerpc/mm/book3s64/= pgtable.c index f4d8d3c40e5c..5a4a75369043 100644 --- a/arch/powerpc/mm/book3s64/pgtable.c +++ b/arch/powerpc/mm/book3s64/pgtable.c @@ -176,6 +176,17 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsi= gned long address, return __pmd(old_pmd); } =20 +pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address, + pud_t *pudp) +{ + unsigned long old_pud; + + VM_WARN_ON_ONCE(!pud_present(*pudp)); + old_pud =3D pud_hugepage_update(vma->vm_mm, address, pudp, _PAGE_PRESENT,= _PAGE_INVALID); + flush_pud_tlb_range(vma, address, address + HPAGE_PUD_SIZE); + return __pud(old_pud); +} + pmd_t pmdp_huge_get_and_clear_full(struct vm_area_struct *vma, unsigned long addr, pmd_t *pmdp, int full) { @@ -259,6 +270,15 @@ pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) pmdv &=3D _HPAGE_CHG_MASK; return pmd_set_protbits(__pmd(pmdv), newprot); } + +pud_t pud_modify(pud_t pud, pgprot_t newprot) +{ + unsigned long pudv; + + pudv =3D pud_val(pud); + pudv &=3D _HPAGE_CHG_MASK; + return pud_set_protbits(__pud(pudv), newprot); +} #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 /* For use by kexec, called with MMU off */ --=20 2.45.0 From nobody Fri Dec 19 19:15:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CE4913AA5F for ; Mon, 15 Jul 2024 19:21:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071321; cv=none; b=gOhP2sTNL/FPiuVnnONkOjGLmwkgLGJwHYJqtrAuC0jnHzddTKOTHiYrslCmEf5Nc5qiNf0Y1uuaNdnIive9Z5JHSxki51HcxlvSC6Goh4yxpAEtP/4IuvKEgSDg2zjyOh/hNBk3C5D+QKcoqlyB+MWzYcfcavxe6+WtTtTg0xM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071321; c=relaxed/simple; bh=HvLeVYUQ3g1BzJ0yN4wtIuLe5eY6YeKrJD1Hba3arrQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=khngBQOFmUdjTfDQRtjEB3rr5k7GYrHS8R4nipuVCaLGWDqkO8KTjR52ZL/jNUwSj96sn43NHc0aZVghwCFrmhB3TING0qXwWh6/XM5nA/TBeWKwL/61swGhOQc3v3sSxyvRSfoXpulDDyLmQTtC6pJhyixSz5UsOc7EWBImGEA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=PPn1g5NK; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PPn1g5NK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721071319; 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: in-reply-to:in-reply-to:references:references; bh=TFEuzrgphkpyKgRaMn/StFmQ1IWZKcW4qj9eIjoToHw=; b=PPn1g5NKBF6lvrQGMlHOWelZ0uJ/O94Nry5RLzJsxZDYlgLFZmFNUO2wXqe2fTnFIrNmT3 nVWSAubG2Yzxpf7uAGJJa/DH6qTXN5A/s4MaS9IlogZDcFAZT7t0iOXWam2R/nGUPaMOHl WbXtZYp3bV/lXZKsKaGjeDQ1igZ47co= Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-341-9oZnw7d3OIKqNFwAATir1g-1; Mon, 15 Jul 2024 15:21:57 -0400 X-MC-Unique: 9oZnw7d3OIKqNFwAATir1g-1 Received: by mail-oo1-f69.google.com with SMTP id 006d021491bc7-5cea2c45403so518968eaf.3 for ; Mon, 15 Jul 2024 12:21:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721071317; x=1721676117; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TFEuzrgphkpyKgRaMn/StFmQ1IWZKcW4qj9eIjoToHw=; b=N4K6e84wvAPjB2Z0cbph8bewvvVv9YWH7C54wzDeMtpYHjJ0zWC7sYLuhNi1c8dNqe qXnZVsNJifcKC/1gBkB6i4bdX7R317gqTM0u+B7VVHRokTi7yVSlfc9iKvGsToDzkqUU 3/ytHq2/yRu4QLYLX2rkhFpeDlnpUAiHsRuaJCnE+b7Kt5huwMjaYXBrMPvlfyScIs/y mLTib2bTMfw8z2HMw4pdJUWf/oB9Pz7KZrY7467z64wE5jsOZ99oj51X1kPyqpj7+crE baWD0iqrJ9oiZekqTk+yyXOOF3PuJgDvBSslAOzF8zQKy9Ca/w29AKx92sx2l6EZiAWs jedQ== X-Forwarded-Encrypted: i=1; AJvYcCWmFYBwCPISa80TWo3JxIVAdAi40LiydODTphIpSXC9zFj2dHiykResX02isM+5KnJs9jNjhiP7Q1BWiLk1IltcJYPqWfmbuxu9gBrR X-Gm-Message-State: AOJu0YxglCvG2TSJPKTPLDVuskxOUtoLjwoGnv4F1/GI+eM94guit081 A5yfZEuC5tENRmhVC7Erbcm0kXHq4zg0CNVlWSN9zjVrYB97CdN7db7zOi1umdzbQ+FNK2OyXZQ oTA6Gs5JQiwAYn9wQfmORHcTPmqZBig3y0XwtN+T+XcJb6ytRiy44VunGDchLSQ== X-Received: by 2002:a4a:4308:0:b0:5c7:aeba:77a0 with SMTP id 006d021491bc7-5d25035be58mr500664eaf.0.1721071317070; Mon, 15 Jul 2024 12:21:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHclECHVFRdVYbFwnrtKH3lJcHIiAHM9jyDo1qTRx8+nslWI3GA72lYOtCld/HD9tYeAzQ33w== X-Received: by 2002:a4a:4308:0:b0:5c7:aeba:77a0 with SMTP id 006d021491bc7-5d25035be58mr500637eaf.0.1721071316695; Mon, 15 Jul 2024 12:21:56 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b761978d30sm24039356d6.31.2024.07.15.12.21.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 12:21:56 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Jiang , Rik van Riel , Dave Hansen , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Matthew Wilcox , Rick P Edgecombe , peterx@redhat.com, Oscar Salvador , Mel Gorman , Andrew Morton , Borislav Petkov , Christophe Leroy , Huang Ying , "Kirill A . Shutemov" , "Aneesh Kumar K . V" , Dan Williams , Thomas Gleixner , Hugh Dickins , x86@kernel.org, Nicholas Piggin , Vlastimil Babka , Ingo Molnar Subject: [PATCH v3 5/8] mm/x86: Make pud_leaf() only cares about PSE bit Date: Mon, 15 Jul 2024 15:21:39 -0400 Message-ID: <20240715192142.3241557-6-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240715192142.3241557-1-peterx@redhat.com> References: <20240715192142.3241557-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" An entry should be reported as PUD leaf even if it's PROT_NONE, in which case PRESENT bit isn't there. I hit bad pud without this when testing dax 1G on zapping a PROT_NONE PUD. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Acked-by: Dave Hansen Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand --- arch/x86/include/asm/pgtable.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 65b8e5bb902c..25fc6d809572 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1073,8 +1073,7 @@ static inline pmd_t *pud_pgtable(pud_t pud) #define pud_leaf pud_leaf static inline bool pud_leaf(pud_t pud) { - return (pud_val(pud) & (_PAGE_PSE | _PAGE_PRESENT)) =3D=3D - (_PAGE_PSE | _PAGE_PRESENT); + return pud_val(pud) & _PAGE_PSE; } =20 static inline int pud_bad(pud_t pud) --=20 2.45.0 From nobody Fri Dec 19 19:15:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 335C313BC03 for ; Mon, 15 Jul 2024 19:22:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071323; cv=none; b=tXfO/sZqCOxurfrFZeWCD93RtQZWUpUIgj+R9+5B49h/b1KJYtirLyzhBZ3ReDc4q1n2J1RwMurPPNHlSg0VmQxaqTAGE+mr6MjJD9fIX6EzyvlxL7rbNj8f1fHEDCPrfDQwzVklXcu17qHUOzuGmpkkUyMkLZyBATUQr9mgdL0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071323; c=relaxed/simple; bh=hY43WIIcWNxPexB6TkE7xeF3vzFmSYWkWv1MKduVE34=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Tk5kNxuchCIFL2jhlM+jFr1fzG/QlVqaPEpER424UG80qaznPZzMSou2tsjzAPqSGU3FUz7hlhZPeMZ7nbJoBFxUwQeuzdzv/LeLArFGlHgf0FlSbhoY0Evm/eZQDJ8S1sJ+jSKWwIaU0rVwg5WY9E9HtdcLJvs9GeqTCiR8F2w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=B0Hmeqeu; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="B0Hmeqeu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721071321; 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: in-reply-to:in-reply-to:references:references; bh=9JbEXa1vvY/QVlVssvu52FPZK180R34zP6ifpD40mks=; b=B0HmeqeucqQnsPnA9X1fNGVT0HoiO+FLZpvIBoHY7Tz6hJAZzO4kl+/N769vcKTtrM9XJP cx+TBFvMN/4YoxMfbEO5d+IVGelQwPNquB9bNr5L0DZ3w4DV1YFMnrh+xYWkBzku2bbo1A e5Iu5qRAi1kbv7vwU4ZpuvVoTAB+Vnw= Received: from mail-yb1-f197.google.com (mail-yb1-f197.google.com [209.85.219.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-486-RUN8ZuNANUSXBi2hAZNZHA-1; Mon, 15 Jul 2024 15:21:59 -0400 X-MC-Unique: RUN8ZuNANUSXBi2hAZNZHA-1 Received: by mail-yb1-f197.google.com with SMTP id 3f1490d57ef6-e039b7a67aeso1253356276.1 for ; Mon, 15 Jul 2024 12:21:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721071319; x=1721676119; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9JbEXa1vvY/QVlVssvu52FPZK180R34zP6ifpD40mks=; b=ci4QqRLG5b3QsM4JGhzkZPPn+FMOuZl2t7Vg6wRtpmrjZ3O95VVYRkrGQO/DhEANUl VVC2qhEc6tyDJBzLlA4y30/R5uimOFwMFLMMU9dsH1iFHXfBQVi+4R/SqbNBSmC4g98T ozDkGdudPFURb03Rmsp7bsSkbTwZepBdaxfTXFVcKO3ttGAULrFYV8LUWsYbSaacKIur fT1eGdBFSfa7VaF+2ZXGVu5f+k7svV2+3Zb/nzAEaRlKt/v46PDsPNtDpYmgPJOultC8 jHaNSdO4n4HWtX5bGNP8ff20G1uJAHkpfvVF1DiaNDRXCPJDdeMmiGOpup+8invdGilz MVIg== X-Forwarded-Encrypted: i=1; AJvYcCXWca5DCKdWy5rl/M6ctT8Yg27H8cOgm+rathKqyC44RrR79J4dgWRzUcTJweVHaeYszyKodp8lpe7xNRvB8RI+sxeklI9nQZCPp+1j X-Gm-Message-State: AOJu0YymrW3dofhuo+KjFbC04pNA8xuyMeIuyNuBUxmI7HLgVaxrDuXC mfLISdZ7+93+pPrtO9TbmZC6Z3gwKayunV1YbJsp4GnSS6E8Xnq6F8m2YUsztNQ4+8StLPH6tpO WAEOh1KaCqEDA67L6qfatw8G7WMz+rCbSZ4RJsuoToeH9Kd6kTNDrUyCIWPcCmA== X-Received: by 2002:a25:2d11:0:b0:e05:900d:32e6 with SMTP id 3f1490d57ef6-e05d3c17338mr350142276.8.1721071319224; Mon, 15 Jul 2024 12:21:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFmDbyBksbC4qmpVrfmmZ0A/VGjIJHLQ2OfuPZrkWmoJhvnJgk6xUDZyR/TdOg4zaQrzDq1HQ== X-Received: by 2002:a25:2d11:0:b0:e05:900d:32e6 with SMTP id 3f1490d57ef6-e05d3c17338mr350112276.8.1721071318815; Mon, 15 Jul 2024 12:21:58 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b761978d30sm24039356d6.31.2024.07.15.12.21.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 12:21:58 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Jiang , Rik van Riel , Dave Hansen , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Matthew Wilcox , Rick P Edgecombe , peterx@redhat.com, Oscar Salvador , Mel Gorman , Andrew Morton , Borislav Petkov , Christophe Leroy , Huang Ying , "Kirill A . Shutemov" , "Aneesh Kumar K . V" , Dan Williams , Thomas Gleixner , Hugh Dickins , x86@kernel.org, Nicholas Piggin , Vlastimil Babka , Ingo Molnar Subject: [PATCH v3 6/8] mm/x86: arch_check_zapped_pud() Date: Mon, 15 Jul 2024 15:21:40 -0400 Message-ID: <20240715192142.3241557-7-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240715192142.3241557-1-peterx@redhat.com> References: <20240715192142.3241557-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce arch_check_zapped_pud() to sanity check shadow stack on PUD zaps. It has the same logic of the PMD helper. One thing to mention is, it might be a good idea to use page_table_check in the future for trapping wrong setups of shadow stack pgtable entries [1]. That is left for the future as a separate effort. [1] https://lore.kernel.org/all/59d518698f664e07c036a5098833d7b56b953305.ca= mel@intel.com Cc: "Edgecombe, Rick P" Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Signed-off-by: Peter Xu Acked-by: David Hildenbrand --- arch/x86/include/asm/pgtable.h | 10 ++++++++++ arch/x86/mm/pgtable.c | 7 +++++++ include/linux/pgtable.h | 7 +++++++ mm/huge_memory.c | 4 +++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 25fc6d809572..cdf044c2ad6e 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -169,6 +169,13 @@ static inline int pud_young(pud_t pud) return pud_flags(pud) & _PAGE_ACCESSED; } =20 +static inline bool pud_shstk(pud_t pud) +{ + return cpu_feature_enabled(X86_FEATURE_SHSTK) && + (pud_flags(pud) & (_PAGE_RW | _PAGE_DIRTY | _PAGE_PSE)) =3D=3D + (_PAGE_DIRTY | _PAGE_PSE); +} + static inline int pte_write(pte_t pte) { /* @@ -1662,6 +1669,9 @@ void arch_check_zapped_pte(struct vm_area_struct *vma= , pte_t pte); #define arch_check_zapped_pmd arch_check_zapped_pmd void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd); =20 +#define arch_check_zapped_pud arch_check_zapped_pud +void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud); + #ifdef CONFIG_XEN_PV #define arch_has_hw_nonleaf_pmd_young arch_has_hw_nonleaf_pmd_young static inline bool arch_has_hw_nonleaf_pmd_young(void) diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 93e54ba91fbf..564b8945951e 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -926,3 +926,10 @@ void arch_check_zapped_pmd(struct vm_area_struct *vma,= pmd_t pmd) VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && pmd_shstk(pmd)); } + +void arch_check_zapped_pud(struct vm_area_struct *vma, pud_t pud) +{ + /* See note in arch_check_zapped_pte() */ + VM_WARN_ON_ONCE(!(vma->vm_flags & VM_SHADOW_STACK) && + pud_shstk(pud)); +} diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 2a6a3cccfc36..2289e9f7aa1b 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -447,6 +447,13 @@ static inline void arch_check_zapped_pmd(struct vm_are= a_struct *vma, } #endif =20 +#ifndef arch_check_zapped_pud +static inline void arch_check_zapped_pud(struct vm_area_struct *vma, + pud_t pud) +{ +} +#endif + #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long address, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 9fec5bd1c8b0..c10247bef08a 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2291,12 +2291,14 @@ int zap_huge_pud(struct mmu_gather *tlb, struct vm_= area_struct *vma, pud_t *pud, unsigned long addr) { spinlock_t *ptl; + pud_t orig_pud; =20 ptl =3D __pud_trans_huge_lock(pud, vma); if (!ptl) return 0; =20 - pudp_huge_get_and_clear_full(vma, addr, pud, tlb->fullmm); + orig_pud =3D pudp_huge_get_and_clear_full(vma, addr, pud, tlb->fullmm); + arch_check_zapped_pud(vma, orig_pud); tlb_remove_pud_tlb_entry(tlb, pud, addr); if (vma_is_special_huge(vma)) { spin_unlock(ptl); --=20 2.45.0 From nobody Fri Dec 19 19:15:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2C7413C83D for ; Mon, 15 Jul 2024 19:22:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071325; cv=none; b=nl+j5vdz3b/+G8Y5Ch2wrJlylVw5yiewsg6Nb3ekLU6voJ5n+wUC4KiEeZ+1B5OwQTCENIkQdLEI85+PosXVmh4PfmKj9fN54chB7WmGfsK9OKdjQyBoHFuunkMWZf1Y5ci83HMeUtC3ZetH8cZnLS89mB10LB2cj4L1YRSmdYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071325; c=relaxed/simple; bh=ZQcIytPpRpgyZIcTGEgOPkZLRG43kHqd/i82zdy4vsU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hQniQXNxHyk47bJmwB5b7dPe6wVbSyZ41unlsp0Bhu+wKPiwmRwfOFuKSDBba85CKWu2BWJJ/jdsAAUWI/EuZRHRstpKsAKS6IEnn7etJSo3Fe5WnSHgdBWhMisRng13t9Y7bfuRTXcagoyBwWNMxBk4mBx1QhnAB+4KQGbrZjI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=KZ5YKnnZ; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KZ5YKnnZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721071322; 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: in-reply-to:in-reply-to:references:references; bh=TIbZhmDyeGPrqbKoRiCTCUG6/nrhKTfNHRA57Ap3jGE=; b=KZ5YKnnZoukzulSb4D7mCcYfH3KN9l2PAXcggYlc/Do9N5BvAF/tLjml6XsZaV1ZwizO5n Jt76TWRqvL5zUpQ1QcIaUJWCk49FWYW/AaLTrZVQbjITm6RLS9JxnazbKvMeu8spdHYbng vbueWer6nZB80eg8cMDZZx+StmwNoPA= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-70--DmIh2AMN0OttAnvqpci8A-1; Mon, 15 Jul 2024 15:22:01 -0400 X-MC-Unique: -DmIh2AMN0OttAnvqpci8A-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-6b7735566bfso2304546d6.3 for ; Mon, 15 Jul 2024 12:22:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721071321; x=1721676121; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TIbZhmDyeGPrqbKoRiCTCUG6/nrhKTfNHRA57Ap3jGE=; b=sWnKowH0lWmAG37MYnB8r+sU0b561uvzIdV0dbvYpCFHgGyf+KQnkijY/GYoHIfT6e GApRFIhXBLUMoREjR/345EMWfBFZIkePybsiWmJh/8tB0IceRjGRRv6qZVI2vVgp1P1W hIy/HbKUNgXZR0GbVW1tJZ7b3QTPaNayRPV9cnVBtRF0Vbrn4q6XtGBJ6VZKjb8yD0lo H4fAfv2hqLKtpLKXns/R1g2Qb1/FNC71htJWN7oD9URMTtwR445t5wyfV/ampM+x2bOM 3P+Vdk9txnphsLmtDD3VKe1T90hk/+Am0SkDZFOdVACbTp502Z1DNsWp/KeJLeoaRhuO LDfQ== X-Forwarded-Encrypted: i=1; AJvYcCXG/vMEC5JtuPjIvSDCrSjWVv1/j3GOwOATHcOXitHvOzXaurJCoB4bnoEp4mZX3erHOX+0Waxaj6TFNvU7IhhGpz12WT5Tb194s0L/ X-Gm-Message-State: AOJu0Ywu/x9hj4BV2IqUunjUKzbPTRkGXY9/0tKOTWOCwqB3tSZ7YYyY C72+Sjf3wsNw/+ELl5+VySnpztxZDS9oVFp5ZyZxMAzWoVDS728pLsYQ6CdN374NxvGlD0I6JpW PQT/zM8so8fS9Lhe4yzoqley3gt5quZSfectlkJtNpGPiWmZjjYoHuEhJR95Zew== X-Received: by 2002:a05:6214:5d0b:b0:6b5:38d4:c9c5 with SMTP id 6a1803df08f44-6b77df20d64mr4194796d6.4.1721071321076; Mon, 15 Jul 2024 12:22:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEuNyAqmev7NeecbFUrfKh3dH4xJxhydhorfZ+q73TgeQxZK3QM+o4zZ2VKk8paCO1QT0QczQ== X-Received: by 2002:a05:6214:5d0b:b0:6b5:38d4:c9c5 with SMTP id 6a1803df08f44-6b77df20d64mr4194626d6.4.1721071320743; Mon, 15 Jul 2024 12:22:00 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b761978d30sm24039356d6.31.2024.07.15.12.21.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 12:22:00 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Jiang , Rik van Riel , Dave Hansen , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Matthew Wilcox , Rick P Edgecombe , peterx@redhat.com, Oscar Salvador , Mel Gorman , Andrew Morton , Borislav Petkov , Christophe Leroy , Huang Ying , "Kirill A . Shutemov" , "Aneesh Kumar K . V" , Dan Williams , Thomas Gleixner , Hugh Dickins , x86@kernel.org, Nicholas Piggin , Vlastimil Babka , Ingo Molnar Subject: [PATCH v3 7/8] mm/x86: Add missing pud helpers Date: Mon, 15 Jul 2024 15:21:41 -0400 Message-ID: <20240715192142.3241557-8-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240715192142.3241557-1-peterx@redhat.com> References: <20240715192142.3241557-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" These new helpers will be needed for pud entry updates soon. Introduce these helpers by referencing the pmd ones. Namely: - pudp_invalidate() - pud_modify() Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Signed-off-by: Peter Xu --- arch/x86/include/asm/pgtable.h | 55 +++++++++++++++++++++++++++++----- arch/x86/mm/pgtable.c | 12 ++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index cdf044c2ad6e..701593c53f3b 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -782,6 +782,12 @@ static inline pmd_t pmd_mkinvalid(pmd_t pmd) __pgprot(pmd_flags(pmd) & ~(_PAGE_PRESENT|_PAGE_PROTNONE))); } =20 +static inline pud_t pud_mkinvalid(pud_t pud) +{ + return pfn_pud(pud_pfn(pud), + __pgprot(pud_flags(pud) & ~(_PAGE_PRESENT|_PAGE_PROTNONE))); +} + static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask); =20 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) @@ -829,14 +835,8 @@ static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t new= prot) pmd_result =3D __pmd(val); =20 /* - * To avoid creating Write=3D0,Dirty=3D1 PMDs, pte_modify() needs to avoi= d: - * 1. Marking Write=3D0 PMDs Dirty=3D1 - * 2. Marking Dirty=3D1 PMDs Write=3D0 - * - * The first case cannot happen because the _PAGE_CHG_MASK will filter - * out any Dirty bit passed in newprot. Handle the second case by - * going through the mksaveddirty exercise. Only do this if the old - * value was Write=3D1 to avoid doing this on Shadow Stack PTEs. + * Avoid creating shadow stack PMD by accident. See comment in + * pte_modify(). */ if (oldval & _PAGE_RW) pmd_result =3D pmd_mksaveddirty(pmd_result); @@ -846,6 +846,29 @@ static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t new= prot) return pmd_result; } =20 +static inline pud_t pud_modify(pud_t pud, pgprot_t newprot) +{ + pudval_t val =3D pud_val(pud), oldval =3D val; + pud_t pud_result; + + val &=3D _HPAGE_CHG_MASK; + val |=3D check_pgprot(newprot) & ~_HPAGE_CHG_MASK; + val =3D flip_protnone_guard(oldval, val, PHYSICAL_PUD_PAGE_MASK); + + pud_result =3D __pud(val); + + /* + * Avoid creating shadow stack PUD by accident. See comment in + * pte_modify(). + */ + if (oldval & _PAGE_RW) + pud_result =3D pud_mksaveddirty(pud_result); + else + pud_result =3D pud_clear_saveddirty(pud_result); + + return pud_result; +} + /* * mprotect needs to preserve PAT and encryption bits when updating * vm_page_prot @@ -1384,10 +1407,26 @@ static inline pmd_t pmdp_establish(struct vm_area_s= truct *vma, } #endif =20 +static inline pud_t pudp_establish(struct vm_area_struct *vma, + unsigned long address, pud_t *pudp, pud_t pud) +{ + page_table_check_pud_set(vma->vm_mm, pudp, pud); + if (IS_ENABLED(CONFIG_SMP)) { + return xchg(pudp, pud); + } else { + pud_t old =3D *pudp; + WRITE_ONCE(*pudp, pud); + return old; + } +} + #define __HAVE_ARCH_PMDP_INVALIDATE_AD extern pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address, pmd_t *pmdp); =20 +pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address, + pud_t *pudp); + /* * Page table pages are page-aligned. The lower half of the top * level is used for userspace and the top half for the kernel. diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 564b8945951e..fa77411bb266 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -641,6 +641,18 @@ pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, u= nsigned long address, } #endif =20 +#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \ + defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) +pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address, + pud_t *pudp) +{ + VM_WARN_ON_ONCE(!pud_present(*pudp)); + pud_t old =3D pudp_establish(vma, address, pudp, pud_mkinvalid(*pudp)); + flush_pud_tlb_range(vma, address, address + HPAGE_PUD_SIZE); + return old; +} +#endif + /** * reserve_top_address - reserves a hole in the top of kernel address space * @reserve - size of hole to reserve --=20 2.45.0 From nobody Fri Dec 19 19:15:48 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C9A913CA99 for ; Mon, 15 Jul 2024 19:22:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071327; cv=none; b=R+Z/hk+4QXEDnBJHnd2MNeawJFkYaGFn9q0Bh6N7VjYDz9Ul0KlbUIrQOzESUFNdWuENS+IqvD6g8t0//PMEibq7GMhLDneBOWiCVl+jjuVYZu2cDK4H9FYhNEgHkj69cznIC3IuqO/8tjwOFsl9YLPKQ2asUSC3kRv2s///rAI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721071327; c=relaxed/simple; bh=KxQoRYAy31rvfs2wpC6MCQr7OcVrMQNNZjPIAFt986s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QQk9PACI+Lcrx66+/OEF46OF35Yq7OYzGfao6Xy2CC4uVQUV7nGyGrgqc12Ce8Up725JHEFFJjSv5hS4xOF+1UMywJurtqswCk0rV2xRdMFPuGd3pKhGZdTIrcokathlsEma9Hglok2zFbsU6hLZKmm+oAqAsMvURWCU9ZbOWV8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fhA3Y//x; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fhA3Y//x" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721071324; 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: in-reply-to:in-reply-to:references:references; bh=MX5pdN1kDqdO60LUKursnvEFOQP0WFBlMnoZkGHINQQ=; b=fhA3Y//xkFHR6n4wtUZ1rVyudWuhk+avUn/SEzrlFOYwepxOjhmW9oEj7H9Xlo6D/ehmKo fPfuiYWwKaAgKGZdgrKyHRp8+g5IdnrefuU7mTE7AMSRcvIExW4AatzaoPYEYu5gvFP9SN /VINJQplAlO3WyzscWp2c4KXBamt7e8= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-28-ZNYTTk9BOmi0KmK1Tt606Q-1; Mon, 15 Jul 2024 15:22:03 -0400 X-MC-Unique: ZNYTTk9BOmi0KmK1Tt606Q-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-6b5e99c06efso9781786d6.2 for ; Mon, 15 Jul 2024 12:22:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721071323; x=1721676123; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MX5pdN1kDqdO60LUKursnvEFOQP0WFBlMnoZkGHINQQ=; b=aODmq9haInGVI0RRaw84rYPpNRM48xxYb12kMj1OJtH0Fwpu33pgq117a55lzBu1WU x7d3sn00NCOwGJCa+TzQqDCd4GIgWU/9XaSOS4cen+Hjj+T46pXtELw+dTpkIhGl8DiR f4rnkHMKUyRAtcRnVp923he+W1e3VB58a2QTSiAqegzDh15aKKH3eB+deDkFnzMuy/OS bYc45rg/MK3K7HKhT55FondN/0FCGmLueEgfohovFC8cDZECtbOtA6j/5jYFh1SwX88w FFSHN4jk6s89aXGgkMapj1BwHyi3mUpxB/FDBjnXCQkyCic8mn6Fgs1fD0Y3vW9yAvQc d/YA== X-Forwarded-Encrypted: i=1; AJvYcCVoH99X6+4/cgDPoP4TMNPA2LTgnPa7A9hlQn3DdwCcgVx4mSSnzELE/ghOW+PXR5ga9qQ8fMZBffuIH6d5qChcv5kgJshVFojzZZTC X-Gm-Message-State: AOJu0YzWKmDFkd4UsbNOsLVSiUEay5i7vBhXA4Ox10f8obCpTRZPr/sq y96u81tVVisanVjVkxM6LcUkSiuq8O0fNvZttxYwwQF+FVRMtGP6bYgyqxH0RVBBdpQWR7qqNQg x39CmNf/Xb1/WSjUEvq4YY1xZRa76EyTYCpFAVqFlMBSLfOlSUDJuOX/r3gZQnA== X-Received: by 2002:a05:6214:3d9c:b0:6b7:586c:6db with SMTP id 6a1803df08f44-6b77e1a9bbbmr4347846d6.9.1721071322940; Mon, 15 Jul 2024 12:22:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGQhnzZI26fHLI1GP/caKDLHgduuPT+eLqK9zXdARycJ8bkdfse6d6/hJ9cmtHwcRv8UZEfEQ== X-Received: by 2002:a05:6214:3d9c:b0:6b7:586c:6db with SMTP id 6a1803df08f44-6b77e1a9bbbmr4347496d6.9.1721071322604; Mon, 15 Jul 2024 12:22:02 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b761978d30sm24039356d6.31.2024.07.15.12.22.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jul 2024 12:22:02 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Dave Jiang , Rik van Riel , Dave Hansen , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, Matthew Wilcox , Rick P Edgecombe , peterx@redhat.com, Oscar Salvador , Mel Gorman , Andrew Morton , Borislav Petkov , Christophe Leroy , Huang Ying , "Kirill A . Shutemov" , "Aneesh Kumar K . V" , Dan Williams , Thomas Gleixner , Hugh Dickins , x86@kernel.org, Nicholas Piggin , Vlastimil Babka , Ingo Molnar Subject: [PATCH v3 8/8] mm/mprotect: fix dax pud handlings Date: Mon, 15 Jul 2024 15:21:42 -0400 Message-ID: <20240715192142.3241557-9-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240715192142.3241557-1-peterx@redhat.com> References: <20240715192142.3241557-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This is only relevant to the two archs that support PUD dax, aka, x86_64 and ppc64. PUD THPs do not yet exist elsewhere, and hugetlb PUDs do not count in this case. DAX have had PUD mappings for years, but change protection path never worked. When the path is triggered in any form (a simple test program would be: call mprotect() on a 1G dev_dax mapping), the kernel will report "bad pud". This patch should fix that. The new change_huge_pud() tries to keep everything simple. For example, it doesn't optimize write bit as that will need even more PUD helpers. It's not too bad anyway to have one more write fault in the worst case once for 1G range; may be a bigger thing for each PAGE_SIZE, though. Neither does it support userfault-wp bits, as there isn't such PUD mappings that is supported; file mappings always need a split there. The same to TLB shootdown: the pmd path (which was for x86 only) has the trick of using _ad() version of pmdp_invalidate*() which can avoid one redundant TLB, but let's also leave that for later. Again, the larger the mapping, the smaller of such effect. Another thing worth mention is this path needs to be careful on handling "retry" event for change_huge_pud() (where it can return 0): it isn't like change_huge_pmd(), as the pmd version is safe with all conditions handled in change_pte_range() later, thanks to Hugh's new pte_offset_map_lock(). In short, change_pte_range() is simply smarter than change_pmd_range() now after the shmem thp collapse rework. For that reason, change_pud_range() will need proper retry if it races with something else when a huge PUD changed from under us. Cc: Dan Williams Cc: Matthew Wilcox Cc: Dave Jiang Cc: Hugh Dickins Cc: Kirill A. Shutemov Cc: Vlastimil Babka Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: Michael Ellerman Cc: Aneesh Kumar K.V Cc: Oscar Salvador Cc: x86@kernel.org Cc: linuxppc-dev@lists.ozlabs.org Fixes: a00cc7d9dd93 ("mm, x86: add support for PUD-sized transparent hugepa= ges") Fixes: 27af67f35631 ("powerpc/book3s64/mm: enable transparent pud hugepage") Signed-off-by: Peter Xu --- include/linux/huge_mm.h | 24 +++++++++++++++++++ mm/huge_memory.c | 52 +++++++++++++++++++++++++++++++++++++++++ mm/mprotect.c | 34 ++++++++++++++++++++++----- 3 files changed, 104 insertions(+), 6 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index cff002be83eb..6e742680590a 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -336,6 +336,17 @@ void split_huge_pmd_address(struct vm_area_struct *vma= , unsigned long address, void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, unsigned long address); =20 +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, + pud_t *pudp, unsigned long addr, pgprot_t newprot, + unsigned long cp_flags); +#else +static inline int +change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, + pud_t *pudp, unsigned long addr, pgprot_t newprot, + unsigned long cp_flags) { return 0; } +#endif + #define split_huge_pud(__vma, __pud, __address) \ do { \ pud_t *____pud =3D (__pud); \ @@ -579,6 +590,19 @@ static inline int next_order(unsigned long *orders, in= t prev) { return 0; } + +static inline void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, + unsigned long address) +{ +} + +static inline int change_huge_pud(struct mmu_gather *tlb, + struct vm_area_struct *vma, pud_t *pudp, + unsigned long addr, pgprot_t newprot, + unsigned long cp_flags) +{ + return 0; +} #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 static inline int split_folio_to_list_to_order(struct folio *folio, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index c10247bef08a..9a00c5955c0c 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2112,6 +2112,53 @@ int change_huge_pmd(struct mmu_gather *tlb, struct v= m_area_struct *vma, return ret; } =20 +/* + * Returns: + * + * - 0: if pud leaf changed from under us + * - 1: if pud can be skipped + * - HPAGE_PUD_NR: if pud was successfully processed + */ +#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD +int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, + pud_t *pudp, unsigned long addr, pgprot_t newprot, + unsigned long cp_flags) +{ + struct mm_struct *mm =3D vma->vm_mm; + pud_t oldpud, entry; + spinlock_t *ptl; + + tlb_change_page_size(tlb, HPAGE_PUD_SIZE); + + /* NUMA balancing doesn't apply to dax */ + if (cp_flags & MM_CP_PROT_NUMA) + return 1; + + /* + * Huge entries on userfault-wp only works with anonymous, while we + * don't have anonymous PUDs yet. + */ + if (WARN_ON_ONCE(cp_flags & MM_CP_UFFD_WP_ALL)) + return 1; + + ptl =3D __pud_trans_huge_lock(pudp, vma); + if (!ptl) + return 0; + + /* + * Can't clear PUD or it can race with concurrent zapping. See + * change_huge_pmd(). + */ + oldpud =3D pudp_invalidate(vma, addr, pudp); + entry =3D pud_modify(oldpud, newprot); + set_pud_at(mm, addr, pudp, entry); + tlb_flush_pud_range(tlb, addr, HPAGE_PUD_SIZE); + + spin_unlock(ptl); + return HPAGE_PUD_NR; +} +#endif + #ifdef CONFIG_USERFAULTFD /* * The PT lock for src_pmd and dst_vma/src_vma (for reading) are locked by @@ -2342,6 +2389,11 @@ void __split_huge_pud(struct vm_area_struct *vma, pu= d_t *pud, spin_unlock(ptl); mmu_notifier_invalidate_range_end(&range); } +#else +void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, + unsigned long address) +{ +} #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ =20 static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, diff --git a/mm/mprotect.c b/mm/mprotect.c index 2a81060b603d..694f13b83864 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -425,31 +425,53 @@ static inline long change_pud_range(struct mmu_gather= *tlb, unsigned long end, pgprot_t newprot, unsigned long cp_flags) { struct mmu_notifier_range range; - pud_t *pud; + pud_t *pudp, pud; unsigned long next; long pages =3D 0, ret; =20 range.start =3D 0; =20 - pud =3D pud_offset(p4d, addr); + pudp =3D pud_offset(p4d, addr); do { +again: next =3D pud_addr_end(addr, end); - ret =3D change_prepare(vma, pud, pmd, addr, cp_flags); + ret =3D change_prepare(vma, pudp, pmd, addr, cp_flags); if (ret) { pages =3D ret; break; } - if (pud_none_or_clear_bad(pud)) + + pud =3D READ_ONCE(*pudp); + if (pud_none(pud)) continue; + if (!range.start) { mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0, vma->vm_mm, addr, end); mmu_notifier_invalidate_range_start(&range); } - pages +=3D change_pmd_range(tlb, vma, pud, addr, next, newprot, + + if (pud_leaf(pud)) { + if ((next - addr !=3D PUD_SIZE) || + pgtable_split_needed(vma, cp_flags)) { + __split_huge_pud(vma, pudp, addr); + goto again; + } else { + ret =3D change_huge_pud(tlb, vma, pudp, + addr, newprot, cp_flags); + if (ret =3D=3D 0) + goto again; + /* huge pud was handled */ + if (ret =3D=3D HPAGE_PUD_NR) + pages +=3D HPAGE_PUD_NR; + continue; + } + } + + pages +=3D change_pmd_range(tlb, vma, pudp, addr, next, newprot, cp_flags); - } while (pud++, addr =3D next, addr !=3D end); + } while (pudp++, addr =3D next, addr !=3D end); =20 if (range.start) mmu_notifier_invalidate_range_end(&range); --=20 2.45.0