From nobody Fri Dec 19 14:06:27 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 99C7214883E for ; Fri, 26 Apr 2024 14:45:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714142723; cv=none; b=EELZERC8sDLuQsyGJ1rhNCIWFPRlyQp26IOT62eOpoHgc8rWbjqcuxGsMdWh1frknriip+HMqjsOHxDJS+gG3mZDrBFOQsltH2VBe6nimS8Rf9xo/ZfHAU8dxF99/WgEtVvDCBnpklMDnmTK6zzF+s3H0uAY0Ovg1lB3xgKDuPI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714142723; c=relaxed/simple; bh=xqXVvOrZ5ltRDA3pAq+U7GnRoZ6AOoUCgQ+NjYr6YuE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O9IhwDde11WPJ5l4SB6p5U4E0/9l6PAZTw/h4TWv5qIXhby1bNy6bsyjC2cJDf8e0Rby7kg8qiq5GfQuZif5Gz3bEMO/5rdtA98QIUvWXt10da2GN9O7KmPi94WNInzVqY5I+09FkaVkWMqyL0jrpRgVIkaN2mli5bxNHO4RiMs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=RyyF7zdw; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="RyyF7zdw" 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=WfdYXkl3VmeSNFl8CeYkQkRSot5hSq2wAYXQXVO2q28=; b=RyyF7zdwWk9nDa97+eyubbdcGh /w28kPOhTqJkoKMGtwC13r+m64VxEWsAi8TiLuVDtxQS2gSkWj39hwiAkuY0x1wxCTh45vs8B9FTn sD30juuwToyG9roIfdS9dV8KS47Mv8h4eldpZC0MIv0XWa+zSjDgFtCb0N0CH3m5CjLA6W/+Ermsv vCRso3N6rfSCVH2Va/m/EBte6Z/aKqUBvmbYVHj936Hjj3wYAuJc77vTg2oam8yNVg16gv4t17js7 4OfTtjSbHss3Gw2WztfJrsDnkdMN5ObcpwwKZPNiMEVbnV6Pcu+aigMm//iKd9fRHCaGg13oL7Du/ /4x72pfw==; Received: from willy by casper.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0Mp2-00000005Pl0-0bLw; Fri, 26 Apr 2024 14:45:08 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Suren Baghdasaryan , Peter Xu Subject: [PATCH 1/4] mm: Assert the mmap_lock is held in __anon_vma_prepare() Date: Fri, 26 Apr 2024 15:45:00 +0100 Message-ID: <20240426144506.1290619-2-willy@infradead.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240426144506.1290619-1-willy@infradead.org> References: <20240426144506.1290619-1-willy@infradead.org> 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" Convert the comment into an assertion. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: David Hildenbrand Reviewed-by: Suren Baghdasaryan --- mm/rmap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 2608c40dffad..619d4d65d99b 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -182,8 +182,6 @@ static void anon_vma_chain_link(struct vm_area_struct *= vma, * for the new allocation. At the same time, we do not want * to do any locking for the common case of already having * an anon_vma. - * - * This must be called with the mmap_lock held for reading. */ int __anon_vma_prepare(struct vm_area_struct *vma) { @@ -191,6 +189,7 @@ int __anon_vma_prepare(struct vm_area_struct *vma) struct anon_vma *anon_vma, *allocated; struct anon_vma_chain *avc; =20 + mmap_assert_locked(mm); might_sleep(); =20 avc =3D anon_vma_chain_alloc(GFP_KERNEL); --=20 2.43.0 From nobody Fri Dec 19 14:06:27 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 5B08B148831 for ; Fri, 26 Apr 2024 14:45:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714142715; cv=none; b=lTzKgyB34sQdo1nu5mPC2499EP6tIpszt0x4g0LmrzHCdPh738L5/qYBPg9HoUv7iHgleF/egd3WFt7A2XQor6boWBcnokCl27IpLAD+F9Iz4M9PenCKe+uA+TsqyUDFaMTrcL4Zvu1+kMqUyoMB/nnpUnUjTx025YSJRazbSqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714142715; c=relaxed/simple; bh=nMWKx35vFMzktdIGZuoIEs17uH263FtlfQQILCkbp34=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sAoHgQUv05ELy6f8GRo57I7lJpAvr+OkeBi1/uxlvJFgYpRXJu2P6BEoOAeg8APAwJKIcKmZEglTBr2THobGNLfBm9M5RsVLcGRn33yTJSsY/9yQIigWfrkoXZtAQBfnZfNvf/548bMcEAyBsjJSZE0hfJiOZwZ8aI5jxTfpkR0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=D4g+WnOb; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="D4g+WnOb" 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=5xao1faSLSLg0StYuxJJ8KUL1V23wgo3qyoN0YuKNr8=; b=D4g+WnOba5ewplYbiUpqL0OM87 0MRzHx47bxn/V4Qa5spqbIR364xnBh9JfepXuF8jyGySd8A8pZ+H6r2qYfEeuerd4sOtcKzqlYOVu AabUF3EG+/v1J2JfpwcqT9db+NpVdaNLwS3vJPSgOxAU2osU2MUTnnycIY8rA8HukYnKgSdcZ4L37 E6oQejNp/b8yzkzlsHL1n5q6KiFl4wi2pfJekzlduFmafXG4LilZJC4IxIwOrx2q81lvyUa3baHKO wezsagK/b5j3avamBA5CmIZHXR79ukfVOcG463vBnwOioDbPKg2lZQ5S0VMZZ4Q4knXXJKIUN0q5a E1/1Wllw==; Received: from willy by casper.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0Mp2-00000005Pl2-103M; Fri, 26 Apr 2024 14:45:08 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Suren Baghdasaryan , Peter Xu Subject: [PATCH 2/4] mm: Delay the check for a NULL anon_vma Date: Fri, 26 Apr 2024 15:45:01 +0100 Message-ID: <20240426144506.1290619-3-willy@infradead.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240426144506.1290619-1-willy@infradead.org> References: <20240426144506.1290619-1-willy@infradead.org> 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" Instead of checking the anon_vma early in the fault path where all page faults pay the cost, delay it until we know we're going to need the anon_vma to be filled in. This will have a slight negative effect on the first fault in an anonymous VMA, but it shortens every other page fault. It also makes the code slightly cleaner as the anon and file backed fault handling look more similar. The Intel kernel test bot reports a 3x improvement in vm-scalability throughput with the small-allocs-mt test. This is clearly an extreme situation that won't be replicated in any real-world workload, but it's a nice win. https://lore.kernel.org/all/202404261055.c5e24608-oliver.sang@intel.com/ Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Suren Baghdasaryan --- mm/huge_memory.c | 6 ++++-- mm/memory.c | 29 ++++++++++++++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 19000fc2c43c..8261b5669397 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1057,11 +1057,13 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fau= lt *vmf) gfp_t gfp; struct folio *folio; unsigned long haddr =3D vmf->address & HPAGE_PMD_MASK; + vm_fault_t ret; =20 if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER)) return VM_FAULT_FALLBACK; - if (unlikely(anon_vma_prepare(vma))) - return VM_FAULT_OOM; + ret =3D vmf_anon_prepare(vmf); + if (ret) + return ret; khugepaged_enter_vma(vma, vma->vm_flags); =20 if (!(vmf->flags & FAULT_FLAG_WRITE) && diff --git a/mm/memory.c b/mm/memory.c index 6647685fd3c4..7dc112d3a7e4 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3214,6 +3214,21 @@ static inline vm_fault_t vmf_can_call_fault(const st= ruct vm_fault *vmf) return VM_FAULT_RETRY; } =20 +/** + * vmf_anon_prepare - Prepare to handle an anonymous fault. + * @vmf: The vm_fault descriptor passed from the fault handler. + * + * When preparing to insert an anonymous page into a VMA from a + * fault handler, call this function rather than anon_vma_prepare(). + * If this vma does not already have an associated anon_vma and we are + * only protected by the per-VMA lock, the caller must retry with the + * mmap_lock held. __anon_vma_prepare() will look at adjacent VMAs to + * determine if this VMA can share its anon_vma, and that's not safe to + * do with only the per-VMA lock held for this VMA. + * + * Return: 0 if fault handling can proceed. Any other value should be + * returned to the caller. + */ vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) { struct vm_area_struct *vma =3D vmf->vma; @@ -4434,8 +4449,9 @@ static vm_fault_t do_anonymous_page(struct vm_fault *= vmf) } =20 /* Allocate our own private page. */ - if (unlikely(anon_vma_prepare(vma))) - goto oom; + ret =3D vmf_anon_prepare(vmf); + if (ret) + return ret; /* Returns NULL on OOM or ERR_PTR(-EAGAIN) if we must retry the fault */ folio =3D alloc_anon_folio(vmf); if (IS_ERR(folio)) @@ -5823,15 +5839,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_= struct *mm, if (!vma_start_read(vma)) goto inval; =20 - /* - * find_mergeable_anon_vma uses adjacent vmas which are not locked. - * This check must happen after vma_start_read(); otherwise, a - * concurrent mremap() with MREMAP_DONTUNMAP could dissociate the VMA - * from its anon_vma. - */ - if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) - goto inval_end_read; - /* Check since vm_start/vm_end might change before we lock the VMA */ if (unlikely(address < vma->vm_start || address >=3D vma->vm_end)) goto inval_end_read; --=20 2.43.0 From nobody Fri Dec 19 14:06:27 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 267C7148FE5 for ; Fri, 26 Apr 2024 14:45:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714142718; cv=none; b=uL/zAO46Yl9CJEXNBCIoah846j842c8rokvGjQz6OyBFrS5n16XzvricGs/boOH7OkhmuyXKos6qBb0oSB0YShcwtBGx740+LNZlPwQEObXcccPMJgbmq+gZZzkubsgExU7lvWg7eg+UhezpASoG9rmNs+MTSY6Kzx3uhqQDoDY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714142718; c=relaxed/simple; bh=lHKjkiMlqqnTSGXO+YOarVat3czks6kIXJgqZkv33Y8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AhYRlejenwUoWXU0cxZhjuzfhxqYzbQh16py0ZPoI7RE1qAV2cMpJ8cJsMU1RMwjjpFNTBmJm4BsfLmu9bONTovwu77p8lN7LPrnMgQNxHAOrBgDnbFc78vPG3he/7bhYZ9nn5336wB3ah9IeBozHkP6T5XV0afawPgQqoPR0vo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=H3Kru3bJ; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="H3Kru3bJ" 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=ahgxolPjR35rGtX1/SJWQbu72Aplypg3iRuwgc7l0ko=; b=H3Kru3bJevR/UbQEi+rQZVHYBs 4jZpcvre5O2AGbp8OOvPMiDJkyyXKYEwehrbAEXn46zMrJ7KBuWpFNQeP5GvM2ym4Znpe2rFHtJGY msYEmfK9UvuayFULCI8kk6G7LUB6YPrB2Zx+N03fvRhfkOQQbI+vtr45609Tt2Kwn6FJlK1MeXJxS WMcWkh72E4cJosWiQFX1pYa+1vdoenBQAajpeUfrFZWmbzAJ9JWV2hofQM8juXYox4jLlGGaod9A5 tg72zpBJGzqFbzLuE2zjQPW+GvalGrwtimdgo9buMc+VIRx3vHv5q3cPTlFfAlqho5X7/s/abOeuJ CWaJ58PA==; Received: from willy by casper.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0Mp2-00000005Pl4-1Nds; Fri, 26 Apr 2024 14:45:08 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Suren Baghdasaryan , Peter Xu Subject: [PATCH 3/4] mm: Fix some minor per-VMA lock issues in userfaultfd Date: Fri, 26 Apr 2024 15:45:02 +0100 Message-ID: <20240426144506.1290619-4-willy@infradead.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240426144506.1290619-1-willy@infradead.org> References: <20240426144506.1290619-1-willy@infradead.org> 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" Rename lock_vma() to uffd_lock_vma() because it really is uffd specific. Remove comment referencing unlock_vma() which doesn't exist. Fix the comment about lock_vma_under_rcu() which I just made incorrect. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Suren Baghdasaryan --- mm/userfaultfd.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index e6486923263c..defa5109cc62 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -56,17 +56,16 @@ struct vm_area_struct *find_vma_and_prepare_anon(struct= mm_struct *mm, =20 #ifdef CONFIG_PER_VMA_LOCK /* - * lock_vma() - Lookup and lock vma corresponding to @address. + * uffd_lock_vma() - Lookup and lock vma corresponding to @address. * @mm: mm to search vma in. * @address: address that the vma should contain. * - * Should be called without holding mmap_lock. vma should be unlocked afte= r use - * with unlock_vma(). + * Should be called without holding mmap_lock. * * Return: A locked vma containing @address, -ENOENT if no vma is found, or * -ENOMEM if anon_vma couldn't be allocated. */ -static struct vm_area_struct *lock_vma(struct mm_struct *mm, +static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm, unsigned long address) { struct vm_area_struct *vma; @@ -74,9 +73,8 @@ static struct vm_area_struct *lock_vma(struct mm_struct *= mm, vma =3D lock_vma_under_rcu(mm, address); if (vma) { /* - * lock_vma_under_rcu() only checks anon_vma for private - * anonymous mappings. But we need to ensure it is assigned in - * private file-backed vmas as well. + * We know we're going to need to use anon_vma, so check + * that early. */ if (!(vma->vm_flags & VM_SHARED) && unlikely(!vma->anon_vma)) vma_end_read(vma); @@ -107,7 +105,7 @@ static struct vm_area_struct *uffd_mfill_lock(struct mm= _struct *dst_mm, { struct vm_area_struct *dst_vma; =20 - dst_vma =3D lock_vma(dst_mm, dst_start); + dst_vma =3D uffd_lock_vma(dst_mm, dst_start); if (IS_ERR(dst_vma) || validate_dst_vma(dst_vma, dst_start + len)) return dst_vma; =20 @@ -1436,7 +1434,7 @@ static int uffd_move_lock(struct mm_struct *mm, struct vm_area_struct *vma; int err; =20 - vma =3D lock_vma(mm, dst_start); + vma =3D uffd_lock_vma(mm, dst_start); if (IS_ERR(vma)) return PTR_ERR(vma); =20 @@ -1451,7 +1449,7 @@ static int uffd_move_lock(struct mm_struct *mm, } =20 /* - * Using lock_vma() to get src_vma can lead to following deadlock: + * Using uffd_lock_vma() to get src_vma can lead to following deadlock: * * Thread1 Thread2 * ------- ------- @@ -1473,7 +1471,7 @@ static int uffd_move_lock(struct mm_struct *mm, err =3D find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap); if (!err) { /* - * See comment in lock_vma() as to why not using + * See comment in uffd_lock_vma() as to why not using * vma_start_read() here. */ down_read(&(*dst_vmap)->vm_lock->lock); --=20 2.43.0 From nobody Fri Dec 19 14:06:27 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 95235149006 for ; Fri, 26 Apr 2024 14:45:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714142720; cv=none; b=ihHF9Z0bxyy7W1dnxzx2npR/hiAGcMO3n+l767J6EJkxXUNuDTlph0/umqyVEzGZ3wL7dCvZZHeQrSqTEfpSENDHkwYnmtAfh+QEWqkYJzF8a30tN+U4hH2TTJNxK//3FeAe9LHttcJvuwzl52ctQrycwJrFxviWT7vHtVHcsEs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714142720; c=relaxed/simple; bh=2A0I/wQ02ZQClsJJvC2xC++2okdVlxUH4BTAOBbX1qo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dLchBwNPU6Bf2DUZiZTGxE8CWEWkJnUbGlXDRem7e23rdTsvZd+a4yGuZ9OaTi3znPeF993dFL1hgpEYnUPvcUA029AG9DviHAAe4gHXtpzhqMSrfpOq/SRXNTmCpK8E6EGO0i7w5uexBVWre9NxpKJesxI+jZyMTY9EqEUg5N8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=avEdWTM/; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="avEdWTM/" 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=xiasNW2bjz4XwywyYv4zK4GYnJbH5N9m8U5+b7/FG0g=; b=avEdWTM/yAlNRrylBNlMhcItT8 yT3lf5sbN2RA11wRK57vjoI6+cQ4lOshf22Lj4uL7AZ/b1ZfApFJfPwIoV+o5X89LxrtQKmovdkWp d6GdOd+oLFrtddsrmgjkb3tHhclq/QbDFb2K9UAk9I0CHB+msjtBqcV+NNzBx3RflOlaD9xy8Dxhk es3hCflPEq9bjg0W2B3ZvZt2nuIbUQ94Vbc6N/36fbLOt32SrhktKVGwgIfqJcg4UWo/Y/MU/xiW+ LQeO3PqBRGk0YU47AeSqIQtKcSKBURhocodIXVgGTbeLOQplgj5BFtuB0HLDEw1g+kXORauvDvxQ1 4dMG+cJw==; Received: from willy by casper.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0Mp2-00000005Pl6-1hWT; Fri, 26 Apr 2024 14:45:08 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: "Matthew Wilcox (Oracle)" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Suren Baghdasaryan , Peter Xu , Jann Horn Subject: [PATCH 4/4] mm: Optimise vmf_anon_prepare() for VMAs without an anon_vma Date: Fri, 26 Apr 2024 15:45:03 +0100 Message-ID: <20240426144506.1290619-5-willy@infradead.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240426144506.1290619-1-willy@infradead.org> References: <20240426144506.1290619-1-willy@infradead.org> 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" If the mmap_lock can be taken for read, we can call __anon_vma_prepare() while holding it, saving ourselves a trip back through the fault handler. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Jann Horn Reviewed-by: Suren Baghdasaryan Reviewed-by: David Hildenbrand --- mm/memory.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 7dc112d3a7e4..b5453b86ec4b 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3232,16 +3232,21 @@ static inline vm_fault_t vmf_can_call_fault(const s= truct vm_fault *vmf) vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) { struct vm_area_struct *vma =3D vmf->vma; + vm_fault_t ret =3D 0; =20 if (likely(vma->anon_vma)) return 0; if (vmf->flags & FAULT_FLAG_VMA_LOCK) { - vma_end_read(vma); - return VM_FAULT_RETRY; + if (!mmap_read_trylock(vma->vm_mm)) { + vma_end_read(vma); + return VM_FAULT_RETRY; + } } if (__anon_vma_prepare(vma)) - return VM_FAULT_OOM; - return 0; + ret =3D VM_FAULT_OOM; + if (vmf->flags & FAULT_FLAG_VMA_LOCK) + mmap_read_unlock(vma->vm_mm); + return ret; } =20 /* --=20 2.43.0