From patchwork Wed Sep 11 06:55:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dev Jain X-Patchwork-Id: 13799766 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 8B31EEE0213 for ; Wed, 11 Sep 2024 06:56:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 028B794000D; Wed, 11 Sep 2024 02:56:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF1DD94000B; Wed, 11 Sep 2024 02:56:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB93694000D; Wed, 11 Sep 2024 02:56:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id BCEB594000B for ; Wed, 11 Sep 2024 02:56:23 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 74072121336 for ; Wed, 11 Sep 2024 06:56:23 +0000 (UTC) X-FDA: 82551548646.07.C66A438 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf23.hostedemail.com (Postfix) with ESMTP id C03F6140008 for ; Wed, 11 Sep 2024 06:56:21 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; spf=pass (imf23.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=1726037678; 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=9YmaZ/K740MigOspdxnER7Ni8BNy4OW9UNAKTNE9qlI=; b=MShHchMvhUaQfVOzLwdmcZuAPnmNolW1KlgFR6jGP1PGF6Obx5yxOKCyMDf9WlpKmPhE3L MYC8aNUnzSuKyFsefKa6fndyDZ1HOoF1tO0F7pwxFt1NieLyYn3X/UeAlKggJajGD6EeC9 4CfSDfT28jpuqjfB3EfFiB46kBWp4uA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726037678; a=rsa-sha256; cv=none; b=wF3JUDZe/kE4RbdkZQhr7GYhyKyJSYD2F5jA6MwlJvGQ0fos1p18MsP16FEiZHgunVJSgd Y+umK6lLXmRp0WEDwzvU/kTSdGhJ+k0pCIf4x48ohh7rpxYdk1UZpKd/sdQirZz3dhln3k Kb5RGCat0QWVibC2nizDKarRxnRp+EY= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; spf=pass (imf23.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 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 2BCA11007; Tue, 10 Sep 2024 23:56:50 -0700 (PDT) Received: from e116581.blr.arm.com (e116581.arm.com [10.162.40.31]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 0B25C3F64C; Tue, 10 Sep 2024 23:56:11 -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 v3 0/2] Do not shatter hugezeropage on wp-fault Date: Wed, 11 Sep 2024 12:25:58 +0530 Message-Id: <20240911065600.1002644-1-dev.jain@arm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: C03F6140008 X-Stat-Signature: ibence5gn6johk3q4umbnk19jwhwqrbh X-HE-Tag: 1726037781-203212 X-HE-Meta: U2FsdGVkX1+lzL8oln7upkFKxInq8W8upCRNmSe0WFEYa5UAL8YeNvc8yH+uEr1BUcw35X7ecUIT/4TBbisHZkr4MMpk5wX/+WGx560rPQHkJ2T3TZdA148IOBJoGxezjl/RBfUUvG2iT1ULYYqNuWp2xWcWqUZ36fIdkAvMtgvW6x6oJhhtvErZV7FVUd1a7Q/T8sKZDPafVt3rw9yKIoVOEzmAkJDq/+v+aDyvQAcLXf+x9cm7yOFjeOWsSBE50yOO8WFmS/xtVMng4ABBniACQsRpiWLryf5NeHuJH4J8CkJ/d1U2fGG6A62CP5XTusMzeFoLG6iNmAcatba11UIVz0GRL4x2cjlSQNvVw5IR7Lvg0Tzp3WJZD8IW73Rib6XlPM2Xl1W6104zwTVDSmrtuqIpyMG2cO26+ivNke3BHMSmFXuVc2sPCsLi6LpnZUnZeTEoQlok8bRTuYDzlg7EbOiV2oVw4p6pDWyCsZBn0djl8ZBwJ2FlYN3E5jfJXTsc3ReK++pS3ksM6NC21HwZV7AXqaWW0j41k3xG1SiWjqX3VNk55IjgSyT6LX2Q5ML7xRnWbd5Ze3TM4qrIrIw0sswLdU4O0ucII8X4GIfhORTk7S2Kb8BvbF8EW5k/CMBN4zv7zzOpuyRDFF7rmAPqiZ3C+ercvOCFJDvbtigkLhzO4jR1rg0jkW4IaGlmDXMst+B9LCEj/2/Wpmz93nEGd0HBnd3001m9agxZfocJNwnkQH2TXw5vCKzTiVLDFOQQECSdhtYDPUzwWqJ7CmZoNk92cmnp+mdGH5tWA44BAioOGOcxl+FPZ/Uz2yEfC/ekLmVyvQrC4DLoKX4bCB6DGxZ2YHKTDABjcXvNT9x287PuwfwVB25qx2mmZ44ybNC3pvewg15o5o6yf0yd5DrUVFsVvndQMo/TSE+PFgpGySUckf1LZgatKUV1W1eJ2+RyvGnwRUQ6FwIdqIa oih7RI1J Ocjb85Q56cQnVS1CYDJ/Cq+oilhLMx4LG22UjSbxa8rl0qiU/b1MZaiR1RSKzzn0bGGpvou61Xrtp1SpZz4knGPyk+gQirfpr3eUf8fQgW3zNEvk/lyXI8HO4COSWucCAQjDcOnpHAfkMLEIWM117APNWt7SlnHDZ4H5IvMB7Xv7p2C4= 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. 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 [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 applies on the latest mm-unstable branch. Dev Jain (2): mm: Abstract THP allocation mm: Allocate THP on hugezeropage wp-fault mm/huge_memory.c | 158 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 114 insertions(+), 44 deletions(-)