From patchwork Thu Feb 15 01:02:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Chris Li X-Patchwork-Id: 13557267 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 38F9CC48BC3 for ; Thu, 15 Feb 2024 01:02:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B4F5B6B009C; Wed, 14 Feb 2024 20:02:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B00686B009D; Wed, 14 Feb 2024 20:02:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99F4E6B009F; Wed, 14 Feb 2024 20:02:38 -0500 (EST) 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 867376B009C for ; Wed, 14 Feb 2024 20:02:38 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3865D40F41 for ; Thu, 15 Feb 2024 01:02:38 +0000 (UTC) X-FDA: 81792237996.26.BACA09C Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf22.hostedemail.com (Postfix) with ESMTP id D82ECC0012 for ; Thu, 15 Feb 2024 01:02:35 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nvFyLvve; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of chrisl@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=chrisl@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707958956; a=rsa-sha256; cv=none; b=bi7isUVL4ZUCSPwz+oibApqcvCSRFErCRoDFoSdqU5x3oGk/O2moTJ+JDf8v+BLWche2lT X28B9LECCyJnhXkyIPOCAY1+De7sf16BzG2sS648YJyo+8XNt3XMhhI0M/vk1qUFO9uGMJ Vw0jrFB3WwVhfLpr9P80CsSD9QSM7tM= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nvFyLvve; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of chrisl@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=chrisl@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707958956; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=1w7nt4uShhvGZ5xLhCLNlkVZqdLeqIbpQ77sA+DD6Pc=; b=5QB+wrR/OoCKfKjmGoxUmUSiINFojQ+ARwEOfd1XtL4Hky4pm6aZ/Yz1N8LTsl0MlFAlaa OzokxIaQFUBUq1OL/Z+kjnypfduvzxVTZK6/kh3wB48KyzMgOH5KnJeRKu/YstxALCxnTj 0Wt0xLTZV86go7kEkVWSOpthvI8kgsc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id DA27FCE240B; Thu, 15 Feb 2024 01:02:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82AE9C433F1; Thu, 15 Feb 2024 01:02:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707958951; bh=vxbCUjb2siC8gO3MI0ihjF8F/AbZiW4QI18D4VWKOfo=; h=From:Date:Subject:To:Cc:From; b=nvFyLvvevhmQzN4QxWdsxAjdizMx+oVdrGsS/A9guWvjpVMnqREOFbwOE3P8WtkEg Yh16v9GXp3NTxlSMoF/ySq2gb1rVwMP/xK5yh2YrrpzwWC6UuLn3P7pU15R9T4NHvw 31SZfo+8PjxkIxECKkm1mQk6r9B9nVdZPjFgT+IBorNt4ZYVsyVyDsOCURczdQu2wT LdV2Iznax/rBkL923BLAjfFjVUmPrUa75rPrTgWz3wsmwXblIMloy2e3r8WvYjb638 Q8nwUtq5bvoBkh/1mEPWAn+B50lwC9vI/OJOWY+0eOnfl3FuMb56/w1gNuywC7blMq tlZI5sOVchDHA== From: Chris Li Date: Wed, 14 Feb 2024 17:02:13 -0800 Subject: [PATCH v4] mm: swap: async free swap slot cache entries MIME-Version: 1.0 Message-Id: <20240214-async-free-v4-1-6abe0d59f85f@kernel.org> X-B4-Tracking: v=1; b=H4sIAJRizWUC/2XMQQ6CMBCF4auQrq3pzBShrryHcQF1gEYDpjVEQ ri7hWgUXb6XfP8oAnvHQeyTUXjuXXBdG4feJMI2RVuzdOe4BSokQNjJIgytlZVnliVXZFBBqgl FBDfPlXssseMp7saFe+eHpd3D/L4yCN+ZHiRIo0vMMmPQlupwYd/yddv5WsydHt9WK6C1xWhTT CtFrDLI9Z+lj0WglaVoy9xYslbn9GOnaXoCqHIsMh0BAAA= To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Wei Xu , Yu Zhao , Greg Thelen , Chun-Tse Shao , Yosry Ahmed , Michal Hocko , Mel Gorman , Huang Ying , Nhat Pham , Kairui Song , Barry Song , Tim Chen , Chris Li X-Mailer: b4 0.12.4 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D82ECC0012 X-Stat-Signature: bigskgzoai5tifeghxrfckciwnfmmagz X-HE-Tag: 1707958955-181804 X-HE-Meta: U2FsdGVkX1/clUsxhrcnx/S4InWlvk1c1a0FeNFg2qYw+tRLW2Z4hHPhWtdHjY3wkB+dC51l/iDKpppK0HDkYhHkBfcncAoxyFntrQqR+pvNwxH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0jqSyuy2tJ5/2aRKCAWdwqoVTKf0sj47hImJ0j5Rh9I7Sa04aw5x48e7IfWog== 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: We discovered that 1% swap page fault is 100us+ while 50% of the swap fault is under 20us. Further investigation shows that a large portion of the time spent in the free_swap_slots() function for the long tail case. The percpu cache of swap slots is freed in a batch of 64 entries inside free_swap_slots(). These cache entries are accumulated from previous page faults, which may not be related to the current process. Doing the batch free in the page fault handler causes longer tail latencies and penalizes the current process. When the swap cache slot is full, schedule async free cached swap slots in a work queue, before the next swap fault comes in. If the next swap fault comes in very fast, before the async free gets a chance to run. It will directly free all the swap cache in the swap fault the same way as previously. Testing: Chun-Tse did some benchmark in chromebook, showing that zram_wait_metrics improve about 15% with 80% and 95% confidence. I recently ran some experiments on about 1000 Google production machines. It shows swapin latency drops in the long tail 100us - 500us bucket dramatically. platform (100-500us) (0-100us) A 1.12% -> 0.36% 98.47% -> 99.22% B 0.65% -> 0.15% 98.96% -> 99.46% C 0.61% -> 0.23% 98.96% -> 99.38% Signed-off-by: Chris Li Reviewed-by: Tim Chen --- Changes in v4: - Remove the sysfs interface file, according the feedback. - Move the full condition test inside the spinlock. - Link to v3: https://lore.kernel.org/r/20240213-async-free-v3-1-b89c3cc48384@kernel.org Changes in v3: - Address feedback from Tim Chen, direct free path will free all swap slots. - Add /sys/kernel/mm/swap/swap_slot_async_fee to enable async free. Default is off. - Link to v2: https://lore.kernel.org/r/20240131-async-free-v2-1-525f03e07184@kernel.org Changes in v2: - Add description of the impact of time changing suggest by Ying. - Remove create_workqueue() and use schedule_work() - Link to v1: https://lore.kernel.org/r/20231221-async-free-v1-1-94b277992cb0@kernel.org --- include/linux/swap_slots.h | 1 + mm/swap_slots.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) --- base-commit: eacce8189e28717da6f44ee492b7404c636ae0de change-id: 20231216-async-free-bef392015432 Best regards, diff --git a/include/linux/swap_slots.h b/include/linux/swap_slots.h index 15adfb8c813a..67bc8fa30d63 100644 --- a/include/linux/swap_slots.h +++ b/include/linux/swap_slots.h @@ -19,6 +19,7 @@ struct swap_slots_cache { spinlock_t free_lock; /* protects slots_ret, n_ret */ swp_entry_t *slots_ret; int n_ret; + struct work_struct async_free; }; void disable_swap_slots_cache_lock(void); diff --git a/mm/swap_slots.c b/mm/swap_slots.c index 0bec1f705f8e..23dc04bce9ca 100644 --- a/mm/swap_slots.c +++ b/mm/swap_slots.c @@ -44,6 +44,7 @@ static DEFINE_MUTEX(swap_slots_cache_mutex); static DEFINE_MUTEX(swap_slots_cache_enable_mutex); static void __drain_swap_slots_cache(unsigned int type); +static void swapcache_async_free_entries(struct work_struct *data); #define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled) #define SLOTS_CACHE 0x1 @@ -149,6 +150,7 @@ static int alloc_swap_slot_cache(unsigned int cpu) spin_lock_init(&cache->free_lock); cache->lock_initialized = true; } + INIT_WORK(&cache->async_free, swapcache_async_free_entries); cache->nr = 0; cache->cur = 0; cache->n_ret = 0; @@ -269,12 +271,27 @@ static int refill_swap_slots_cache(struct swap_slots_cache *cache) return cache->nr; } +static void swapcache_async_free_entries(struct work_struct *data) +{ + struct swap_slots_cache *cache; + + cache = container_of(data, struct swap_slots_cache, async_free); + spin_lock_irq(&cache->free_lock); + /* Swap slots cache may be deactivated before acquiring lock */ + if (cache->slots_ret && cache->n_ret) { + swapcache_free_entries(cache->slots_ret, cache->n_ret); + cache->n_ret = 0; + } + spin_unlock_irq(&cache->free_lock); +} + void free_swap_slot(swp_entry_t entry) { struct swap_slots_cache *cache; cache = raw_cpu_ptr(&swp_slots); if (likely(use_swap_slot_cache && cache->slots_ret)) { + bool full; spin_lock_irq(&cache->free_lock); /* Swap slots cache may be deactivated before acquiring lock */ if (!use_swap_slot_cache || !cache->slots_ret) { @@ -292,7 +309,10 @@ void free_swap_slot(swp_entry_t entry) cache->n_ret = 0; } cache->slots_ret[cache->n_ret++] = entry; + full = cache->n_ret >= SWAP_SLOTS_CACHE_SIZE; spin_unlock_irq(&cache->free_lock); + if (full) + schedule_work(&cache->async_free); } else { direct_free: swapcache_free_entries(&entry, 1);