From patchwork Thu Nov 14 06:59:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13874609 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 095DCD65C52 for ; Thu, 14 Nov 2024 07:00:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80CB26B0083; Thu, 14 Nov 2024 02:00:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7BC026B0085; Thu, 14 Nov 2024 02:00:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 682FB6B0088; Thu, 14 Nov 2024 02:00:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 44A286B0083 for ; Thu, 14 Nov 2024 02:00:31 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EC0A0ACD60 for ; Thu, 14 Nov 2024 07:00:30 +0000 (UTC) X-FDA: 82783799196.17.95571CA Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf17.hostedemail.com (Postfix) with ESMTP id 142EB40459 for ; Thu, 14 Nov 2024 06:59:53 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=QuF4oiqP; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf17.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731567540; 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:dkim-signature; bh=ppWhpTd52TjJCCDS+EQr23PMbbYE2t0bTxCQVPbMPwg=; b=AFhVxqzHJCRVyGHXYlyueDsE1Cox17/Ku7uVlnuI8qDbTR4QeImVH9uw+uLjqp/pSThsz4 z5btPUofkeLRoEOuY9bsdg0D8CUA3T91LBqPugH1XSdiOOZghpJ6EwQL9t9Z1lv4G9h3YH kXDD4Ld/l3sdOcQhAATCzGQXCUr0vbM= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=QuF4oiqP; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf17.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731567540; a=rsa-sha256; cv=none; b=SWOFIpfHbv6k6tWIkTNuRP8YFnaX/Vkhr7smTZ9R9DkFg7b7nZfPlGQ6minPyuA0IhS9dg LIBw2veEJNCAk+kITsh9lzCqK5Y+zSsHpk1rgMQaHxmTuLKfsaFGy9p3RdAdbb4PJWA918 BotPBP1ZNBsLdEDRTUKNwCsFUz6vrnM= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20c7ee8fe6bso2510935ad.2 for ; Wed, 13 Nov 2024 23:00:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1731567626; x=1732172426; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ppWhpTd52TjJCCDS+EQr23PMbbYE2t0bTxCQVPbMPwg=; b=QuF4oiqPpd9flpsw0jmLrZ75wZAe083ofJLWp7zdm87vMJ60jvqzHukAuOm80cCtHo 1jolS/ZT6h4JRgxloXVSjaqBi5TnPA79Yg655rYDTG/nLOmXSFz43QE6WjWQNd+Pw/79 ZuSOHxsruJXPR36+fFvy/6S4xwCV1tLvZDpkGDHn4gm7u/oJ37w+yFdWzYI4zi59IiSm PdUgZERkpaJyj3t0Lvid7DAmOriLA5ViLeT4iBiFefsgnYiG0LLgModEr8t2uLJKkX3I FfJPZB2jSCkf/j+m84kTzlUNJVDEloXdLum9mIk7dhlG63zL7LTGZS8Nw6d2o4z+NupF RDQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731567626; x=1732172426; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ppWhpTd52TjJCCDS+EQr23PMbbYE2t0bTxCQVPbMPwg=; b=PdfTtnrbTMjL5i979QGcPlvMOLB4qczowUyIfs7M6+8qjan5H5YuwY3Prkw2kM1Dsg JVfvQTrbWsi8JvZlFjr9B4DJOtzXOceLP4dwwhIbfRIzcogdWbUzjMgZm+oRLfL+2hxY VpOYLU/5xclU3JRfIZ210HPvWx0sriKhMHVjNAl8xOSZTBqHzV3AMBoZzGRArhtb8Bp8 Ec2YW+zbqZVSfd0fR5vsrznbfrdj//Y3fKmKrBD70wM8OrhewjbctXLizbwjXW8/FZ3q Har1wvajrvGJMZdvrauD/GOtxOizFktmdNhFfwx3tbuAggR4/OaEZynvXTAto6qF5ZTK D5wQ== X-Forwarded-Encrypted: i=1; AJvYcCXIkqs6rN6I4Jn+Z69NoDcLKxYbWogFJ//1My1MmTZVDsTaWl2E2gk4WUcBMz/j6uhCb74RT3vjXA==@kvack.org X-Gm-Message-State: AOJu0YzhwD3MxfYUspjDCTDFkrjTgR7SzLESGAJbJ83WyrAuER6ZHLS9 BlItzWcVjT+US1OCT8c+rsIrw1RdJLQPK8Mr+L77AJa2yGxrnT6agcgRNVx4An0= X-Google-Smtp-Source: AGHT+IEfZggo5NO+eFLQrRxC+GYnbRgduJHWaZDad+eOwdF2nsEeKaCuWMxC+Yr43OjjTZNi7plhXA== X-Received: by 2002:a17:902:f541:b0:20d:1866:ed6f with SMTP id d9443c01a7336-21183ccf11bmr310127245ad.4.1731567626121; Wed, 13 Nov 2024 23:00:26 -0800 (PST) Received: from C02DW0BEMD6R.bytedance.net ([63.216.146.178]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-211c7d389c2sm4119065ad.268.2024.11.13.23.00.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 23:00:24 -0800 (PST) From: Qi Zheng To: david@redhat.com, jannh@google.com, hughd@google.com, willy@infradead.org, muchun.song@linux.dev, vbabka@kernel.org, akpm@linux-foundation.org, peterx@redhat.com Cc: mgorman@suse.de, catalin.marinas@arm.com, will@kernel.org, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, x86@kernel.org, lorenzo.stoakes@oracle.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, zokeefe@google.com, rientjes@google.com, Qi Zheng Subject: [PATCH v3 0/9] synchronously scan and reclaim empty user PTE pages Date: Thu, 14 Nov 2024 14:59:51 +0800 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 142EB40459 X-Stat-Signature: 45fnue5qt6d9sktdq1jj8o7u9fuiyky9 X-HE-Tag: 1731567593-633043 X-HE-Meta: U2FsdGVkX1/h8MpD2c8aDNou9YFXdIcupk/Op2Eoz9N+qGXgCcqGzpjwTfO2n4W98MYZdeoDyfUDmkFzUyLJog6VFuIoZWGpP6/DCYNhaZ0MAkf7KZB8vGxqofcemgS0S9lvR74k9oTxAmsdto/lYDrkw12EVHAO9FF+sXhquZxtYPZHr6qyJk94qgOvKqm4AN2lPkP9IJ3OumkGwT14LFrjwM9wtqTyWhvnj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5YyryeDXOGvitw== 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: Changes in v3: - recheck pmd state instead of pmd_same() in retract_page_tables() (suggested by Jann Horn) - recheck dst_pmd entry in move_pages_pte() (pointed by Jann Horn) - introduce new skip_none_ptes() (suggested by David Hildenbrand) - minor changes in [PATCH v2 5/7] - remove tlb_remove_table_sync_one() if CONFIG_PT_RECLAIM is enabled. - use put_page() instead of free_page_and_swap_cache() in __tlb_remove_table_one_rcu() (pointed by Jann Horn) - collect the Reviewed-bys and Acked-bys - rebase onto the next-20241112 Changes in v2: - fix [PATCH v1 1/7] (Jann Horn) - reset force_flush and force_break to false in [PATCH v1 2/7] (Jann Horn) - introduce zap_nonpresent_ptes() and do_zap_pte_range() - check pte_none() instead of can_reclaim_pt after the processing of PTEs (remove [PATCH v1 3/7] and [PATCH v1 4/7]) - reorder patches - rebase onto the next-20241031 Changes in v1: - replace [RFC PATCH 1/7] with a separate serise (already merge into mm-unstable): https://lore.kernel.org/lkml/cover.1727332572.git.zhengqi.arch@bytedance.com/ (suggested by David Hildenbrand) - squash [RFC PATCH 2/7] into [RFC PATCH 4/7] (suggested by David Hildenbrand) - change to scan and reclaim empty user PTE pages in zap_pte_range() (suggested by David Hildenbrand) - sent a separate RFC patch to track the tlb flushing issue, and remove that part form this series ([RFC PATCH 3/7] and [RFC PATCH 6/7]). link: https://lore.kernel.org/lkml/20240815120715.14516-1-zhengqi.arch@bytedance.com/ - add [PATCH v1 1/7] into this series - drop RFC tag - rebase onto the next-20241011 Changes in RFC v2: - fix compilation errors in [RFC PATCH 5/7] and [RFC PATCH 7/7] reproted by kernel test robot - use pte_offset_map_nolock() + pmd_same() instead of check_pmd_still_valid() in retract_page_tables() (in [RFC PATCH 4/7]) - rebase onto the next-20240805 Hi all, Previously, we tried to use a completely asynchronous method to reclaim empty user PTE pages [1]. After discussing with David Hildenbrand, we decided to implement synchronous reclaimation in the case of madvise(MADV_DONTNEED) as the first step. So this series aims to synchronously free the empty PTE pages in madvise(MADV_DONTNEED) case. We will detect and free empty PTE pages in zap_pte_range(), and will add zap_details.reclaim_pt to exclude cases other than madvise(MADV_DONTNEED). In zap_pte_range(), mmu_gather is used to perform batch tlb flushing and page freeing operations. Therefore, if we want to free the empty PTE page in this path, the most natural way is to add it to mmu_gather as well. Now, if CONFIG_MMU_GATHER_RCU_TABLE_FREE is selected, mmu_gather will free page table pages by semi RCU: - batch table freeing: asynchronous free by RCU - single table freeing: IPI + synchronous free But this is not enough to free the empty PTE page table pages in paths other that munmap and exit_mmap path, because IPI cannot be synchronized with rcu_read_lock() in pte_offset_map{_lock}(). So we should let single table also be freed by RCU like batch table freeing. As a first step, we supported this feature on x86_64 and selectd the newly introduced CONFIG_ARCH_SUPPORTS_PT_RECLAIM. For other cases such as madvise(MADV_FREE), consider scanning and freeing empty PTE pages asynchronously in the future. This series is based on next-20241112 (which contains the series [2]). Note: issues related to TLB flushing are not new to this series and are tracked in the separate RFC patch [3]. And more context please refer to this thread [4]. Comments and suggestions are welcome! Thanks, Qi [1]. https://lore.kernel.org/lkml/cover.1718267194.git.zhengqi.arch@bytedance.com/ [2]. https://lore.kernel.org/lkml/cover.1727332572.git.zhengqi.arch@bytedance.com/ [3]. https://lore.kernel.org/lkml/20240815120715.14516-1-zhengqi.arch@bytedance.com/ [4]. https://lore.kernel.org/lkml/6f38cb19-9847-4f70-bbe7-06881bb016be@bytedance.com/ Qi Zheng (9): mm: khugepaged: recheck pmd state in retract_page_tables() mm: userfaultfd: recheck dst_pmd entry in move_pages_pte() mm: introduce zap_nonpresent_ptes() mm: introduce skip_none_ptes() mm: introduce do_zap_pte_range() mm: make zap_pte_range() handle full within-PMD range mm: pgtable: try to reclaim empty PTE page in madvise(MADV_DONTNEED) x86: mm: free page table pages by RCU instead of semi RCU x86: select ARCH_SUPPORTS_PT_RECLAIM if X86_64 arch/x86/Kconfig | 1 + arch/x86/include/asm/tlb.h | 19 +++ arch/x86/kernel/paravirt.c | 7 ++ arch/x86/mm/pgtable.c | 10 +- include/linux/mm.h | 1 + include/linux/mm_types.h | 4 +- mm/Kconfig | 15 +++ mm/Makefile | 1 + mm/internal.h | 19 +++ mm/khugepaged.c | 45 ++++--- mm/madvise.c | 7 +- mm/memory.c | 250 +++++++++++++++++++++++++------------ mm/mmu_gather.c | 9 +- mm/pt_reclaim.c | 71 +++++++++++ mm/userfaultfd.c | 51 +++++--- 15 files changed, 397 insertions(+), 113 deletions(-) create mode 100644 mm/pt_reclaim.c