From patchwork Thu Jan 11 06:07:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13516782 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 0FFBAC4707B for ; Thu, 11 Jan 2024 06:08:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 888196B008C; Thu, 11 Jan 2024 01:08:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 837886B0092; Thu, 11 Jan 2024 01:08:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FFBD6B0093; Thu, 11 Jan 2024 01:08:13 -0500 (EST) 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 5BD4B6B008C for ; Thu, 11 Jan 2024 01:08:13 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 24E6C140610 for ; Thu, 11 Jan 2024 06:08:13 +0000 (UTC) X-FDA: 81666000066.29.1BDB0EC Received: from invmail4.hynix.com (exvmail4.skhynix.com [166.125.252.92]) by imf24.hostedemail.com (Postfix) with ESMTP id AC272180011 for ; Thu, 11 Jan 2024 06:08:10 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704953291; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=ClsQgofMsXPgGLtxXaEEHnlA0yK4MJzA4UxgEaW4ZSM=; b=cuD+cSirswgCvhEJ93L2MwVXiI69ds3F0bFUIXyqCgNTiUQLrxAVjap8nzVM3OuSuOYjwf uNdmIN7GsfHbPVom89buKJv0NPfyCwFxzk8TwRonvWAL5ulejpgeUthAUGMQsj2O9tYL42 j5shEPhbKUalglsJ+xFxDXjrw7NbZeg= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704953291; a=rsa-sha256; cv=none; b=IVMspo8yXnZdQqJm9A7QSS9f1LKSSis/FIoEmBd7VHaAF2Mt3WH1ln6//ktDB/W5dNPjMy TDPzRggl7vRGx4vM0EIzJ4ltJu81JkkIHqlsuogcGHJlxSqK6cLRyONSEOOVZA+9XRwU0m WgN2ecGA5kbVTuiffIb+VmDEKkvtVQQ= X-AuditID: a67dfc5b-d6dff70000001748-da-659f85c789e9 From: Byungchul Park To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: kernel_team@skhynix.com, akpm@linux-foundation.org, ying.huang@intel.com, namit@vmware.com, xhao@linux.alibaba.com, mgorman@techsingularity.net, hughd@google.com, willy@infradead.org, david@redhat.com, peterz@infradead.org, luto@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com Subject: [v5 0/7] Reduce TLB flushes by 94% by improving folio migration Date: Thu, 11 Jan 2024 15:07:50 +0900 Message-Id: <20240111060757.13563-1-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrNLMWRmVeSWpSXmKPExsXC9ZZnoe6J1vmpBg8WslrMWb+GzeLzhn9s Fi82tDNafF3/i9ni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ICJhKrV3eygthsAuoSN278ZAaxRQTMJA62 /mHvYuTiYBZ4wCQx9+0KRpCEsICHxJveNnYQm0VAVaL1wlOwOK+AqcSH54eYIIbKS6zecIB5 AiPHAkaGVYwimXlluYmZOaZ6xdkZlXmZFXrJ+bmbGIFBuaz2z8QdjF8uux9iFOBgVOLhNXg5 L1WINbGsuDL3EKMEB7OSCK/C5zmpQrwpiZVVqUX58UWlOanFhxilOViUxHm9wlMThATSE0tS s1NTC1KLYLJMHJxSDYzz1dzPaOVFFv22i5E8Kv7eVzxRl98hWSXbXWLTralz3lYeEluzikV+ Z4LNBpGSv5Yntz36/zTBjePJutN3V09JUZ79ri7RN+ZMt/zdRWVHV9zyj7qonxHyefEEHS+b +pQVgT4eG8pjEk5ErJjZvyhUdNar/JQjqu9CEl71RO6W+dlnwX3zkpsSS3FGoqEWc1FxIgD7 AFVfRgIAAA== X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: AC272180011 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 7oquy7mnwbzg6g9na8a1nd1p7g6aa757 X-HE-Tag: 1704953290-346376 X-HE-Meta: U2FsdGVkX1/O/GnOq63TMCe8W0Q73RjjLeqrlWkfY0+tVmR6T89HOv5ja0peuSELx49FamXHnb/ybJVOveQXoP7pgIlm0zMR1rvmhMsJkJaswtmhF8Am1vGSLVpKbnDfycdSlaxMiSwC7ysgH5Kh+ai4+P2S7NNNA1UqliY0OzBqlUlFqHIWjIvqUxRlJlk7EWHXyizB9qiinUW5Ah2c/Wnb/Ik4/ol3m01LSQNiYtENIkW64EUvnmTZtF9Z0gXd5cS5ocWsv2h3FYKkhyJJDDh8RHRX0IW1HLmes/SsfWYEPDLo6/6B++sHb5GnCSbkwoQTcu4qANhpWZ0dIm3LPVg6igBsdLH5N6x4L7e6AcNqp3DUVA/R9qeMN6wvLGP3gTYK2ADme61RHN03VuiDtQ2yp3eo4qD0HyzmwGPFktism2oT6rThj4fAgIBS2SGlt1YXQQ9fL0VpJmSAa5D/KVFUrXqtXM2XUHZN/ajCQDGAZBztFDTXRIJAFZDYiICSZjr4IXarKMgTst43xBUOPIc3Zo3jUNPBfdLECTiQioEztuD2LL38xrIlUNhGl9nPPhpr62ttNVLtNOc+k+XYMkLgfHa2aO5rGs4MfAPVguWTQdSx8s/8CaPq9VLSKswYGAlRJmb7WmF5Gexd5U3x7Gpcu+JSnUSET5JgT/WyWtQhLDtGbjeGftgNyBX/4lvwFJRQZ2eOAKnvhE6J8n1hdtnPUPI55FRy7WdV3XUv2xC/hTwvl4LQ098J83O/WVzLWBaZ83HKg8NhsVfz+yRtQPShh0F/OsjZJ1KQZtGAf3NHbJwmFL+YaMGcAfX6KcPlHm4xfDto5ys+S9Ju3rupLZ2pr4x4OZxko59ys838jpacSaeTGnC4+I+kBau3KETJAyblJCmerTzaTzBbf00+hA5/xz9bDLNVCpu0rYndCq3BBUiYwYXDGQP7PQQ8NueaIUm89cO2yT8T6rSqdHi vXDMT2cw sRc4t6al2NcRhlK4ekttnTiR1NT45kke+AkqoAk9e/xzLXFQ4Jriv7G4GBQ== 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: Hi everyone, While I'm working with CXL memory, I have been facing migration overhead esp. TLB shootdown on promotion or demotion between different tiers. Yeah.. most TLB shootdowns on migration through hinting fault can be avoided thanks to Huang Ying's work, commit 4d4b6d66db ("mm,unmap: avoid flushing TLB in batch if PTE is inaccessible"). See the following link: https://lore.kernel.org/lkml/20231115025755.GA29979@system.software.com/ However, it's only for ones using hinting fault. I thought it'd be much better if we have a general mechanism to reduce the number of TLB flushes and TLB misses, that we can apply to any type of migration. I tried it only for tiering migration for now tho. I'm suggesting a mechanism to reduce TLB flushes by keeping source and destination of folios participated in the migrations until all TLB flushes required are done, only if those folios are not mapped with write permission PTE entries at all. I worked Based on v6.7. I saw the number of iTLB full flush was reduced by 94%, iTLB miss was reduced by 45.5% and the total runtime was reduced by 3.5% with the workload I tested with, XSBench. However, I believe that it would help more with other ones or any real ones. It'd be appreciated to let me know if I'm missing something. Byungchul --- Changes from v4: 1. Rebase on v6.7. 2. Fix build errors in arm64 that is doing nothing for TLB flush but has CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH. (reported by kernel test robot) 3. Don't use any page flag. So the system would give up migrc mechanism more often but it's okay. The final improvement is good enough. 4. Instead, optimize full TLB flush(arch_tlbbatch_flush()) by avoiding redundant CPUs from TLB flush. Changes from v3: 1. Don't use the kconfig, CONFIG_MIGRC, and remove sysctl knob, migrc_enable. (feedbacked by Nadav) 2. Remove the optimization skipping CPUs that have already performed TLB flushes needed by any reason when performing TLB flushes by migrc because I can't tell the performance difference between w/ the optimization and w/o that. (feedbacked by Nadav) 3. Minimize arch-specific code. While at it, move all the migrc declarations and inline functions from include/linux/mm.h to mm/internal.h (feedbacked by Dave Hansen, Nadav) 4. Separate a part making migrc paused when the system is in high memory pressure to another patch. (feedbacked by Nadav) 5. Rename: a. arch_tlbbatch_clean() to arch_tlbbatch_clear(), b. tlb_ubc_nowr to tlb_ubc_ro, c. migrc_try_flush_free_folios() to migrc_flush_free_folios(), d. migrc_stop to migrc_pause. (feedbacked by Nadav) 6. Use ->lru list_head instead of introducing a new llist_head. (feedbacked by Nadav) 7. Use non-atomic operations of page-flag when it's safe. (feedbacked by Nadav) 8. Use stack instead of keeping a pointer of 'struct migrc_req' in struct task, which is for manipulating it locally. (feedbacked by Nadav) 9. Replace a lot of simple functions to inline functions placed in a header, mm/internal.h. (feedbacked by Nadav) 10. Add additional sufficient comments. (feedbacked by Nadav) 11. Remove a lot of wrapper functions. (feedbacked by Nadav) Changes from RFC v2: 1. Remove additional occupation in struct page. To do that, unioned with lru field for migrc's list and added a page flag. I know page flag is a thing that we don't like to add but no choice because migrc should distinguish folios under migrc's control from others. Instead, I force migrc to be used only on 64 bit system to mitigate you guys from getting angry. 2. Remove meaningless internal object allocator that I introduced to minimize impact onto the system. However, a ton of tests showed there was no difference. 3. Stop migrc from working when the system is in high memory pressure like about to perform direct reclaim. At the condition where the swap mechanism is heavily used, I found the system suffered from regression without this control. 4. Exclude folios that pte_dirty() == true from migrc's interest so that migrc can work simpler. 5. Combine several patches that work tightly coupled to one. 6. Add sufficient comments for better review. 7. Manage migrc's request in per-node manner (from globally). 8. Add TLB miss improvement in commit message. 9. Test with more CPUs(4 -> 16) to see bigger improvement. Changes from RFC: 1. Fix a bug triggered when a destination folio at the previous migration becomes a source folio at the next migration, before the folio gets handled properly so that the folio can play with another migration. There was inconsistency in the folio's state. Fixed it. 2. Split the patch set into more pieces so that the folks can review better. (Feedbacked by Nadav Amit) 3. Fix a wrong usage of barrier e.g. smp_mb__after_atomic(). (Feedbacked by Nadav Amit) 4. Tried to add sufficient comments to explain the patch set better. (Feedbacked by Nadav Amit) Byungchul Park (7): x86/tlb: Add APIs manipulating tlb batch's arch data arm64: tlbflush: Add APIs manipulating tlb batch's arch data mm/rmap: Recognize read-only TLB entries during batched TLB flush mm: Separate move/undo doing on folio list from migrate_pages_batch() mm: Add APIs to free a folio directly to the buddy bypassing pcp mm: Defer TLB flush by keeping both src and dst folios at migration mm: Pause migrc mechanism at high memory pressure arch/arm64/include/asm/tlbflush.h | 19 ++ arch/x86/include/asm/tlbflush.h | 18 ++ arch/x86/mm/tlb.c | 7 + include/linux/mm.h | 23 ++ include/linux/mmzone.h | 5 + include/linux/sched.h | 7 + mm/internal.h | 84 +++++++ mm/memory.c | 8 + mm/migrate.c | 381 +++++++++++++++++++++++++----- mm/page_alloc.c | 34 ++- mm/rmap.c | 37 ++- mm/swap.c | 7 + 12 files changed, 574 insertions(+), 56 deletions(-)