From patchwork Tue Sep 24 10:16:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13810679 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 59638CF9C6B for ; Tue, 24 Sep 2024 10:17:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4E066B0088; Tue, 24 Sep 2024 06:17:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CFE396B008A; Tue, 24 Sep 2024 06:17:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C141B6B008C; Tue, 24 Sep 2024 06:17:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id A64976B0088 for ; Tue, 24 Sep 2024 06:17:13 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6D358161911 for ; Tue, 24 Sep 2024 10:17:13 +0000 (UTC) X-FDA: 82599229146.08.3970AAB Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf24.hostedemail.com (Postfix) with ESMTP id 6A8B118000F for ; Tue, 24 Sep 2024 10:17:11 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.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=1727172900; 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=LNiSClzqMthMSGHY9ZybIoDglAwvnRuun9Dv6gUbWD0=; b=I3w4dHxdqk1pMlxKy/XMMXlYorpJ85qMpsD46196irM4YG17VCKUxBz1ASicEquzK7iMY2 OsDBy+rxx4TXvFLnr2mGJkh7y4haKEf1aKTwgbQhHVSpfvOYmGTm1DNaOykk6zzT3otxlF uEBKIFwcom/IvRNdhlag3y78RcA8JJ4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727172900; a=rsa-sha256; cv=none; b=gx3XgyyDtWxsqJAvOQhHMsFQAXzlnSv9ydjlBBhM6xt8BnHfmHEZPQu9CrR1VMmGtyn1gO edGfWuOxykMq+qkLNHrZJbhd4MgVhk5wtDLyt15xd1hETKCbhjoEcaQ0lS4B5Az9GYlL5l 8l94zUjGKUS0R9OODgcDLgF8C19JSpU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@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 E7BC8DA7; Tue, 24 Sep 2024 03:17:39 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.43.15]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 750913F528; Tue, 24 Sep 2024 03:17:01 -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 v5 0/2] Do not shatter hugezeropage on wp-fault Date: Tue, 24 Sep 2024 15:46:52 +0530 Message-Id: <20240924101654.1777697-1-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6A8B118000F X-Stat-Signature: 5jccoadef65g4cwwhctkezbw9mb7sbup X-Rspam-User: X-HE-Tag: 1727173031-438717 X-HE-Meta: U2FsdGVkX1/dHkb+kwF+NUARadREy2XgO2PEr/gT3fGTGDEYuy/vcb7HRO/366uDRfKd9K1GHt+EhqKBPzsVwzfph9qFPBDIGNqfO55JZiv39lINRLAHtYy1gAFuMhXnVqs/j9TVBr4VDIwahWKrym75wBRYk53zmU/LPXw+gC0tasGGc/PH+iWWGVZrLqKWtr0OgrNjZDAuSBPZ1HDB7qUxrYkSuZyH5mXbjQAWNCuRla9NpSEe1zaxbq7ezt1ZY3v0jO2ynQThso5zLRG4/0K5+FZzmNmJ6MaG6Oo6e9bAEL3V3bjO9q1T7o+L3aKsL7BUQ4GE8vU2njrQP4TZzsVhdr4/1U1npQIZQ2E0JjdajmjYtIfftIT0jW633f5pXnnP6EMiZXisAWp7ifZ/Val3QnwiimRoF41IgMzOCHH4BzaTZMjIFuwEHkpDeaRyvZbJ/RlAFYtRHrcdcZL53mgM9gtHLl5KonH6yqlMOMSLYVJCDpXqLdgQzPs8YSggvxm9qc1WIEJ7Bk/On3buPvAQynbQCWVqyICFYkvC9Y3B5R47lLtCradsa//a1dvBlewvx04aWTadWWaLnODMUj7kdhqf46jvjhv1dRQIgiKTQ0hSU6MCSz9nwl/n2BwqxmaHA4mlYo/6Vi14WVZWYtNW9VKTjsug7ospkuro4TQuGoa9PBKSJ/OzggpVggbKIun9G66c9pGmWHEJ4ayhDGYsaGpn9db5guQDBhA1O6YihPaM1BGdW+Ejmh3oQQkQf0QKvLFT4Se10WrwXa+uG0wDwA57JRLUgkozdNGeW0HQfqnZRVkLfZ3ihP3q270IzxGCHqO9y1BQobAkuEojOeEs87+sHU1/FgBICH/2Q+vbUyJ40PLfzKqsLHxiCefEha+Vj4U8t0FaiGZNwofmexosOeWo5d9X5w1hURyX56nQVbkFptancwTJbRtgnyU2FD3rQu5PuBqsQ/RQRqq +Y2d1X6S i+bwc45XY+v0PnRTijmdrZjWZbdbz+D2nceob8NNb8hkmSFp1TmHHxJRC5DpGP8rtmpHUGILljmCqX8rfVv078hsCwYehNxDe6NuNv8onVPY6CjI7UTk584z2WHvuSdJMx/zoD98Xw7uhWhlQq7i5xy0lGMhnmBBZzNI6TVOmWsqz5J0= 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. 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 | 142 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 100 insertions(+), 42 deletions(-)