From patchwork Tue Oct 8 06:17:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13825668 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 0F485CED26B for ; Tue, 8 Oct 2024 06:18:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 34F4D6B0083; Tue, 8 Oct 2024 02:18:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FE956B0085; Tue, 8 Oct 2024 02:18:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EDE26B0088; Tue, 8 Oct 2024 02:18:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 029E56B0083 for ; Tue, 8 Oct 2024 02:18:09 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7D787C190A for ; Tue, 8 Oct 2024 06:18:08 +0000 (UTC) X-FDA: 82649429898.13.A064381 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf04.hostedemail.com (Postfix) with ESMTP id 9650840003 for ; Tue, 8 Oct 2024 06:18:07 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf04.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728368188; a=rsa-sha256; cv=none; b=CYMhx1I2WMKxy8m2yqyv0bhznmDF9yQE1pg3QxJo6aq6XopqyZ2meN3g2vMKi5JDxUQNJN HzJj/hwSsRS/YF5SFqb5DAkiJsr3m0PhC7TJI6QChBsPm32l6UWRg7tpg7h0J3Xz5huRUD CRClSlIwtZdgHTP8wiDaaStXtn12cok= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf04.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728368188; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=tHPHwP7Tp8k/32rfrSKz2E1ogPbCqXJq9itbEwMJ8mo=; b=UG/Kqma/x0W1223vnnoCxlfUyBAVgiLWVPWrgx0GwGq9ukYRpiNI0RcoyYqU/kOCYI1iZi +G5pVhuwr4yjHfZvDxYJXgtobm5pR+l5jV9RkQuFOMp7q1/fpspkQIgxuSNYOf52EYsNKh XrwOUZGln1k8TXu26BLytdXrSEpRtn8= 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 175A7DA7; Mon, 7 Oct 2024 23:18:36 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.43.142]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 77E8F3F73F; Mon, 7 Oct 2024 23:17:57 -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 v7 0/2] Do not shatter hugezeropage on wp-fault Date: Tue, 8 Oct 2024 11:47:44 +0530 Message-Id: <20241008061746.285961-1-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 9650840003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: i3wqooarnurtrtidkynaw6ugc6dtu4s3 X-HE-Tag: 1728368287-430141 X-HE-Meta: U2FsdGVkX19vPwVmCehPa3XZGeb3+1z8s/TsfZdEuVGoHLq92LKMZQlYBdjWeOYr1wp2nFWtBdrgzxqooS36AnwMBm0zaIASQ81KMZSgkn2CcdhEU0cZHOmyIDRsLhbqvGg47cAbYQMDWEp07rgfP2yh7zCJeBLHfaOb4OfSrtDOpsIwZBbFu+mjcX1uE2mEFw568zzfJ0upb0XAkh++OJ5BwwII5C95xK9px14FJER+23myczgVIdLBW8DbsJHaEuUWxeMo2VLuGEbZ4fg2PzhFTLnMOmmK0jA9OSvcKw5j0WYx8A2DstX6PBzq10nkwrNRKi2BIxiWf78XPv1HJoZh9A7v5u8XaCBaWjShC86m/i7qpiA+nI4uo+LS5qdiijZbx6MhXAjV/Gtafwe83vjQmdpCrl7CK08vVl0TXwXZQZu5VPrCSYqhwKDkbH2C1IjPdqzJGH9ouHqBuqWPnguHH7E8gyt7vUdUOpQfMXiIlZecyD57Yy1ELo+YPVevLep3lJiYC13MWSlYTKZnZ+ncG4T9KWgysosPBa1j9n41zTV0mi/btwYRVkv/Ttx0QSnkmsm3kPBiXwv02X4nySGlKMt7Pd4oPn3m4MSbboOx6PVver9WdA7RQkAq+n+PB8wDdS31f2OY04tj4xd13x+YO7toJLb4J0XNKGRvlKavk1/B2E0yA6S0K/JC2F0vRZQwJ+jbync5SY/xHip2wutcXjzAD4fNCtNDFSRKdEH/POh2A9GL/nn4/GPhFnqchV/JteBaS/h29Sfabfb3HiMxQzoNnFXk8bco+GKRQzVsmwJwQ8CK/YwveQwt8mgY6mnKCBGyb3zmDu+rWDa9CeYeAmUZfvgdbmEc2mXY5hDFCQhMaWtjJMAzPzYYSxULEEFRFSgzk+faIpXEujx+3zA5MdxSddUl2qnJqwkZnbQudH3qi2n8gYdgU0ualeyd0k+9S4UllaD7XMTYFym PzL5ho2s eINx0EX6jdkshu0RsGWZwDEgcEZYQTXcjfwHhndU/gcTiRyBak5/oV4grmcJeAW7ehNYHoHBM8F9gupkQYxaQpNs1u5YEJGmzgMWfXZ6mOqcFR/j3MMcRVbQ5Ct99qPX2q0aV9t1Iz+xYx38NT5+Qu8Fs4tidR65IewDyhTUTjt1faozGSZudJNY9WQ== 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: It was observed at [1] and [2] that the current kernel behaviour of shattering a hugezeropage is inconsistent and suboptimal. For a VMA with a THP allowable order, when we write-fault on it, the kernel installs a PMD-mapped THP. On the other hand, if we first get a read fault, we get a PMD pointing to the hugezeropage; subsequent write will trigger a write-protection fault, shattering the hugezeropage into one writable page, and all the other PTEs write-protected. The conclusion being, as compared to the case of a single write-fault, applications have to suffer 512 extra page faults if they were to use the VMA as such, plus we get the overhead of khugepaged trying to replace that area with a THP anyway. Instead, replace the hugezeropage with a THP on wp-fault. [1]: https://lore.kernel.org/all/3743d7e1-0b79-4eaf-82d5-d1ca29fe347d@arm.com/ [2]: https://lore.kernel.org/all/1cfae0c0-96a2-4308-9c62-f7a640520242@arm.com/ The patchset has been rebased on the mm-unstable branch. v6->v7: Align function parameters in the second line by two tabs v5->v6: - More goto ommissions, remove build warning for !CONFIG_NUMA v4->v5: - Directly return VM_FAULT_FALLBACK in case of !folio v3->v4: - Renames: pmd_thp_fault_alloc -> vma_alloc_anon_folio_pmd, map_pmd_thp -> map_anon_folio_pmd - Instead of passing around, compute haddr at various places, similar with gfp flags - Pass haddr to update_mmu_cache_pmd() instead of unaligned address - Do not pass vmf to map_anon_folio_pmd - Do declarations in reverse xmas tree order - Drop a new line which was introduced accidentally - Call __pmd_thp_fault_success_stats from map_anon_folio_pmd - Correctly return NULL from vma_alloc_anon_folio_pmd - Initialize pgtable to NULL in __do_huge_pmd_anonymous_page, to prevent freeing pgtable when not even allocated - Drop if conditions from map_anon_folio_pmd, let the caller handle that v2->v3: - Drop foliop and order parameters, prefix the thp functions with pmd_ - First allocate THP, then pgtable, not vice-versa - Move pgtable_trans_huge_deposit() from map_pmd_thp() to caller - Drop exposing functions in include/linux/huge_mm.h - Open code do_huge_zero_wp_pmd_locked() - Release folio in case of pmd change after taking the lock, or check_stable_address_space() returning VM_FAULT_SIGBUS - Drop uffd-wp preservation. Looking at page_table_check_pmd_flags(), preserving uffd-wp on a writable entry is invalid. Looking at mfill_atomic(), uffd_copy() is a null operation when pmd is marked uffd-wp. v1->v2: - Wrap do_huge_zero_wp_pmd_locked() around lock and unlock - Call thp_fault_alloc() before do_huge_zero_wp_pmd_locked() to avoid - calling sleeping function from spinlock context Dev Jain (2): mm: Abstract THP allocation mm: Allocate THP on hugezeropage wp-fault mm/huge_memory.c | 139 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 97 insertions(+), 42 deletions(-)