From nobody Thu Nov 28 20:40:16 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 74A6513A265 for ; Mon, 30 Sep 2024 05:28:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727674128; cv=none; b=Kw+ovTMrTM6+8CiSs9DhiX4jmocF1NKFQrUjjMz3Prkjatd7//EzFIWFV5v8QyZ7Qo2M3pMamBCDHR6yW1V5ChwVQFlJv2Jy2J0417iLfmRcaZ/1AP3/td/bRHRM3uvtorffcqNH7nabcUcbIKCZ5MPJujVmrlYBYSbf1BD63Y8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727674128; c=relaxed/simple; bh=XFmF+NPflfjpcfafvvp9Xsg60aFMqnY4fVPbq8xrNOI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Zps4va0C8EDf2dZebRUTk5haJYIZWAU/rB+eQ6WtjDewODSuRR0udesgjEltqrnydUsQN0sVsw4K2622nsAd7mtGeAcmOXRFnmypRHgzlKuadPwhUNfsg4YuFONSrEFVWQjrktp0DsUwqmOICQaNlK15JjJsjfh200jI/hVo9wg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8A20AFEC; Sun, 29 Sep 2024 22:29:15 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.41.8]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 50F0F3F587; Sun, 29 Sep 2024 22:28:37 -0700 (PDT) From: Dev Jain To: akpm@linux-foundation.org, david@redhat.com, willy@infradead.org, kirill.shutemov@linux.intel.com Cc: ryan.roberts@arm.com, anshuman.khandual@arm.com, catalin.marinas@arm.com, cl@gentwo.org, vbabka@suse.cz, mhocko@suse.com, apopple@nvidia.com, dave.hansen@linux.intel.com, will@kernel.org, baohua@kernel.org, jack@suse.cz, mark.rutland@arm.com, hughd@google.com, aneesh.kumar@kernel.org, yang@os.amperecomputing.com, peterx@redhat.com, ioworker0@gmail.com, jglisse@google.com, wangkefeng.wang@huawei.com, ziy@nvidia.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Dev Jain Subject: [PATCH v6 2/2] mm: Allocate THP on hugezeropage wp-fault Date: Mon, 30 Sep 2024 10:58:12 +0530 Message-Id: <20240930052812.2627395-3-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240930052812.2627395-1-dev.jain@arm.com> References: <20240930052812.2627395-1-dev.jain@arm.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 do_huge_zero_wp_pmd() to handle wp-fault on a hugezeropage and replace it with a PMD-mapped THP. Remember to flush TLB entry corresponding to the hugezeropage. In case of failure, fallback to splitting the PMD. Acked-by: David Hildenbrand =20 Reviewed-by: Kefeng Wang =20 Signed-off-by: Dev Jain --- mm/huge_memory.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index e3bcdbc9baa2..4677ed76953c 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1790,6 +1790,38 @@ void huge_pmd_set_accessed(struct vm_fault *vmf) spin_unlock(vmf->ptl); } =20 +static vm_fault_t do_huge_zero_wp_pmd(struct vm_fault *vmf) +{ + unsigned long haddr =3D vmf->address & HPAGE_PMD_MASK; + struct vm_area_struct *vma =3D vmf->vma; + struct mmu_notifier_range range; + struct folio *folio; + vm_fault_t ret =3D 0; + + folio =3D vma_alloc_anon_folio_pmd(vma, vmf->address); + if (unlikely(!folio)) + return VM_FAULT_FALLBACK; + + mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm, haddr, + haddr + HPAGE_PMD_SIZE); + mmu_notifier_invalidate_range_start(&range); + vmf->ptl =3D pmd_lock(vma->vm_mm, vmf->pmd); + if (unlikely(!pmd_same(pmdp_get(vmf->pmd), vmf->orig_pmd))) + goto release; + ret =3D check_stable_address_space(vma->vm_mm); + if (ret) + goto release; + (void)pmdp_huge_clear_flush(vma, haddr, vmf->pmd); + map_anon_folio_pmd(folio, vmf->pmd, vma, haddr); + goto unlock; +release: + folio_put(folio); +unlock: + spin_unlock(vmf->ptl); + mmu_notifier_invalidate_range_end(&range); + return ret; +} + vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) { const bool unshare =3D vmf->flags & FAULT_FLAG_UNSHARE; @@ -1802,8 +1834,15 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) vmf->ptl =3D pmd_lockptr(vma->vm_mm, vmf->pmd); VM_BUG_ON_VMA(!vma->anon_vma, vma); =20 - if (is_huge_zero_pmd(orig_pmd)) + if (is_huge_zero_pmd(orig_pmd)) { + vm_fault_t ret =3D do_huge_zero_wp_pmd(vmf); + + if (!(ret & VM_FAULT_FALLBACK)) + return ret; + + /* Fallback to splitting PMD if THP cannot be allocated */ goto fallback; + } =20 spin_lock(vmf->ptl); =20 --=20 2.30.2