From patchwork Fri Apr 26 14:45:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13644919 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E34F8C04FFE for ; Fri, 26 Apr 2024 14:45:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10F056B009A; Fri, 26 Apr 2024 10:45:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0F8316B0099; Fri, 26 Apr 2024 10:45:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F155F6B009A; Fri, 26 Apr 2024 10:45:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D08066B0098 for ; Fri, 26 Apr 2024 10:45:14 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 81E64C1366 for ; Fri, 26 Apr 2024 14:45:14 +0000 (UTC) X-FDA: 82051955748.06.9C4B624 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf29.hostedemail.com (Postfix) with ESMTP id F22A112000F for ; Fri, 26 Apr 2024 14:45:12 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=D4g+WnOb; spf=none (imf29.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714142713; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5xao1faSLSLg0StYuxJJ8KUL1V23wgo3qyoN0YuKNr8=; b=voYXu8CSl1b8Jr6CYRfq/3x/POhOBUS1iK365gNw4+YTAbVUUQhrq05FpsrtEJYSfpsvW+ iTm9P3se9UOnXRikdTLht8M8fe0jXPH7U7YSg6RImyTBeIFUSrlnSQSu0xwEfAO0cBPkyO bO7LMh9TfymQqZ11d9GKKvUqgAqZshk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714142713; a=rsa-sha256; cv=none; b=PUhN++aVtl73m9BpO1h98Kf+mvp6o3g8ZN3OVz2RMt64S16AmXLWHr2eLvh10MbbscrfSf ujNX/L1yRsn+IJ7OEozIsEBiCCgx+hC8Vl7WMjvN/kltIxR5R/vAYuOXehE2QO2dpPT45U FxKcvFmD4oO86tkFuApPdj2tbmk6rOI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=D4g+WnOb; spf=none (imf29.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none 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> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: F22A112000F X-Rspam-User: X-Stat-Signature: y5gpryzegddc6w79oo1s9ptgbiehaipm X-HE-Tag: 1714142712-986600 X-HE-Meta: U2FsdGVkX19zF6hsDwHO7kLzd2vachj0/WrFvdHDSEhsFtE1H8N9EOUQYkfbC/9O5cgMIymieMs58qW/7t5s3qlTF2wozyqMSuSmsXvdqfxnwmsGHmsLor/Hb81z4K4xA02Bcs++Wf9aSBoXYqIHZloz7slKRMurDdFAAdaR5lA3/sGvKKUbZ8mEHi0vm87TEvMFLIeVfkIUl790rvhuMYCvsSabJwF2pRuaT/Jrdinrnlesbjb3h9fpboldR6FkwGiWZc6tE9fu3qlmvumLwfCsqAMuLrbOymXxN1qpi/2J8JULvf7dRlxCxp9G+66G5ZyJ+BX1BFBt2AxSxP/yOSiIQH4FEGbtWWYUV0pdbKvI9JrYdUlWP1z9ig2KBwe+QTGRqeasvP0QS62sfEAtOcxGfHX9pQU5Xj/WYEiP6ZRGLcK+KQM/epqVjOUTYyWpkZd4O3bDuPRyNzJyjHaJlGjw/z/tlyOb0i2CtzkjcHegWzTOpw1w4HwEnAtc4MX+G6TtBMUnTzCtkyHuXKdYTtqaGCYkvfp+Mdyo2jjrqSx9Z8jOqztFrHX4qoFx118Knq1zNeoE+ynpZiCca7JHOc5p7dUWH5f7uoh6Wae7FkARq/zMk+hnWRjTjipi20cow5hy9vL7XOhJiUNPNEOWIU2oEa3kqqem9LanJT8Zj8dvsqJ09JJ+uhkvTTUPlkbrkvI2IRguQSv513KN3vImwa3+zK/9frQ//tWgMJOQjLLxbGgpZK8Z1NAHHbr6bZpi+k4IwuxQu/YDUGxscDa0ODARhhkbob8kIgBsIuARQuZUfGeVJuIOwYOawiJccy2oHMWHQraXOjMrBUrUQ4OIlMPQaIogdRypZeGw+6FRcrUS1l5hdflsZztiObQCMCimzlFQy2KPD7O13vmBAEE3gr+ImJ9oR3wYbVe61LmbuXQg9aVhRstTICODhf44oEa8y2u6x5rzTJ9QEBXVZtW 6oe+WPp+ XHHhtSWx374tihHcTJjxriwACjhtfmmSQSdLlgJP0CC3e/sXJyi4/sgCmU69hSQFkTN0hxq3JorgdqolamGFYfzZc5U5bylWdgSQcGoKwrd3sXOWJLsySJ/njzm39cUJBYU0bJOLTGYly9nzyJu5kt+33nLm8mIFopfVBdXi24fvuOuTk5iIZ+DCqRGJmAnUam4LF+96OZ5Jb0LUHGCrYSPchEnORlmzTtKMk6OM392Md6FK/NkzM2GjrmyOolTcRLZYlqOtW0PJTY7cq8FTWZk7n6sDgdk/5eT0OebJWHLonvetZe04jhEy9G6U6rpaRCBZyYlI3u8zsrqk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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_fault *vmf) gfp_t gfp; struct folio *folio; unsigned long haddr = vmf->address & HPAGE_PMD_MASK; + vm_fault_t ret; if (!thp_vma_suitable_order(vma, haddr, PMD_ORDER)) return VM_FAULT_FALLBACK; - if (unlikely(anon_vma_prepare(vma))) - return VM_FAULT_OOM; + ret = vmf_anon_prepare(vmf); + if (ret) + return ret; khugepaged_enter_vma(vma, vma->vm_flags); 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 struct vm_fault *vmf) return VM_FAULT_RETRY; } +/** + * 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 = vmf->vma; @@ -4434,8 +4449,9 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) } /* Allocate our own private page. */ - if (unlikely(anon_vma_prepare(vma))) - goto oom; + ret = vmf_anon_prepare(vmf); + if (ret) + return ret; /* Returns NULL on OOM or ERR_PTR(-EAGAIN) if we must retry the fault */ folio = 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; - /* - * 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 >= vma->vm_end)) goto inval_end_read;