From patchwork Mon Sep 30 05:28:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13815372 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 D2A27CF6491 for ; Mon, 30 Sep 2024 05:28:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 12AF76B00CF; Mon, 30 Sep 2024 01:28:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DB5C6B0205; Mon, 30 Sep 2024 01:28:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE4E86B020A; Mon, 30 Sep 2024 01:28:30 -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 CC9776B00CF for ; Mon, 30 Sep 2024 01:28:30 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3EB351C6BD5 for ; Mon, 30 Sep 2024 05:28:30 +0000 (UTC) X-FDA: 82620274380.04.661A601 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf29.hostedemail.com (Postfix) with ESMTP id 5060B12000F for ; Mon, 30 Sep 2024 05:28:28 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=none; spf=pass (imf29.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727674070; 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=iEnWNOcvgmFz4OziXljh3ROJsV1R8LCeUouVl+9epLo=; b=BYOwiekb4wU6iy5U9GPz3SrXigFq/+G1Bh85cgEoSispc4hKiYQtzQ+OMVaThpjizTe1PV KVOUjuXRNIQtCRbvMEeRwxkR0aOq/3HMIyz1d8bSf4MCSwnlV865tPPYnFrJ4p+Arp78sp 6iS0VrDpNhu0KIi3wdyq6JjSC0Fxmyo= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; spf=pass (imf29.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727674070; a=rsa-sha256; cv=none; b=pakEJdbcPicuxnrCv1vhzdadKBiNFfm0qjeMznkt7ad7z/YmDKgWVREIYACuPQSTbg7ljW 9St94GkdAbP0Bh6fj36MdZp2vaXgndQ7H29qxrQfYnNEpt+IKxEchU0YIbvowEdp8JKAy+ 73BIuANQECm2mmaNz5pxHQxLmUVjvNo= 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 A4997FEC; Sun, 29 Sep 2024 22:28:56 -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 0CD273F587; Sun, 29 Sep 2024 22:28:17 -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 0/2] Do not shatter hugezeropage on wp-fault Date: Mon, 30 Sep 2024 10:58:10 +0530 Message-Id: <20240930052812.2627395-1-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: wbfsn4zczha6g5pz9sc6za9t3dbjksr6 X-Rspamd-Queue-Id: 5060B12000F X-Rspamd-Server: rspam11 X-HE-Tag: 1727674108-903611 X-HE-Meta: U2FsdGVkX19bI7hUqTgLjsgH4MZHnpARNp6hSyMcXK4a8XDwJLXI3qX/6a5tH4RFuZOAXiPaz4+Lg0IglGRU2Qe7dc7DvJULh3VGqjRGXpXClZ/8f81q5ZfHWal9gid/tsa3o0dFXub4XIahqC0AVxrOt2DtVBcWOatgF0NcjsDahIuwWrFxQJu9Wyz1q7l/7QiohHpZvnZT9G1WBzRoIvCwmBQ+f6cvkAdG5ZxUQFWwHlLEgjTNFgGn02+N+MwYxIwQNcDeRrsyQKmwLXIdd+PcHC3wWUnT8ON9Whg/buI8OXQoxt/18SvDhf/0ZHZWUd3C+6tlx8F/QQ37hdr5dii/szoirECl6GPh732hAC2s/xaIVX9RgLnfDC+bBCCq2Tu4/ijCEfY4IZuDveVGKS81aSrc+gsrWEsfN7FgOgM+JvcQixmS4Hv9iqwGt+u5TF5iAKfQ27kJNvNXNVmfEXFNvLXCsFvNKHJCNMuLXtUlzRg0qHzUl166FfnalU4+TK9InE62gsnSwuYXE5SZt7OgXq9vIeThTOOtv6RK3xC7YiQUTRPeFBX3JFXlD2JdzNRAPxFUxamy7kUPc133qLzagOSfzDygHl6K2OS+2AXFhEyl0M9bVgxeT8YMNTXO1+ZoCbqqaVs3RP2TjkVXP4AQWk9xC6X9CnAv3qBqd0Tl6N/vWTMgebBSPMsxpl7mDq44YcQfXwNzG/keQwCHXD4gP+k0JfuSOkxPDwS/tXnI2gxWNBQCWAm5jqMVggD7KuznBfRvHnodp4FzXO6KvBp5Zdk1j1RGsNRPwoNDnvd85XLNfcVDV6re7f21nC77duVPz1LYsVKsMjZcr0C5foggtiLYII6fnIJpcb9BrcOinl/oz/CK9RBp51E33MBgW/xA9zyQIlYotFrdOUpRAYAtLAgX52+Byal6v3SqjyosmQ8OyyITJmZsemEHiQ+lfDcAn9QvfjSdCoO0X+5 XvIYj/DG /Mxk5pGiSz3rJc1cKyxPffiAcNJqSA3eH7FrKsvwAcDvXCh8m4ud6QIn2T/wKrJrZ/fGDxcuefAZvks0PTMHwCbSDkMfa8U60nPkZlilo3XLdxUxWrxXEeBjzAx/Q9XufStpnUxlls+rQpsZCaSnwGQp1OsZeLmtXklCi8y/ku7Vvr48W0UFYVNletQ== 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. 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(-)