From patchwork Mon May 20 02:17:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13667998 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 A8E04C25B79 for ; Mon, 20 May 2024 02:17:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 422276B0095; Sun, 19 May 2024 22:17:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C0CD6B0099; Sun, 19 May 2024 22:17:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA2266B0095; Sun, 19 May 2024 22:17:50 -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 B15276B0096 for ; Sun, 19 May 2024 22:17:50 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 68321140854 for ; Mon, 20 May 2024 02:17:50 +0000 (UTC) X-FDA: 82137163500.19.D6F5E5A Received: from invmail4.hynix.com (exvmail4.skhynix.com [166.125.252.92]) by imf21.hostedemail.com (Postfix) with ESMTP id 5E8A81C0008 for ; Mon, 20 May 2024 02:17:47 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf21.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=1716171468; 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:in-reply-to: references:references; bh=lYfFMbAEM1F2cBEc4miPa+Jx1WoZRiG7k2wKNFJlSqA=; b=OsAOw2PX1/GUsSrSnfv+B8duNMJFgBDBXsXTS5O4rdHCEuOJFP2ggtGciz8XHPoOnJpV+N 9Rh+QW2fE+Q0sencyAlQLZ4QrwXgvLfR7/HRwPE42sQjJ41XtU23r03nWq9RBFOfJ+TMYb LZYVommYmmE5+TtShsma+gQmPh9Jg2o= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf21.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=1716171468; a=rsa-sha256; cv=none; b=AbRLCmCxbBe4om0NYbRXIPUlwn8d9zKQY6ASWPzcaXFFfxx+6vkPmnTtnfaAvklxlYQkCq 86NeOEkiSX0PIQoq5ZRBqEX4hmh+IDn741xhH9rlA0QlD5dO7ZT6lX06GGZsLh6q4itHVE XF41B/e/sjQsTO3EkY8oaZQdGuZhoVU= X-AuditID: a67dfc5b-d6dff70000001748-9a-664ab2c8f665 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 01/12] x86/tlb: add APIs manipulating tlb batch's arch data Date: Mon, 20 May 2024 11:17:23 +0900 Message-Id: <20240520021734.21527-2-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrCLMWRmVeSWpSXmKPExsXC9ZZnoe6JTV5pBlP3WlrMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M k/0bmAreCFSceTqPvYFxDl8XIyeHhICJxLfry5hh7Dv7V7GD2GwC6hI3bvwEi4sImEkcbP0D FmcWuMskcaCfrYuRg0NYIEyia68kSJhFQFXi595/7CBhXgFTiT0nlSEmykus3nAAbAon0JQZ q3aygNhCQCU/jn4FmsIFVPOeTeLd/C4WiAZJiYMrbrBMYORdwMiwilEoM68sNzEzx0QvozIv s0IvOT93EyMw7JfV/onewfjpQvAhRgEORiUe3h2PPNOEWBPLiitzDzFKcDArifBu2gIU4k1J rKxKLcqPLyrNSS0+xCjNwaIkzmv0rTxFSCA9sSQ1OzW1ILUIJsvEwSnVwLh88623XLuf1xx+ 4nCgb8Zh2+6ieEm1B7kiuyeuc/+d+z8/7JJ8Q+duthmZ+aLBV988WFP8T7D4OrOS+j0nnbLX 6yLcdE/tOhPe/cXKct5bE0Mf7v7drku2aEVbiNmuv5F3mpv7+LcFh1ZdaZnuFlQseI2TrYxb qTb5R37So2eJu38LWj090KLEUpyRaKjFXFScCAC3hbI8dwIAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrNLMWRmVeSWpSXmKPExsXC5WfdrHtik1eawap7OhZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgyjjZv4Gp4I1AxZmn89gbGOfwdTFyckgImEjc2b+KHcRmE1CXuHHjJzOILSJg JnGw9Q9YnFngLpPEgX62LkYODmGBMImuvZIgYRYBVYmfe/+xg4R5BUwl9pxUhpgoL7F6wwGw KZxAU2as2skCYgsBlfw4+pVtAiPXAkaGVYwimXlluYmZOaZ6xdkZlXmZFXrJ+bmbGIFBvKz2 z8QdjF8uux9iFOBgVOLh3XDbM02INbGsuDL3EKMEB7OSCO+mLUAh3pTEyqrUovz4otKc1OJD jNIcLErivF7hqQlCAumJJanZqakFqUUwWSYOTqkGxrx9Rwvbpp6yK+u6cTZo77fTcRGJemr6 F3U46vztUzd/qX3Z+HGV6pMryQ/fskm937CFtzHfKl4+8gRPd2im8Gv+unOOX+78boi3Yiwp kdr78+/N/cEHf0rvi26Iu1b22/nEQ+ZN36OYkit5O35FzjcqUy3dZChcmLX3z3KZg5F+OxJX XlSfqsRSnJFoqMVcVJwIABWbBh5eAgAA X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 5E8A81C0008 X-Stat-Signature: kgkm9bduxp3ky3ijjta7ehs158maz3m1 X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1716171467-879970 X-HE-Meta: U2FsdGVkX1/E+CdDgY4vLjVEYxNE2o6OkQiWFfaPpaeuWmr7Oqe6BNpqinPMV51FYxx1XTko+WI3LNSmK7yl/ife8IbPduE4aWyC3QX9Cw0UeBWxSFPzFy/Z0cCDs3ULjH2eyxpJIxACRGP3DHH8sBwZNnBicOB+eZ1bLCXTfymmjBtwB8C71Tp/qVGxLfcoKKBjyj7xBVe2VBwgYBsXWg8HhvApxFk9/0wqsFDyQOkEqB1SAadxyk6h0KwNdUlhLoMQDe/QzZNC+mEGEdJoKW3Wi28f0oidJjI8wrnL62+uuPP724PtEf2kMrr1bANgvmwILw8roO69Y1sZ8SnmFL5nQHfPPYD5iJxNOlkTvpcFCVVgVblNHE3V28v/Kam0mR914r/6VaOeieaFrd5RlIGMHbe6bV4FQFZplnW4UW5b96ix14SwYnmQ7eAbVij4C5YshIKv/ClbPL13Zv/qbvxUz59BgXnDdhnh/kHK6G2umyA+0s8vNECdoZ59657S/7G8pg5NnS21hq3xQFwwKg58+zOz/sOZzb8atx7hoRKCYqHiPGDHc8Uvnv8vfytCUYWhdWv2UOD5B9pNsRAgYfpVlabd0Hlp3sLy2IMo0mMYPbVPOvGelO7pbuEaSfnGGQa4Mt1FzqcUqwoTwTuRABkNvsQRTtIYDKWabsnNXkwVOg0yA7UV4ngWemvHCZuNSI2zvUYvKOOtjeFunAnnoieKnZneZhqkrnPZTxRCe3b3LRP7GuRVPiFQB54B6+R9LmjZlBcAdQ0pp1ackBaWPpyBWwFhYQJm8E6m24mEInEQ1nXY+8dMol54K+vrC8SJeRcN9uZOr9TJG28KL273yNwap6ucqnerZQNrs7kNPRtJTeHLFljYT8OW868yI7PJwBTsfAIGTnM41nofmrlO6yIiXPYK38OhneiupMfgNtnjQ2YvwKDcgApx9DnglVt6AFZCz/of8nTQF1/aLyA NfiQpTk7 1miiw7WGJq8Cnq+vxtKk5900B6Q== 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: A new mechanism, LUF(Lazy Unmap Flush), defers tlb flush until folios that have been unmapped and freed, eventually get allocated again. It's safe for folios that had been mapped read-only and were unmapped, since the contents of the folios wouldn't change while staying in pcp or buddy so we can still read the data through the stale tlb entries. This is a preparation for the mechanism that needs to recognize read-only tlb entries by separating tlb batch arch data into two, one is for read-only entries and the other is for writable ones, and merging those two when needed. It also optimizes tlb shootdown by skipping CPUs that have already performed tlb flush needed since. To support it, added APIs manipulating arch data for x86. Signed-off-by: Byungchul Park --- arch/x86/include/asm/tlbflush.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 25726893c6f4..a14f77c5cdde 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -293,6 +294,23 @@ static inline void arch_flush_tlb_batched_pending(struct mm_struct *mm) extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch); +static inline void arch_tlbbatch_clear(struct arch_tlbflush_unmap_batch *batch) +{ + cpumask_clear(&batch->cpumask); +} + +static inline void arch_tlbbatch_fold(struct arch_tlbflush_unmap_batch *bdst, + struct arch_tlbflush_unmap_batch *bsrc) +{ + cpumask_or(&bdst->cpumask, &bdst->cpumask, &bsrc->cpumask); +} + +static inline bool arch_tlbbatch_done(struct arch_tlbflush_unmap_batch *bdst, + struct arch_tlbflush_unmap_batch *bsrc) +{ + return !cpumask_andnot(&bdst->cpumask, &bdst->cpumask, &bsrc->cpumask); +} + static inline bool pte_flags_need_flush(unsigned long oldflags, unsigned long newflags, bool ignore_access) From patchwork Mon May 20 02:17:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13667997 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 8744CC25B7A for ; Mon, 20 May 2024 02:17:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 073046B0093; Sun, 19 May 2024 22:17:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3D166B0096; Sun, 19 May 2024 22:17:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB6FB6B0098; Sun, 19 May 2024 22:17:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AD8B16B0095 for ; Sun, 19 May 2024 22:17:50 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6C221A19B9 for ; Mon, 20 May 2024 02:17:50 +0000 (UTC) X-FDA: 82137163500.14.CC71D80 Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by imf23.hostedemail.com (Postfix) with ESMTP id 97E93140008 for ; Mon, 20 May 2024 02:17:48 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716171468; 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:in-reply-to: references:references; bh=CmKo+Qes+ApyBvy+wRevo+t3ePQZu2OoUHH/ub4twUM=; b=7HdxzUrBwQ70eRyGTJgJm/zGz7HkSe+ybfiPyyG6oqmNQqb2ClCRc2sujT7Bqq63KHWda+ NZMyF1tywPvL9ptz/Ao5zZwX4vqptyaurSdPpxNwQHyUJqmyuJ1AFe/rOS8oKS5JagSTRW fSQo1Lcv6yZ+io8xFzHYUaOWVeF0VB0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171469; a=rsa-sha256; cv=none; b=OVQ/SEZluGXvlOxOhglvry+cZ/8tfyYavc3x8Ipb34RKt2oXjc3KczkiVmdjKYsUcsg0CS FOyLyg1SnAFtbTrAjXGyMQzEI/fhbbsdKCx7APr8kqW/2vHWLhrujRT0ai2QVlu8+JZUpH z8sU5X6q3d8YKYNGB9c8Z1SQZbN7pBA= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none X-AuditID: a67dfc5b-d6dff70000001748-9f-664ab2c844b4 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 02/12] arm64: tlbflush: add APIs manipulating tlb batch's arch data Date: Mon, 20 May 2024 11:17:24 +0900 Message-Id: <20240520021734.21527-3-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrCLMWRmVeSWpSXmKPExsXC9ZZnke6JTV5pBq0LLC3mrF/DZvF5wz82 ixcb2hktvq7/xWzx9FMfi8XlXXPYLO6t+c9qcX7XWlaLHUv3MVlcOrCAyeJ47wEmi/n3PrNZ bN40ldni+JSpjBa/fwAVn5w1mcVBwON7ax+Lx85Zd9k9Fmwq9di8Qstj8Z6XTB6bVnWyeWz6 NInd4925c+weJ2b8ZvGYdzLQ4/2+q2weW3/ZeTROvcbm8XmTXABfFJdNSmpOZllqkb5dAlfG gYZ9LAWLeSuutqo2MP7m6mLk5JAQMJG4//sYUxcjB5g9d0Y9SJhNQF3ixo2fzCC2iICZxMHW P+wgNrPAXSaJA/1sILawQJxE67XvYDaLgKrEq3cnwep5BUwlpnx5xQgxXl5i9YYDYHFOoDkz Vu1kAbGFgGp+HP0K1MsFVPOeTeLgzJUsEA2SEgdX3GCZwMi7gJFhFaNQZl5ZbmJmjoleRmVe ZoVecn7uJkZg2C+r/RO9g/HTheBDjAIcjEo8vDseeaYJsSaWFVfmHmKU4GBWEuHdtAUoxJuS WFmVWpQfX1Sak1p8iFGag0VJnNfoW3mKkEB6YklqdmpqQWoRTJaJg1OqgdHwpBVvvddEGzm/ 1glcCSf62V6VLy/l6VSzSeuUbtx69lE5W/AW0+QpVybZzp4VFHPg3pVPn6xu39bJ3xlf+Pwh w+67udLfGeRXq4t8MuvRM7D9zBw3dyEL2+YcbyOzhrfuKhtuMSQlKPtPaT2cN3mbiqNQLI/T wVcLWr2dBfd/Dk2ut9rfq8RSnJFoqMVcVJwIALG4RNt3AgAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrNLMWRmVeSWpSXmKPExsXC5WfdrHtik1eaQecafYs569ewWXze8I/N 4sWGdkaLr+t/MVs8/dTHYnF47klWi8u75rBZ3Fvzn9Xi/K61rBY7lu5jsrh0YAGTxfHeA0wW 8+99ZrPYvGkqs8XxKVMZLX7/ACo+OWsyi4Ogx/fWPhaPnbPusnss2FTqsXmFlsfiPS+ZPDat 6mTz2PRpErvHu3Pn2D1OzPjN4jHvZKDH+31X2TwWv/jA5LH1l51H49RrbB6fN8kF8Edx2aSk 5mSWpRbp2yVwZRxo2MdSsJi34mqragPjb64uRg4OCQETibkz6rsYOTnYBNQlbtz4yQxiiwiY SRxs/cMOYjML3GWSONDPBmILC8RJtF77DmazCKhKvHp3EqyeV8BUYsqXV4wgtoSAvMTqDQfA 4pxAc2as2skCYgsB1fw4+pVtAiPXAkaGVYwimXlluYmZOaZ6xdkZlXmZFXrJ+bmbGIFBvKz2 z8QdjF8uux9iFOBgVOLh3XDbM02INbGsuDL3EKMEB7OSCO+mLUAh3pTEyqrUovz4otKc1OJD jNIcLErivF7hqQlCAumJJanZqakFqUUwWSYOTqkGxtrTk2zis81l7UP2SM7PVYqRCBQR7v7g 8vtI0wqJPaUBunM3iH1+ddwi+fgyifLcipiQ/c7dnRbtG2tZGj0NZD82T9v2e5Kfhfn7Hje2 Z049Mwz0Elx4+PVUhWoznsw7It8TJXApR6L+o0+RgV7tjaUT0pr3vYldJvn0QXTZkUvv2ur5 +xKUWIozEg21mIuKEwHkHhI9XgIAAA== X-CFilter-Loop: Reflected X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 97E93140008 X-Rspam-User: X-Stat-Signature: kngfcwqiuygocw4fhqpuasmt4zre9yuz X-HE-Tag: 1716171468-491748 X-HE-Meta: U2FsdGVkX18GV2nE2+0lvMNJLizI080DugC7Y/W4xqAcg7tWiuBzXUyhQdOZv3EXiozfDaripKNszPevMeYBUaiMuQcqhVzRMygUv+Lo447ev/ciBP0OQ6ULRFup23b5b3ren0bfEPlsfLeHCrfrsutgI89ajRF4evnc0tONbc9S+6gz0a0FsI7HTe9L1Yd9Ktv1b1jz8u5OjJSvSbbMG3hHMH1o/2JRlJr9sGqw/UFR3D/prXFsxQ/9Tr6Dqnlbb9ycsQceR7qMQU8xRcQzsYrnVr0xBeLwCFOh1kaiQfaTtxCpafocWSqxpY0AGxmB1fVimE7r827HY5wEL6G0I4KUMDyM8lEpaA4xwxzOVQUBdZyKtvwGM/toOIBDt0UmcbWaTr2c8rG2iJxYV3JmkaUFOVNOs+q2nSjlvg+4Tn4dlksuphHPTPFpDjYYYX1iU0+fqtxlNzsjNIsRqvsics05AzVHrkJjoVcud7ZdhtXd9Gaicwtn9bPhU3YNkxAZvNpFY0U9r90R+Cjq6fGOm+82LSM86f0WOIsBogHThNYm2eOcpMXGlQm6xaTBo9YA6U7wzBHKoznJvzkBqeBWjcqoGkVaIVyqrYQzLyYDW/ffLWcUDy9ihT3SIbuIhUVTwBmH3OV107m0/wEyNnyjXsWFr8vdEpidh6Rm8AEz324/ucq9a5lbzxtAD9p3EpHlDUS+c/wzL4Z5mBM2G+1F70z/65E45AebscA2pJXEaicJ2OsVZGrIWxgiwJbCyVi/iMw3TK3xr/t5i7NNSCYNPuATh+gyKjK1ugnqN+QvT55Zhr/gCPMg1I3l7gzVJDjKQCfBx+CiwRLsnP5Bw6fCGIZsKClHvmnKzVW65etb8U5zJJajICw0ouQ9VXlmrVYEDExlRCzulUFdP8wtEnCx1w+LGFmwySJQ85CHpp83a8pKZvKMhTpQ4SewNQYPG5uqGtyI9T7BJMGdMHo6iZI SLw== 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: A new mechanism, LUF(Lazy Unmap Flush), defers tlb flush until folios that have been unmapped and freed, eventually get allocated again. It's safe for folios that had been mapped read only and were unmapped, since the contents of the folios don't change while staying in pcp or buddy so we can still read the data through the stale tlb entries. This is a preparation for the mechanism that requires to manipulate tlb batch's arch data. Even though arm64 does nothing for tlb things, arch with CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH should provide the APIs. Signed-off-by: Byungchul Park --- arch/arm64/include/asm/tlbflush.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/arm64/include/asm/tlbflush.h b/arch/arm64/include/asm/tlbflush.h index a75de2665d84..b8c7fbc1c68e 100644 --- a/arch/arm64/include/asm/tlbflush.h +++ b/arch/arm64/include/asm/tlbflush.h @@ -347,6 +347,24 @@ static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) dsb(ish); } +static inline void arch_tlbbatch_clear(struct arch_tlbflush_unmap_batch *batch) +{ + /* nothing to do */ +} + +static inline void arch_tlbbatch_fold(struct arch_tlbflush_unmap_batch *bdst, + struct arch_tlbflush_unmap_batch *bsrc) +{ + /* nothing to do */ +} + +static inline bool arch_tlbbatch_done(struct arch_tlbflush_unmap_batch *bdst, + struct arch_tlbflush_unmap_batch *bsrc) +{ + /* Kernel can consider tlb batch always has been done. */ + return true; +} + /* * This is meant to avoid soft lock-ups on large TLB flushing ranges and not * necessarily a performance improvement. From patchwork Mon May 20 02:17:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13667995 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 193F2C25B7A for ; Mon, 20 May 2024 02:17:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A372C6B008C; Sun, 19 May 2024 22:17:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BED96B0093; Sun, 19 May 2024 22:17:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C7AB6B0096; Sun, 19 May 2024 22:17:50 -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 4ED376B008C for ; Sun, 19 May 2024 22:17:50 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D31641A0AE9 for ; Mon, 20 May 2024 02:17:49 +0000 (UTC) X-FDA: 82137163458.11.E602D73 Received: from invmail4.hynix.com (exvmail4.skhynix.com [166.125.252.92]) by imf01.hostedemail.com (Postfix) with ESMTP id 5F83240008 for ; Mon, 20 May 2024 02:17:47 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716171468; 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:in-reply-to: references:references; bh=Ilpey0YHF3hm79X1vBIhnNe8zbnRWNG/60DUNWlxvcE=; b=ymy18wzJgG1IkbwD75kgFDT0MkmHH47y8ARYUC8x78G5mG6TCarJaQ9/7nKDeQM7v1emzO tbVl00zk4PLrQLymtJqzivkd5zustMnRmWA8og5bo6V4z0Z0uLEtPLt3IBuEfDRljZyfvp SCcGZ3B4Xba8RGjm+pF7wWq/THXvav0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171468; a=rsa-sha256; cv=none; b=jgGEul3KpcV5vcdB41N7Q+ex7mjCKw7yMcq4qIoi/je1wznePguHpFeqy+0GFkvuH2BR7Z 2tEFj5qZX5q13TFFHAKolC4upJdNlwARDq6gqJCbydxS4PMZnoYmyNJp+z4R4bTSZhOM9S v/juA9lAZR+zd5Iea68M/M4KC3B4NYk= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none X-AuditID: a67dfc5b-d6dff70000001748-a4-664ab2c82afd 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 03/12] riscv, tlb: add APIs manipulating tlb batch's arch data Date: Mon, 20 May 2024 11:17:25 +0900 Message-Id: <20240520021734.21527-4-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrGLMWRmVeSWpSXmKPExsXC9ZZnke6JTV5pBgd22ljMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M 5evuMRZ8FKg4+m8VewPjMr4uRk4OCQETiecXfrLC2IebzrGB2GwC6hI3bvxkBrFFBMwkDrb+ YQexmQXuMkkc6AerERaIlLhyuR8sziKgKnHhwVQWEJtXwFTi7Kt3jBAz5SVWbzgANocTaM6M VTvBaoSAan4c/Qo0hwuo5j2bxIv2qywQDZISB1fcYJnAyLuAkWEVo1BmXlluYmaOiV5GZV5m hV5yfu4mRmDoL6v9E72D8dOF4EOMAhyMSjy8Ox55pgmxJpYVV+YeYpTgYFYS4d20BSjEm5JY WZValB9fVJqTWnyIUZqDRUmc1+hbeYqQQHpiSWp2ampBahFMlomDU6qB0dzQNv9bhHdLzLGn 0/LP3mnRbP8lfS/+++J/jhOnzTBYxym/38m7yez3xq+W0h4cbZ/5pjHeNr95asVZF1fGaZdc Eo/lCi9v04rnkXhfyLR5juETx8ZtB5rufFllIKhxP+8S2xWX97U/l6i+3zPpnHWrrYjX4Sc9 /3msuu7dlv3/YpfCvVvaMUosxRmJhlrMRcWJAGJSZnd5AgAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrLLMWRmVeSWpSXmKPExsXC5WfdrHtik1eawcNvhhZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgyli+7h5jwUeBiqP/VrE3MC7j62Lk5JAQMJE43HSODcRmE1CXuHHjJzOILSJg JnGw9Q87iM0scJdJ4kA/WI2wQKTElcv9YHEWAVWJCw+msoDYvAKmEmdfvWOEmCkvsXrDAbA5 nEBzZqzaCVYjBFTz4+hXtgmMXAsYGVYximTmleUmZuaY6hVnZ1TmZVboJefnbmIEBvKy2j8T dzB+uex+iFGAg1GJh3fDbc80IdbEsuLK3EOMEhzMSiK8m7YAhXhTEiurUovy44tKc1KLDzFK c7AoifN6hacmCAmkJ5akZqemFqQWwWSZODilGhh5QlaWJ0SXfXt1V6+JVTbxymsnGeed0R3c 90163954nhTNI2XXrRy37k2vkQX/2QKnbXv9TvC0aRed+98m7Cf74tHUG1Uz3BYFParzzBUT Dlva1iExLeWCVBTrqwAp6yeBdyZubV12X9O4bv7ML5PeRysadp/WW7eql0c0TTc/8+RViwM7 PiuxFGckGmoxFxUnAgCiDQqAYAIAAA== X-CFilter-Loop: Reflected X-Rspam-User: X-Stat-Signature: gzeeou58r116jnqiu5z5e7ab4cmezq74 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5F83240008 X-HE-Tag: 1716171467-259318 X-HE-Meta: U2FsdGVkX19G/MbmnTnnyCjBZQqhptnb1iWPbVE7SkNST9x7BtXGXBBh2xCDBDNmtSy24outlGBTTx54nPXB+6hedf5FlWEv3KaXlE9I7zWaGgepgGtT1r7OdZenI0/xFbygIrslewcfgjR7V+KslSvOCxjR4R61OldkPNzsLzSiOTr2nSXkbQw5FShVmcFQvZrfGztXzUjwSesgMcK7j/XtWzHB7WV0aroL6XJrlkiu6VI4UcIYKDiPd+jznvoKqtvn9HSGCct6ArsuukI02CRzNQW4Mmmti8znr3m4xBlQlOKzjGvrd4NV1+XejaB6cqTn/vmaXmFuvk4GpDSumbugd7BpWB4rh88tnElEtKPVWmRvELQHI3zAinRpZtxp2GkABXoUVb/2eqJpv5fqGoG+Bu6MRqCfI/Ne9puGcpURvd7cEcMd7cgcWazQUyjGiTBslVeMUtf723wX0Z6rwEPVbPqSShbhcXDFWZ9zpS74Xx5rCAIhqfIkZf50i0Z28JpA00x1FrrTCoUawoey6ZIfG0e+cCzJD+dZ5cD80IefCRMrGGtzXPZmT2+ldYNDw3Qsifx+jyLptl9/jmD5UDz2Xtlg2Z/jfI/hnsEP6nqZqfqHtck7cva6muXNkERA+8l3sy6tmGdxgCU9lExNLGdUD4pt2F4JoRHL+XelDWyaHnqRhdSY7JNw2wZI5eGinl3O3i9D7+KsMs9LXJrAvpeKuJzJJPyRSX/fXLw5cG7KRx8Q7magpyK4ZW8Nxg322BDv4jFSjguGM2/CBl0GtXcvZxXBZ8PalgaynqAkXHBLhr/UipTBv1sVWz7gyIonDIqmFrfQFqAlHW6CSorYozlbwR4gKujLQH7Zhkynyf8czezZk9fQPBULFoSuapgwac8JBHKNrFonm/d7TxUFkXTaYhctBQBotiGSHnyywQUzJ/B+M7tgnK3t+BNYTVluZE09eMJZ95hzYHXLheA y6BwsNkO 6SX6A2DQZ+wVwvHtWCUOV3kkJoA== 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: A new mechanism, LUF(Lazy Unmap Flush), defers tlb flush until folios that have been unmapped and freed, eventually get allocated again. It's safe for folios that had been mapped read only and were unmapped, since the contents of the folios don't change while staying in pcp or buddy so we can still read the data through the stale tlb entries. This is a preparation for the mechanism that needs to recognize read-only tlb entries by separating tlb batch arch data into two, one is for read-only entries and the other is for writable ones, and merging those two when needed. It also optimizes tlb shootdown by skipping CPUs that have already performed tlb flush needed since. To support it, added APIs manipulating arch data for riscv. Signed-off-by: Byungchul Park --- arch/riscv/include/asm/tlbflush.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/arch/riscv/include/asm/tlbflush.h b/arch/riscv/include/asm/tlbflush.h index 4112cc8d1d69..480c082ccde3 100644 --- a/arch/riscv/include/asm/tlbflush.h +++ b/arch/riscv/include/asm/tlbflush.h @@ -8,6 +8,7 @@ #define _ASM_RISCV_TLBFLUSH_H #include +#include #include #include @@ -55,6 +56,26 @@ void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, void arch_flush_tlb_batched_pending(struct mm_struct *mm); void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch); +static inline void arch_tlbbatch_clear(struct arch_tlbflush_unmap_batch *batch) +{ + cpumask_clear(&batch->cpumask); + +} + +static inline void arch_tlbbatch_fold(struct arch_tlbflush_unmap_batch *bdst, + struct arch_tlbflush_unmap_batch *bsrc) +{ + cpumask_or(&bdst->cpumask, &bdst->cpumask, &bsrc->cpumask); + +} + +static inline bool arch_tlbbatch_done(struct arch_tlbflush_unmap_batch *bdst, + struct arch_tlbflush_unmap_batch *bsrc) +{ + return !cpumask_andnot(&bdst->cpumask, &bdst->cpumask, &bsrc->cpumask); + +} + #else /* CONFIG_SMP && CONFIG_MMU */ #define flush_tlb_all() local_flush_tlb_all() From patchwork Mon May 20 02:17:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13668000 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 283EDC25B7A for ; Mon, 20 May 2024 02:18:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2431F6B0098; Sun, 19 May 2024 22:17:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EA5F76B0096; Sun, 19 May 2024 22:17:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B221A6B0099; Sun, 19 May 2024 22:17:52 -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 7F0DD6B0098 for ; Sun, 19 May 2024 22:17:52 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3C9CB80B44 for ; Mon, 20 May 2024 02:17:52 +0000 (UTC) X-FDA: 82137163584.27.09E56B4 Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by imf14.hostedemail.com (Postfix) with ESMTP id 6715D10000B for ; Mon, 20 May 2024 02:17:50 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171470; a=rsa-sha256; cv=none; b=u8jgLaSXhNA2YW/fG8FFS6vMIoF5lTKThMhKMo0ys72Mj80wV/Uu4wulNlQVTI0xr6h/Xz YLovdBEA2+nYpUT+QN4jFcillQcGpMsG0yFETLnOcDiuVsVaJ8TRh2Q5mOoxwBkUTVqiCJ qipq5GkXaIL4Au/kbF1wJgisMq1AGq8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716171470; 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:in-reply-to: references:references; bh=evCmjUh3KdBYN1DfbQBG2ARQXc+DAdC3ZcSVsI5RRA8=; b=wEB8cTRHbpDnBGxXWAjiPHp5Nh3R9M337RW+Jm9JwDvPy4gOvcz/gSNeH1s97xU1NXZDQJ OuorfsrPlQpjZdex4g/EnCd3XH0kxCs7zBaG6K9Hl75mh9pfj6PXfmwYrx6w/1vEmV2AIF 0vzTzX97cx5cxKyU2euSE498d4pKaH8= X-AuditID: a67dfc5b-d6dff70000001748-a9-664ab2c8cf5b 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 04/12] x86/tlb, riscv/tlb, mm/rmap: separate arch_tlbbatch_clear() out of arch_tlbbatch_flush() Date: Mon, 20 May 2024 11:17:26 +0900 Message-Id: <20240520021734.21527-5-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrKLMWRmVeSWpSXmKPExsXC9ZZnke7JTV5pBtceMFrMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M R78msBfc56tYM3EPawPjVJ4uRg4OCQETifmzmbsYOcHM1U/bwGw2AXWJGzd+gtkiAmYSB1v/ sIPYzAJ3mSQO9LOB2MICVRLtSx+C2SwCqhK3ts1lAbF5BUwl2hYvYYeYKS+xesMBsDmcQHNm rNoJViMEVPPj6FegXi6gmtdsEnc+NEI1SEocXHGDZQIj7wJGhlWMQpl5ZbmJmTkmehmVeZkV esn5uZsYgYG/rPZP9A7GTxeCDzEKcDAq8fDueOSZJsSaWFZcmXuIUYKDWUmEd9MWoBBvSmJl VWpRfnxRaU5q8SFGaQ4WJXFeo2/lKUIC6YklqdmpqQWpRTBZJg5OqQZGrlVnlrBvObjA6OIp Rxb9RZv3CcseuXN9SflEBX1nrcj02Ubz9ogv3tf/4ZiNsaK7V6CoXZ5w/5KjaoflfhhOWbyq X7vU//gbIz+JjwLmipHlOd+vbm9S8E08pBlWOUHJeftDgUuBnk+217OJW/8SUjok3vHqudVH oa+rbt/f79o+vXSGh/9vJZbijERDLeai4kQAji8YPXgCAAA= X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrLLMWRmVeSWpSXmKPExsXC5WfdrHtik1eawbGNJhZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgynj0awJ7wX2+ijUT97A2ME7l6WLk5JAQMJFY/bSNGcRmE1CXuHHjJ5gtImAm cbD1DzuIzSxwl0niQD8biC0sUCXRvvQhmM0ioCpxa9tcFhCbV8BUom3xEnaImfISqzccAJvD CTRnxqqdYDVCQDU/jn5lm8DItYCRYRWjSGZeWW5iZo6pXnF2RmVeZoVecn7uJkZgIC+r/TNx B+OXy+6HGAU4GJV4eDfc9kwTYk0sK67MPcQowcGsJMK7aQtQiDclsbIqtSg/vqg0J7X4EKM0 B4uSOK9XeGqCkEB6YklqdmpqQWoRTJaJg1OqgTHBpXxGoe7uK9OeLaxQj5H9fHSLnp5wUDw/ V7B0KGvrlQv2XiFJtlOmPI1vOFT7rCfzp09goYvc0cov2pYPtm2zymE/7ZRWauEbExzzy+35 6qZ1p/491JlZKdzQEFxnMf1ZzbF3rr43XaZef3C9v3ThlugzwRdE08+dNOjMDbykZjb9Ztdb XiWW4oxEQy3mouJEADDRNYVgAgAA X-CFilter-Loop: Reflected X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 6715D10000B X-Stat-Signature: th9g3pxigt8oh7j885phyo7rcpy4p481 X-HE-Tag: 1716171470-33925 X-HE-Meta: U2FsdGVkX18Dr1EYPS6dlKE8F2OYTfKEPhx4uZWuy0UBcoBrj9arxFvQgVfabzvfxuZE49HMrL2a8HzNQaHcBKucXwvpo660d+awqwV8OIi9iSJQ0igtrBzH7GTeKFxBXb7tZadMkccBfWU6Y07C6V05+j/ULLYqUwau/Q4txgfYclYxkG3iPM1B00DFXzPBPFln1aZhPNGtFLCs/KtvkAgsFYxGNvuSO2+7u/NTL3y/m4Tg6rFLy9HRZocbXlpoA2CrKwNee8bejSFokYA3VwLRBt+Ck9axjusAvl0m7hPHZ4jgdfQEAyEBHWqZSF5NJji5xp07X7Q/oETEPSV+9NjINGIA+WTt/zrc00N1bzbl/lu42nxQm9NkqBc6S2F4l7TnXiWlKvoBQAOU01SsKUXZaPX8lNjFCHdnoika97PWnrUkmmTDn8kjCBvH5HEKfTVq8la35hA0xKbDl046SDjGLhrHY66LL1gTVOIVyPEh7za0e56VbyZQPbRE8R2tuBBk5a0CDWsHN1s90NzHYC9fNc499UBjSLtyn0nIIzla5gAXCyB5qw5BE46k+i64kkGPtLBlHCyPpgQzcElIvCMvkjf4Eur3xEUEKBpuQtK2BGMz8Skvl8psHsK9Q/Tb9+uwT9BiYUHznTTc8dti8eeTX7vg8BARsuY6X4hXzPFLMlaIldXNJWZ0mIA0Rx7HGz29pg1YwuzrCFJL4yEOtQLgOq2B45VYyXAAmkek3aKC/u1VMlE6PRoUtiKJO1JZEODzjfjLYfmGirVXl9R83KUU8z2pfZ1btyPd1ZB2QiX8RB/a6G63Tgwd32hJW2zm2+XXoQ5IF9/P3t9wvFSwK4dGXFPgWXDCxAvXJ4yhEOY6nh6thFReg2GMLATCTjLxgmsyswEuuH1FhKaVhDP/yrEZM499jlEyIlpDzETL9UiqaModWQNteYrHuDKaHjI4+IlgtbNgTUU6Be5OSkD 8tbxIDZ8 0yEGBYcj7y0N0fauh5gH17IsntZYGSrdrxNRsV88n9RvYJ2t5vcnpa5Ek2YfXf38YPrKoE2sKVh9hWu4wGH+GT/BcXQ== 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: A new mechanism, LUF(Lazy Unmap Flush), defers tlb flush until folios that have been unmapped and freed, eventually get allocated again. It's safe for folios that had been mapped read only and were unmapped, since the contents of the folios don't change while staying in pcp or buddy so we can still read the data through the stale tlb entries. This is a preparation for the mechanism that requires to avoid redundant tlb flush by manipulating tlb batch's arch data. To achieve that, we need to separate the part clearing the tlb batch's arch data out of arch_tlbbatch_flush(). Signed-off-by: Byungchul Park --- arch/riscv/mm/tlbflush.c | 1 - arch/x86/mm/tlb.c | 2 -- mm/rmap.c | 1 + 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/riscv/mm/tlbflush.c b/arch/riscv/mm/tlbflush.c index 07d743f87b3f..9cbd27148357 100644 --- a/arch/riscv/mm/tlbflush.c +++ b/arch/riscv/mm/tlbflush.c @@ -234,5 +234,4 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) { __flush_tlb_range(&batch->cpumask, FLUSH_TLB_NO_ASID, 0, FLUSH_TLB_MAX_SIZE, PAGE_SIZE); - cpumask_clear(&batch->cpumask); } diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 44ac64f3a047..24bce69222cd 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1265,8 +1265,6 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) local_irq_enable(); } - cpumask_clear(&batch->cpumask); - put_flush_tlb_info(); put_cpu(); } diff --git a/mm/rmap.c b/mm/rmap.c index 2608c40dffad..cf8a99a49aef 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -649,6 +649,7 @@ void try_to_unmap_flush(void) return; arch_tlbbatch_flush(&tlb_ubc->arch); + arch_tlbbatch_clear(&tlb_ubc->arch); tlb_ubc->flush_required = false; tlb_ubc->writable = false; } From patchwork Mon May 20 02:17:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13667999 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 CB7DEC25B75 for ; Mon, 20 May 2024 02:17:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB3956B0099; Sun, 19 May 2024 22:17:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D1DAC6B0098; Sun, 19 May 2024 22:17:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5E576B009A; Sun, 19 May 2024 22:17:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7BB236B0096 for ; Sun, 19 May 2024 22:17:52 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 35B381C058B for ; Mon, 20 May 2024 02:17:52 +0000 (UTC) X-FDA: 82137163584.28.4BA000E Received: from invmail4.hynix.com (exvmail4.skhynix.com [166.125.252.92]) by imf01.hostedemail.com (Postfix) with ESMTP id 5E6DD40012 for ; Mon, 20 May 2024 02:17:50 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716171470; 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:in-reply-to: references:references; bh=Riu8nANP1gQmwqYLhEmoeVBEqJ6gKrJm9jcEuXrAJxE=; b=yq1LdMRR9RT+zeX2bAa5crTC1JJCL+kt99wSKQqPzEy9l9aHHAMvs2wohjpUasHuGeZCLx 53Rea0pQUF5jiGQ9eoY+y6kSn3UWVwV5qjPVMy6otLacL5rzWL0HDYa+0vE4rFsA1gOI3O dXycZD2F3AQV44tLYnu87GhtPwUDuFo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171470; a=rsa-sha256; cv=none; b=Y+GJysmQlmcbGGJ3nc5M927XKAvNHo9CpSIktDn4iu305RLeaS6GmkUHl+xcTNrjy43Nhc NlieN3M2ESnm/LDRrH+TgGGcyb8tJr6CNQo4Z8ZG84UGKp3kZTx7cajNhWmC68g5wgnn3x VrztqZrU89dhHFcR5sR/TAHCEKFik+U= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none X-AuditID: a67dfc5b-d6dff70000001748-ae-664ab2c88e13 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 05/12] mm: buddy: make room for a new variable, ugen, in struct page Date: Mon, 20 May 2024 11:17:27 +0900 Message-Id: <20240520021734.21527-6-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrGLMWRmVeSWpSXmKPExsXC9ZZnoe7JTV5pBsdus1jMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M Bd8eshVcVak43DmJpYGxTbaLkZNDQsBEonvlRBYYe+rrZlYQm01AXeLGjZ/MILaIgJnEwdY/ 7CA2s8BdJokD/WwgtrBAvMTfOzvBbBYBVYn13Z1g9bwCphKLb15hgpgpL7F6wwGwOCfQnBmr doLtEgKq+XH0K1AvF1DNezaJi6eOsUI0SEocXHGDZQIj7wJGhlWMQpl5ZbmJmTkmehmVeZkV esn5uZsYgaG/rPZP9A7GTxeCDzEKcDAq8fDueOSZJsSaWFZcmXuIUYKDWUmEd9MWoBBvSmJl VWpRfnxRaU5q8SFGaQ4WJXFeo2/lKUIC6YklqdmpqQWpRTBZJg5OqQZG/v/3r/10/bc1JHse a7XXV8bXGWEd3s6bo7xE5oZKTVbemPFn6at161w+xx99IKD3d9Htg4r7j8S++BBrVhC6K+fE n9VTuwo+ns9eMu1NfO/Po85fLmZYzN/J1JmjOLnMiD/ha/x0YVbZDddDTVk+p7Hc55ywbevG nRfttkxuj+xXjt8hKWW2RYmlOCPRUIu5qDgRADq+n6F5AgAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrLLMWRmVeSWpSXmKPExsXC5WfdrHtik1eawYQbZhZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgyljw7SFbwVWVisOdk1gaGNtkuxg5OSQETCSmvm5mBbHZBNQlbtz4yQxiiwiY SRxs/cMOYjML3GWSONDPBmILC8RL/L2zE8xmEVCVWN/dCVbPK2AqsfjmFSaImfISqzccAItz As2ZsWonC4gtBFTz4+hXtgmMXAsYGVYximTmleUmZuaY6hVnZ1TmZVboJefnbmIEBvKy2j8T dzB+uex+iFGAg1GJh3fDbc80IdbEsuLK3EOMEhzMSiK8m7YAhXhTEiurUovy44tKc1KLDzFK c7AoifN6hacmCAmkJ5akZqemFqQWwWSZODilGhinrg2xCigXv7j77lRNn2ciXUfnbrnRYzpv j0f6ey+N2MWvjWfk/lG89kte/ckCqzkTTrhNO/aopCvVO/bSkuIHXxRZ+nJWTVZ4MktpysFL RcsWT069s6OXwyQ74vPiWCf/vbd4xDb+v/Ar0znZ+NifCW1X2I/mvPmk8nc3Y9XfOULr5Td5 RK5mUWIpzkg01GIuKk4EAClSB6NgAgAA X-CFilter-Loop: Reflected X-Rspam-User: X-Stat-Signature: i4wa8x9mzix1jnqgnzqr3fh6juqzh7ox X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5E6DD40012 X-HE-Tag: 1716171470-407054 X-HE-Meta: U2FsdGVkX1+jSTl/cg3aBrSNTbcQOBSMaYI/lpMn8IdV5r5rD91siJu7bigXvuRqbXjaoEjt00/ytog9uyC4dLLlg0XadJ1Vq32vHbSBfRYIXFxV7moLJTqsMwDu1rtGcXcPoopCbpUsFFNdvr/ene1DiEH1K5wcd6GwnwdZ2QyAotz6m0mIdGpweBG+Oz1dxDmgJSGG8Y7haX3BYs2sHoYVKeLXn5UgWvWRI6OpF7t6DCeytaHisjvjdJy9zaIHaVOwfw5O80eARDz8GzDTNzPJfV+7lGKE/MYV11Girg3DPEgd0FdQ4mEWQb+JVII1hqT2XC3Q7rEfrd7qHrNXScrOnBybB2j8QC2DDttOETMMnzPFhr+vlblbIJWQW53csgw47jHN9SZKgcsn+FMfg5vzF8cHOy8bwyR1FK3qf0zUrN/WikNzQKWnxRS9fcdqIxuR2JjgwzaflOG3kqm6f0yZeCakcOJ2E11YA1sMtGDj3bCDJK+Ln/csMsIKmlFNXBdmmJoT6PkpSVpWP18FH+c/EXwS4j5l3KI0N56z51gJ5xORLV+oFgoMJO8zERNbcDVt9KhGUUZ81iqeI2/DM3y8Z1XFdLY5Y4MUERSkiava+tacVQWQXmQ1Fq69/iaPAY25MejK2R7NTLooRAQ3nW8PpRFEFL/NSNwbVPszFgJGcO50cTjEOWRohy3Fmg+T7dH5ipFk8MlJuozPxfq0LkovdE6+TLxBaHBor/QMtsspMdIYi4yPDkoNC9oWpYnV4sgd+NbOjw5sofzKUs/GaC45m90Y9FU3A0dxmzuecH+ydndtbG7ySaVuSPWkFFNJZ1gB9B65BaeusRhoPee5YxHBSTqAisnNYd9MPQJlDhLOcuOwYqIczwL3tEb614KIZpii08V+5e7NT/jZyAoxJ6tH06veEKdz2+pbXT/0zbfNSESF/yHTdEHbn7cCE/HttyJnzk1uXUkMYL7AVPl MpQ== 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: Functionally, no change. This is a preparation for luf mechanism that tracks need of tlb flush for each page residing in buddy, using a generation number in struct page. Fortunately, since the private field in struct page is used only to store page order in buddy, ranging from 0 to MAX_PAGE_ORDER, that can be covered with unsigned short int. So splitted it into two smaller ones, order and ugen, so that the both can be used in buddy at the same time. Signed-off-by: Byungchul Park --- include/linux/mm_types.h | 40 +++++++++++++++++++++++++++++++++------- mm/internal.h | 4 ++-- mm/page_alloc.c | 13 ++++++++----- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index db0adf5721cc..cd4ec0d10ffb 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -108,13 +108,25 @@ struct page { pgoff_t index; /* Our offset within mapping. */ unsigned long share; /* share count for fsdax */ }; - /** - * @private: Mapping-private opaque data. - * Usually used for buffer_heads if PagePrivate. - * Used for swp_entry_t if PageSwapCache. - * Indicates order in the buddy system if PageBuddy. - */ - unsigned long private; + union { + /** + * @private: Mapping-private opaque data. + * Usually used for buffer_heads if PagePrivate. + * Used for swp_entry_t if PageSwapCache. + */ + unsigned long private; + struct { + /* + * Indicates order in the buddy system if PageBuddy. + */ + unsigned short int order; + /* + * Tracks need of tlb flush used by luf, + * which stands for lazy unmap flush. + */ + unsigned short int ugen; + }; + }; }; struct { /* page_pool used by netstack */ /** @@ -521,6 +533,20 @@ static inline void set_page_private(struct page *page, unsigned long private) page->private = private; } +#define page_buddy_order(page) ((page)->order) + +static inline void set_page_buddy_order(struct page *page, unsigned int order) +{ + page->order = (unsigned short int)order; +} + +#define page_buddy_ugen(page) ((page)->ugen) + +static inline void set_page_buddy_ugen(struct page *page, unsigned short int ugen) +{ + page->ugen = ugen; +} + static inline void *folio_get_private(struct folio *folio) { return folio->private; diff --git a/mm/internal.h b/mm/internal.h index c6483f73ec13..eb9c7d8650fc 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -453,7 +453,7 @@ struct alloc_context { static inline unsigned int buddy_order(struct page *page) { /* PageBuddy() must be checked by the caller */ - return page_private(page); + return page_buddy_order(page); } /* @@ -467,7 +467,7 @@ static inline unsigned int buddy_order(struct page *page) * times, potentially observing different values in the tests and the actual * use of the result. */ -#define buddy_order_unsafe(page) READ_ONCE(page_private(page)) +#define buddy_order_unsafe(page) READ_ONCE(page_buddy_order(page)) /* * This function checks whether a page is free && is the buddy diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 33d4a1be927b..917b22b429d1 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -565,9 +565,12 @@ void prep_compound_page(struct page *page, unsigned int order) prep_compound_head(page, order); } -static inline void set_buddy_order(struct page *page, unsigned int order) +static inline void set_buddy_order_ugen(struct page *page, + unsigned int order, + unsigned short int ugen) { - set_page_private(page, order); + set_page_buddy_order(page, order); + set_page_buddy_ugen(page, order); __SetPageBuddy(page); } @@ -834,7 +837,7 @@ static inline void __free_one_page(struct page *page, } done_merging: - set_buddy_order(page, order); + set_buddy_order_ugen(page, order, 0); if (fpi_flags & FPI_TO_TAIL) to_tail = true; @@ -1344,7 +1347,7 @@ static inline void expand(struct zone *zone, struct page *page, continue; __add_to_free_list(&page[size], zone, high, migratetype, false); - set_buddy_order(&page[size], high); + set_buddy_order_ugen(&page[size], high, 0); nr_added += size; } account_freepages(zone, nr_added, migratetype); @@ -6802,7 +6805,7 @@ static void break_down_buddy_pages(struct zone *zone, struct page *page, continue; add_to_free_list(current_buddy, zone, high, migratetype, false); - set_buddy_order(current_buddy, high); + set_buddy_order_ugen(current_buddy, high, 0); } } From patchwork Mon May 20 02:17:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13668002 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 DA40CC25B75 for ; Mon, 20 May 2024 02:18:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9583E6B009A; Sun, 19 May 2024 22:17:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 860F16B009C; Sun, 19 May 2024 22:17:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44CB06B009D; Sun, 19 May 2024 22:17:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 043A56B009C for ; Sun, 19 May 2024 22:17:52 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C350740AA1 for ; Mon, 20 May 2024 02:17:52 +0000 (UTC) X-FDA: 82137163584.01.425290B Received: from invmail4.hynix.com (exvmail4.skhynix.com [166.125.252.92]) by imf21.hostedemail.com (Postfix) with ESMTP id B38291C0008 for ; Mon, 20 May 2024 02:17:50 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf21.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=1716171471; 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:in-reply-to: references:references; bh=uBxbUsmXvbfxNg91RYfx1ML3CQW0y+GUAqHMQwvODHE=; b=Cp3AasTrh0Jc5I4MAJgHyCm6EArsQ9Gl6r/kXq7i/5ebpbHNQTs9Un9tOtEDx3E1QRJ5X/ a9BeQ5nllPVZZ/SsN7tDygvIE1VQr7Afet0B/TyfMoRPDbYW/zohRqyPlIpFlHEKHQlnPT cRso1r+62Rs8BhvBV1MjHhx001cdHS0= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf21.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=1716171471; a=rsa-sha256; cv=none; b=DNEo7kzblNLYGoHuPIuc/PsdRLkM4Vawr1WwKonWmBIWUMIx/zYxDZUjy18iXxiTDkveNI cSO44zdboU9r8RJSOFX1UMDAY/CmcEJQC4FWke2YOnKtIixa+Sk1nCD85sYqQK27C4fsvO IliS2Wt/ldqZS0DFg+Iu3ilImsk5qVw= X-AuditID: a67dfc5b-d6dff70000001748-b3-664ab2c99612 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 06/12] mm: add folio_put_ugen() to deliver unmap generation number to pcp or buddy Date: Mon, 20 May 2024 11:17:28 +0900 Message-Id: <20240520021734.21527-7-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrBLMWRmVeSWpSXmKPExsXC9ZZnoe7JTV5pBlu2cljMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M y7M72ArO7mKs+D/3NEsD49epjF2MnBwSAiYSlz5NZ4Wx95z6wwxiswmoS9y48RPMFhEwkzjY +ocdxGYWuMskcaCfDcQWFsiVWPzyAVgNi4CqxI6P78FqeAVMJXrvXWKCmCkvsXrDAbAaTqA5 M1btZAGxhYBqfhz9CjSHC6jmPZvElgUbmCEaJCUOrrjBMoGRdwEjwypGocy8stzEzBwTvYzK vMwKveT83E2MwPBfVvsnegfjpwvBhxgFOBiVeHh3PPJME2JNLCuuzD3EKMHBrCTCu2kLUIg3 JbGyKrUoP76oNCe1+BCjNAeLkjiv0bfyFCGB9MSS1OzU1ILUIpgsEwenVANjfsW8An+zqvIz BQrTRJz3as9TNm5yf32FefavhERP05KagGW1bCuvu734xceVyhwgr+6otMrIdcKidyvLNE0+ +yeF3BQy6uZi42s4v/e3hsfEbyr7t1Rz+yaueJ3OwZvEN0tRfeLsnisi0T/Pr1Z8diXXLPqb onlkcdiK3TY3P18Q4P7oe02JpTgj0VCLuag4EQB3SVe6ewIAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrPLMWRmVeSWpSXmKPExsXC5WfdrHtik1eawZWpVhZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgyrg8u4Ot4Owuxor/c0+zNDB+ncrYxcjJISFgIrHn1B9mEJtNQF3ixo2fYLaI gJnEwdY/7CA2s8BdJokD/WwgtrBArsTilw/AalgEVCV2fHwPVsMrYCrRe+8SE8RMeYnVGw6A 1XACzZmxaicLiC0EVPPj6Fe2CYxcCxgZVjGKZOaV5SZm5pjqFWdnVOZlVugl5+duYgQG87La PxN3MH657H6IUYCDUYmHd8NtzzQh1sSy4srcQ4wSHMxKIrybtgCFeFMSK6tSi/Lji0pzUosP MUpzsCiJ83qFpyYICaQnlqRmp6YWpBbBZJk4OKUaGDeJRpRX2IqxKDw0F38qMHtp23/vCw/P xt87knC0TKrIuCbKnr9WlKvOIilj6w3NzuWLnO63df7/18t6Vi6hmZeXPY91s3nOW8H9CsdU G/7f2SQyIaBUTEAzf9rDw99Sjgu/n7bh5pbMl8mrrP4ITNab4XJOWH73a+va/7+lLjMeVpjX vvDsYyWW4oxEQy3mouJEAOJWxfViAgAA X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: B38291C0008 X-Stat-Signature: f1mc5w6wnfaisiunk96od4o3n4wdk7q5 X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1716171470-289957 X-HE-Meta: U2FsdGVkX19Qinv8VmexrcTjrAsJGS0+XlFNQ8cDYbYAGulBR73W6FrXHX7UIc485iUifAb72Tcr7mRdDvWrx2RpuutEoOz+K74QW0jrlWyvWTgZ148KwXMlGEFIE5BuOgXY9pYL03ZYZypCRXWZ7mI+/X3L7SbPAkAmG9NxlCJTT8BIEge2sUycGVN9f+g99Scmyvzx8dFLFuqT8OQ1v5OgSFx7PSw1zxcQdLB/5JWug6BuKOzMf6JaH1TZErsOSL3AV9xXQk0sLQLFdTzW29z1/S9QOVwyOR+lWZ8czXy77G3rW4frYlDB5MQpX2sc51xEl68YCmqKaBkZc7TEibnjvT5Z7+almhbCF+99oZ+TuAJD7zV2HglBi7x0UTM/99FgNbHhwxrVOzXCzvBce08BP9y2hNZ/RNFD/mI9sVSvXTpDG0oG2LIyq/QCg7tE4P2B2xW3lwaWHRlM5J6J/KMYO3E/1Cz0kefy2RXfMUgJ1dpmgmQbUHOq3w4/mNrTr1qeHvZG6vkuUBP6wd/WA13pzrkalGD9YEeLl2E1t1nAIU386sTqcyb4iSmDrPvhbxZ1Q1zU5PDsAoeEP2DGWMUstgGgm8KCdtEURrQBJR7GikPRLzWqv5bYHJBUj9O0VyCV3bLkvQ8ws6zk+XZGXwxtcp7ewlLhPUnjd1S0tm/E7OG9J9xuhCfAQZJ8pogPiHeH7lq54U2ZmgCcHusQJtuFqndgDblDgJlgL8rCvKim1izp7PV6+pFYA3uV0rflfs7HwSB/u1FqUc3M58dKcA2877vNvS7uhi6f2wDfZZpPKD0zPqzOKNEA5Ya9B+zXKbnJ8atxF/CAgVpyDRGACfSu2ZfBdZ4+X2HX/mLqPTsFPm4Nhav/wKAk8DQ6+E1K5GSmC7qXWcoWEQOS2/JIgSlxxHKaINFObyYnzgO0hlgSScFCJba+jkWf2/buE0rw/sG9VkmhuMl5Hq//EzY nFJMRJYg Pojyk6UzqG1wPk1m2MsIGvwxqhQ/8hUDuJA2FpqIgz1c/03U= 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: Introduced a new API, folio_put_ugen(), to deliver unmap generation number to pcp or buddy that will be used by luf mechanism to track need of tlb flush for each page residing in pcp or buddy. For now, the delivery should work for the following call path that is of releasing source folios during migration: folio_put_ugen() __folio_put_ugen() free_unref_page() free_unref_page_commit() free_one_page() __free_one_page() The generation number should be handed over properly when pages travel between pcp and buddy, and must do necessary handling on exit from pcp or buddy. This patch doesn't include actual body for tlb flush on the exit, which will be filled by the main patch of luf mechanism. Signed-off-by: Byungchul Park --- include/linux/mm.h | 22 +++++++ include/linux/sched.h | 1 + mm/compaction.c | 10 +++ mm/internal.h | 70 +++++++++++++++++++- mm/page_alloc.c | 144 ++++++++++++++++++++++++++++++++++-------- mm/page_isolation.c | 6 ++ mm/page_reporting.c | 10 +++ mm/swap.c | 12 +++- 8 files changed, 247 insertions(+), 28 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index dc33f8269fb5..2369ebedb8bd 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1312,6 +1312,7 @@ static inline struct folio *virt_to_folio(const void *x) } void __folio_put(struct folio *folio); +void __folio_put_ugen(struct folio *folio, unsigned short int ugen); void put_pages_list(struct list_head *pages); @@ -1509,6 +1510,27 @@ static inline void folio_put(struct folio *folio) __folio_put(folio); } +/** + * folio_put_ugen - Decrement the last reference count on a folio. + * @folio: The folio. + * @ugen: The unmap generation # of TLB flush that the folio requires. + * + * The folio's reference count should be one since the only user, folio + * migration code, calls folio_put_ugen() only when the folio has no + * reference else. The memory will be released back to the page + * allocator and may be used by another allocation immediately. Do not + * access the memory or the struct folio after calling folio_put_ugen(). + * + * Context: May be called in process or interrupt context, but not in NMI + * context. May be called while holding a spinlock. + */ +static inline void folio_put_ugen(struct folio *folio, unsigned short int ugen) +{ + if (WARN_ON(!folio_put_testzero(folio))) + return; + __folio_put_ugen(folio, ugen); +} + /** * folio_put_refs - Reduce the reference count on a folio. * @folio: The folio. diff --git a/include/linux/sched.h b/include/linux/sched.h index 4118b3f959c3..2aa48adad226 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1339,6 +1339,7 @@ struct task_struct { #endif struct tlbflush_unmap_batch tlb_ubc; + unsigned short int ugen; /* Cache last used pipe for splice(): */ struct pipe_inode_info *splice_pipe; diff --git a/mm/compaction.c b/mm/compaction.c index e731d45befc7..13799fbb2a9a 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -701,6 +701,11 @@ static unsigned long isolate_freepages_block(struct compact_control *cc, if (locked) spin_unlock_irqrestore(&cc->zone->lock, flags); + /* + * Check and flush before using the isolated pages. + */ + check_flush_task_ugen(); + /* * Be careful to not go outside of the pageblock. */ @@ -1673,6 +1678,11 @@ static void fast_isolate_freepages(struct compact_control *cc) spin_unlock_irqrestore(&cc->zone->lock, flags); + /* + * Check and flush before using the isolated pages. + */ + check_flush_task_ugen(); + /* Skip fast search if enough freepages isolated */ if (cc->nr_freepages >= cc->nr_migratepages) break; diff --git a/mm/internal.h b/mm/internal.h index eb9c7d8650fc..332662047c17 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -638,7 +638,7 @@ extern bool free_pages_prepare(struct page *page, unsigned int order); extern int user_min_free_kbytes; -void free_unref_page(struct page *page, unsigned int order); +void free_unref_page(struct page *page, unsigned int order, unsigned short int ugen); void free_unref_folios(struct folio_batch *fbatch); extern void zone_pcp_reset(struct zone *zone); @@ -1512,4 +1512,72 @@ static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry, void workingset_update_node(struct xa_node *node); extern struct list_lru shadow_nodes; +#if defined(CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH) +static inline unsigned short int ugen_latest(unsigned short int a, unsigned short int b) +{ + if (!a || !b) + return a + b; + + /* + * The ugen is wrapped around so let's use this trick. + */ + if ((short int)(a - b) < 0) + return b; + else + return a; +} + +static inline void update_task_ugen(unsigned short int ugen) +{ + current->ugen = ugen_latest(current->ugen, ugen); +} + +static inline unsigned short int hand_over_task_ugen(void) +{ + unsigned short int ret = current->ugen; + + current->ugen = 0; + return ret; +} + +static inline void check_flush_task_ugen(void) +{ + /* + * XXX: luf mechanism will handle this. For now, do nothing but + * reset current's ugen to finalize this turn. + */ + current->ugen = 0; +} + +/* + * Check the constratints of what luf currently supports. + */ +static inline bool can_luf_folio(struct folio *f) +{ + bool can_luf = true; + + /* + * XXX: Remove the constraint once luf handles zone device folio. + */ + can_luf = can_luf && likely(!folio_is_zone_device(f)); + + /* + * XXX: Remove the constraint once luf handles hugetlb folio. + */ + can_luf = can_luf && likely(!folio_test_hugetlb(f)); + + /* + * XXX: Remove the constraint once luf handles large folio. + */ + can_luf = can_luf && likely(!folio_test_large(f)); + + return can_luf; +} +#else /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ +static inline unsigned short int ugen_latest(unsigned short int a, unsigned short int b) { return 0; } +static inline void update_task_ugen(unsigned short int ugen) {} +static inline unsigned short int hand_over_task_ugen(void) { return 0; } +static inline void check_flush_task_ugen(void) {} +static inline bool can_luf_folio(struct folio *f) { return false; } +#endif #endif /* __MM_INTERNAL_H */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 917b22b429d1..2cd278c207d1 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -696,6 +696,7 @@ static inline void __del_page_from_free_list(struct page *page, struct zone *zon if (page_reported(page)) __ClearPageReported(page); + update_task_ugen(page_buddy_ugen(page)); list_del(&page->buddy_list); __ClearPageBuddy(page); set_page_private(page, 0); @@ -768,7 +769,7 @@ buddy_merge_likely(unsigned long pfn, unsigned long buddy_pfn, static inline void __free_one_page(struct page *page, unsigned long pfn, struct zone *zone, unsigned int order, - int migratetype, fpi_t fpi_flags) + int migratetype, fpi_t fpi_flags, unsigned short int ugen) { struct capture_control *capc = task_capc(zone); unsigned long buddy_pfn = 0; @@ -783,12 +784,22 @@ static inline void __free_one_page(struct page *page, VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page); VM_BUG_ON_PAGE(bad_range(zone, page), page); + /* + * Ensure private is zero before using it inside buddy. + */ + set_page_private(page, 0); + account_freepages(zone, 1 << order, migratetype); while (order < MAX_PAGE_ORDER) { int buddy_mt = migratetype; if (compaction_capture(capc, page, order, migratetype)) { + /* + * Capturer will check_flush_task_ugen() through + * prep_new_page(). + */ + update_task_ugen(ugen); account_freepages(zone, -(1 << order), migratetype); return; } @@ -819,6 +830,11 @@ static inline void __free_one_page(struct page *page, if (page_is_guard(buddy)) clear_page_guard(zone, buddy, order); else + /* + * __del_page_from_free_list() updates current's + * ugen that pairs with hand_over_task_ugen() below + * in this funtion. + */ __del_page_from_free_list(buddy, zone, order, buddy_mt); if (unlikely(buddy_mt != migratetype)) { @@ -837,7 +853,8 @@ static inline void __free_one_page(struct page *page, } done_merging: - set_buddy_order_ugen(page, order, 0); + ugen = ugen_latest(ugen, hand_over_task_ugen()); + set_buddy_order_ugen(page, order, ugen); if (fpi_flags & FPI_TO_TAIL) to_tail = true; @@ -1048,6 +1065,11 @@ __always_inline bool free_pages_prepare(struct page *page, VM_BUG_ON_PAGE(PageTail(page), page); + /* + * Ensure private is zero before using it inside pcp. + */ + set_page_private(page, 0); + trace_mm_page_free(page, order); kmsan_free_page(page, order); @@ -1179,17 +1201,23 @@ static void free_pcppages_bulk(struct zone *zone, int count, do { unsigned long pfn; int mt; + unsigned short int ugen; page = list_last_entry(list, struct page, pcp_list); pfn = page_to_pfn(page); mt = get_pfnblock_migratetype(page, pfn); + /* + * pcp uses private to store ugen. + */ + ugen = page_private(page); + /* must delete to avoid corrupting pcp list */ list_del(&page->pcp_list); count -= nr_pages; pcp->count -= nr_pages; - __free_one_page(page, pfn, zone, order, mt, FPI_NONE); + __free_one_page(page, pfn, zone, order, mt, FPI_NONE, ugen); trace_mm_page_pcpu_drain(page, order, mt); } while (count > 0 && !list_empty(list)); } @@ -1199,14 +1227,14 @@ static void free_pcppages_bulk(struct zone *zone, int count, static void free_one_page(struct zone *zone, struct page *page, unsigned long pfn, unsigned int order, - fpi_t fpi_flags) + fpi_t fpi_flags, unsigned short int ugen) { unsigned long flags; int migratetype; spin_lock_irqsave(&zone->lock, flags); migratetype = get_pfnblock_migratetype(page, pfn); - __free_one_page(page, pfn, zone, order, migratetype, fpi_flags); + __free_one_page(page, pfn, zone, order, migratetype, fpi_flags, ugen); spin_unlock_irqrestore(&zone->lock, flags); } @@ -1219,7 +1247,7 @@ static void __free_pages_ok(struct page *page, unsigned int order, if (!free_pages_prepare(page, order)) return; - free_one_page(zone, page, pfn, order, fpi_flags); + free_one_page(zone, page, pfn, order, fpi_flags, 0); __count_vm_events(PGFREE, 1 << order); } @@ -1484,6 +1512,10 @@ inline void post_alloc_hook(struct page *page, unsigned int order, static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags, unsigned int alloc_flags) { + /* + * Check and flush before using the pages. + */ + check_flush_task_ugen(); post_alloc_hook(page, order, gfp_flags); if (order && (gfp_flags & __GFP_COMP)) @@ -1519,6 +1551,10 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, page = get_page_from_free_area(area, migratetype); if (!page) continue; + /* + * del_page_from_free_list() updates current's ugen that + * pairs with check_flush_task_ugen() in prep_new_page(). + */ del_page_from_free_list(page, zone, current_order, migratetype); expand(zone, page, order, current_order, migratetype); trace_mm_page_alloc_zone_locked(page, order, migratetype, @@ -1681,7 +1717,8 @@ static unsigned long find_large_buddy(unsigned long start_pfn) /* Split a multi-block free page into its individual pageblocks */ static void split_large_buddy(struct zone *zone, struct page *page, - unsigned long pfn, int order) + unsigned long pfn, int order, + unsigned short int ugen) { unsigned long end_pfn = pfn + (1 << order); @@ -1694,7 +1731,7 @@ static void split_large_buddy(struct zone *zone, struct page *page, while (pfn != end_pfn) { int mt = get_pfnblock_migratetype(page, pfn); - __free_one_page(page, pfn, zone, pageblock_order, mt, FPI_NONE); + __free_one_page(page, pfn, zone, pageblock_order, mt, FPI_NONE, ugen); pfn += pageblock_nr_pages; page = pfn_to_page(pfn); } @@ -1736,22 +1773,34 @@ bool move_freepages_block_isolate(struct zone *zone, struct page *page, if (pfn != start_pfn) { struct page *buddy = pfn_to_page(pfn); int order = buddy_order(buddy); + unsigned short int ugen; + /* + * del_page_from_free_list() updates current's ugen that + * pairs with the following hand_over_task_ugen(). + */ del_page_from_free_list(buddy, zone, order, get_pfnblock_migratetype(buddy, pfn)); + ugen = hand_over_task_ugen(); set_pageblock_migratetype(page, migratetype); - split_large_buddy(zone, buddy, pfn, order); + split_large_buddy(zone, buddy, pfn, order, ugen); return true; } /* We're the starting block of a larger buddy */ if (PageBuddy(page) && buddy_order(page) > pageblock_order) { int order = buddy_order(page); + unsigned short int ugen; + /* + * del_page_from_free_list() updates current's ugen that + * pairs with the following hand_over_task_ugen(). + */ del_page_from_free_list(page, zone, order, get_pfnblock_migratetype(page, pfn)); + ugen = hand_over_task_ugen(); set_pageblock_migratetype(page, migratetype); - split_large_buddy(zone, page, pfn, order); + split_large_buddy(zone, page, pfn, order, ugen); return true; } move: @@ -1871,6 +1920,10 @@ steal_suitable_fallback(struct zone *zone, struct page *page, /* Take ownership for orders >= pageblock_order */ if (current_order >= pageblock_order) { + /* + * del_page_from_free_list() updates current's ugen that + * pairs with check_flush_task_ugen() in prep_new_page(). + */ del_page_from_free_list(page, zone, current_order, block_type); change_pageblock_range(page, current_order, start_type); expand(zone, page, order, current_order, start_type); @@ -1926,6 +1979,10 @@ steal_suitable_fallback(struct zone *zone, struct page *page, } single_page: + /* + * del_page_from_free_list() updates current's ugen that pairs + * with check_flush_task_ugen() in prep_new_page(). + */ del_page_from_free_list(page, zone, current_order, block_type); expand(zone, page, order, current_order, block_type); return page; @@ -2547,7 +2604,7 @@ static int nr_pcp_high(struct per_cpu_pages *pcp, struct zone *zone, static void free_unref_page_commit(struct zone *zone, struct per_cpu_pages *pcp, struct page *page, int migratetype, - unsigned int order) + unsigned int order, unsigned short int ugen) { int high, batch; int pindex; @@ -2561,6 +2618,11 @@ static void free_unref_page_commit(struct zone *zone, struct per_cpu_pages *pcp, pcp->alloc_factor >>= 1; __count_vm_events(PGFREE, 1 << order); pindex = order_to_pindex(migratetype, order); + + /* + * pcp uses private to store ugen. + */ + set_page_private(page, ugen); list_add(&page->pcp_list, &pcp->lists[pindex]); pcp->count += 1 << order; @@ -2596,7 +2658,8 @@ static void free_unref_page_commit(struct zone *zone, struct per_cpu_pages *pcp, /* * Free a pcp page */ -void free_unref_page(struct page *page, unsigned int order) +void free_unref_page(struct page *page, unsigned int order, + unsigned short int ugen) { unsigned long __maybe_unused UP_flags; struct per_cpu_pages *pcp; @@ -2622,7 +2685,7 @@ void free_unref_page(struct page *page, unsigned int order) migratetype = get_pfnblock_migratetype(page, pfn); if (unlikely(migratetype >= MIGRATE_PCPTYPES)) { if (unlikely(is_migrate_isolate(migratetype))) { - free_one_page(page_zone(page), page, pfn, order, FPI_NONE); + free_one_page(page_zone(page), page, pfn, order, FPI_NONE, ugen); return; } migratetype = MIGRATE_MOVABLE; @@ -2632,10 +2695,10 @@ void free_unref_page(struct page *page, unsigned int order) pcp_trylock_prepare(UP_flags); pcp = pcp_spin_trylock(zone->per_cpu_pageset); if (pcp) { - free_unref_page_commit(zone, pcp, page, migratetype, order); + free_unref_page_commit(zone, pcp, page, migratetype, order, ugen); pcp_spin_unlock(pcp); } else { - free_one_page(zone, page, pfn, order, FPI_NONE); + free_one_page(zone, page, pfn, order, FPI_NONE, ugen); } pcp_trylock_finish(UP_flags); } @@ -2666,7 +2729,7 @@ void free_unref_folios(struct folio_batch *folios) */ if (!pcp_allowed_order(order)) { free_one_page(folio_zone(folio), &folio->page, - pfn, order, FPI_NONE); + pfn, order, FPI_NONE, 0); continue; } folio->private = (void *)(unsigned long)order; @@ -2702,7 +2765,7 @@ void free_unref_folios(struct folio_batch *folios) */ if (is_migrate_isolate(migratetype)) { free_one_page(zone, &folio->page, pfn, - order, FPI_NONE); + order, FPI_NONE, 0); continue; } @@ -2715,7 +2778,7 @@ void free_unref_folios(struct folio_batch *folios) if (unlikely(!pcp)) { pcp_trylock_finish(UP_flags); free_one_page(zone, &folio->page, pfn, - order, FPI_NONE); + order, FPI_NONE, 0); continue; } locked_zone = zone; @@ -2730,7 +2793,7 @@ void free_unref_folios(struct folio_batch *folios) trace_mm_page_free_batched(&folio->page); free_unref_page_commit(zone, pcp, &folio->page, migratetype, - order); + order, 0); } if (pcp) { @@ -2781,6 +2844,11 @@ int __isolate_free_page(struct page *page, unsigned int order) return 0; } + /* + * del_page_from_free_list() updates current's ugen. The user of + * the isolated page should check_flush_task_ugen() before using + * it. + */ del_page_from_free_list(page, zone, order, mt); /* @@ -2822,7 +2890,7 @@ void __putback_isolated_page(struct page *page, unsigned int order, int mt) /* Return isolated page to tail of freelist. */ __free_one_page(page, page_to_pfn(page), zone, order, mt, - FPI_SKIP_REPORT_NOTIFY | FPI_TO_TAIL); + FPI_SKIP_REPORT_NOTIFY | FPI_TO_TAIL, 0); } /* @@ -2965,6 +3033,11 @@ struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, } page = list_first_entry(list, struct page, pcp_list); + + /* + * Pairs with check_flush_task_ugen() in prep_new_page(). + */ + update_task_ugen(page_private(page)); list_del(&page->pcp_list); pcp->count -= 1 << order; } while (check_new_pages(page, order)); @@ -4791,11 +4864,11 @@ void __free_pages(struct page *page, unsigned int order) struct alloc_tag *tag = pgalloc_tag_get(page); if (put_page_testzero(page)) - free_unref_page(page, order); + free_unref_page(page, order, 0); else if (!head) { pgalloc_tag_sub_pages(tag, (1 << order) - 1); while (order-- > 0) - free_unref_page(page + (1 << order), order); + free_unref_page(page + (1 << order), order, 0); } } EXPORT_SYMBOL(__free_pages); @@ -4857,7 +4930,7 @@ void __page_frag_cache_drain(struct page *page, unsigned int count) VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); if (page_ref_sub_and_test(page, count)) - free_unref_page(page, compound_order(page)); + free_unref_page(page, compound_order(page), 0); } EXPORT_SYMBOL(__page_frag_cache_drain); @@ -4898,7 +4971,7 @@ void *__page_frag_alloc_align(struct page_frag_cache *nc, goto refill; if (unlikely(nc->pfmemalloc)) { - free_unref_page(page, compound_order(page)); + free_unref_page(page, compound_order(page), 0); goto refill; } @@ -4942,7 +5015,7 @@ void page_frag_free(void *addr) struct page *page = virt_to_head_page(addr); if (unlikely(put_page_testzero(page))) - free_unref_page(page, compound_order(page)); + free_unref_page(page, compound_order(page), 0); } EXPORT_SYMBOL(page_frag_free); @@ -6751,10 +6824,19 @@ void __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) BUG_ON(!PageBuddy(page)); VM_WARN_ON(get_pageblock_migratetype(page) != MIGRATE_ISOLATE); order = buddy_order(page); + /* + * del_page_from_free_list() updates current's ugen that + * pairs with check_flush_task_ugen() below in this function. + */ del_page_from_free_list(page, zone, order, MIGRATE_ISOLATE); pfn += (1 << order); } spin_unlock_irqrestore(&zone->lock, flags); + + /* + * Check and flush before using it. + */ + check_flush_task_ugen(); } #endif @@ -6830,6 +6912,11 @@ bool take_page_off_buddy(struct page *page) int migratetype = get_pfnblock_migratetype(page_head, pfn_head); + /* + * del_page_from_free_list() updates current's + * ugen that pairs with check_flush_task_ugen() below + * in this function. + */ del_page_from_free_list(page_head, zone, page_order, migratetype); break_down_buddy_pages(zone, page_head, page, 0, @@ -6842,6 +6929,11 @@ bool take_page_off_buddy(struct page *page) break; } spin_unlock_irqrestore(&zone->lock, flags); + + /* + * Check and flush before using it. + */ + check_flush_task_ugen(); return ret; } @@ -6860,7 +6952,7 @@ bool put_page_back_buddy(struct page *page) int migratetype = get_pfnblock_migratetype(page, pfn); ClearPageHWPoisonTakenOff(page); - __free_one_page(page, pfn, zone, 0, migratetype, FPI_NONE); + __free_one_page(page, pfn, zone, 0, migratetype, FPI_NONE, 0); if (TestClearPageHWPoison(page)) { ret = true; } diff --git a/mm/page_isolation.c b/mm/page_isolation.c index 042937d5abe4..5823da60a621 100644 --- a/mm/page_isolation.c +++ b/mm/page_isolation.c @@ -260,6 +260,12 @@ static void unset_migratetype_isolate(struct page *page, int migratetype) zone->nr_isolate_pageblock--; out: spin_unlock_irqrestore(&zone->lock, flags); + + /* + * Check and flush for the pages that have been isolated. + */ + if (isolated_page) + check_flush_task_ugen(); } static inline struct page * diff --git a/mm/page_reporting.c b/mm/page_reporting.c index e4c428e61d8c..4f94a3ea1b22 100644 --- a/mm/page_reporting.c +++ b/mm/page_reporting.c @@ -221,6 +221,11 @@ page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone, /* release lock before waiting on report processing */ spin_unlock_irq(&zone->lock); + /* + * Check and flush before using the isolated pages. + */ + check_flush_task_ugen(); + /* begin processing pages in local list */ err = prdev->report(prdev, sgl, PAGE_REPORTING_CAPACITY); @@ -253,6 +258,11 @@ page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone, spin_unlock_irq(&zone->lock); + /* + * Check and flush before using the isolated pages. + */ + check_flush_task_ugen(); + return err; } diff --git a/mm/swap.c b/mm/swap.c index f0d478eee292..0fc5a5e8457f 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -126,10 +126,20 @@ void __folio_put(struct folio *folio) if (folio_test_large(folio) && folio_test_large_rmappable(folio)) folio_undo_large_rmappable(folio); mem_cgroup_uncharge(folio); - free_unref_page(&folio->page, folio_order(folio)); + free_unref_page(&folio->page, folio_order(folio), 0); } EXPORT_SYMBOL(__folio_put); +void __folio_put_ugen(struct folio *folio, unsigned short int ugen) +{ + if (WARN_ON(!can_luf_folio(folio))) + return; + + page_cache_release(folio); + mem_cgroup_uncharge(folio); + free_unref_page(&folio->page, 0, ugen); +} + /** * put_pages_list() - release a list of pages * @pages: list of pages threaded on page->lru From patchwork Mon May 20 02:17:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13668001 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 4E2C2C25B75 for ; Mon, 20 May 2024 02:18:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 68CF66B0096; Sun, 19 May 2024 22:17:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5F1F86B009A; Sun, 19 May 2024 22:17:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 136AE6B009B; Sun, 19 May 2024 22:17:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D3F1D6B009A for ; Sun, 19 May 2024 22:17:52 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 96432160B55 for ; Mon, 20 May 2024 02:17:52 +0000 (UTC) X-FDA: 82137163584.10.E65E176 Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by imf23.hostedemail.com (Postfix) with ESMTP id BB375140008 for ; Mon, 20 May 2024 02:17:50 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716171471; 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:in-reply-to: references:references; bh=ZKx3F49B6gn8AWD8yrAfoz7FZCy6q02Z9LdSwJIimPw=; b=qowZaGqwId/ECKjU58rFXNpJqjTeRCNu6q/eMvPyfQ4pvb9k6Sp1oL/f4m4wkQQhDdCXNk E1X+Ww4bnHnqOArRgGGHHiJPdgvvg+9KdulpNG/ILv4OvNNTEslUMaUeebVglIN1PF4QIe RxvvZscFgNFGK+hM9hMpD6tbO4Aoj88= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171471; a=rsa-sha256; cv=none; b=LfFZ6BWB7zaNWoXd4laL4oVoeSeqWuLpe6ixsEhW4zV5bOe77NvuVecxUoAmc3OEedypLK It4360G3Yq9Hk7YkRAFYjO61FMoX0xx2zo7dLcgVaQWdAx9Vt2orPNJ9toUflA4vU3Pe78 VC3xXcaYurfqsoTpIunSe32lxXUT5ZQ= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none X-AuditID: a67dfc5b-d6dff70000001748-b8-664ab2c98732 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 07/12] mm: add a parameter, unmap generation number, to free_unref_folios() Date: Mon, 20 May 2024 11:17:29 +0900 Message-Id: <20240520021734.21527-8-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrKLMWRmVeSWpSXmKPExsXC9ZZnke7JTV5pBn/WcVrMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M Td8msBXcVavouuPfwLhTvouRk0NCwETi+ZJX7DD21ZX3mEBsNgF1iRs3fjKD2CICZhIHW/+A 1TAL3GWSONDPBmILC6RJ3J84H8xmEVCVONV1GqyXV8BUYsvHx4wQM+UlVm84ADaHE2jOjFU7 WUBsIaCaH0e/AvVyAdW8Z5M4+r6LBaJBUuLgihssExh5FzAyrGIUyswry03MzDHRy6jMy6zQ S87P3cQIDPxltX+idzB+uhB8iFGAg1GJh3fHI880IdbEsuLK3EOMEhzMSiK8m7YAhXhTEiur Uovy44tKc1KLDzFKc7AoifMafStPERJITyxJzU5NLUgtgskycXBKNTAGKB97+tTbeKZ1XSHr 239H9e70ewTlztj+MHhjlLnD+l9/xd83Zj1dYSQ7oSx1x7NJtyrrp6u3b5D/2+/SklL528zs 2G+d84+y9+XxFTHbSbEK6J+4ciLj5rE1kbmqF89qMNufnbW8QHjTnK2aWTM2GzYFH9s8d2qs 7jPj622LpiVsv/rYaPFJJZbijERDLeai4kQAutg0mngCAAA= X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrNLMWRmVeSWpSXmKPExsXC5WfdrHtyk1eawXZeiznr17BZfN7wj83i xYZ2Rouv638xWzz91MdicXjuSVaLy7vmsFncW/Of1eL8rrWsFjuW7mOyuHRgAZPF8d4DTBbz 731ms9i8aSqzxfEpUxktfv8AKj45azKLg6DH99Y+Fo+ds+6yeyzYVOqxeYWWx+I9L5k8Nq3q ZPPY9GkSu8e7c+fYPU7M+M3iMe9koMf7fVfZPBa/+MDksfWXnUfj1GtsHp83yQXwR3HZpKTm ZJalFunbJXBlbPo2ga3grlpF1x3/Bsad8l2MnBwSAiYSV1feYwKx2QTUJW7c+MkMYosImEkc bP3DDmIzC9xlkjjQzwZiCwukSdyfOB/MZhFQlTjVdRqsl1fAVGLLx8eMEDPlJVZvOAA2hxNo zoxVO1lAbCGgmh9Hv7JNYORawMiwilEkM68sNzEzx1SvODujMi+zQi85P3cTIzCIl9X+mbiD 8ctl90OMAhyMSjy8G257pgmxJpYVV+YeYpTgYFYS4d20BSjEm5JYWZValB9fVJqTWnyIUZqD RUmc1ys8NUFIID2xJDU7NbUgtQgmy8TBKdXAeL/BPtem2GmeynLu1j/Pl07tLPnMoqUxpajs TFXDe+s1nzbm+Kl71hUU9vSWXv04ne2IZp2MwvtTD3NMLk86wTznpuC96mK5czoHVwg8yT3J eWD+9AU3t71Yf3lHS9lvgc4VMRsPVQcJHmW4yFT68vU3q5jNFxwcevdMUxVftWEb++P2M+Uv liixFGckGmoxFxUnAgB0MP5JXgIAAA== X-CFilter-Loop: Reflected X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: BB375140008 X-Rspam-User: X-Stat-Signature: xfzo3nwubcnzo7mnyo3g8jbm6dfccpki X-HE-Tag: 1716171470-742294 X-HE-Meta: U2FsdGVkX1+aaBT5uFOndnaTy66+do+nP4Sab2eBa1RbPxskBccBJvWL7oRCdvS4mC18dc5F5Zy/IrToIyrjgG1DRtQ0aaq1VsRuVK5L7O2BEZg/uSUhHFBuvCCfIKjOM3hSyR8ZaUWfHXpzCXqgU+ESI4BAXiOo4cZ/Fbva+nRD0JEbbCkdl9ypkGdNZFIA7ONVMotAdo0u+w0gsmGRRJIvTZ1p9QNSVuScWvAWa/UglSMFdJdkUjZKbfWC7n7pfe14ICE7LEL0fXOcReCLDxTqgUzp8UHzAThpzssKVQdyDU7ivV5NTHxLiyPYWnzpf21WeX7f6X9NTGBZFB0ALuai8MS2hMjdSMBLJ9W7bWdgbPGXBhI4DNomEdt/5O+Qta8GGtrUU524KX0ejXlJ9KQ5StfbY0aoaJARSyCl2fJ2vEsELqOc6TzP9lvasUXsJ3arcWBbgcqJ2nSYqdSs/I67Ur6BcaDm5m+Bu9qt6+lI+TTnut5Lrp6Q9zRK/E/h0XZqXwdoiFeXrcVPJBklnyg7pp02bRRM2F+Lzn8LtQgJdxDBY39CIGveZPXtCw51AEZJAIi++QO2tQgobuF6rK2MhcL3Jyz42BVmX8fWyHe8MCM9jjZeDsFwCMvnzWVTKPYBPIsUKB5m2yRS2hHn465PnV60TeM/wwWA+sTYq+/TBe+eDAn79hpE0bbR4m+siSVRM3UGexM4KJTDCZtJykfNiq6PTHLFcfCEWYWiu7iqVMxxDZQCksOmW1d7SimUcyRb70pRE9Jcs8BFkEHTOlScFyKLXtDmo0BS/PHtYZS7Te4P6jWlDjCzhCxvaeLo9d/yTv8VKrm6sT6fkoyCZJnFXHJgfUukvtxhj3ovidODPCqvrgDfIhbgx/JZ65+eZvwbT4joA5EsJ0IfJTnIsZlUQVZKF4clWMdKsbSKQX4NKKfJ6Q6l7/IdHQi/x24ypE8pH0YhNXtgOFeJr/Z +AzE/r8n qKS5O2BG79Nj0TENX677fYy0/91RMaZoflBeLxFKF6Kxp7Xg= 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: Unmap generation number is used by luf mechanism to track need of tlb flush for each page residing in pcp or buddy. The number should be delivered to pcp or buddy via free_unref_folios() that is for releasing folios that have been unmapped during reclaim in shrink_folio_list(). Signed-off-by: Byungchul Park --- mm/internal.h | 2 +- mm/page_alloc.c | 10 +++++----- mm/swap.c | 6 +++--- mm/vmscan.c | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 332662047c17..0d4c74e76de6 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -639,7 +639,7 @@ extern bool free_pages_prepare(struct page *page, unsigned int order); extern int user_min_free_kbytes; void free_unref_page(struct page *page, unsigned int order, unsigned short int ugen); -void free_unref_folios(struct folio_batch *fbatch); +void free_unref_folios(struct folio_batch *fbatch, unsigned short int ugen); extern void zone_pcp_reset(struct zone *zone); extern void zone_pcp_disable(struct zone *zone); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 2cd278c207d1..63f14305f4de 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -2706,7 +2706,7 @@ void free_unref_page(struct page *page, unsigned int order, /* * Free a batch of folios */ -void free_unref_folios(struct folio_batch *folios) +void free_unref_folios(struct folio_batch *folios, unsigned short int ugen) { unsigned long __maybe_unused UP_flags; struct per_cpu_pages *pcp = NULL; @@ -2729,7 +2729,7 @@ void free_unref_folios(struct folio_batch *folios) */ if (!pcp_allowed_order(order)) { free_one_page(folio_zone(folio), &folio->page, - pfn, order, FPI_NONE, 0); + pfn, order, FPI_NONE, ugen); continue; } folio->private = (void *)(unsigned long)order; @@ -2765,7 +2765,7 @@ void free_unref_folios(struct folio_batch *folios) */ if (is_migrate_isolate(migratetype)) { free_one_page(zone, &folio->page, pfn, - order, FPI_NONE, 0); + order, FPI_NONE, ugen); continue; } @@ -2778,7 +2778,7 @@ void free_unref_folios(struct folio_batch *folios) if (unlikely(!pcp)) { pcp_trylock_finish(UP_flags); free_one_page(zone, &folio->page, pfn, - order, FPI_NONE, 0); + order, FPI_NONE, ugen); continue; } locked_zone = zone; @@ -2793,7 +2793,7 @@ void free_unref_folios(struct folio_batch *folios) trace_mm_page_free_batched(&folio->page); free_unref_page_commit(zone, pcp, &folio->page, migratetype, - order, 0); + order, ugen); } if (pcp) { diff --git a/mm/swap.c b/mm/swap.c index 0fc5a5e8457f..1937ac937b8f 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -163,11 +163,11 @@ void put_pages_list(struct list_head *pages) /* LRU flag must be clear because it's passed using the lru */ if (folio_batch_add(&fbatch, folio) > 0) continue; - free_unref_folios(&fbatch); + free_unref_folios(&fbatch, 0); } if (fbatch.nr) - free_unref_folios(&fbatch); + free_unref_folios(&fbatch, 0); INIT_LIST_HEAD(pages); } EXPORT_SYMBOL(put_pages_list); @@ -1029,7 +1029,7 @@ void folios_put_refs(struct folio_batch *folios, unsigned int *refs) folios->nr = j; mem_cgroup_uncharge_folios(folios); - free_unref_folios(folios); + free_unref_folios(folios, 0); } EXPORT_SYMBOL(folios_put_refs); diff --git a/mm/vmscan.c b/mm/vmscan.c index 49bd94423961..bb0ff11f9ec9 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1460,7 +1460,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, if (folio_batch_add(&free_folios, folio) == 0) { mem_cgroup_uncharge_folios(&free_folios); try_to_unmap_flush(); - free_unref_folios(&free_folios); + free_unref_folios(&free_folios, 0); } continue; @@ -1527,7 +1527,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, mem_cgroup_uncharge_folios(&free_folios); try_to_unmap_flush(); - free_unref_folios(&free_folios); + free_unref_folios(&free_folios, 0); list_splice(&ret_folios, folio_list); count_vm_events(PGACTIVATE, pgactivate); @@ -1869,7 +1869,7 @@ static unsigned int move_folios_to_lru(struct lruvec *lruvec, if (folio_batch_add(&free_folios, folio) == 0) { spin_unlock_irq(&lruvec->lru_lock); mem_cgroup_uncharge_folios(&free_folios); - free_unref_folios(&free_folios); + free_unref_folios(&free_folios, 0); spin_lock_irq(&lruvec->lru_lock); } @@ -1891,7 +1891,7 @@ static unsigned int move_folios_to_lru(struct lruvec *lruvec, if (free_folios.nr) { spin_unlock_irq(&lruvec->lru_lock); mem_cgroup_uncharge_folios(&free_folios); - free_unref_folios(&free_folios); + free_unref_folios(&free_folios, 0); spin_lock_irq(&lruvec->lru_lock); } From patchwork Mon May 20 02:17:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13668003 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 A6AA1C25B79 for ; Mon, 20 May 2024 02:18:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DA1F6B00A0; Sun, 19 May 2024 22:17:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C2316B009F; Sun, 19 May 2024 22:17:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF5C86B009D; Sun, 19 May 2024 22:17:54 -0400 (EDT) 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 ABF9F6B009B for ; Sun, 19 May 2024 22:17:54 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5DD3614014A for ; Mon, 20 May 2024 02:17:54 +0000 (UTC) X-FDA: 82137163668.17.2E040DD Received: from invmail4.hynix.com (exvmail4.skhynix.com [166.125.252.92]) by imf01.hostedemail.com (Postfix) with ESMTP id 9460440008 for ; Mon, 20 May 2024 02:17:52 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716171473; 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:in-reply-to: references:references; bh=x9wh2Uy2LBAY2jLtNMSHkvHujd6XB9pxIP0VooR2LTw=; b=wOT8JZhCYcTKjC7OJKRc/UM6v7EjU4NC17iXVJbbcn2/xh/iHLvwqC6bu/0ALGnzjJTx1l OvSFoPklUR1YMjs/tgExJRjsPnJci/WiX7RQBUqhEF5xWqg+SB0X68AbztyEQUQQDJGOl7 pA5soacJu6bPm/KlMk0UREiH4QGdAPM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171473; a=rsa-sha256; cv=none; b=1uL/E8rUyJs2imbboEM1WIBNpNZHM7Y3pI69hZI2X0qXxWueg7D06AngXx8Hat7TWC1+0+ dNvAyrPE8LFnlp86SLIqT2bpqqRbiuHZQquMXFhviT7da6loEWuj20q97J7ExNWpbTr6Fi k7CU0Nu961/TfDEcRnvfq/hpKtvxnCs= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none X-AuditID: a67dfc5b-d6dff70000001748-bd-664ab2c98c0d 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 08/12] mm/rmap: recognize read-only tlb entries during batched tlb flush Date: Mon, 20 May 2024 11:17:30 +0900 Message-Id: <20240520021734.21527-9-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrMLMWRmVeSWpSXmKPExsXC9ZZnoe7JTV5pBpdn8FjMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M hneP2AvuyFZc3D2DrYHxiEQXIzuHhICJRJN1FyMnmDX10XUWEJtNQF3ixo2fzCC2iICZxMHW P+wgNrPAXSaJA/1sILawQLLEm12bwWwWAVWJhqk3GEFsXgFTie6lxxghZspLrN5wAGwOJ9Cc Gat2gs0XAqr5cfQrG0TNezaJQ+eg6iUlDq64wTKBkXcBI8MqRqHMvLLcxMwcE72MyrzMCr3k /NxNjMCgX1b7J3oH46cLwYcYBTgYlXh4dzzyTBNiTSwrrsw9xCjBwawkwrtpC1CINyWxsiq1 KD++qDQntfgQozQHi5I4r9G38hQhgfTEktTs1NSC1CKYLBMHp1QDo33CvvkfvWWC/1u2sf6X qVe/d67ljMDSQ8++v375uFD2BJcQz8aZ+n6+KelGDTpGZ3nueLq5SBz/1NAarSfMYMR70fOm nWdFb7NfOkPGEqnSzKLflxkfOHkZ69oEJLzdGOk+w6Qm6btJeCcL85OzOueWcufOmziJh1n6 0wndI0p1yuINb1qVWIozEg21mIuKEwHfpzXxdgIAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrLLMWRmVeSWpSXmKPExsXC5WfdrHtyk1eawf3jTBZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgymh494i94I5sxcXdM9gaGI9IdDFyckgImEhMfXSdBcRmE1CXuHHjJzOILSJg JnGw9Q87iM0scJdJ4kA/G4gtLJAs8WbXZjCbRUBVomHqDUYQm1fAVKJ76TFGiJnyEqs3HACb wwk0Z8aqnWDzhYBqfhz9yjaBkWsBI8MqRpHMvLLcxMwcU73i7IzKvMwKveT83E2MwEBeVvtn 4g7GL5fdDzEKcDAq8fBuuO2ZJsSaWFZcmXuIUYKDWUmEd9MWoBBvSmJlVWpRfnxRaU5q8SFG aQ4WJXFer/DUBCGB9MSS1OzU1ILUIpgsEwenVAPjGuXpyvuCa4Wu5Zi71sTNiF1o+txDyetj bdGGb6fOL2szUmfOtvJ1uPO4b07X67ieDfWPcibJpix8tp39xuLE1jwZR4ZNZ8L+7JNhkDz8 veLy73xfa6sPltFa618dPjm1tlJ53Rq+oJvr3nw7Grs30Fxn8hOBf8+2ZLpMqrH5F1H9fnMK Z890JZbijERDLeai4kQA4J+2GmACAAA= X-CFilter-Loop: Reflected X-Rspam-User: X-Stat-Signature: ct1dkkgms4xqrq9rgp7cuypi9tscongx X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9460440008 X-HE-Tag: 1716171472-578750 X-HE-Meta: U2FsdGVkX19JKEUH/CHsiEZrEHpjV3Kq2ZFFMKv5x3/1ETqj5EKbyJuOyo4lnsn0kAkec+gmNe4inggky+OQUQ0iZAL6kw+dqxu9CAE4tmuQqGDxfrx+BjEuc0/4Z4bxwta0lKNXh2GWciRv0dhu4MTIe1LD+XSIay5YN7DtXRaeSGue5ZVrdSpxNM24T1jeKq8Qe79YzyvBYIUYwFszlctMXOuqQxJyPJeZjdoxij21bO2vn+lawKtzdQqROdXPZl1V5W5NHhtZj11HeW8A0kTcvyi5AXAOy45Z5GoWWymNkbZqqGF2YJ8HmDGzA0FKbUpPczJEPEffkgPg4h1cq2VenPkVma8adhCGu9Lbuv8Py407TGfKxn2iCZExJB2Ln0UGJ9ygv10AlxdSMt0FCJLfsXLX4LXBcGvT4pP6JoiB7J+aVK8/MjnCHC4ln1wm3WdBbdOnu6e7QdQjWhMMtBajBS6oiUFKYq/QiyAo1JtyPmfj2cQYQ031LqTudwpRbw6lY81nU2ZFZHshBJsX8hY3VfsGM6T+4UNqsrmnW9Z0LGpOO9Mc3ydUto6pMtHkWLH5RZh75q3OvjAnbzFq6F7Xhknt3nzrzHYUtuITucPhNLSQnhKPtbg3UdTRzTnC7yt49/Ls3Fcb8V10SrtoHwCi410ckcY0B95qrJQyHXPM0fu0TMfMlpmzhu9xZOvRA/VflgYXzER+kXN7as6+VySy8XrlwXzNLFMLadKqdgcE9IQjUZLAZZdrJm9IMfatQOXIHYMpEyAPMyZYZ05n0w4Hmrtr6pbaqeIhfqqDtsQh6g8DcArK2YNrpbuZnzZYj8j8kEl0EyjgXBE46NeQ/N2m7gNlcXLX7ba8uEK5l58efnQyfQbAizzqDO0YPCEzqaTSFJkrIKievJ2V7oTPN27dPxAMj+AhThuugM+fTB6qBJFZQsnbDxpl8eeUOxSntJx/7qMlvlflF2WJeQI vGA== 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: Functionally, no change. This is a preparation for luf mechanism that requires to recognize read-only tlb entries and handle them in a different way. The newly introduced API in this patch, fold_ubc(), will be used by luf mechanism. Signed-off-by: Byungchul Park --- include/linux/sched.h | 1 + mm/internal.h | 4 ++++ mm/rmap.c | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 2aa48adad226..0915390b1b5e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1339,6 +1339,7 @@ struct task_struct { #endif struct tlbflush_unmap_batch tlb_ubc; + struct tlbflush_unmap_batch tlb_ubc_ro; unsigned short int ugen; /* Cache last used pipe for splice(): */ diff --git a/mm/internal.h b/mm/internal.h index 0d4c74e76de6..805f0e6ecab4 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1100,6 +1100,7 @@ extern struct workqueue_struct *mm_percpu_wq; void try_to_unmap_flush(void); void try_to_unmap_flush_dirty(void); void flush_tlb_batched_pending(struct mm_struct *mm); +void fold_ubc(struct tlbflush_unmap_batch *dst, struct tlbflush_unmap_batch *src); #else static inline void try_to_unmap_flush(void) { @@ -1110,6 +1111,9 @@ static inline void try_to_unmap_flush_dirty(void) static inline void flush_tlb_batched_pending(struct mm_struct *mm) { } +static inline void fold_ubc(struct tlbflush_unmap_batch *dst, struct tlbflush_unmap_batch *src) +{ +} #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ extern const struct trace_print_flags pageflag_names[]; diff --git a/mm/rmap.c b/mm/rmap.c index cf8a99a49aef..328b5e2217e6 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -635,6 +635,28 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, } #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH + +void fold_ubc(struct tlbflush_unmap_batch *dst, + struct tlbflush_unmap_batch *src) +{ + if (!src->flush_required) + return; + + /* + * Fold src to dst. + */ + arch_tlbbatch_fold(&dst->arch, &src->arch); + dst->writable = dst->writable || src->writable; + dst->flush_required = true; + + /* + * Reset src. + */ + arch_tlbbatch_clear(&src->arch); + src->flush_required = false; + src->writable = false; +} + /* * Flush TLB entries for recently unmapped pages from remote CPUs. It is * important if a PTE was dirty when it was unmapped that it's flushed @@ -644,7 +666,9 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, void try_to_unmap_flush(void) { struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc_ro = ¤t->tlb_ubc_ro; + fold_ubc(tlb_ubc, tlb_ubc_ro); if (!tlb_ubc->flush_required) return; @@ -658,8 +682,9 @@ void try_to_unmap_flush(void) void try_to_unmap_flush_dirty(void) { struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc_ro = ¤t->tlb_ubc_ro; - if (tlb_ubc->writable) + if (tlb_ubc->writable || tlb_ubc_ro->writable) try_to_unmap_flush(); } @@ -676,13 +701,18 @@ void try_to_unmap_flush_dirty(void) static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, unsigned long uaddr) { - struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc; int batch; bool writable = pte_dirty(pteval); if (!pte_accessible(mm, pteval)) return; + if (pte_write(pteval)) + tlb_ubc = ¤t->tlb_ubc; + else + tlb_ubc = ¤t->tlb_ubc_ro; + arch_tlbbatch_add_pending(&tlb_ubc->arch, mm, uaddr); tlb_ubc->flush_required = true; From patchwork Mon May 20 02:17:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13668004 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 36764C25B75 for ; Mon, 20 May 2024 02:18:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 818AA6B009E; Sun, 19 May 2024 22:17:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 750DD6B009B; Sun, 19 May 2024 22:17:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D4086B009E; Sun, 19 May 2024 22:17:55 -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 DA3026B009B for ; Sun, 19 May 2024 22:17:54 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9BF2480B40 for ; Mon, 20 May 2024 02:17:54 +0000 (UTC) X-FDA: 82137163668.28.5239176 Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by imf14.hostedemail.com (Postfix) with ESMTP id 9512210000B for ; Mon, 20 May 2024 02:17:52 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171473; a=rsa-sha256; cv=none; b=7rMuCt2prGaXOPqG4qs/Xev6LA6KCfYC81Ypy0Oy5MSlnhy38OncDp+EhboIJgZZA30lIF nInnbYn/3ssfZns9Kpt8hlmqzfZo3YqHioUVT6rnguNW+FoWOhOvolzoKPut1LpwVCR0NO sk3umg3rbAUBgCFrgbePJa4462q7pBE= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716171473; 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:in-reply-to: references:references; bh=Ye+uDH4ekKhxitmpX2Za+Hhru+j/PYywAcSJDSAhNF8=; b=Lh7XSfa3CY41pjK1mCvJL7jIc9oUVuuOCpFCmUPz8YWdK7BXu90wmrwxzxm2HoPT3/qNxh zJ9hGP96Pr/DL6NzfY4VoXm+cfonp5vHHFUoU3BEw3X4d0h06lawYSbv+ka1fttF5qovpB aIxHv21E6938hxK3hDJjsmhREUqYGMU= X-AuditID: a67dfc5b-d6dff70000001748-c2-664ab2c9d6f7 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 09/12] mm: implement LUF(Lazy Unmap Flush) defering tlb flush when folios get unmapped Date: Mon, 20 May 2024 11:17:31 +0900 Message-Id: <20240520021734.21527-10-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrOLMWRmVeSWpSXmKPExsXC9ZZnoe7JTV5pBgcnCFrMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M zZM+shd82sNYcb0xu4Hxdj9jFyMnh4SAicS0FY/YYOy+mz9ZQGw2AXWJGzd+MoPYIgJmEgdb /7CD2MwCd5kkDvSD1QsLFEo0T3oMNodFQFWi52IvmM0LVP95y19WiJnyEqs3HACbwwkUn7Fq J9h8IQFTiR9HvwLN4QKqec8msaP/GtQRkhIHV9xgmcDIu4CRYRWjUGZeWW5iZo6JXkZlXmaF XnJ+7iZGYPAvq/0TvYPx04XgQ4wCHIxKPLw7HnmmCbEmlhVX5h5ilOBgVhLh3bQFKMSbklhZ lVqUH19UmpNafIhRmoNFSZzX6Ft5ipBAemJJanZqakFqEUyWiYNTqoFxWoCObtDe2wX/pzxw PXA3VEZa4EZ1SP51xZKkiw3z1j5f9j3/an7NLBVxV4n0b3f+TJh+ZtGn8LS3DN/vGvHf4n+w Q+6McbWEENOBnD6x4EfH0rJLt11Q9JzEoW5W2R2e9mG93gJRrbLbZQv5AicetHh3r/HZy6PP g14wmxxcWH4r+fHLFr8QJZbijERDLeai4kQAY8dIKXoCAAA= X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrHLMWRmVeSWpSXmKPExsXC5WfdrHtyk1eawZ13rBZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgytg86SN7wac9jBXXG7MbGG/3M3YxcnJICJhI9N38yQJiswmoS9y48ZMZxBYR MJM42PqHHcRmFrjLJHGgnw3EFhYolGie9Bisl0VAVaLnYi+YzQtU/3nLX1aImfISqzccAJvD CRSfsWon2HwhAVOJH0e/sk1g5FrAyLCKUSQzryw3MTPHVK84O6MyL7NCLzk/dxMjMJSX1f6Z uIPxy2X3Q4wCHIxKPLwbbnumCbEmlhVX5h5ilOBgVhLh3bQFKMSbklhZlVqUH19UmpNafIhR moNFSZzXKzw1QUggPbEkNTs1tSC1CCbLxMEp1cB4/tD/m7PrZOS6xZfduhjf7MHuHNzgsWFV PEPk/AdVPYtrdnRKK+z21fijmBq/J+0Xr/sH5Yqypxddcrol/WJeLCh/LFXYXik68ct67cvr v11/wr+w/Pv7HbJR7vuPP7rPeMK4pVtA3NA0RDcp+8COtVZ911+98Kn4vdNE2ylrpcnWpcsP +dxRYinOSDTUYi4qTgQAoksxS2ECAAA= X-CFilter-Loop: Reflected X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9512210000B X-Stat-Signature: 5o51jycd1r16fuj59r5qxm64s3qwkz7q X-HE-Tag: 1716171472-304713 X-HE-Meta: U2FsdGVkX19D6+V32MmtITtO7wKSQNVPMfKyAAV2Ig2IZ+cAk5sSV91iertDT+r2g2OnW0kKacSfOu9QJ7bQwiRnZIjo4W7HrumxALvYrastNjdvc82RwRNMgFCENNYuntf4qOWfrbo1YrRneSL3gYh8aQksaHNltKz74t4nq/a2cVmw+RzRrQys3/VzCLyMCbleUqeEG62n7D5qU0LwXH2cEP9HiWOpvrV9AX2NXWsbaJP+OwzrSJuahUQRvnvz+PeVE6P42ubHjJVvDPFldTVR9FnNMWiKVBoObY9tRu/y9S6FYmTD2gkq9Dq6ggLQg2sQBFseBBNFbHHEKVYVKCU0nw/9Y3LUkq8s69FrT85EixPKqXMDC7h1Icrwrd+WBHOlScuj2chIXv5T+RM19v3iaFPR0t6B4oZLCukI0/snWzH0bma+ucSyWvldjqXl+1+CAmpOwcJwCvre/RkjczIoTjs33GddD/BVftothFVJ+zLI/X37W5YJjFDSR6YELmGcjIjrw4XMPRK7n7TKgr68aYmPUOoou7yAI18AM3emPI5VV/4eWuO/Gh+jgh+bW0lwwaFZLrnSarlLKFOFuuMwT4FhUCY8zUqVd8DLd/VRG6un9MPUJ3KtN09Zpuo0tap+5L/OsPGzrHLmCvx8QbzwqZsY0/a6RHfTBG3dam38PwwFDF3AeOGBOil+gkoWnQivRYGYrVPzUO4qB66sp/a4oD5rosXXWYbel71xxmvGnegyT7TmO9TurylXCE85BBk5nbmSFLiQc0MbYdOT9lD96min8UQYXWTk3viy3jNt471MYdCe+UjnBmxfRisuKe7vNCKWGy8j4QYQ63szDo5+VQkb6htSk1hh6BHQSziU1vs2gpPH0WSplvYyoqtcTuelyi+PVcBjSGtHV7PWdnCoCFEuJ1e44cdH2tm4Kiq/gkDRvGaLST5QsrCk03Je3QKGbJhtXCdgRg7jHvs 8Pw== 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: A new mechanism, LUF(Lazy Unmap Flush), defers tlb flush until folios that have been unmapped and freed, eventually get allocated again. It's safe for folios that had been mapped read-only and were unmapped, since the contents of the folios don't change while staying in pcp or buddy so we can still read the data through the stale tlb entries. tlb flush can be defered when folios get unmapped as long as it guarantees to perform tlb flush needed, before the folios actually become used, of course, only if all the corresponding ptes don't have write permission. Otherwise, the system will get messed up. To achieve that: 1. For the folios that map only to non-writable tlb entries, prevent tlb flush during unmapping but perform it just before the folios actually become used, out of buddy or pcp. 2. When any non-writable ptes change to writable e.g. through fault handler, give up luf mechanism and perform tlb flush required right away. 3. When a writable mapping is created e.g. through mmap(), give up luf mechanism and perform tlb flush required right away. No matter what type of workload is used for performance evaluation, the result would be positive thanks to the unconditional reduction of tlb flushes, tlb misses and interrupts. For the test, I picked up one of the most popular and heavy workload, llama.cpp that is a LLM(Large Language Model) inference engine. The result would depend on memory latency and how often reclaim runs, which implies tlb miss overhead and how many times unmapping happens. In my system, the result shows: 1. tlb flushes are reduced about 95%. 2. tlb misses(itlb) are reduced about 80%. 3. tlb misses(dtlb store) are reduced about 57%. 4. tlb misses(dtlb load) are reduced about 24%. 5. tlb shootdown interrupts are reduced about 95%. 6. The test program runtime is reduced about 5%. The test environment and the result is like: Machine: bare metal, x86_64, Intel(R) Xeon(R) Gold 6430 CPU: 1 socket 64 core with hyper thread on Numa: 2 nodes (64 CPUs DRAM 42GB, no CPUs CXL expander 98GB) Config: swap off, numa balancing tiering on, demotion enabled The test set: llama.cpp/main -m $(70G_model1) -p "who are you?" -s 1 -t 15 -n 20 & llama.cpp/main -m $(70G_model2) -p "who are you?" -s 1 -t 15 -n 20 & llama.cpp/main -m $(70G_model3) -p "who are you?" -s 1 -t 15 -n 20 & wait where -t: nr of threads, -s: seed used to make the runtime stable, -n: nr of tokens that determines the runtime, -p: prompt to ask, -m: LLM model to use. Run the test set 10 times successively with caches dropped every run via 'echo 3 > /proc/sys/vm/drop_caches'. Each inference prints its runtime at the end of each. 1. Runtime from the output of llama.cpp: BEFORE ------ llama_print_timings: total time = 1002461.95 ms / 24 tokens llama_print_timings: total time = 1044978.38 ms / 24 tokens llama_print_timings: total time = 1000653.09 ms / 24 tokens llama_print_timings: total time = 1047104.80 ms / 24 tokens llama_print_timings: total time = 1069430.36 ms / 24 tokens llama_print_timings: total time = 1068201.16 ms / 24 tokens llama_print_timings: total time = 1078092.59 ms / 24 tokens llama_print_timings: total time = 1073200.45 ms / 24 tokens llama_print_timings: total time = 1067136.00 ms / 24 tokens llama_print_timings: total time = 1076442.56 ms / 24 tokens llama_print_timings: total time = 1004142.64 ms / 24 tokens llama_print_timings: total time = 1042942.65 ms / 24 tokens llama_print_timings: total time = 999933.76 ms / 24 tokens llama_print_timings: total time = 1046548.83 ms / 24 tokens llama_print_timings: total time = 1068671.48 ms / 24 tokens llama_print_timings: total time = 1068285.76 ms / 24 tokens llama_print_timings: total time = 1077789.63 ms / 24 tokens llama_print_timings: total time = 1071558.93 ms / 24 tokens llama_print_timings: total time = 1066181.55 ms / 24 tokens llama_print_timings: total time = 1076767.53 ms / 24 tokens llama_print_timings: total time = 1004065.63 ms / 24 tokens llama_print_timings: total time = 1044522.13 ms / 24 tokens llama_print_timings: total time = 999725.33 ms / 24 tokens llama_print_timings: total time = 1047510.77 ms / 24 tokens llama_print_timings: total time = 1068010.27 ms / 24 tokens llama_print_timings: total time = 1068999.31 ms / 24 tokens llama_print_timings: total time = 1077648.05 ms / 24 tokens llama_print_timings: total time = 1071378.96 ms / 24 tokens llama_print_timings: total time = 1066326.32 ms / 24 tokens llama_print_timings: total time = 1077088.92 ms / 24 tokens AFTER ----- llama_print_timings: total time = 988522.03 ms / 24 tokens llama_print_timings: total time = 997204.52 ms / 24 tokens llama_print_timings: total time = 996605.86 ms / 24 tokens llama_print_timings: total time = 991985.50 ms / 24 tokens llama_print_timings: total time = 1035143.31 ms / 24 tokens llama_print_timings: total time = 993660.18 ms / 24 tokens llama_print_timings: total time = 983082.14 ms / 24 tokens llama_print_timings: total time = 990431.36 ms / 24 tokens llama_print_timings: total time = 992707.09 ms / 24 tokens llama_print_timings: total time = 992673.27 ms / 24 tokens llama_print_timings: total time = 989285.43 ms / 24 tokens llama_print_timings: total time = 996710.06 ms / 24 tokens llama_print_timings: total time = 996534.64 ms / 24 tokens llama_print_timings: total time = 991344.17 ms / 24 tokens llama_print_timings: total time = 1035210.84 ms / 24 tokens llama_print_timings: total time = 994714.13 ms / 24 tokens llama_print_timings: total time = 984184.15 ms / 24 tokens llama_print_timings: total time = 990909.45 ms / 24 tokens llama_print_timings: total time = 991881.48 ms / 24 tokens llama_print_timings: total time = 993918.03 ms / 24 tokens llama_print_timings: total time = 990061.34 ms / 24 tokens llama_print_timings: total time = 998076.69 ms / 24 tokens llama_print_timings: total time = 997082.59 ms / 24 tokens llama_print_timings: total time = 990677.58 ms / 24 tokens llama_print_timings: total time = 1036054.94 ms / 24 tokens llama_print_timings: total time = 994125.93 ms / 24 tokens llama_print_timings: total time = 982467.01 ms / 24 tokens llama_print_timings: total time = 990191.60 ms / 24 tokens llama_print_timings: total time = 993319.24 ms / 24 tokens llama_print_timings: total time = 992540.57 ms / 24 tokens 2. tlb shootdowns from 'cat /proc/interrupts': BEFORE ------ TLB: 125553646 141418810 161932620 176853972 186655697 190399283 192143823 196414038 192872439 193313658 193395617 192521416 190788161 195067598 198016061 193607347 194293972 190786732 191545637 194856822 191801931 189634535 190399803 196365922 195268398 190115840 188050050 193194908 195317617 190820190 190164820 185556071 226797214 229592631 216112464 209909495 205575979 205950252 204948111 197999795 198892232 205287952 199344631 195015158 195869844 198858745 195692876 200961904 203463252 205921722 199850838 206145986 199613202 199961345 200129577 203020521 207873649 203697671 197093386 204243803 205993323 200934664 204193128 194435376 TLB shootdowns AFTER ----- TLB: 5648092 6610142 7032849 7882308 8088518 8352310 8656536 8705136 8647426 8905583 8985408 8704522 8884344 9026261 8929974 8869066 8877575 8810096 8770984 8754503 8801694 8865925 8787524 8656432 8755912 8682034 8773935 8832925 8797997 8515777 8481240 8891258 10595243 10285973 9756935 9573681 9398968 9069244 9242984 8899009 9310690 9029095 9069758 9105825 9092703 9270202 9460287 9258546 9180415 9232723 9270611 9175020 9490420 9360316 9420818 9057663 9525631 9310152 9152242 8654483 9181804 9050847 8919916 8883856 TLB shootdowns 3. tlb numbers from 'perf stat' per test set: BEFORE ------ 3163679332 dTLB-load-misses 2017751856 dTLB-store-misses 327092903 iTLB-load-misses 1357543886 tlb:tlb_flush AFTER ----- 2394694609 dTLB-load-misses 861144167 dTLB-store-misses 64055579 iTLB-load-misses 69175002 tlb:tlb_flush Signed-off-by: Byungchul Park --- include/linux/sched.h | 9 ++ mm/internal.h | 43 +++++- mm/memory.c | 8 ++ mm/mmap.c | 8 ++ mm/rmap.c | 308 +++++++++++++++++++++++++++++++++++++++++- 5 files changed, 366 insertions(+), 10 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 0915390b1b5e..6f83703ec284 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1340,8 +1340,17 @@ struct task_struct { struct tlbflush_unmap_batch tlb_ubc; struct tlbflush_unmap_batch tlb_ubc_ro; + struct tlbflush_unmap_batch tlb_ubc_luf; unsigned short int ugen; +#if defined(CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH) + /* + * whether all the mappings of a folio during unmap are read-only + * so that luf can work on the folio + */ + bool can_luf; +#endif + /* Cache last used pipe for splice(): */ struct pipe_inode_info *splice_pipe; diff --git a/mm/internal.h b/mm/internal.h index 805f0e6ecab4..2a44194f5d39 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1517,6 +1517,38 @@ void workingset_update_node(struct xa_node *node); extern struct list_lru shadow_nodes; #if defined(CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH) +unsigned short int try_to_unmap_luf(void); +void check_luf_flush(unsigned short int ugen); +void luf_flush(void); + +/* + * Reset the indicator indicating there are no writable mappings at the + * beginning of every rmap traverse for unmap. luf can work only when + * all the mappings are read-only. + */ +static inline void can_luf_init(void) +{ + current->can_luf = true; +} + +/* + * Mark the folio is not applicable to luf once it found a writble or + * dirty pte during rmap traverse for unmap. + */ +static inline void can_luf_fail(void) +{ + current->can_luf = false; +} + +/* + * Check if all the mappings are read-only and read-only mappings even + * exist. + */ +static inline bool can_luf_test(void) +{ + return current->can_luf && current->tlb_ubc_ro.flush_required; +} + static inline unsigned short int ugen_latest(unsigned short int a, unsigned short int b) { if (!a || !b) @@ -1546,10 +1578,7 @@ static inline unsigned short int hand_over_task_ugen(void) static inline void check_flush_task_ugen(void) { - /* - * XXX: luf mechanism will handle this. For now, do nothing but - * reset current's ugen to finalize this turn. - */ + check_luf_flush(current->ugen); current->ugen = 0; } @@ -1578,6 +1607,12 @@ static inline bool can_luf_folio(struct folio *f) return can_luf; } #else /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */ +static inline unsigned short int try_to_unmap_luf(void) { return 0; } +static inline void check_luf_flush(unsigned short int ugen) {} +static inline void luf_flush(void) {} +static inline void can_luf_init(void) {} +static inline void can_luf_fail(void) {} +static inline bool can_luf_test(void) { return false; } static inline unsigned short int ugen_latest(unsigned short int a, unsigned short int b) { return 0; } static inline void update_task_ugen(unsigned short int ugen) {} static inline unsigned short int hand_over_task_ugen(void) { return 0; } diff --git a/mm/memory.c b/mm/memory.c index 33d87b64d15d..f218c275d307 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3617,6 +3617,14 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) if (vmf->page) folio = page_folio(vmf->page); + /* + * The folio may or may not be one that is under luf's control + * and might be about to change its permission to writable. + * Conservatively give up deferring tlb flush just in case. + */ + if (folio) + luf_flush(); + /* * Shared mapping: we are guaranteed to have VM_WRITE and * FAULT_FLAG_WRITE set at this point. diff --git a/mm/mmap.c b/mm/mmap.c index 47363e7f7ea2..3b3bece4b079 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1271,6 +1271,14 @@ unsigned long do_mmap(struct file *file, unsigned long addr, pkey = 0; } + /* + * This mmap may or may not be mapping to ones that is under + * luf's control. However, conservatively give up deferring tlb + * flush just in case. + */ + if (prot & PROT_WRITE) + luf_flush(); + /* Do simple checking here so the lower-level routines won't have * to. we assume access permissions have been handled by the open * of the memory object, so we don't do any here. diff --git a/mm/rmap.c b/mm/rmap.c index 328b5e2217e6..e42783c02114 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -635,6 +635,270 @@ struct anon_vma *folio_lock_anon_vma_read(struct folio *folio, } #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH +static struct tlbflush_unmap_batch luf_ubc; +static DEFINE_SPINLOCK(luf_lock); + +/* + * Don't be zero to distinguish from invalid ugen, 0. + */ +static unsigned short int ugen_next(unsigned short int a) +{ + return a + 1 ?: a + 2; +} + +static bool ugen_before(unsigned short int a, unsigned short int b) +{ + return (short int)(a - b) < 0; +} + +/* + * Need to synchronize between tlb flush and managing pending CPUs in + * luf_ubc. Take a look at the following scenario, where CPU0 is in + * try_to_unmap_flush() and CPU1 is in migrate_pages_batch(): + * + * CPU0 CPU1 + * ---- ---- + * tlb flush + * unmap folios (needing tlb flush) + * add pending CPUs to luf_ubc + * <-- not performed tlb flush needed by + * the unmap above yet but the request + * will be cleared by CPU0 shortly. bug! + * clear the CPUs from luf_ubc + * + * The pending CPUs added in CPU1 should not be cleared from luf_ubc + * in CPU0 because the tlb flush for luf_ubc added in CPU1 has not + * been performed this turn. To avoid this, using 'on_flushing' + * variable, prevent adding pending CPUs to luf_ubc and give up luf + * mechanism if someone is in the middle of tlb flush, like: + * + * CPU0 CPU1 + * ---- ---- + * on_flushing++ + * tlb flush + * unmap folios (needing tlb flush) + * if on_flushing == 0: + * add pending CPUs to luf_ubc + * else: <-- hit + * give up luf mechanism + * clear the CPUs from luf_ubc + * on_flushing-- + * + * Only the following case would be allowed for luf mechanism to work: + * + * CPU0 CPU1 + * ---- ---- + * unmap folios (needing tlb flush) + * if on_flushing == 0: <-- hit + * add pending CPUs to luf_ubc + * else: + * give up luf mechanism + * on_flushing++ + * tlb flush + * clear the CPUs from luf_ubc + * on_flushing-- + */ +static int on_flushing; + +/* + * When more than one thread enter check_luf_flush() at the same + * time, each should wait for the request on progress to be done to + * avoid the following scenario, where the both CPUs are in + * check_luf_flush(): + * + * CPU0 CPU1 + * ---- ---- + * if !luf_ubc.flush_required: + * return + * luf_ubc.flush_required = false + * if !luf_ubc.flush_requied: <-- hit + * return <-- not performed tlb flush + * needed yet but return. bug! + * luf_ubc.flush_required = false + * try_to_unmap_flush() + * finalize + * try_to_unmap_flush() <-- performs tlb flush needed + * finalize + * + * So it should be handled: + * + * CPU0 CPU1 + * ---- ---- + * atomically execute { + * if luf_on_flushing: + * wait for the completion + * return + * if !luf_ubc.flush_required: + * return + * luf_ubc.flush_required = false + * luf_on_flushing = true + * } + * atomically execute { + * if luf_on_flushing: <-- hit + * wait for the completion + * return <-- tlb flush needed is done + * if !luf_ubc.flush_requied: + * return + * luf_ubc.flush_required = false + * luf_on_flushing = true + * } + * + * try_to_unmap_flush() + * luf_on_flushing = false + * finalize + * try_to_unmap_flush() <-- performs tlb flush needed + * luf_on_flushing = false + * finalize + */ +static bool luf_on_flushing; + +/* + * Generation number for the current request of deferred tlb flush. + */ +static unsigned short int luf_gen; + +/* + * Generation number for the next request. + */ +static unsigned short int luf_gen_next = 1; + +/* + * Generation number for the latest request handled. + */ +static unsigned short int luf_gen_done; + +unsigned short int try_to_unmap_luf(void) +{ + struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc_luf = ¤t->tlb_ubc_luf; + unsigned long flags; + unsigned short int ugen; + + if (!spin_trylock_irqsave(&luf_lock, flags)) { + /* + * Give up luf mechanism. Just let tlb flush needed + * handled by try_to_unmap_flush() at the caller side. + */ + fold_ubc(tlb_ubc, tlb_ubc_luf); + return 0; + } + + if (on_flushing || luf_on_flushing) { + spin_unlock_irqrestore(&luf_lock, flags); + + /* + * Give up luf mechanism. Just let tlb flush needed + * handled by try_to_unmap_flush() at the caller side. + */ + fold_ubc(tlb_ubc, tlb_ubc_luf); + return 0; + } + + fold_ubc(&luf_ubc, tlb_ubc_luf); + ugen = luf_gen = luf_gen_next; + spin_unlock_irqrestore(&luf_lock, flags); + + return ugen; +} + +static void rmap_flush_start(void) +{ + unsigned long flags; + + spin_lock_irqsave(&luf_lock, flags); + on_flushing++; + spin_unlock_irqrestore(&luf_lock, flags); +} + +static void rmap_flush_end(struct tlbflush_unmap_batch *batch) +{ + unsigned long flags; + + spin_lock_irqsave(&luf_lock, flags); + if (arch_tlbbatch_done(&luf_ubc.arch, &batch->arch)) { + luf_ubc.flush_required = false; + luf_ubc.writable = false; + } + on_flushing--; + spin_unlock_irqrestore(&luf_lock, flags); +} + +/* + * It must be guaranteed to have completed tlb flush requested on return. + */ +void check_luf_flush(unsigned short int ugen) +{ + struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; + unsigned long flags; + + /* + * Nothing has been requested. We are done. + */ + if (!ugen) + return; +retry: + /* + * We can see a larger value than or equal to luf_gen_done, + * which means the tlb flush we need has been done. + */ + if (!ugen_before(READ_ONCE(luf_gen_done), ugen)) + return; + + spin_lock_irqsave(&luf_lock, flags); + + /* + * With luf_lock held, we might read luf_gen_done updated. + */ + if (ugen_next(luf_gen_done) != ugen) { + spin_unlock_irqrestore(&luf_lock, flags); + return; + } + + /* + * Others are already working for us. + */ + if (luf_on_flushing) { + spin_unlock_irqrestore(&luf_lock, flags); + goto retry; + } + + if (!luf_ubc.flush_required) { + spin_unlock_irqrestore(&luf_lock, flags); + return; + } + + fold_ubc(tlb_ubc, &luf_ubc); + luf_gen_next = ugen_next(luf_gen); + luf_on_flushing = true; + spin_unlock_irqrestore(&luf_lock, flags); + + try_to_unmap_flush(); + + spin_lock_irqsave(&luf_lock, flags); + luf_on_flushing = false; + + /* + * luf_gen_done can be read by another with luf_lock not + * held so use WRITE_ONCE() to prevent tearing. + */ + WRITE_ONCE(luf_gen_done, ugen); + spin_unlock_irqrestore(&luf_lock, flags); +} + +void luf_flush(void) +{ + unsigned long flags; + unsigned short int ugen; + + /* + * Obtain the latest ugen number. + */ + spin_lock_irqsave(&luf_lock, flags); + ugen = luf_gen; + spin_unlock_irqrestore(&luf_lock, flags); + + check_luf_flush(ugen); +} void fold_ubc(struct tlbflush_unmap_batch *dst, struct tlbflush_unmap_batch *src) @@ -666,13 +930,15 @@ void fold_ubc(struct tlbflush_unmap_batch *dst, void try_to_unmap_flush(void) { struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; - struct tlbflush_unmap_batch *tlb_ubc_ro = ¤t->tlb_ubc_ro; + struct tlbflush_unmap_batch *tlb_ubc_luf = ¤t->tlb_ubc_luf; - fold_ubc(tlb_ubc, tlb_ubc_ro); + fold_ubc(tlb_ubc, tlb_ubc_luf); if (!tlb_ubc->flush_required) return; + rmap_flush_start(); arch_tlbbatch_flush(&tlb_ubc->arch); + rmap_flush_end(tlb_ubc); arch_tlbbatch_clear(&tlb_ubc->arch); tlb_ubc->flush_required = false; tlb_ubc->writable = false; @@ -682,9 +948,9 @@ void try_to_unmap_flush(void) void try_to_unmap_flush_dirty(void) { struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; - struct tlbflush_unmap_batch *tlb_ubc_ro = ¤t->tlb_ubc_ro; + struct tlbflush_unmap_batch *tlb_ubc_luf = ¤t->tlb_ubc_luf; - if (tlb_ubc->writable || tlb_ubc_ro->writable) + if (tlb_ubc->writable || tlb_ubc_luf->writable) try_to_unmap_flush(); } @@ -708,9 +974,15 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, if (!pte_accessible(mm, pteval)) return; - if (pte_write(pteval)) + if (pte_write(pteval)) { tlb_ubc = ¤t->tlb_ubc; - else + + /* + * luf cannot work with the folio once it found a + * writable or dirty mapping on it. + */ + can_luf_fail(); + } else tlb_ubc = ¤t->tlb_ubc_ro; arch_tlbbatch_add_pending(&tlb_ubc->arch, mm, uaddr); @@ -1976,11 +2248,23 @@ void try_to_unmap(struct folio *folio, enum ttu_flags flags) .done = folio_not_mapped, .anon_lock = folio_lock_anon_vma_read, }; + struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc_ro = ¤t->tlb_ubc_ro; + struct tlbflush_unmap_batch *tlb_ubc_luf = ¤t->tlb_ubc_luf; + bool can_luf; + + can_luf_init(); if (flags & TTU_RMAP_LOCKED) rmap_walk_locked(folio, &rwc); else rmap_walk(folio, &rwc); + + can_luf = can_luf_folio(folio) && can_luf_test(); + if (can_luf) + fold_ubc(tlb_ubc_luf, tlb_ubc_ro); + else + fold_ubc(tlb_ubc, tlb_ubc_ro); } /* @@ -2325,6 +2609,10 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) .done = folio_not_mapped, .anon_lock = folio_lock_anon_vma_read, }; + struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; + struct tlbflush_unmap_batch *tlb_ubc_ro = ¤t->tlb_ubc_ro; + struct tlbflush_unmap_batch *tlb_ubc_luf = ¤t->tlb_ubc_luf; + bool can_luf; /* * Migration always ignores mlock and only supports TTU_RMAP_LOCKED and @@ -2349,10 +2637,18 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) if (!folio_test_ksm(folio) && folio_test_anon(folio)) rwc.invalid_vma = invalid_migration_vma; + can_luf_init(); + if (flags & TTU_RMAP_LOCKED) rmap_walk_locked(folio, &rwc); else rmap_walk(folio, &rwc); + + can_luf = can_luf_folio(folio) && can_luf_test(); + if (can_luf) + fold_ubc(tlb_ubc_luf, tlb_ubc_ro); + else + fold_ubc(tlb_ubc, tlb_ubc_ro); } #ifdef CONFIG_DEVICE_PRIVATE From patchwork Mon May 20 02:17:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13668005 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 C3994C25B7A for ; Mon, 20 May 2024 02:18:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A61ED6B009B; Sun, 19 May 2024 22:17:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 99F796B00A1; Sun, 19 May 2024 22:17:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 472686B009D; Sun, 19 May 2024 22:17:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id F0F776B009C for ; Sun, 19 May 2024 22:17:54 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B9EBDA1D8D for ; Mon, 20 May 2024 02:17:54 +0000 (UTC) X-FDA: 82137163668.22.F231733 Received: from invmail4.hynix.com (exvmail4.hynix.com [166.125.252.92]) by imf23.hostedemail.com (Postfix) with ESMTP id EAFDA140005 for ; Mon, 20 May 2024 02:17:52 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716171473; 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:in-reply-to: references:references; bh=u2mjUnFQvLeiyt0EIbd9RAlKFlEDSFVl1qIOuPU7u6k=; b=DyG1fgKu0sJbtpR37Eit2tTjQEmwrf6/Mm1TZ+3qxAw/Gr5oHSmAtFZeEVD803TPRIMSp3 VjkUSlH1WHGUQuxF5k/OdX2b7Ai1yCc54S3HbvFGEo7Mb++ws7nqZ8IVuseLVuuDIwwqpX LsrrgowCHeo9DZDfTk+zftJvwZBj94A= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171473; a=rsa-sha256; cv=none; b=aKTGGIfIOKCmXDEPliJc5LJkyDXb6QxVRmxoD27pmrIH9bN6MEOeCRk9y6525emo8EK//v +0/WXdXaJ54XF53lUyqbU+ECm3KG6F9+GvfjFlTNCuSRSxNQe8Q3NXyKVPlq37LWVoAhHv gw8NTOSCVb+fuJfZOHFqzeAT1gHIogA= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none X-AuditID: a67dfc5b-d6dff70000001748-c7-664ab2c9c7c5 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 10/12] mm: separate move/undo parts from migrate_pages_batch() Date: Mon, 20 May 2024 11:17:32 +0900 Message-Id: <20240520021734.21527-11-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrGLMWRmVeSWpSXmKPExsXC9ZZnke7JTV5pBvcWi1jMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M LZOWsxT81K64fW81awPjdOUuRk4OCQETiaNT9jPB2CsPTAOz2QTUJW7c+MkMYosImEkcbP3D DmIzC9xlkjjQzwZiCwtESqxvfMoIYrMIqEpse7MDrJ4XqP57+042iJnyEqs3HACLcwLFZ6za yQJiCwmYSvw4+hWohguo5j2bxJqv11ggGiQlDq64wTKBkXcBI8MqRqHMvLLcxMwcE72MyrzM Cr3k/NxNjMDQX1b7J3oH46cLwYcYBTgYlXh4dzzyTBNiTSwrrsw9xCjBwawkwrtpC1CINyWx siq1KD++qDQntfgQozQHi5I4r9G38hQhgfTEktTs1NSC1CKYLBMHp1QDo9i6TfPfGgUuZOud e7hv9vdJLslOl77VWnNNDk032Dln7v0ue3ZDt5p9occ+b2sIUjv+4MUBNzaZ6a+3PT25/m5M TQ2fJ4/kjB1yBg7tvMFCxx/WvWBhf3x2o8ebyssbPScZCNyKm/s4XeEsE0f42aN3FV/eicrY aaH05nD8LMmqjuv9PEt3P1ViKc5INNRiLipOBACLhzNzeQIAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrLLMWRmVeSWpSXmKPExsXC5WfdrHtyk1eawfXlHBZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgytgyaTlLwU/titv3VrM2ME5X7mLk5JAQMJFYeWAaE4jNJqAucePGT2YQW0TA TOJg6x92EJtZ4C6TxIF+NhBbWCBSYn3jU0YQm0VAVWLbmx1g9bxA9d/bd7JBzJSXWL3hAFic Eyg+Y9VOFhBbSMBU4sfRr2wTGLkWMDKsYhTJzCvLTczMMdUrzs6ozMus0EvOz93ECAzkZbV/ Ju5g/HLZ/RCjAAejEg/vhtueaUKsiWXFlbmHGCU4mJVEeDdtAQrxpiRWVqUW5ccXleakFh9i lOZgURLn9QpPTRASSE8sSc1OTS1ILYLJMnFwSjUwXim/sPG8woXUZZpls36/s7ujpLNTXU5o 2sNHG1XOZHDv1td7ocMQWGeYsTvx8vVVQSIT2VplZ3BM1H2fOFG2w+o9I/OznvPnjV9s2PTC 4FZXUGp844F8nVIv+8anVvtsl7En3EoxaPLO271g7p4Lsu6i3YLaO9qWJM3ral3VrVA/8c9j iQg2JZbijERDLeai4kQAotwRFmACAAA= X-CFilter-Loop: Reflected X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: EAFDA140005 X-Rspam-User: X-Stat-Signature: x9z9jfaydhx3u3uaiw4fqhstmwirboxs X-HE-Tag: 1716171472-612644 X-HE-Meta: U2FsdGVkX19FB+kTrEmUX6qojdPtuMkbPd5eTI/bHFnHC/MzQcP/owOCQxH7nh9C6OoKnENJgBsaBb5yTVfACMkVTYrjWMa061EZdJpCbrLb5V5T2uHv5MOWzekjfw6S0D+lHwivnFqTsaDVM5FFtd/li4aGgDIDghadiUibS6t0ulbHSSx9z21nCXR4QnkNv39MU5o5X2suG+vTgAs5uMUZZwqVXkYsqPV/LncIbjV836cOo0KrVwQtPuummoD/n4wpwdB7MZ91PUSYpJ26txJaESh2YY2GW4NmbtqvnlhGGn40MoU+89EoIoq+W5KgTABB78ii7Bd3ODfe4a1czfLDzNVwAzFOK3w/yV1LxxAKMRblO7G5AKoxYW0zg5AOGRe4+ClSiEm5PyLE+NCGDZCW3khWSLOqNkK9P6S1w86jjDtOU8Cw1dvWYgJzrsUSZWVRrvbDpiSC/wFvHS1Rb7EGYelQGg4ywzY2iTg5tQyyNjEahg5jAmkVKdFamRB8pCH43L7apGKWXGJMGIZtWVnDzH4zxtcM0bGLHYleT50IUnjiuDRhh/tj/wyuPJziFESc49/1RxFdazxH+PsrUf16LL3w5zsowkXlbXOp4cxByRkttf7ul922mVb1ZUCcWL0/ANdVfGloPcROZyb6bRqZqoQv8IbWJXlx+oZ3qD4u1Ke6UACjURwyUkwzPMLNW1jOThsPL/nyvLJZthpHl/aKqadFbPrkE0s51tpflUoJgLHzIQ5AypNdt3yvhEWpsb8rdWl2pYJMk15iHQGkjTqRK9y2y73FCbrdIaSNkz/DQpU8UWJLa5hRA2zMdFwe8WMS+5v1StwpmucIBtJbzG/dz4rQ9sn0PKO3zQumNuZPgtAcYwr7NDCxIbpJ+UyNOyunId8vOZcQ72i3okj029PO9sji3BmfkeVNFMqJ5/cXTLMbxT/GSKnWJJLpGppSDMSojnum0KGd51i0h5N VT9E0Kw6 T5Ddua33GTH6g0VAL5/3kOTM6atKN4fzHXQED 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: Functionally, no change. This is a preparation for luf mechanism that requires to use separated folio lists for its own handling during migration. Refactored migrate_pages_batch() so as to separate move/undo parts from migrate_pages_batch(). Signed-off-by: Byungchul Park --- mm/migrate.c | 134 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 83 insertions(+), 51 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index c7692f303fa7..f9ed7a2b8720 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1609,6 +1609,81 @@ static int migrate_hugetlbs(struct list_head *from, new_folio_t get_new_folio, return nr_failed; } +static void migrate_folios_move(struct list_head *src_folios, + struct list_head *dst_folios, + free_folio_t put_new_folio, unsigned long private, + enum migrate_mode mode, int reason, + struct list_head *ret_folios, + struct migrate_pages_stats *stats, + int *retry, int *thp_retry, int *nr_failed, + int *nr_retry_pages) +{ + struct folio *folio, *folio2, *dst, *dst2; + bool is_thp; + int nr_pages; + int rc; + + dst = list_first_entry(dst_folios, struct folio, lru); + dst2 = list_next_entry(dst, lru); + list_for_each_entry_safe(folio, folio2, src_folios, lru) { + is_thp = folio_test_large(folio) && folio_test_pmd_mappable(folio); + nr_pages = folio_nr_pages(folio); + + cond_resched(); + + rc = migrate_folio_move(put_new_folio, private, + folio, dst, mode, + reason, ret_folios); + /* + * The rules are: + * Success: folio will be freed + * -EAGAIN: stay on the unmap_folios list + * Other errno: put on ret_folios list + */ + switch(rc) { + case -EAGAIN: + *retry += 1; + *thp_retry += is_thp; + *nr_retry_pages += nr_pages; + break; + case MIGRATEPAGE_SUCCESS: + stats->nr_succeeded += nr_pages; + stats->nr_thp_succeeded += is_thp; + break; + default: + *nr_failed += 1; + stats->nr_thp_failed += is_thp; + stats->nr_failed_pages += nr_pages; + break; + } + dst = dst2; + dst2 = list_next_entry(dst, lru); + } +} + +static void migrate_folios_undo(struct list_head *src_folios, + struct list_head *dst_folios, + free_folio_t put_new_folio, unsigned long private, + struct list_head *ret_folios) +{ + struct folio *folio, *folio2, *dst, *dst2; + + dst = list_first_entry(dst_folios, struct folio, lru); + dst2 = list_next_entry(dst, lru); + list_for_each_entry_safe(folio, folio2, src_folios, lru) { + int old_page_state = 0; + struct anon_vma *anon_vma = NULL; + + __migrate_folio_extract(dst, &old_page_state, &anon_vma); + migrate_folio_undo_src(folio, old_page_state & PAGE_WAS_MAPPED, + anon_vma, true, ret_folios); + list_del(&dst->lru); + migrate_folio_undo_dst(dst, true, put_new_folio, private); + dst = dst2; + dst2 = list_next_entry(dst, lru); + } +} + /* * migrate_pages_batch() first unmaps folios in the from list as many as * possible, then move the unmapped folios. @@ -1631,7 +1706,7 @@ static int migrate_pages_batch(struct list_head *from, int pass = 0; bool is_thp = false; bool is_large = false; - struct folio *folio, *folio2, *dst = NULL, *dst2; + struct folio *folio, *folio2, *dst = NULL; int rc, rc_saved = 0, nr_pages; LIST_HEAD(unmap_folios); LIST_HEAD(dst_folios); @@ -1790,42 +1865,11 @@ static int migrate_pages_batch(struct list_head *from, thp_retry = 0; nr_retry_pages = 0; - dst = list_first_entry(&dst_folios, struct folio, lru); - dst2 = list_next_entry(dst, lru); - list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) { - is_thp = folio_test_large(folio) && folio_test_pmd_mappable(folio); - nr_pages = folio_nr_pages(folio); - - cond_resched(); - - rc = migrate_folio_move(put_new_folio, private, - folio, dst, mode, - reason, ret_folios); - /* - * The rules are: - * Success: folio will be freed - * -EAGAIN: stay on the unmap_folios list - * Other errno: put on ret_folios list - */ - switch(rc) { - case -EAGAIN: - retry++; - thp_retry += is_thp; - nr_retry_pages += nr_pages; - break; - case MIGRATEPAGE_SUCCESS: - stats->nr_succeeded += nr_pages; - stats->nr_thp_succeeded += is_thp; - break; - default: - nr_failed++; - stats->nr_thp_failed += is_thp; - stats->nr_failed_pages += nr_pages; - break; - } - dst = dst2; - dst2 = list_next_entry(dst, lru); - } + /* Move the unmapped folios */ + migrate_folios_move(&unmap_folios, &dst_folios, + put_new_folio, private, mode, reason, + ret_folios, stats, &retry, &thp_retry, + &nr_failed, &nr_retry_pages); } nr_failed += retry; stats->nr_thp_failed += thp_retry; @@ -1834,20 +1878,8 @@ static int migrate_pages_batch(struct list_head *from, rc = rc_saved ? : nr_failed; out: /* Cleanup remaining folios */ - dst = list_first_entry(&dst_folios, struct folio, lru); - dst2 = list_next_entry(dst, lru); - list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) { - int old_page_state = 0; - struct anon_vma *anon_vma = NULL; - - __migrate_folio_extract(dst, &old_page_state, &anon_vma); - migrate_folio_undo_src(folio, old_page_state & PAGE_WAS_MAPPED, - anon_vma, true, ret_folios); - list_del(&dst->lru); - migrate_folio_undo_dst(dst, true, put_new_folio, private); - dst = dst2; - dst2 = list_next_entry(dst, lru); - } + migrate_folios_undo(&unmap_folios, &dst_folios, + put_new_folio, private, ret_folios); return rc; } From patchwork Mon May 20 02:17:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13668006 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 55CC1C25B75 for ; Mon, 20 May 2024 02:18:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6C686B009C; Sun, 19 May 2024 22:17:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C61966B009F; Sun, 19 May 2024 22:17:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 68D406B009C; Sun, 19 May 2024 22:17:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 431E96B00A1 for ; Sun, 19 May 2024 22:17:55 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F39EEC0AE8 for ; Mon, 20 May 2024 02:17:54 +0000 (UTC) X-FDA: 82137163668.17.AC7B76A Received: from invmail4.hynix.com (exvmail4.skhynix.com [166.125.252.92]) by imf21.hostedemail.com (Postfix) with ESMTP id 23F981C0008 for ; Mon, 20 May 2024 02:17:52 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf21.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=1716171473; 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:in-reply-to: references:references; bh=ECjT2gPRLy0NnXoAXwZXSHAZTvCcvFYKGTfTRXwtQs4=; b=RQUNnxdFLKcPDtS2e9yw+5DTWwp7GWgoQDAkfH/L6oUTZAGbMXzY3qtBDYcgeD/Dph/cBK c1mJpLO0CXV84btfhQMSuzBKvzbbkZ9YmhB2pw3u/hgVI4rYxbxBG/psYSYl0T6jqKBLI4 bbj/KU6u27SQbOChQlsyqYO7q8gI+B8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf21.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=1716171473; a=rsa-sha256; cv=none; b=2LtAZjac+6UVNyRDll8KSH+dVU3K+H1a4AoLcLUy5dAZjQ6Xu6MbWiTEyTbOgBzblf0CDh lwJFJ7g0KdINUyEanlk0O19k/SZLBBXKHViPU2lNpm7z6qNEbbkkSq8mz1J+2t00OLP+nU 8DRn7BmpNH84HjLPUU6A9+WR2v6ezAQ= X-AuditID: a67dfc5b-d6dff70000001748-cc-664ab2c941db 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 11/12] mm, migrate: apply luf mechanism to unmapping during migration Date: Mon, 20 May 2024 11:17:33 +0900 Message-Id: <20240520021734.21527-12-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrKLMWRmVeSWpSXmKPExsXC9ZZnoe7JTV5pBs/miVnMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M t/susRQ0u1WsOr+YvYFxoUUXIyeHhICJxPI/e5lg7DmX9jCC2GwC6hI3bvxkBrFFBMwkDrb+ YQexmQXuMkkc6GcDsYUFEiRebNwLVM/BwSKgKvHnViVImBeofOP1H2wQI+UlVm84ADaGEyg+ Y9VOFhBbSMBU4sfRr0A1XEA1n9kkjs14DnWDpMTBFTdYJjDyLmBkWMUolJlXlpuYmWOil1GZ l1mhl5yfu4kRGPjLav9E72D8dCH4EKMAB6MSD++OR55pQqyJZcWVuYcYJTiYlUR4N20BCvGm JFZWpRblxxeV5qQWH2KU5mBREuc1+laeIiSQnliSmp2aWpBaBJNl4uCUamCcL/tY8NmdXAfv 13+Ob9ipebXn+sSpF7jXW+3U+d04oaF6al7F68d3cjzYFt522bOka8U5rYU8q8pf3RSaoVW2 hX9lIUe/ifEfnw3Hu7onftL5d8L+/aLvS2vWL1nCff4+K8Pu3TaX4uP7ExfYXk9ZG9BjHnT9 lg5rQkqO1ZWFQV/Pp/up31hmocRSnJFoqMVcVJwIAMFQjwF4AgAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrDLMWRmVeSWpSXmKPExsXC5WfdrHtyk1eawaQ13BZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgyni77xJLQbNbxarzi9kbGBdadDFyckgImEjMubSHEcRmE1CXuHHjJzOILSJg JnGw9Q87iM0scJdJ4kA/G4gtLJAg8WLjXqB6Dg4WAVWJP7cqQcK8QOUbr/9ggxgpL7F6wwGw MZxA8RmrdrKA2EICphI/jn5lm8DItYCRYRWjSGZeWW5iZo6pXnF2RmVeZoVecn7uJkZgGC+r /TNxB+OXy+6HGAU4GJV4eDfc9kwTYk0sK67MPcQowcGsJMK7aQtQiDclsbIqtSg/vqg0J7X4 EKM0B4uSOK9XeGqCkEB6YklqdmpqQWoRTJaJg1OqgXGif6/1p/rYzTHfZn/d28hQ57fbMUh+ PfNWr3eakZZfcm7dSO24Vj7fYf2yn3f/v2qWOe5ssbY0/OeJxpMC6+2mnIpMfhVxQX0O9wTm E7OeGpiozdgp+0Gqo2cGs+7TzXZ7W8rWV66cGud943ft/q1Cn9++lfW83aYyRXuf/8T43UeN IyQDV55UYinOSDTUYi4qTgQA9OaanF8CAAA= X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 23F981C0008 X-Stat-Signature: 3fxf9dnqxxqeqn3scbyx9noeysbecrdb X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1716171472-865863 X-HE-Meta: U2FsdGVkX18Bec7udz7Yuf72UgCzEa3TPgLuDcgth3jM49E6BqvbXMLrxB/aZx5zTsYui3HisZMA1i/N5+b1MxenAoS9cKjqT+1TLAgF2mxlHDYBDDyan3+GOlr4tGA+4qsWDBiDrU6kHxaOzZXgVbHTfkNcCbqHWFpHsHwE3lDvE0rcjM89jpbtHjF2F4nN3INpSL6gIKUxImNV/tD2k+rIZcaGx3W2TnmkmV/MHpcpn488GfFhVoXcbWoJ73fyExyowsYh/vwk5wj6LyOHNh82luYddYr9JTc64Yyfc0ZnPtWFsoD2PTi/S/Eyz06rW6HusjpRb09UKhZolCjl2m6Givmdn8hcZynVSbB7+VIvAAdABtw5Ovwp90TwOumnIdq9ZGU51zUAo5EVP+rzPTqNjmXbgE1MsiAedH2COlfL6oUO7oXNjs2YN9l1tCuKFWDQrDpzMpa1vbPCnQ/R9Z9KC5sTTlQbRiu944DJ3EMs9baC9E59PW4bK9X+khcvQwvQQiJDdMkdOjge9nWceFFDVJU/FWDK1Rj2TKpGBw6OmRrhFypQ2Zf81ATT/GPjBti9lE4X6Nns1s64AKy+6eCHG/4JU//cqil4QJUzg3nSq0q/FVsFcFxRS2tAVbbWIrZB+0fUv+IPb8i8HR/Ofopqfo0NBipqBHQb13qCindU+R/LFnUFCPIcvVPl2TY3QLn4DdTWiXUxun2fIBjQ1E1+JOWwDr2K9HPmOyezJ0Ol2mCMRiLwxi3RfRisEzeu/I8/doqDz+toaMmN7weOhJzuuVT6BkvV5Cavjzb4VPAntB8imZzjqEQXD2Uamlh/z+ZsqzA6WeN2SgDFoZvtPPa4AZisJl6BJrNnLEvdsw8Xfn46rObLan59qhyFf/oExUOznt4o3lco9etyTLfrA0iKKiuxbIiPKey9mQrNGeC9NHRnaHV8E4plHJc41afjTHYiqF3YoFDislCgpas NEg== 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: A new mechanism, LUF(Lazy Unmap Flush), defers tlb flush until folios that have been unmapped and freed, eventually get allocated again. It's safe for folios that had been mapped read only and were unmapped, since the contents of the folios don't change while staying in pcp or buddy so we can still read the data through the stale tlb entries. Applied the mechanism to unmapping during migration. Signed-off-by: Byungchul Park --- include/linux/rmap.h | 2 +- mm/migrate.c | 56 ++++++++++++++++++++++++++++++++------------ mm/rmap.c | 9 ++++--- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 0f906dc6d280..1898a2c1c087 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -657,7 +657,7 @@ static inline int folio_try_share_anon_rmap_pmd(struct folio *folio, int folio_referenced(struct folio *, int is_locked, struct mem_cgroup *memcg, unsigned long *vm_flags); -void try_to_migrate(struct folio *folio, enum ttu_flags flags); +bool try_to_migrate(struct folio *folio, enum ttu_flags flags); void try_to_unmap(struct folio *, enum ttu_flags flags); int make_device_exclusive_range(struct mm_struct *mm, unsigned long start, diff --git a/mm/migrate.c b/mm/migrate.c index f9ed7a2b8720..c8b0e5203e9a 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1090,7 +1090,8 @@ static void migrate_folio_undo_dst(struct folio *dst, bool locked, /* Cleanup src folio upon migration success */ static void migrate_folio_done(struct folio *src, - enum migrate_reason reason) + enum migrate_reason reason, + unsigned short int ugen) { /* * Compaction can migrate also non-LRU pages which are @@ -1101,8 +1102,12 @@ static void migrate_folio_done(struct folio *src, mod_node_page_state(folio_pgdat(src), NR_ISOLATED_ANON + folio_is_file_lru(src), -folio_nr_pages(src)); - if (reason != MR_MEMORY_FAILURE) - /* We release the page in page_handle_poison. */ + /* We release the page in page_handle_poison. */ + if (reason == MR_MEMORY_FAILURE) + check_luf_flush(ugen); + else if (ugen) + folio_put_ugen(src, ugen); + else folio_put(src); } @@ -1110,7 +1115,8 @@ static void migrate_folio_done(struct folio *src, static int migrate_folio_unmap(new_folio_t get_new_folio, free_folio_t put_new_folio, unsigned long private, struct folio *src, struct folio **dstp, enum migrate_mode mode, - enum migrate_reason reason, struct list_head *ret) + enum migrate_reason reason, struct list_head *ret, + bool *can_luf) { struct folio *dst; int rc = -EAGAIN; @@ -1126,7 +1132,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio, folio_clear_unevictable(src); /* free_pages_prepare() will clear PG_isolated. */ list_del(&src->lru); - migrate_folio_done(src, reason); + migrate_folio_done(src, reason, 0); return MIGRATEPAGE_SUCCESS; } @@ -1244,7 +1250,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio, /* Establish migration ptes */ VM_BUG_ON_FOLIO(folio_test_anon(src) && !folio_test_ksm(src) && !anon_vma, src); - try_to_migrate(src, mode == MIGRATE_ASYNC ? TTU_BATCH_FLUSH : 0); + *can_luf = try_to_migrate(src, mode == MIGRATE_ASYNC ? TTU_BATCH_FLUSH : 0); old_page_state |= PAGE_WAS_MAPPED; } @@ -1272,7 +1278,7 @@ static int migrate_folio_unmap(new_folio_t get_new_folio, static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private, struct folio *src, struct folio *dst, enum migrate_mode mode, enum migrate_reason reason, - struct list_head *ret) + struct list_head *ret, unsigned short int ugen) { int rc; int old_page_state = 0; @@ -1326,7 +1332,7 @@ static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private, if (anon_vma) put_anon_vma(anon_vma); folio_unlock(src); - migrate_folio_done(src, reason); + migrate_folio_done(src, reason, ugen); return rc; out: @@ -1616,7 +1622,7 @@ static void migrate_folios_move(struct list_head *src_folios, struct list_head *ret_folios, struct migrate_pages_stats *stats, int *retry, int *thp_retry, int *nr_failed, - int *nr_retry_pages) + int *nr_retry_pages, unsigned short int ugen) { struct folio *folio, *folio2, *dst, *dst2; bool is_thp; @@ -1633,7 +1639,7 @@ static void migrate_folios_move(struct list_head *src_folios, rc = migrate_folio_move(put_new_folio, private, folio, dst, mode, - reason, ret_folios); + reason, ret_folios, ugen); /* * The rules are: * Success: folio will be freed @@ -1710,7 +1716,11 @@ static int migrate_pages_batch(struct list_head *from, int rc, rc_saved = 0, nr_pages; LIST_HEAD(unmap_folios); LIST_HEAD(dst_folios); + LIST_HEAD(unmap_folios_luf); + LIST_HEAD(dst_folios_luf); bool nosplit = (reason == MR_NUMA_MISPLACED); + unsigned short int ugen; + bool can_luf; VM_WARN_ON_ONCE(mode != MIGRATE_ASYNC && !list_empty(from) && !list_is_singular(from)); @@ -1773,9 +1783,11 @@ static int migrate_pages_batch(struct list_head *from, continue; } + can_luf = false; rc = migrate_folio_unmap(get_new_folio, put_new_folio, private, folio, &dst, mode, reason, - ret_folios); + ret_folios, &can_luf); + /* * The rules are: * Success: folio will be freed @@ -1821,7 +1833,8 @@ static int migrate_pages_batch(struct list_head *from, /* nr_failed isn't updated for not used */ stats->nr_thp_failed += thp_retry; rc_saved = rc; - if (list_empty(&unmap_folios)) + if (list_empty(&unmap_folios) && + list_empty(&unmap_folios_luf)) goto out; else goto move; @@ -1835,8 +1848,13 @@ static int migrate_pages_batch(struct list_head *from, stats->nr_thp_succeeded += is_thp; break; case MIGRATEPAGE_UNMAP: - list_move_tail(&folio->lru, &unmap_folios); - list_add_tail(&dst->lru, &dst_folios); + if (can_luf) { + list_move_tail(&folio->lru, &unmap_folios_luf); + list_add_tail(&dst->lru, &dst_folios_luf); + } else { + list_move_tail(&folio->lru, &unmap_folios); + list_add_tail(&dst->lru, &dst_folios); + } break; default: /* @@ -1856,6 +1874,8 @@ static int migrate_pages_batch(struct list_head *from, stats->nr_thp_failed += thp_retry; stats->nr_failed_pages += nr_retry_pages; move: + /* Should be before try_to_unmap_flush() */ + ugen = try_to_unmap_luf(); /* Flush TLBs for all unmapped folios */ try_to_unmap_flush(); @@ -1869,7 +1889,11 @@ static int migrate_pages_batch(struct list_head *from, migrate_folios_move(&unmap_folios, &dst_folios, put_new_folio, private, mode, reason, ret_folios, stats, &retry, &thp_retry, - &nr_failed, &nr_retry_pages); + &nr_failed, &nr_retry_pages, 0); + migrate_folios_move(&unmap_folios_luf, &dst_folios_luf, + put_new_folio, private, mode, reason, + ret_folios, stats, &retry, &thp_retry, + &nr_failed, &nr_retry_pages, ugen); } nr_failed += retry; stats->nr_thp_failed += thp_retry; @@ -1880,6 +1904,8 @@ static int migrate_pages_batch(struct list_head *from, /* Cleanup remaining folios */ migrate_folios_undo(&unmap_folios, &dst_folios, put_new_folio, private, ret_folios); + migrate_folios_undo(&unmap_folios_luf, &dst_folios_luf, + put_new_folio, private, ret_folios); return rc; } diff --git a/mm/rmap.c b/mm/rmap.c index e42783c02114..d25ae20a47b5 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2600,8 +2600,9 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, * * Tries to remove all the page table entries which are mapping this folio and * replace them with special swap entries. Caller must hold the folio lock. + * Return true if all the mappings are read-only, otherwise false. */ -void try_to_migrate(struct folio *folio, enum ttu_flags flags) +bool try_to_migrate(struct folio *folio, enum ttu_flags flags) { struct rmap_walk_control rwc = { .rmap_one = try_to_migrate_one, @@ -2620,11 +2621,11 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) */ if (WARN_ON_ONCE(flags & ~(TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD | TTU_SYNC | TTU_BATCH_FLUSH))) - return; + return false; if (folio_is_zone_device(folio) && (!folio_is_device_private(folio) && !folio_is_device_coherent(folio))) - return; + return false; /* * During exec, a temporary VMA is setup and later moved. @@ -2649,6 +2650,8 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) fold_ubc(tlb_ubc_luf, tlb_ubc_ro); else fold_ubc(tlb_ubc, tlb_ubc_ro); + + return can_luf; } #ifdef CONFIG_DEVICE_PRIVATE From patchwork Mon May 20 02:17:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Byungchul Park X-Patchwork-Id: 13668007 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 EA423C25B7A for ; Mon, 20 May 2024 02:18:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 494D56B009D; Sun, 19 May 2024 22:17:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 330966B009F; Sun, 19 May 2024 22:17:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 15CBC6B00A1; Sun, 19 May 2024 22:17:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D75726B009D for ; Sun, 19 May 2024 22:17:56 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9AE18A19B9 for ; Mon, 20 May 2024 02:17:56 +0000 (UTC) X-FDA: 82137163752.23.9E14A39 Received: from invmail4.hynix.com (exvmail4.skhynix.com [166.125.252.92]) by imf01.hostedemail.com (Postfix) with ESMTP id C40F040008 for ; Mon, 20 May 2024 02:17:54 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716171475; 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:in-reply-to: references:references; bh=AbBZEoi3YvdH4MCXIQk6zH0Bh463GhyHOD1MRB89vaI=; b=KFLgA0EMVd0hrpmfyE++DXOfdyQc6WqIqGjznpd8/zLyUX2xeekiitI6BgjoO4aBSiltZb VRL3KoDoX+BnlBiPxTBd+Xwe7SKLXDN3jIu1i7LILUAIpkePKkWLXdn/AwtAbtWVcfxuxN I9IbXkRdBhl6/L4XUROw9oLl1+37of0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171475; a=rsa-sha256; cv=none; b=tcRFMRa9NQ+VrIisIephwk/W9untjbU4SpB364DuKg+l+nzEYzvYXwC9Of9vqWs6sy0i8p kegr/X9iX6kAWJRLCaDNpJpFY907WSbgNPc7NSiL4SCh0YhsUhPcaf5aW7v5Y0VbV5b+R4 PhPBz7r9F57Umn9oEVM5yCN49YYV2n0= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of byungchul@sk.com designates 166.125.252.92 as permitted sender) smtp.mailfrom=byungchul@sk.com; dmarc=none X-AuditID: a67dfc5b-d6dff70000001748-d1-664ab2c9763c 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, vernhao@tencent.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, rjgolo@gmail.com Subject: [RESEND PATCH v10 12/12] mm, vmscan: apply luf mechanism to unmapping during folio reclaim Date: Mon, 20 May 2024 11:17:34 +0900 Message-Id: <20240520021734.21527-13-byungchul@sk.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240520021734.21527-1-byungchul@sk.com> References: <20240520021734.21527-1-byungchul@sk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrGLMWRmVeSWpSXmKPExsXC9ZZnke7JTV5pBm9mi1vMWb+GzeLzhn9s Fi82tDNafF3/i9ni6ac+FovLu+awWdxb85/V4vyutawWO5buY7K4dGABk8Xx3gNMFvPvfWaz 2LxpKrPF8SlTGS1+/wAqPjlrMouDgMf31j4Wj52z7rJ7LNhU6rF5hZbH4j0vmTw2repk89j0 aRK7x7tz59g9Tsz4zeIx72Sgx/t9V9k8tv6y82iceo3N4/MmuQC+KC6blNSczLLUIn27BK6M ub83MRX8V6t4tvQrUwPjV/kuRk4OCQETieXrW5hh7P557xlBbDYBdYkbN36CxUUEzCQOtv5h B7GZBe4ySRzoZwOxhQWSJfp/PAWLswioSrz8+Busnheofvn2z0wQM+UlVm84ABbnBIrPWLWT BcQWEjCV+HH0K9AcLqCa92wSn9oXMkI0SEocXHGDZQIj7wJGhlWMQpl5ZbmJmTkmehmVeZkV esn5uZsYgaG/rPZP9A7GTxeCDzEKcDAq8fDueOSZJsSaWFZcmXuIUYKDWUmEd9MWoBBvSmJl VWpRfnxRaU5q8SFGaQ4WJXFeo2/lKUIC6YklqdmpqQWpRTBZJg5OqQZGpQVfFrHfkz+fMuvW locZB/dOnnk/y/nFP+1Pgbr/ZhfEdUjv5wzXur/2ioienMUUN9dz3T4dC0tYRF2t1u74Wmir ZSg0eXnrmm9nJs/ctD34oEnNaa4pb/aH3RCeOI0/W+2d2+mkq57lR89Eib99uo5z2ifjSIY9 O5oXzdWaIpJrM/eehJrFMSWW4oxEQy3mouJEAGOoIUV5AgAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrLLMWRmVeSWpSXmKPExsXC5WfdrHtyk1eawcNPvBZz1q9hs/i84R+b xYsN7YwWX9f/YrZ4+qmPxeLw3JOsFpd3zWGzuLfmP6vF+V1rWS12LN3HZHHpwAImi+O9B5gs 5t/7zGaxedNUZovjU6YyWvz+AVR8ctZkFgdBj++tfSweO2fdZfdYsKnUY/MKLY/Fe14yeWxa 1cnmsenTJHaPd+fOsXucmPGbxWPeyUCP9/uusnksfvGByWPrLzuPxqnX2Dw+b5IL4I/isklJ zcksSy3St0vgypj7exNTwX+1imdLvzI1MH6V72Lk5JAQMJHon/eeEcRmE1CXuHHjJzOILSJg JnGw9Q87iM0scJdJ4kA/G4gtLJAs0f/jKVicRUBV4uXH32D1vED1y7d/ZoKYKS+xesMBsDgn UHzGqp0sILaQgKnEj6Nf2SYwci1gZFjFKJKZV5abmJljqlecnVGZl1mhl5yfu4kRGMjLav9M 3MH45bL7IUYBDkYlHt4Ntz3ThFgTy4orcw8xSnAwK4nwbtoCFOJNSaysSi3Kjy8qzUktPsQo zcGiJM7rFZ6aICSQnliSmp2aWpBaBJNl4uCUamA8qquh/+/PghOfblkJyjay72M1Wjjt0sxl 1W3rfKbYPTM/4phe1mSjvpDpwqNzagfeuCTouZdqfnyo8rLhyqvVtc94tl5vczrONO3mq65r rqte5G/iqXmcXR5da8xWKXV607z8q98OS3C+7K/+6Fu2q5dBqvV4MZPjX9XE+n3c4htc7wp8 yf6kxFKckWioxVxUnAgAxnKitWACAAA= X-CFilter-Loop: Reflected X-Rspam-User: X-Stat-Signature: 4drpsxgrpqo5gj6a8y8p347fycndgdnh X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C40F040008 X-HE-Tag: 1716171474-97652 X-HE-Meta: U2FsdGVkX1+DQzUVA6K3Di6YfsNMpwG65wwwRhe7t4sX2SqHcK0IOzgAj6kETPxuKrF1UdJF06EM0LRYFz2xghAdqQR0kGLfbPY2/0X0QxYlBnBf5JFB3CZk35VcN8V0ts8qNoKfeCN4kEiU+bSnC5kNF/QDkOSfgdSz/HK9NA+TWtTLpDjX/zuHS51rZaHn5HsCbxlW8Q9eu5oTOGbC9FWDg4WE2L/Lg3yyppxouN0/kBG0gubz/yYhfPoSFEMCoTpXKun3eF1B1G06I5MQHkfNN9ZMRF33rkPrUXKzX6jti1AURkk6XrJI1pMn/3bTtC0KfevYNO4kjH703PvrZg6Fms+pJHErOoH8RZCfIfs6dOJ/QX0x4OuzIeeIo6lTKaFiJpSqgGowhehiQuIP1AXGkvd4ZG8MYFvnIjfVloixAwyYqPUrrE+uj8ybQ4yHPcIQU0ObtFpIc577zZKLI6XO5EvK2Dd4gnfQoQ0TdAl5mt7h9L+rV3MCtdCxlT3fcEA1BHVymtNMM6Y+qStE5DQcyZMzfrTONy8v8D9FCN4pGbZ2dQT+4PkL/0xzi5M0/FRwstMD3yBnRDCK9tqE/qH2+FSSQ48aqlmdFizAhBD8358rR2vb4bEazGuLyTniI123s3mBBCJffxEbHhPjpbEGNKkuhGfYbSXvjy2TpL6IsWaUmQJnn5Jv+uq304avRhTKvwpBgfGD7n0lcfNqA/knrl1DEv6AvYD6dIgCs+bjFxLSRD5rtb2G/3v1HYdpWPjBQ5P9yz7tfHN701f6XUIzmSmm0jlb2MkA8i+fubHLI1ZBWGsD1PXJCPBfOlM9tuMHIUR79SpUKLq4WXaldQAWI6Z4k/+0SjlP30FOkebAjHr/Ii2PCrkYR0AWp2z4z3njDsaoSFx4zyfHFw3FkWGlO/Yy8jL49iAeM3TCyGEEYDYHWZr9c+0Y309yrF4L2oyo3N6sg1XnIOrNcKQ Ybg== 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: A new mechanism, LUF(Lazy Unmap Flush), defers tlb flush until folios that have been unmapped and freed, eventually get allocated again. It's safe for folios that had been mapped read only and were unmapped, since the contents of the folios don't change while staying in pcp or buddy so we can still read the data through the stale tlb entries. Applied the mechanism to unmapping during folio reclaim. Signed-off-by: Byungchul Park --- include/linux/rmap.h | 5 +++-- mm/rmap.c | 5 ++++- mm/vmscan.c | 21 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 1898a2c1c087..9ca752f8de97 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -658,7 +658,7 @@ int folio_referenced(struct folio *, int is_locked, struct mem_cgroup *memcg, unsigned long *vm_flags); bool try_to_migrate(struct folio *folio, enum ttu_flags flags); -void try_to_unmap(struct folio *, enum ttu_flags flags); +bool try_to_unmap(struct folio *, enum ttu_flags flags); int make_device_exclusive_range(struct mm_struct *mm, unsigned long start, unsigned long end, struct page **pages, @@ -777,8 +777,9 @@ static inline int folio_referenced(struct folio *folio, int is_locked, return 0; } -static inline void try_to_unmap(struct folio *folio, enum ttu_flags flags) +static inline bool try_to_unmap(struct folio *folio, enum ttu_flags flags) { + return false; } static inline int folio_mkclean(struct folio *folio) diff --git a/mm/rmap.c b/mm/rmap.c index d25ae20a47b5..571e337af448 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2237,10 +2237,11 @@ static int folio_not_mapped(struct folio *folio) * Tries to remove all the page table entries which are mapping this * folio. It is the caller's responsibility to check if the folio is * still mapped if needed (use TTU_SYNC to prevent accounting races). + * Return true if all the mappings are read-only, otherwise false. * * Context: Caller must hold the folio lock. */ -void try_to_unmap(struct folio *folio, enum ttu_flags flags) +bool try_to_unmap(struct folio *folio, enum ttu_flags flags) { struct rmap_walk_control rwc = { .rmap_one = try_to_unmap_one, @@ -2265,6 +2266,8 @@ void try_to_unmap(struct folio *folio, enum ttu_flags flags) fold_ubc(tlb_ubc_luf, tlb_ubc_ro); else fold_ubc(tlb_ubc, tlb_ubc_ro); + + return can_luf; } /* diff --git a/mm/vmscan.c b/mm/vmscan.c index bb0ff11f9ec9..4e2e9d07cd96 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1031,14 +1031,17 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, struct reclaim_stat *stat, bool ignore_references) { struct folio_batch free_folios; + struct folio_batch free_folios_luf; LIST_HEAD(ret_folios); LIST_HEAD(demote_folios); unsigned int nr_reclaimed = 0; unsigned int pgactivate = 0; bool do_demote_pass; struct swap_iocb *plug = NULL; + unsigned short int ugen; folio_batch_init(&free_folios); + folio_batch_init(&free_folios_luf); memset(stat, 0, sizeof(*stat)); cond_resched(); do_demote_pass = can_demote(pgdat->node_id, sc); @@ -1050,6 +1053,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, enum folio_references references = FOLIOREF_RECLAIM; bool dirty, writeback; unsigned int nr_pages; + bool can_luf = false; cond_resched(); @@ -1292,7 +1296,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, if (folio_test_large(folio) && list_empty(&folio->_deferred_list)) flags |= TTU_SYNC; - try_to_unmap(folio, flags); + can_luf = try_to_unmap(folio, flags); if (folio_mapped(folio)) { stat->nr_unmap_fail += nr_pages; if (!was_swapbacked && @@ -1457,6 +1461,18 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, if (folio_test_large(folio) && folio_test_large_rmappable(folio)) folio_undo_large_rmappable(folio); + + if (can_luf) { + if (folio_batch_add(&free_folios_luf, folio) == 0) { + mem_cgroup_uncharge_folios(&free_folios_luf); + ugen = try_to_unmap_luf(); + if (!ugen) + try_to_unmap_flush(); + free_unref_folios(&free_folios_luf, ugen); + } + continue; + } + if (folio_batch_add(&free_folios, folio) == 0) { mem_cgroup_uncharge_folios(&free_folios); try_to_unmap_flush(); @@ -1526,8 +1542,11 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, pgactivate = stat->nr_activate[0] + stat->nr_activate[1]; mem_cgroup_uncharge_folios(&free_folios); + mem_cgroup_uncharge_folios(&free_folios_luf); + ugen = try_to_unmap_luf(); try_to_unmap_flush(); free_unref_folios(&free_folios, 0); + free_unref_folios(&free_folios_luf, ugen); list_splice(&ret_folios, folio_list); count_vm_events(PGACTIVATE, pgactivate);