From patchwork Mon Feb 24 18:02:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13988662 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 C46D1C021A4 for ; Mon, 24 Feb 2025 18:02:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C84F28000F; Mon, 24 Feb 2025 13:02:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5771828000C; Mon, 24 Feb 2025 13:02:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F2F028000F; Mon, 24 Feb 2025 13:02:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1A4E628000C for ; Mon, 24 Feb 2025 13:02:58 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7E0BE121A4E for ; Mon, 24 Feb 2025 18:02:57 +0000 (UTC) X-FDA: 83155609194.28.1EDD69B Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf12.hostedemail.com (Postfix) with ESMTP id 998F340017 for ; Mon, 24 Feb 2025 18:02:55 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AMTdFVBR; spf=pass (imf12.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740420175; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xwT//N+9nfh7P5Px11klS6FaPMqYvLp+fVr/05Baw3w=; b=RdF8PtR77HQ47XUybkMiL06aoLSdj5SVHxHhEmeGNTro1MQxpV4vrCzHt5nsujeubWBypV O5LwMpGJPaiJLx/4C0NWcMocatZGJXjZwJwuN2Df4dBC1ILIHtabXz0sq+DLyPM1gKmvo8 3e5VCkKP+4CnwzGDt4rdQy6f7tArHco= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AMTdFVBR; spf=pass (imf12.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740420175; a=rsa-sha256; cv=none; b=fVsII/2BfdeQl/Jbd2XQv79SS7B6tYL60BFUS/lNa0WjHfhieBXtHfFAVPzln+EnIfaS5v sr3ZXobU1BJBQ12t7xXWG4SNX6PTdssN8eYNKY5lIFldHmGwsHeDXkWYLT0NDc0GKW+y1N R6LgCwE5hQe7nfP9h2plezZ5wXAKNGU= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-222e8d07dc6so22080405ad.1 for ; Mon, 24 Feb 2025 10:02:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740420174; x=1741024974; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=xwT//N+9nfh7P5Px11klS6FaPMqYvLp+fVr/05Baw3w=; b=AMTdFVBRm6k6zH3Wn5dDoUjeixrZjoxYNZiMnKhVJ7rA0+oKryw0BTHOjZCTJ8y902 W0E/UD4fdfCIsWDR6GACfCIAKjNjp08XEAGEUBSQfMBJ4gEGu39uGBymcMcLIfX/uH6J tl7MTitXZzW0tyEN6Z3fZwykaZkEq1eWJL1YdzUgGW6Y3yR+zCE+14LUhMDhhzqBi975 H6+PGO5MVZ0ajDD0b2U78ZGw8A/U3tHfCA+Airx9BKjSzNk97C6+WDAkk27kyW/KY8WY os+Pn3RWqk+dGtzo8gc8WazuZLqCL6dDy1LuU0OQ4VfVH6ymWf5s++xnkp4pp4ft13RH 1cpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740420174; x=1741024974; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=xwT//N+9nfh7P5Px11klS6FaPMqYvLp+fVr/05Baw3w=; b=cZ5XanaJK/07RoNO9WFY8MiidhNHamVsPxI43fWQ7Gtx/0XVejppQQSn/2fnrzyV9o wIYnD/pOgl7wWO+33rxhcWpADYjM+0Ml9x7nvzABHsqlqKKPEVKoww8ZZmySki4OMj/9 prc5bRwiuI3x0FnJq90vlp0i3tcAxDwE+Po6kLVMgTpiTnYBbjnw0l6vncaTKiCvU8x1 +gEOTDjv+ld3lz0DJeecBcr8Ols0+S7xYZW2TxxY60H3j7ZeUzapKTBbsaEi8iLmxG9v Q3nI1t3LrE7vPAfBJKKoYRJN1pAMddYuzJ+uOypYWstdODadp7jujkmxW27BUIFekKLV dKTQ== X-Gm-Message-State: AOJu0YzdkWRKr4Wm9ZaLV9wA+4P3sbnrRIwxTzqkkPd0kyOFfZ3NR/g4 oyek3RjF4A+4FhzNNOoaeZIF1c3h09sMHRCgJ5ty+9KC2cf6nLDx+nT1rLTo3x8= X-Gm-Gg: ASbGncvIKtBpM3IPZ8A07UYngoQWBR4Bt616q+4qiilsvXwtnsjaFAcA29uN49flJVu zrVkjfxilPADuGFm47j770wAOPQaHbZi6tv2ZgX6cLDfuXOtG6Pqc2xselRS7voa3oqRS3RMW5m qt1rUlzoCXSZHCoAqXYHAmCbSUPxo8LgPTe/SCw4zrDc9esIWW/ZL90pzH3rwDvX4IG3MjtNuB/ q2bt28OPnSSRMSABQZA4iRnGPFsDo5B5ARqCrCnohAiu0s9YBHmGSWZOIrIgGeMJKATo3ZivagS CZEqiIWU27osBsEpBvAKhYttRli3SODZp5X9X/DmRE2L X-Google-Smtp-Source: AGHT+IHg1obCVKFszbKO58ucl3S2uuTMPf5zZW6roBu4cVs9S04wje26rUs4hychmcu7wQV7//msbA== X-Received: by 2002:a17:902:d2c1:b0:21a:7e04:7021 with SMTP id d9443c01a7336-221a001fb74mr213193225ad.24.1740420173812; Mon, 24 Feb 2025 10:02:53 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556e15esm184834695ad.190.2025.02.24.10.02.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 24 Feb 2025 10:02:53 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Baolin Wang , Kalesh Singh , Matthew Wilcox , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 1/7] mm, swap: avoid reclaiming irrelevant swap cache Date: Tue, 25 Feb 2025 02:02:06 +0800 Message-ID: <20250224180212.22802-2-ryncsn@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224180212.22802-1-ryncsn@gmail.com> References: <20250224180212.22802-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Queue-Id: 998F340017 X-Stat-Signature: 73zx3cnh66rbc7kme8sxkfq1tmxcn949 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1740420175-420131 X-HE-Meta: U2FsdGVkX19i/74kPw9F2GPjAQWn8aukW3iY6KkeEf4Y+Dc0HQRrD7R4qdc84lq/1iiAM4HsFgu1qpB9o99aftzSy9ammIVZiJ9y0/DtBsfMnSC6uLBeSfctAtAO2tIphQQmqruyvVLdeNY8vrQVuNptYEtGTzWZXlniiF+M1fYOyiBDXjEW+/f8/OWhE3T7geKdv1Y4/ywqwQXlgtRjVi4fByXsvj5i348JyzLube4r2DcfCjF6vK/qDA7XTM8fNGvq2lqCE+Z5LUZJjpA5biCdf7q7BHkOBKSWOcMI5FBjdSLbZiqvkpZ09XFedxDPk814VyoPx42YcSg+Eg6+sxPf4gWsXZqk969EE32Qr0Z0cXK56PdeQ1wduANngcRJByHXGPZDJzjQokEBWLNHmujLw0Dik8ysTpQ6l0madbnQWqjUKjWOEd6gHE9y4GYZFyxaPXRuZ/SM2qWJWm6qJSVsQQZIpiRcZpvtephQPUD4h35JPkX7zKIKRq+Ftf2X6JrIuFrcVanHzq/m6P2jR8D2IPPCCmIsqJxPlVd8HCyWQxsavNZb8F/HWrUP8qEc2Ki+Vh3n898d+s7eSxsxTsYaC4t9msg/ZIKNRLeqcVe7ZbMSz5qUwX7bMIuYLa6jXKjBGm38QidDPCErofSJH6DYJvo9KWvFNV3dXdOK9YnavSYDfWm0gxEC0D+PXLPOi+f2af/D6saeKQM0Ojg7taAYmGYEEfUjYgJ6o3U6LxSvNnscCVBoQ1EvKuenDHCZ8m3xwkjBASXCMif+21KqDdH2I61d1q8MLgr10Zu8JzRfeY8yYn9kX25DcuqnL/4AeiiBK/VAXxBA42Rp+D19yRIQfwHzFKvRghkGAkG7hoTxpz5m3JSVwqXMH9kGVsPnQOsT4JXTC8Q1u2+ZNBb7JhgeTse0TUYQwxjsn8eeF8dwUww6lW50JvWKKMcwU8c95Hvb7QMKSSsE6/LnWgQ 7Zqe1/Ok eWz5q1zSnlcobB4/HSBs647Oun13Siiuvrz4v5io8yFfrsxvLDffCawQVWA2rsw/cTgYwEtUUBZFrK6PQrhxNIlqO2G+hnuAbTEuUFtHlqKPCcrroVV1Vh/O3K0E/SSluUBR6vdE4CrYtOW2fRWI7x4reHpwR31sf2QqUGcsKyFP9Mfc+MVDEgsQSm9XR19oUQboFt0gaqtkpkrVd0oUnHM083XLxlBaKxixV+qm+zFTIxtrv1LD5njHQFSJ95zurLj9ZGEtvOdLDbC2Pc8WYiCfFBtIVvGCgdwcjunCvvYS8+Lu5oZTLI2BTfrdL/TP2gxUUsb6ST684cQbJeCWOifvOvFDDYcovzOgUyJOMIMdnQovkKX/FnTDqv7FR5vHv0eRzyD+YXCWBKwIsyduDnESJZ5rWYqwHjJI08+P7z8cgZnbIxe2EZWtzljgjmILFJ2Fz4gY16aOEpfCPtk82JsLlYMI1U4rVp2jJnEJX3aNan3lfYI7oJpciqUviZAeAGVnH204On/kGLeZ5ne0myjTOka2QsUW6aEJTaiVVIJopQJKCwZ67Cct4XJDiOdYMvuEhpIw3aed7t8uZSSQzb+JmFg== 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: From: Kairui Song Swap allocator will do swap cache reclaim to recycle HAS_CACHE slots for allocation. It initiates the reclaim from the offset to be reclaimed and looks up the corresponding folio. The lookup process is lockless, so it's possible the folio will be removed from the swap cache and given a different swap entry before the reclaim locks the folio. If it happens, the reclaim will end up reclaiming an irrelevant folio, and return wrong return value. This shouldn't cause any problem with correctness or stability, but it is indeed confusing and unexpected, and will increase fragmentation, decrease performance. Fix this by checking whether the folio is still pointing to the offset the allocator want to reclaim before reclaiming it. Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- mm/swapfile.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index a7f60006c52c..5618cd1c4b03 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -210,6 +210,7 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, int ret, nr_pages; bool need_reclaim; +again: folio = filemap_get_folio(address_space, swap_cache_index(entry)); if (IS_ERR(folio)) return 0; @@ -227,8 +228,16 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, if (!folio_trylock(folio)) goto out; - /* offset could point to the middle of a large folio */ + /* + * Offset could point to the middle of a large folio, or folio + * may no longer point to the expected offset before it's locked. + */ entry = folio->swap; + if (offset < swp_offset(entry) || offset >= swp_offset(entry) + nr_pages) { + folio_unlock(folio); + folio_put(folio); + goto again; + } offset = swp_offset(entry); need_reclaim = ((flags & TTRS_ANYWAY) || From patchwork Mon Feb 24 18:02:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13988663 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 539C3C021BB for ; Mon, 24 Feb 2025 18:03:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8F5A280011; Mon, 24 Feb 2025 13:03:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D401028000C; Mon, 24 Feb 2025 13:03:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB8AB280011; Mon, 24 Feb 2025 13:03:01 -0500 (EST) 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 9A76328000C for ; Mon, 24 Feb 2025 13:03:01 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 28B881C87C7 for ; Mon, 24 Feb 2025 18:03:01 +0000 (UTC) X-FDA: 83155609362.28.1288880 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf11.hostedemail.com (Postfix) with ESMTP id 2D3D040027 for ; Mon, 24 Feb 2025 18:02:59 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eZsMq9BJ; spf=pass (imf11.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740420179; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QRxfoQL+pBTLKNCpBu9kbqbY4X297q/ZVa8ZHPjqplw=; b=2urujGeo1sTNAk0p6cp0QkP0o2fj0NwydPxzT3qacxuHEuFOD9OdvZK9WbbR16LNYdArKE OSDrkE7RT2jPz36GbGUCKfJ8Dn8B1rDq6f5GwIN3+q/Cg4hAj5MYIHdIKM11JQY/6lv02U LbsazFT9hnCHahVqrm8zwvBUV+qXZi8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740420179; a=rsa-sha256; cv=none; b=z+DVoVtOSoEtbXhwbXL64lmoUC8liSmZUlSk/RQC03oxYMGjBb3WnqGFTDJo7A8xNiKFys Z0X7JaRQzl1MK0gear3CB4hpVn1/wy5xAFcAYBhEOMC+PE3RZTpRytl4X7ZZd2CrF9r94C gfy7n5RLWnoszvKY8qVBRrPaMPg/biw= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eZsMq9BJ; spf=pass (imf11.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-220c8f38febso100586715ad.2 for ; Mon, 24 Feb 2025 10:02:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740420178; x=1741024978; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=QRxfoQL+pBTLKNCpBu9kbqbY4X297q/ZVa8ZHPjqplw=; b=eZsMq9BJz+hU/hUSWbWZ66ffmh7dzXVEq9ueZ2gA3CE+dZDEy7oXImZ+NAEvtuvbdB cP9xjR3He1Nv56js3XbjBm+/aTdvXVqev8CAQ1o6zi9Vkox3AbUQXAwqHA1iVWsVN4Qb 2r1nyDTLi7ZkBKEf2mT+bUwGOFdK60EAbGHMIqM2iQIAKiJjvMV9FEhUhTRWC4/ItDSv YUP7Cg3DJUsWry5prWQMA8a0vwzRqvNGehed1lXX4hJYryhsBRp73NXN8VskkTpjmmec /o1TljvqjpxbiOlK0TxKwlxgXo3OU86E5pbrNkPbfgH1sIgj+ZHqj1j0dCoGKsvp4NCB Z27Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740420178; x=1741024978; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QRxfoQL+pBTLKNCpBu9kbqbY4X297q/ZVa8ZHPjqplw=; b=kuz/wj0aww3JsTfwG5IEFXlElWuzmCzqA1xrzLAGy1alBRqPajaTroC9q9fXP70goE D9ShPO33yWmuM/3nQTwS+mFYhRe/3rWoZyE0/ctIcBQRSjv5a0pmyPa3I0K2/Dv9FeB/ U4Nhap+9NEVDLK/RZooGhgfWaA0uAHtRCvqVycUb35ZscuXfhYee50f5Gc/lsWPTuxJ7 o5cZ45KTo2fTqCc7nMxcAVEuD4oehAteWELqQHns/a7E6AeLkudrrrhFeQj9Ui5B9Sdc l6uqdBN2upbzCoZwPhgzIqc1Hvr3YXT7Z6Pne6IuAQFW+jr0IaspUrnZ4HNqHnINGAd8 7Y9A== X-Gm-Message-State: AOJu0YwC0WGyk1OllrC+PEPJiM8Iet6OiPVgEvHYgfBR/tUG9wljF22C hapeJ7c0gVOTiF7xFTKF1fNhAoy2ca1mrtf4x1f88Q45mmBE3Mf8rp2tudoCqtk= X-Gm-Gg: ASbGncv6qWFj7J7/oeLX27OKwj1ZrSetzWOWpRYnS81JEZymC24+cCFdcRuyMe5F3xN Pq8wRiREcY3aqpY7/VaJUsqYsOrC+c03s5iDCVQVkw/dI+dTuHWqdR42K3vflDQ2md03VNninPI C0/geDOZk7rleHKSSk0JRtEiirxgciRltpQRoOv1jQrbsgfzDwYN3gNNQNo4AJbyvZN2cQ3Z0ww 1fXpc+NRmfc2BxS9XIztUBjlwOs7fOsAQNIKSaxJJNpGfe8ppgXWPN54Cyn1cYUgqaakto0MM18 p4Y7b3aeMEE8h+cdnWX9jx7iJYu4R4rbOzWLgDWEUah6 X-Google-Smtp-Source: AGHT+IH2vkZ4qNq/k2BTmYvnntomhPccNa7QFOZ5vqVX7VxUA99Ns1FNfhFZfIv+cr8MOTzBlLBrlw== X-Received: by 2002:a17:902:d2cd:b0:220:c911:3f60 with SMTP id d9443c01a7336-221a00260e0mr232172315ad.47.1740420177678; Mon, 24 Feb 2025 10:02:57 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556e15esm184834695ad.190.2025.02.24.10.02.54 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 24 Feb 2025 10:02:57 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Baolin Wang , Kalesh Singh , Matthew Wilcox , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 2/7] mm, swap: drop the flag TTRS_DIRECT Date: Tue, 25 Feb 2025 02:02:07 +0800 Message-ID: <20250224180212.22802-3-ryncsn@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224180212.22802-1-ryncsn@gmail.com> References: <20250224180212.22802-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 2D3D040027 X-Rspamd-Server: rspam07 X-Stat-Signature: g8co6q4eykpkzk4ccb7nm6kwtkkgpion X-HE-Tag: 1740420179-912432 X-HE-Meta: U2FsdGVkX19kMTeMx+czy0Wn8BCR+dfY8j34iYcHVCxlWSTxSYizAwd/zSEYoewTwltZeeyK74GVM3YjsuDs5Js07CK9/0+Qwgls4ntXezcfHE0hUy2F2S2AY17Ou9y2hAFgF7L6dZmJvSNHRCaC5dZrtFLE7gm023+XNXx8lpGMV8/G0InVyCMEJG7oCuSue/wFr2Buq9FWWmjKLxgM4eZ1ULY9PWy2ZsaLjh8BjSg/UTZfK9lbxolmOeCX3DNSI3+yYOEeLz9DF9Z1OU2eoZwfEbJU8Bf3OBP2JnePv2Y4G8OX1z7QhiwvjTvmfQmh0lYl9ZDPXxrZFP/49nZlTrbPg6f0CAzL4FSrEjniO9iJ/t43Ycl5bmb56l/J+d16Cx731xp0RMFbvf6pdSogMK4yNGRImRSP4G6+UrlCZdqeAXBVAUyyN7cxaDD4LGFURk1Vpv0C2h52DUGkFSPLw3CmlKHIOMUKXtq9ugF7EcsJbPDMUfL96fuNktpidW/tKfMpEz7rQjsXrXrt7+ppwRJ5XihtnY7Yq/TdChTqwzVVL5kC3GoLnOVd9u3oRWCXFURJbVJfZKmjoZVvP8QKNEUG7CHO8zbfG1wzjk8YQ3ClHXQ0OgEs19ZF7PrrfMhBGYsHJxYavZzay7YE2cVnF2zW6KltAsAfANpUXwpzKo6a3oyKMgmGmbmLImKnHC7DeK5YpCBNARwfRVnMJwykFpmL14nNnjT2Mhx0H32ug2Xv8zFyR2at3h0NKtQzoGFFEebjmU5e3Cz1ygCqIqzGkOyo8BDbokfnePS7wWCL7K10SjuKvjlC4184e/ewd6XE6d7qdUcO0jA1bfuqrHTB3IhwBulEgvHffd3QD3RQmyBlMrr7HMjQnRwQd4Io29FU4LQDOXR+EjFmvJqiEqm1EnR7M6DpUlNyMbT88bsV/hY0e37Ou0sq3JYsFjKvTNX8ehJjCNDJzE3kDG74DYe lciwogKN vyLG12rtmcz6Xen1Y9JaZGze0YRvj93nDAmFs8G1qZE189AoDKtltBTjPfTs2a9jG2j8lMaa3AokWBxDsZ7V16uAC3SlBi2sjQoSc9SOXJd8kc7C6uUESWF9KryTTxz07pIDO8bBs/ZimJwTjrKmnIxfzaaQ0L6/jgl2gN9aWO1DlNp7B60tVgPAxA7rXPkFtSE7ufYMsT8Ez8grp/Gwh05+HYAW8tLxiRhLHe4UOYYG77NdvgYM3GV9rnw63kc9KL2TjfuRepfoA8CpT1yBG5S1fIUOQuZBr/pyvSie1InHelybOxn8XXQ9/Z/LN6ZRWpT18uaRAIpkouZ2lf4cRJzCHq3CKhTtGRX0XiwveQhjcZpUfWmzazBNTFU0FHbYvKRWtmiK16C32eJJi3e+7njRply/m/pzMI0+cjMZBj9APc4AjW5leqdTggs514+KlGvGSxd+tbhDtuCMJWn1QW5bdscq4Do0Yzd6DNrbjsMlZYasF3srjl/puToIk67FD6F+eyvYVw2oUINiNNgku7w0LCzjASobkgr7kbtRgTx4lxHvRWLSyLB3n9aZSrRG6Gkqd8NaaqgVNeu84iCiLlD6h8q/xJVR2y5cCTDpWhCg8gfpj+wdHLNSIpocoL86lb7f3Yzk/CT6V270= 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: From: Kairui Song This flag exists temporarily to allow the allocator to bypass the slot cache during freeing, so reclaiming one slot will free the slot immediately. But now we have already removed slot cache usage on freeing, so this flag has no effect now. Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- mm/swapfile.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 5618cd1c4b03..6f2de59c6355 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -158,8 +158,6 @@ static long swap_usage_in_pages(struct swap_info_struct *si) #define TTRS_UNMAPPED 0x2 /* Reclaim the swap entry if swap is getting full */ #define TTRS_FULL 0x4 -/* Reclaim directly, bypass the slot cache and don't touch device lock */ -#define TTRS_DIRECT 0x8 static bool swap_only_has_cache(struct swap_info_struct *si, unsigned long offset, int nr_pages) @@ -257,23 +255,8 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, if (!need_reclaim) goto out_unlock; - if (!(flags & TTRS_DIRECT)) { - /* Free through slot cache */ - delete_from_swap_cache(folio); - folio_set_dirty(folio); - ret = nr_pages; - goto out_unlock; - } - - xa_lock_irq(&address_space->i_pages); - __delete_from_swap_cache(folio, entry, NULL); - xa_unlock_irq(&address_space->i_pages); - folio_ref_sub(folio, nr_pages); + delete_from_swap_cache(folio); folio_set_dirty(folio); - - ci = lock_cluster(si, offset); - swap_entry_range_free(si, ci, entry, nr_pages); - unlock_cluster(ci); ret = nr_pages; out_unlock: folio_unlock(folio); @@ -697,7 +680,7 @@ static bool cluster_reclaim_range(struct swap_info_struct *si, offset++; break; case SWAP_HAS_CACHE: - nr_reclaim = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY | TTRS_DIRECT); + nr_reclaim = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); if (nr_reclaim > 0) offset += nr_reclaim; else @@ -849,7 +832,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) if (READ_ONCE(map[offset]) == SWAP_HAS_CACHE) { spin_unlock(&ci->lock); nr_reclaim = __try_to_reclaim_swap(si, offset, - TTRS_ANYWAY | TTRS_DIRECT); + TTRS_ANYWAY); spin_lock(&ci->lock); if (nr_reclaim) { offset += abs(nr_reclaim); From patchwork Mon Feb 24 18:02:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13988664 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 4215BC021A4 for ; Mon, 24 Feb 2025 18:03:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6FAB280013; Mon, 24 Feb 2025 13:03:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B1CE828000C; Mon, 24 Feb 2025 13:03:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 997FA280013; Mon, 24 Feb 2025 13:03:05 -0500 (EST) 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 7792528000C for ; Mon, 24 Feb 2025 13:03:05 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2D0F4161AC0 for ; Mon, 24 Feb 2025 18:03:05 +0000 (UTC) X-FDA: 83155609530.29.F045E0A Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf13.hostedemail.com (Postfix) with ESMTP id 303C02001F for ; Mon, 24 Feb 2025 18:03:02 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gzqjBBU9; spf=pass (imf13.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740420183; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HFN+prd/q5R/gZ+dsQNPlGWJ7GO+s/LReOSW13ezbxY=; b=YOWBlg5VfkhBZzrUIe9uVIVDyhV3aNvM5KeVNWoABAeDqnTqXMf1zX1YpZofWxJz6N2RkR ce4WTIqXoS134xoTw2K/bIKJS5WGhsUlzlUfyzPdzRtG8lRuVeUsXal1bqox94kdkWjBj8 trv0Eyjd3M4qc5rN79SfwS3JZglxHLM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gzqjBBU9; spf=pass (imf13.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740420183; a=rsa-sha256; cv=none; b=Kw8NsqCENHYvmz6ZHYL4TZq4q79Ic8Ii6Lo5YE1nr8J5JAPR+DpBVqZ3Vu53scKJZ3ARRe WZPMs5EGqk3tGKKlNrgXCuM+0kzuX2QF8qG48wd5w1Eu2sTGrFkhZuypfs6i2C5akp75ze v/VrjK9AKHTQWbHtaFXmRt3R6NH298A= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-220ca204d04so76490775ad.0 for ; Mon, 24 Feb 2025 10:03:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740420182; x=1741024982; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=HFN+prd/q5R/gZ+dsQNPlGWJ7GO+s/LReOSW13ezbxY=; b=gzqjBBU9PaRJ5cPOLfD5QPFTLwQu3RlUR6v+RFUjUXsxW7WJMfbWIKox1IhiCwbA+h 0YVe/9iOEjILMZVavoMVzR2SDcksPYjp2ywRw2NAtdeFSdpbtNGqieJKKs7JaWP6X4ch ewxMN5NNgT6kIS73I5f+9hgWaiL4c4OETOCOWId8MxYeIqbxGnyERG7ixSEhTBbDUoix jmRJmPgVu9wMgmpWdb00JUy1HNj5oP+vxlvj7aY8uMApMvC7vcb+5lCq2v5BAhDkpnAU MrS+9J7VeUDDeO2fDQrTpa4/wfFlxSl0Cg6WKkvuGALpmo9EzZPU/vMiIl1xVQ3DSLKB YA5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740420182; x=1741024982; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=HFN+prd/q5R/gZ+dsQNPlGWJ7GO+s/LReOSW13ezbxY=; b=Pfhssch1HdlUiNZjhse70k16kXeqryVPuHRZr6ZYaSK/323xbK/JnQim6H8YTWdq4H 0bwOp3QNiyW2gYAoAtJfq32gXIXDFSeUklMoQI+lqZeZVaZC1xKDikqSg02bQFfjfyf+ psvowAL7VfaWC/8ibThcEHy7X64kRozIG09qSQkQy8y1dhUlXK2Zqel+UEBWZjfKXdnO 5DF9lGIl/fqYhPYl/L6qfPLjTkdINwfLuipomEAonhh/zNhy1cgzsv9vAg5/0c4qyuIM 1y/2W3I0KmU/reLq3JA5bJk/luVBQ/5/2NhiU3SEFgs5Y1Bw3WQx+/5CY8PrUdXAqfo+ M1ew== X-Gm-Message-State: AOJu0Yy7HItpU40MfNh8Rs7d/nZsbJWRbUPh37DsnnUCCG40scErTKRx SbiLIqtB+bGO9YGVaCOUpQYXwRE1IN/mJd89tiCAh3bwJ5NOzH662b356PjxArg= X-Gm-Gg: ASbGncvdjsNmzO2d3xTdbf6h5geCYjM6Dgs8+TByrDCOAdGljpvXBdWKLqxmvogBcSl ul+SkOghOcCORzVZ7SjvEjMb0pyD0C7JWs/4XiCK/fWHhYgTowUu02f+ZjbonQq/wBsWY+UDjvu xuc+gUWfNx5/gq30774MWaxeciXJf0mUpeZgkHTxpBvt9wWFtekgftL5wEIduk/tH8mj1jsnIw2 xg39z+duGSE8JthicwqBzCAV4kLd5NXAO8AhBa5NMNO3X8JlT5/8JpjEem1lcvaH+VSHklVsenN sVu8u9hcnwTmbIjzbjsWSrLBHHsjJw1tV8gtqELfpM9b X-Google-Smtp-Source: AGHT+IE7STMjzV87+hEURfhIdB6UH8LCMfNhfiVrnK0UHPVcExU8+vGKEEn+3A78YlQKgQnFURWA4g== X-Received: by 2002:a17:902:c950:b0:21f:507b:9ad7 with SMTP id d9443c01a7336-2219ff5e619mr252864045ad.25.1740420181541; Mon, 24 Feb 2025 10:03:01 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556e15esm184834695ad.190.2025.02.24.10.02.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 24 Feb 2025 10:03:01 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Baolin Wang , Kalesh Singh , Matthew Wilcox , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 3/7] mm, swap: avoid redundant swap device pinning Date: Tue, 25 Feb 2025 02:02:08 +0800 Message-ID: <20250224180212.22802-4-ryncsn@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224180212.22802-1-ryncsn@gmail.com> References: <20250224180212.22802-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 303C02001F X-Stat-Signature: nura764zkkei6fa98uwzi5wnh6d66ij6 X-HE-Tag: 1740420182-426539 X-HE-Meta: U2FsdGVkX18X+6qyzcBNQhpU39x5j4FA+NeKzHV2W6uZ41okBa3PkfkO0aizuOQMsQweIG+V6lgEtRmOF0eqrmkQKL9yPxXXuPO307z3ZEqxoQ5e4ts9FmLDwkhZ9dpbAn+hghngNEafl3+OkLUqNC7kLO3KOkIXLIdEoaQ7MZi2XiFfKUDiRa+apPD3ceGd4uKZ3wif12DujiKhuMT2t2NIhv9r6Mkq8Pz/FCUXNll7Pj3gAFi4VER3W60cvO0Lb8bbjXBssUXtQ5MXSL4P8hR3KGursl7EokOH70SAvL5Nfs6Ce3sk7ur1/QCDdT1qs3Vp91UFnTQMn8lUHVtI2tbHqIjzOyAK28uIRtfzxx0UM/dHCtcV2BD2DGHIlrH668/jLbw7jvW0fPtza65H1HsMgOmSlUAb3IweWeMNCcUxHVq6WwWg7NjNcbFlbsMzbddz6hiJtpxtKfmVVL5Zte6mKph7N6H3DZdUlir+mjAPycI4NjBgKVWZeQ028s5W2hFqgANCCC/EG/MwueWBRU49ecHsUv6Hv8c7Gjryk9tuCDS7D7q8Vd0iv1bLnuaZWdUT7YLEXdEWelhTUnas+HoMmA0LyDpaQfI4Z+m+RotrRJr+NNU05/p5mhE2rwkAsMPqzuAsdMP6voFpYQJ5wzKZopEt9Ba1LG8Nbk1tIi46mFbwNbV3WL9hPkEJmpfuDbi1T+47KVRf0mkeHHYKBXATOX8Zt/kITPW1WDpDNZnT/rKu4usXIM1Pbbjy/y0EhqpT6zP9ivgLM/XyWcQU0GdmuV5YqvhrKDnAG/EWKqzD2licKTlOIdxJHooLDMQUgdefr74b4ce7KodRicIocDSG+jR2LPKmJ+XkBBeWSyC/j1yhGRN5rEearjGEO6rbmOwtUGP9RMrS9YnRh1JWKCzFSOF/kVjJy3MIKFWNKBHFd1Ep4zJdqCd1wRuj0LwFDichWK0AEhHtYshdemB nd/huQWn Gbf4dSUjnlsEXcPvDObsENOC0SVHlYgsdkZkizRVzfBx+3vplzasGGpj7sKc+04z9L5T9O85ic5f9mEGfjpcQHaBjtLDH5zOJ8b/Aj5juRLpThyieuqu9bQnGy3b2LK1HCEt8N1hzOeziSGJoRVXJqPXDZ9m99CWWwENhAZR1i7PUUzN1E411VEpj4MAdZSM/x+J6n2OMS7c375o6DrWf7Tl7wssP0bnB5BW1ddzqMyhJ5lTLprvt9LCtF4ph2HMyXLTonx6P/ukAqKDh8EjFMraS7DhdHKsSTiDg1eN+8rFaPpyfuo7AkMutHegX1ACcRVH/BZD50HjlG3ufNONBeQI33t7TelP+p1tw/cnwMchSDu5yja2aiZlIb08Wsv9nfXDowwyI7bfRCZ86zwIoIlmJMd6FvQaBUADjk5wpJ/KbeTB0G50ZFcu+uZ8x/v/zFbas2fUlk6TJvoE8EBZ5Wk0kLc42THpBAvvAR0Yxg+T4cM7pefqMDnajkc3lleANRbyMdgAvG2+sOh84pjajvKkjuSoKT4aNy8/6knVG+dTPnCgryzaBxDQJgr7kNwcKr37c1oeQdmnDViQK6teo1DW0PQ== 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: From: Kairui Song Currently __read_swap_cache_async() has get/put_swap_device() calls to increase/decrease a swap device reference to prevent swapoff. While some of its callers have already held the swap device reference, e.g in do_swap_page() and shmem_swapin_folio() where __read_swap_cache_async() will finally called. Now there are only two callers not holding a swap device reference, so make them hold a reference instead. And drop the get/put_swap_device calls in __read_swap_cache_async. This should reduce the overhead for swap in during page fault slightly. Signed-off-by: Kairui Song Reviewed-by: Baoquan He --- mm/swap_state.c | 14 ++++++++------ mm/zswap.c | 6 ++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index a54b035d6a6c..50840a2887a5 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -426,17 +426,13 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated, bool skip_if_exists) { - struct swap_info_struct *si; + struct swap_info_struct *si = swp_swap_info(entry); struct folio *folio; struct folio *new_folio = NULL; struct folio *result = NULL; void *shadow = NULL; *new_page_allocated = false; - si = get_swap_device(entry); - if (!si) - return NULL; - for (;;) { int err; /* @@ -532,7 +528,6 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, put_swap_folio(new_folio, entry); folio_unlock(new_folio); put_and_return: - put_swap_device(si); if (!(*new_page_allocated) && new_folio) folio_put(new_folio); return result; @@ -552,11 +547,16 @@ struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct vm_area_struct *vma, unsigned long addr, struct swap_iocb **plug) { + struct swap_info_struct *si; bool page_allocated; struct mempolicy *mpol; pgoff_t ilx; struct folio *folio; + si = get_swap_device(entry); + if (!si) + return NULL; + mpol = get_vma_policy(vma, addr, 0, &ilx); folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, &page_allocated, false); @@ -564,6 +564,8 @@ struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, if (page_allocated) swap_read_folio(folio, plug); + + put_swap_device(si); return folio; } diff --git a/mm/zswap.c b/mm/zswap.c index ac9d299e7d0c..83dfa1f9e689 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1051,14 +1051,20 @@ static int zswap_writeback_entry(struct zswap_entry *entry, struct folio *folio; struct mempolicy *mpol; bool folio_was_allocated; + struct swap_info_struct *si; struct writeback_control wbc = { .sync_mode = WB_SYNC_NONE, }; /* try to allocate swap cache folio */ + si = get_swap_device(swpentry); + if (!si) + return -EEXIST; + mpol = get_task_policy(current); folio = __read_swap_cache_async(swpentry, GFP_KERNEL, mpol, NO_INTERLEAVE_INDEX, &folio_was_allocated, true); + put_swap_device(si); if (!folio) return -ENOMEM; From patchwork Mon Feb 24 18:02:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13988665 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 BDE06C021A4 for ; Mon, 24 Feb 2025 18:03:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 50484280014; Mon, 24 Feb 2025 13:03:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B50528000C; Mon, 24 Feb 2025 13:03:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DF6E280014; Mon, 24 Feb 2025 13:03:10 -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 0BC9A28000C for ; Mon, 24 Feb 2025 13:03:10 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 83CE4121435 for ; Mon, 24 Feb 2025 18:03:09 +0000 (UTC) X-FDA: 83155609698.14.5F049D1 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf30.hostedemail.com (Postfix) with ESMTP id 9483F80014 for ; Mon, 24 Feb 2025 18:03:07 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gl5IjUgI; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740420187; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XyR+PRc/eBOLkBnPi/9fuJnUVozTaiV4/upc2f3YPZU=; b=qqPXqFIhkYUErsHdoU5+/ULE8BvHyC4DiTb4IaQzIDZU3OHAKjq/NPCGYgAfRRRBun9PN9 HWepoA7Cuq32UYHwYzWa2Wjx1F+sMUaSkagU5xUtvcZoDApbabzayUh0+bo1m4GUwYhTcm ICOXR6AZjjvsWUm4Vgo+7CWCiofztV0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740420187; a=rsa-sha256; cv=none; b=nbNnusQubLAaDVl/wljNxaLf/ZmoRi1hZWDg35zyoNxcwKDNe76oZ+qH294pB6YxY+v04D 3EdChmMntA38Cdrsl72O7yaHaJBmrfdWhkxqzaj5kt8aKL2tUH0il/gaSmu1AZHMw8WED5 RnmtooL54WpSjfTcwmkzZWP8vvqyhrY= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=gl5IjUgI; spf=pass (imf30.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-222e8d07dc6so22084975ad.1 for ; Mon, 24 Feb 2025 10:03:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740420185; x=1741024985; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=XyR+PRc/eBOLkBnPi/9fuJnUVozTaiV4/upc2f3YPZU=; b=gl5IjUgIgVXBU+R46aF8HGa4vHlrsmF1HKP9jeD+7As8IixYOFCR/lwM2jrbdhTk6/ TZZiRhXTrXw+sX326mrKZttsyaSqAi0uFGMlwGGbuGnPCv3NNRqQyCVcFajXcBGJ6/i9 xbHr2ETT9JzOwOJHC9a4jcshSltdTBBeh+uxccFsSgsGQSL68IgDuuPKATEu9LKQB5zu hR7DxUhxUYQJefY2uyCpliAdNep0+C7Rw+MGPqGYY2NdP86tuTuq2ddCh4Bgqmz49T5p cZyDwIVQLE2/BXjMaFR0eWmhRKz0sSgm8g5z3PgkWg9p5QbW767YYk1WTLUgnXKmk1Rg B+Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740420185; x=1741024985; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=XyR+PRc/eBOLkBnPi/9fuJnUVozTaiV4/upc2f3YPZU=; b=hlJJ6tD1wKvRjP24wqPdvy0pF+GihSbRmRpev8ijm+maQSDMen+hP1OmLqo8XJu7BA glbkRhVmi7TVyiTq1CJG6sUcxHyRD9x6O8+lVYFy/IVtrV6bgugOK+3kRA5XNPw67mdA 3ESh07qMRU38Mx9ruJ7BWADOqGdZIUE72hnvMqFdclKAUGnq8b1XBr2uTya/Ko+dE7oT UbDNEPJ+iazVqpS7+PsNLgdlHoV0+iv54Bd2Gt5Wc7UOYZt5LmSAkN9tsNNjHjJQPVOo IisUcrShRk+OVRdaTmR9rERS114vlwS2/0wD9MEqwjO3PxJho8jHkghy77fsolHzeVEz 4btA== X-Gm-Message-State: AOJu0YyCMiLjXMtO50nyeKi4UVTYpzpnpsipEMu/zNP2pnLVfjYoL8Nd eHx683CGeoBybK+8GEo+qObxTtq+gq2573ninwQERZaVXQhlqeanhaUcHy/MZVw= X-Gm-Gg: ASbGnctLuTaL1hZYCIiZGeybx+l4mAKCIJDuYGu8OwCZF+PR5z76OpcldBr3wFQDFgD 8EL8/CmgfMK6DOQbiswBW2Fzq0yvaX+0lsJk06sMFfwGfx7q7Ysd5v4qrXCXozAp88mJf1e8oM+ GVDIrxo2Pae0LbHaz7vUf0Exu92k3+wEi9c449ZRmEE9l/72gAf671EuQn0+Co6VFTGktS0o+H+ MsM2+lWJnwqmIF7bv4xztwOcL3ITIWegJhIwPvXIIwSHTxmwBl71Wv+W9L/WrTJ5jcFyR/n8NmE /+PMioXFayMvTNwUD6KGJASvnxZxI4f1fvNShy1JC5M2 X-Google-Smtp-Source: AGHT+IEWFKdxJ4VlDrM+6lJej0/o+uztgn5SKjZ2meUIah0cwkjkOUeetzc3bGXlgudO6SvyG97xhQ== X-Received: by 2002:a17:903:2312:b0:221:1eac:bf7a with SMTP id d9443c01a7336-2218c765b1amr310546725ad.24.1740420185393; Mon, 24 Feb 2025 10:03:05 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556e15esm184834695ad.190.2025.02.24.10.03.01 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 24 Feb 2025 10:03:04 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Baolin Wang , Kalesh Singh , Matthew Wilcox , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 4/7] mm, swap: don't update the counter up-front Date: Tue, 25 Feb 2025 02:02:09 +0800 Message-ID: <20250224180212.22802-5-ryncsn@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224180212.22802-1-ryncsn@gmail.com> References: <20250224180212.22802-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 9483F80014 X-Rspamd-Server: rspam07 X-Stat-Signature: 3oteu4prs9znszb37hhn8pc749o6qmti X-HE-Tag: 1740420187-678472 X-HE-Meta: U2FsdGVkX18pzivHT4WFut/9Q7RDB+1nfP8FXTxGTT8w7ZayjZMNk3KqwI8lt/CxVSSty37sjV2/m3p3tt7/rjS5dGJy2fUFYB9v47kjseswIfr6hmiAElcAA+bx0H6swisJVltlt9zHQwvzriivOlqBg3pcr0+blm+iR3upyG96zpCbJyerpmz+NY4vVu+QDV0414mD/w10QI4ogCzEn/o0fD02bmb4YW8aMmSOHeJ/RTMSZorojvFp/y0ApdO9urvf832ahL7OkM1/MYzmxe5IBUsYUGKfpOX+hYQnUoAdlXE9StG96TkMexR8QGuQRaC2l7Lnuh3XAX9MjTDoYNLY6N+xjdly8Q06SNn2L/NjDRGjvqL0CD3iGFVJjcSa1ev88/r7vpCrdW8PPIQQ9830pO3heXGZb4u1qvW5pTko8PYn7fgzbelkR08Ll0CJ4mPde+S9ZlfQFMiaeQcBVdq1wvlFRBmkF/jEefHqXuDFsGXvVR/xnuHr7Dp1PxXqvtVqCpD4pP0/9oim6FBZFWje09BqdcKkh0XlRMy9RhdY6FcM+Wfi64pzIP6ZY0JnOuoamTl3I9T536xX0QJU3C+d4gUEpo2wCFF00CAsPK2vps3R6rKcpJV8lDw7hRqWvnJvUTbC1Lk4ggZxLOhzwKYJf26VaWCGGPgrtgME62Kgxmd9RYTYEc7W/ypjfExvypxYJkjfVEQ4ZY5pROQbG76igoLAYxPTdvXwy8kJgv3EbG1O7VupFGB8ZygqDnynsLrrz2aTMF6BVkuStVHLVNcebTyFvqS1ThRACwu0XqNZYur1at/nS/1bkRUizCC2Pcvon+VGWXW3smbB5u1q9dx3YaW9cGwxMgGJHJvB+RWOqHLfcsxewDJ2qb9NZN/ZNMOvm+jd+Rxjqdh0fG8fPax4moHfSyYpaNxnwvwz1GVNRiVv9YybZx2GR852KN8Q2aHJ+ewB5MTFwwPpsIQ rqTy8AJM HWFs4evGQflevYAJHEJP21bjpK0bN7uY/b1TGGAsD84yntOP5Nc6AvaMtkCk8UkMTeiU729B43gHgxH03FvwyV38cuSCexmQteUGgjjbF/eJZAFaX5kmDMfjtjyQKDU4jqK2CZnlKQRX8iCK3w84hNQsIHh4vkyyb/EhD4ZyuNyKJfgKRTwHsQMb6PFOn+827Vfj+7+IDK6MbhU+s05cneLz/7XiZTL7EdaWnaUxbZ6V5UGbPJYZC6QqOGhOTXtSFnK5atSSmaShYrJUMA1dt+WrDfiuiC3NFp6e2C68Rx2cr2GNhBhc+pkLud4o6Wg/cbeNmkVRkzJk1OhCHBqw2TbanqkFRiJZo25g8IroBDxOa8wM30ZF27GidGtTMK828i2nsbJ6/OfZcifT2p/qtdFjZp9JPDB4Pe3NiZCcz344obSYn+zon5BXevQQVPHE60Px8s86bpp4eSNaIaGC1oFg5o0MKL58OcCHMHHKoKfZi83V3KejU7eob4nBWYF9jotskyqzy9sf2H4LTa7W0XsWdsvDc0UiU6XugdPwsQLGObbhyrUIqV1XV+aohsLtZF/89 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: From: Kairui Song The counter update before allocation design was useful to avoid unnecessary scan when device is full, so it will abort early if the counter indicates the device is full. But that is an uncommon case, and now scanning of a full device is very fast, so the up-front update is not helpful any more. Remove it and simplify the slot allocation logic. Signed-off-by: Kairui Song --- mm/swapfile.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 6f2de59c6355..db836670c334 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1201,22 +1201,10 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) int order = swap_entry_order(entry_order); unsigned long size = 1 << order; struct swap_info_struct *si, *next; - long avail_pgs; int n_ret = 0; int node; spin_lock(&swap_avail_lock); - - avail_pgs = atomic_long_read(&nr_swap_pages) / size; - if (avail_pgs <= 0) { - spin_unlock(&swap_avail_lock); - goto noswap; - } - - n_goal = min3((long)n_goal, (long)SWAP_BATCH, avail_pgs); - - atomic_long_sub(n_goal * size, &nr_swap_pages); - start_over: node = numa_node_id(); plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { @@ -1250,10 +1238,8 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) spin_unlock(&swap_avail_lock); check_out: - if (n_ret < n_goal) - atomic_long_add((long)(n_goal - n_ret) * size, - &nr_swap_pages); -noswap: + atomic_long_sub(n_ret * size, &nr_swap_pages); + return n_ret; } From patchwork Mon Feb 24 18:02:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13988666 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 29FF9C021A4 for ; Mon, 24 Feb 2025 18:03:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6D2D280015; Mon, 24 Feb 2025 13:03:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B1C1328000C; Mon, 24 Feb 2025 13:03:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94918280015; Mon, 24 Feb 2025 13:03:13 -0500 (EST) 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 6E0AF28000C for ; Mon, 24 Feb 2025 13:03:13 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 020B1C19BC for ; Mon, 24 Feb 2025 18:03:12 +0000 (UTC) X-FDA: 83155609866.09.A7CD16A Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf11.hostedemail.com (Postfix) with ESMTP id 1D7654001F for ; Mon, 24 Feb 2025 18:03:10 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=e+ruaV21; spf=pass (imf11.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740420191; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WYFJk5aGTp6MYsHBjRjRVr+n+yHW6h7e82rjV++fDV8=; b=yAkODYVDpRp6osl7BXNJuMK7ZUnf0HmrwxjJYXguQKUkfT4Hq0R8LEsvEJxnyDG2EJESrm CHvyPDSzHQCncJC8DjmKbPSqBB367nwvcTH9RHyyn2igMKhK3OGtVCUMU22YeSi0GwZldo NBuJQrDT6Bslt3tml7TmBCwVHQ9LIyE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=e+ruaV21; spf=pass (imf11.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740420191; a=rsa-sha256; cv=none; b=0u959LFh0jZ1M5XXLNy66EZkpyyTH2ldSY25tVFi8Iq2O7LslNXEny7I5wCgmmz7EA9sFm zzqf7EI677REY6a/WR04CNJzjy1X/HPtdW9vP4BbaEWu9pVd3+4FAQofkZ7z3fREmIw6j5 j9G8iGDSsi9zkIn7mTkg2shAL7IVmyQ= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-22128b7d587so91369545ad.3 for ; Mon, 24 Feb 2025 10:03:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740420189; x=1741024989; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=WYFJk5aGTp6MYsHBjRjRVr+n+yHW6h7e82rjV++fDV8=; b=e+ruaV216gjbDejYfDqUZ0LuGz8fEyXumYOFuNHFMsktjHJZ9/p87qi+hzluUoFrF3 OO5klXSgrTcMbKCZeTS0jIXE/t37sdB3lmcxLCy8rCCWcjpVMC9XQhk8EKyTGt/fHP7N xGEEkuIyf24/x/d/PqzH1mpjgCMSSGFPvET/45Xkr4mWuS7S8upXwtxdUMIahkx5irPK Zp2Ai3zHjtCeZCmgROAzny1V0kA/qwUMVWW5F0ebi4B7ECd6oi/J10PyULBhv6LPeeXV JCNfgILt1wHNrBo4RTHP/B4La1+5/0X2kQ6kabLC+SikhmapR2lR6/X8Kgnhtjg7Mhkg GtiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740420189; x=1741024989; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=WYFJk5aGTp6MYsHBjRjRVr+n+yHW6h7e82rjV++fDV8=; b=deZsL/mO/cqQ48e2YsmV9RwuvpodgoCZzrgT6GhQnJ33O6hKr9WObmWXifqLE6J8d1 HiLgZVmfw8oGHuStsho+wZ7ANLjMAOHsfNo6RGqy/VJRTslnkjJ80cDUgUCOwEtDdrZd F53KcVH0RtcaXqEIA0bCgPoI/EViAi21P74oIZIzwKy0FvezNfvnLKyR+s5TESVMcWK3 mjE7GfLbtvO3kqhYDcBmdNbvmmuneK33u/hjb9h0HUZsIaDz+7mAh2mPu0S30up29p0E 4lsFkZAK4Iptr6J6CZIrkd57UVDbCRWv5TGBDn0jToQJHg+E/28UlQOAf3N6xl8+UJ03 skVw== X-Gm-Message-State: AOJu0Yw0v1sFGtPOLIAQNS2HruGLDl5zd9qnKuj35Ys1eIxc8OKQ+FgY wYF+J0U8tdyDQ4a5+1ZqC4U2KxVX5DrhRsrEghGBnLsf741mkYJiG73YJkm59sQ= X-Gm-Gg: ASbGnct021Mw2e/GS3MyHyYUUJ5B0iMgWMlt4RBXXgkh+7mGqhleIThXbbA9SOiR/zI hlz5XZDe8fwUsvItXxs2XNSzgzB6T/sATy0K0a5krNpRFkC8LVX1wBuwQKlc59ukdotO+hu0wii e7CacgTj1/rlU0ULGG5aXDpKVOBAg/TEEgXzbhuBxWWAdO7AB+Z59klVpo4hJKBVeUbW7+w+vv+ t8aaMesnay39qP/axuJ1d2gt895Drcc4kr4XTjlvq/3UTH1VxtGqSg2mCdFET/tsFtRsqQBcjow UfMpo0EMcXzLS7fSNyxYfpPFL6Gy+YpU3QfqutpIh+Ii X-Google-Smtp-Source: AGHT+IF+nUtt9yT1whYy8czY+uFBQEuZI14FZHiJnUYmIVHKhu2fnDX//MoXY7yrvfn2QU5ZdSXwoA== X-Received: by 2002:a17:902:d502:b0:21f:988d:5758 with SMTP id d9443c01a7336-2219ffd288emr253625875ad.35.1740420189294; Mon, 24 Feb 2025 10:03:09 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556e15esm184834695ad.190.2025.02.24.10.03.05 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 24 Feb 2025 10:03:08 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Baolin Wang , Kalesh Singh , Matthew Wilcox , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 5/7] mm, swap: use percpu cluster as allocation fast path Date: Tue, 25 Feb 2025 02:02:10 +0800 Message-ID: <20250224180212.22802-6-ryncsn@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224180212.22802-1-ryncsn@gmail.com> References: <20250224180212.22802-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 1D7654001F X-Stat-Signature: 9cbnq3ia8wj96ppwxjrkpauxxupcb75b X-HE-Tag: 1740420190-747593 X-HE-Meta: U2FsdGVkX1+HrB5epWLhIQgIlFFwIY94NVMoQ8r0eu9vXC4wZ0NAaIf+6TN0m6uQ1Kapi0v74lDjHCrNK4ZGb5KqJwLXwmNIDib5sS3w8VNJTcZPqq16FaidRgsC6JTrKqUVUOdUbAFjuk2GDVCtn8WZ0wTXg1Lfj5wV+yR2RVIkBdK3ziu3RRnNZFOz/ZOEKVPNlaG2CNcyG0S71WBe+jdHXNjTzrZPz6YmnElX1f3zOSymsq9/Y7ScszeZST8gLU6pQXYz1nBPGoj6ySzEA/mCU/o1PIEzHk1uUeBmBhCQIbJb9fdleKxmZhGhtCW0qYxo7f1iPX3J1YecndgvfqKXYpDjgmla36qFpkpVsULwu2CAMzB0ipNSY7Iy/NXbJjKT+OEYwPWzctp55Kko6C8w/Jk5yskq4BEZf8IxpnNnJqr3yY0+7Ny8NC5qNZzwxtXowoAJypSq8qeKcEQhc9RyXyslKWjWPDjI+nISu5rlthFjKSt/Z/TVGwLLT/QQUlCj3NRH66e3BMwmF3Qnf+WcYTXjCCRyzptqofMKoKbuWH5R8qwAo1PTzcnQS3nabAry97DVmZx0572vBxCFl7/Cdz1mTxMRXCh7aeCE2JktfJIS4zVYP5Ktun4CvzeGw9oyqsUXaRwT2lksVZLh+R0BzpRc1HaZua+ss+R4Wq+Pe4fLL4IuLBCjlshvs2lckL1MhXJTEvoIWghhAJ6Pnu6zeY+o0jA+6+seW4xF30weMvP8cuw4h5xehdfjBh1gqHR1YMb7VpIAO0GaoXgBaQm3TnkgoIl4svS2BlJ8CCBPOO1nZdtTWDTHBrpDS5yyQka/kL/m2VvQ2llIliBHfCP2IyUyZ/ttfCb/i+ECR+tgY2QTu2gVsz3fp136HDV583hHXJ4ejvAmiCrWjeYFdQf1cXze/uehQl5nPJkj4XQTbpRSc1QYWcSdOcxoWy/yvOW/ltcGumekj8uGEJw VSZgIY6z BLI32PepUWPzRZjfy0izdrW24U1qGuAX4uNXvHbYLULvA5ZBw70/4aKY962gq06atYEtyqnuTcMl7Yn0ws4HPla0b9J/0+lBGKxxPxglQCAZm9SipQxzSPuCJswIzdjbS8XznUsnT7vtqvtdyW64aU+olDzRJTVvEB2qDgJj57Lo65IJLzliN8PHZ4nVVvwkG465tgckTpykDXXERbyB2UHqceOVErtl9afZBiCyC+dTHPqEHYaA/HCwmT1RJEtHk8oM472CXCC3H0slzp0z+mj9pPGRV8prucHhPMSoQlXqP7tg+/A1K/e/GQH4z6z8GZAmJmgoHGJuFb5bZx8i5AExi0SRSS/T1tgfeS/EWDuJMxfvGUoqYB9b5LHXsMw8RxRU359vbIMo7VG9Pfb+TF2DXx1OveqlJW3cSUYGhzLhP7JsxVLvFUbLZdxuFAKpc9AgM8raw7yopsldRZWp0ea9qVSFEGY9XNnt2gEo3LW/KIRLYwJkJ9Eks/FpFNxvVgmzFuMNoG26uS/u2NEcMH5/patL/2AMJc9ef6+T9rbTq0N/AUqzDMvQN4P8vJZPcZtyc 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: From: Kairui Song Current allocation workflow first traverses the plist with a global lock held, after choosing a device, it uses the percpu cluster on that swap device. This commit moves the percpu cluster variable out of being tied to individual swap devices, making it a global percpu variable, and will be used directly for allocation as a fast path. The global percpu cluster variable will never point to a HDD device, and allocations on a HDD device are still globally serialized. This improves the allocator performance and prepares for removal of the slot cache in later commits. There shouldn't be much observable behavior change, except one thing: this changes how swap device allocation rotation works. Currently, each allocation will rotate the plist, and because of the existence of slot cache (one order 0 allocation usually returns 64 entries), swap devices of the same priority are rotated for every 64 order 0 entries consumed. High order allocations are different, they will bypass the slot cache, and so swap device is rotated for every 16K, 32K, or up to 2M allocation. The rotation rule was never clearly defined or documented, it was changed several times without mentioning. After this commit, and once slot cache is gone in later commits, swap device rotation will happen for every consumed cluster. Ideally non-HDD devices will be rotated if 2M space has been consumed for each order. Fragmented clusters will rotate the device faster, which seems OK. HDD devices is rotated for every allocation regardless of the allocation order, which should be OK too and trivial. This commit also slightly changes allocation behaviour for slot cache. The new added cluster allocation fast path may allocate entries from different device to the slot cache, this is not observable from user space, only impact performance very slightly, and slot cache will be just gone in next commit, so this can be ignored. Signed-off-by: Kairui Song --- include/linux/swap.h | 11 ++-- mm/swapfile.c | 136 +++++++++++++++++++++++++++++-------------- 2 files changed, 95 insertions(+), 52 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 2fe91c293636..374bffc87427 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -284,12 +284,10 @@ enum swap_cluster_flags { #endif /* - * We assign a cluster to each CPU, so each CPU can allocate swap entry from - * its own cluster and swapout sequentially. The purpose is to optimize swapout - * throughput. + * We keep using same cluster for rotational device so IO will be sequential. + * The purpose is to optimize SWAP throughput on these device. */ -struct percpu_cluster { - local_lock_t lock; /* Protect the percpu_cluster above */ +struct swap_sequential_cluster { unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */ }; @@ -315,8 +313,7 @@ struct swap_info_struct { atomic_long_t frag_cluster_nr[SWAP_NR_ORDERS]; unsigned int pages; /* total of usable pages of swap */ atomic_long_t inuse_pages; /* number of those currently in use */ - struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap location */ - struct percpu_cluster *global_cluster; /* Use one global cluster for rotating device */ + struct swap_sequential_cluster *global_cluster; /* Use one global cluster for rotating device */ spinlock_t global_cluster_lock; /* Serialize usage of global cluster */ struct rb_root swap_extent_root;/* root of the swap extent rbtree */ struct block_device *bdev; /* swap device or bdev of swap file */ diff --git a/mm/swapfile.c b/mm/swapfile.c index db836670c334..7caaaea95408 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -116,6 +116,18 @@ static atomic_t proc_poll_event = ATOMIC_INIT(0); atomic_t nr_rotate_swap = ATOMIC_INIT(0); +struct percpu_swap_cluster { + struct swap_info_struct *si[SWAP_NR_ORDERS]; + unsigned long offset[SWAP_NR_ORDERS]; + local_lock_t lock; +}; + +static DEFINE_PER_CPU(struct percpu_swap_cluster, percpu_swap_cluster) = { + .si = { NULL }, + .offset = { SWAP_ENTRY_INVALID }, + .lock = INIT_LOCAL_LOCK(), +}; + static struct swap_info_struct *swap_type_to_swap_info(int type) { if (type >= MAX_SWAPFILES) @@ -539,7 +551,7 @@ static bool swap_do_scheduled_discard(struct swap_info_struct *si) ci = list_first_entry(&si->discard_clusters, struct swap_cluster_info, list); /* * Delete the cluster from list to prepare for discard, but keep - * the CLUSTER_FLAG_DISCARD flag, there could be percpu_cluster + * the CLUSTER_FLAG_DISCARD flag, percpu_swap_cluster could be * pointing to it, or ran into by relocate_cluster. */ list_del(&ci->list); @@ -805,10 +817,12 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, out: relocate_cluster(si, ci); unlock_cluster(ci); - if (si->flags & SWP_SOLIDSTATE) - __this_cpu_write(si->percpu_cluster->next[order], next); - else + if (si->flags & SWP_SOLIDSTATE) { + __this_cpu_write(percpu_swap_cluster.si[order], si); + __this_cpu_write(percpu_swap_cluster.offset[order], next); + } else { si->global_cluster->next[order] = next; + } return found; } @@ -862,9 +876,8 @@ static void swap_reclaim_work(struct work_struct *work) } /* - * Try to get swap entries with specified order from current cpu's swap entry - * pool (a cluster). This might involve allocating a new cluster for current CPU - * too. + * Try to allocate swap entries with specified order and try set a new + * cluster for current CPU too. */ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int order, unsigned char usage) @@ -872,18 +885,12 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o struct swap_cluster_info *ci; unsigned int offset, found = 0; - if (si->flags & SWP_SOLIDSTATE) { - /* Fast path using per CPU cluster */ - local_lock(&si->percpu_cluster->lock); - offset = __this_cpu_read(si->percpu_cluster->next[order]); - } else { + if (!(si->flags & SWP_SOLIDSTATE)) { /* Serialize HDD SWAP allocation for each device. */ spin_lock(&si->global_cluster_lock); offset = si->global_cluster->next[order]; - } - - if (offset) { ci = lock_cluster(si, offset); + /* Cluster could have been used by another order */ if (cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) @@ -973,9 +980,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o } } done: - if (si->flags & SWP_SOLIDSTATE) - local_unlock(&si->percpu_cluster->lock); - else + if (!(si->flags & SWP_SOLIDSTATE)) spin_unlock(&si->global_cluster_lock); return found; } @@ -1196,6 +1201,49 @@ static bool get_swap_device_info(struct swap_info_struct *si) return true; } +/* + * Fast path try to get swap entries with specified order from current + * CPU's swap entry pool (a cluster). + */ +static int swap_alloc_fast(swp_entry_t entries[], + unsigned char usage, + int order, int n_goal) +{ + struct swap_cluster_info *ci; + struct swap_info_struct *si; + unsigned int offset, found; + int n_ret = 0; + + n_goal = min(n_goal, SWAP_BATCH); + + /* + * Once allocated, swap_info_struct will never be completely freed, + * so checking it's liveness by get_swap_device_info is enough. + */ + si = __this_cpu_read(percpu_swap_cluster.si[order]); + offset = __this_cpu_read(percpu_swap_cluster.offset[order]); + if (!si || !offset || !get_swap_device_info(si)) + return 0; + + while (offset) { + ci = lock_cluster(si, offset); + if (!cluster_is_usable(ci, order)) + break; + if (cluster_is_empty(ci)) + offset = cluster_offset(si, ci); + found = alloc_swap_scan_cluster(si, ci, offset, order, usage); + if (!found) + break; + entries[n_ret++] = swp_entry(si->type, found); + if (n_ret == n_goal) + break; + offset = __this_cpu_read(percpu_swap_cluster.offset[order]); + } + + put_swap_device(si); + return n_ret; +} + int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) { int order = swap_entry_order(entry_order); @@ -1204,19 +1252,36 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) int n_ret = 0; int node; + /* Fast path using percpu cluster */ + local_lock(&percpu_swap_cluster.lock); + n_ret = swap_alloc_fast(swp_entries, + SWAP_HAS_CACHE, + order, n_goal); + if (n_ret == n_goal) + goto out; + + n_goal = min_t(int, n_goal - n_ret, SWAP_BATCH); + /* Rotate the device and switch to a new cluster */ spin_lock(&swap_avail_lock); start_over: node = numa_node_id(); plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { - /* requeue si to after same-priority siblings */ plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); spin_unlock(&swap_avail_lock); if (get_swap_device_info(si)) { - n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, - n_goal, swp_entries, order); + /* + * For order 0 allocation, try best to fill the request + * as it's used by slot cache. + * + * For mTHP allocation, it always have n_goal == 1, + * and falling a mTHP swapin will just make the caller + * fallback to order 0 allocation, so just bail out. + */ + n_ret += scan_swap_map_slots(si, SWAP_HAS_CACHE, n_goal, + swp_entries + n_ret, order); put_swap_device(si); if (n_ret || size > 1) - goto check_out; + goto out; } spin_lock(&swap_avail_lock); @@ -1234,12 +1299,10 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) if (plist_node_empty(&next->avail_lists[node])) goto start_over; } - spin_unlock(&swap_avail_lock); - -check_out: +out: + local_unlock(&percpu_swap_cluster.lock); atomic_long_sub(n_ret * size, &nr_swap_pages); - return n_ret; } @@ -2725,8 +2788,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) arch_swap_invalidate_area(p->type); zswap_swapoff(p->type); mutex_unlock(&swapon_mutex); - free_percpu(p->percpu_cluster); - p->percpu_cluster = NULL; kfree(p->global_cluster); p->global_cluster = NULL; vfree(swap_map); @@ -3125,7 +3186,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); struct swap_cluster_info *cluster_info; unsigned long i, j, idx; - int cpu, err = -ENOMEM; + int err = -ENOMEM; cluster_info = kvcalloc(nr_clusters, sizeof(*cluster_info), GFP_KERNEL); if (!cluster_info) @@ -3134,20 +3195,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, for (i = 0; i < nr_clusters; i++) spin_lock_init(&cluster_info[i].lock); - if (si->flags & SWP_SOLIDSTATE) { - si->percpu_cluster = alloc_percpu(struct percpu_cluster); - if (!si->percpu_cluster) - goto err_free; - - for_each_possible_cpu(cpu) { - struct percpu_cluster *cluster; - - cluster = per_cpu_ptr(si->percpu_cluster, cpu); - for (i = 0; i < SWAP_NR_ORDERS; i++) - cluster->next[i] = SWAP_ENTRY_INVALID; - local_lock_init(&cluster->lock); - } - } else { + if (!(si->flags & SWP_SOLIDSTATE)) { si->global_cluster = kmalloc(sizeof(*si->global_cluster), GFP_KERNEL); if (!si->global_cluster) @@ -3424,8 +3472,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) bad_swap_unlock_inode: inode_unlock(inode); bad_swap: - free_percpu(si->percpu_cluster); - si->percpu_cluster = NULL; kfree(si->global_cluster); si->global_cluster = NULL; inode = NULL; From patchwork Mon Feb 24 18:02:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13988667 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 A8999C021A4 for ; Mon, 24 Feb 2025 18:03:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 358796B0083; Mon, 24 Feb 2025 13:03:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 305D26B0089; Mon, 24 Feb 2025 13:03:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 131D86B008A; Mon, 24 Feb 2025 13:03:18 -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 DD95A6B0083 for ; Mon, 24 Feb 2025 13:03:17 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4A58E1C7358 for ; Mon, 24 Feb 2025 18:03:17 +0000 (UTC) X-FDA: 83155610034.11.72C63D1 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf20.hostedemail.com (Postfix) with ESMTP id 1D1F01C0005 for ; Mon, 24 Feb 2025 18:03:14 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YlBoRqnY; spf=pass (imf20.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740420195; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=z/H/eT26mOgQNU3BllgWxVbfjIUbjkvzBf7y7ZTGGVA=; b=yHBAGdTA40q4WAhelXvS8XwCmJxyeJLKgB50pSTvM/XPyRXfy5WP+w06tktJT9TaDaL4sQ WazJyh2hl4T95uo4trXX44VstYcmQnsGhGKNJfppgbrfi9df9ryVC9OVUzWglmYYDOtoIN h0gCa5rDKQuwhIZF4zNImtnOHdUgCjI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YlBoRqnY; spf=pass (imf20.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740420195; a=rsa-sha256; cv=none; b=2qXexndAdxFlMCmkzGHyZhDTLJsT3xnkHMUrKYihd6jty9s5HcSS1Plw0hQNM62kgOD7Bw 0UsrS6b0I3jWto020nE1Kbl+q6JCUtnVH2+do490hNEBr3KdfHgr8mDU8PN3GB8ceQqiJs ovlwsVamVRkVbm2G+V53W8U3+NESRjY= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-220d398bea9so75080095ad.3 for ; Mon, 24 Feb 2025 10:03:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740420193; x=1741024993; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=z/H/eT26mOgQNU3BllgWxVbfjIUbjkvzBf7y7ZTGGVA=; b=YlBoRqnYlLJ4GLK6dyCq1vRTkAwLzTKIvRzXKVHFU9vduP1VZ0Mn9Q2r//5puJyEtW xZG0Ubje+FahPJpWIprCfKMstWAFJX6ZCZG4umPc0E8jYKs4CrbmMhy2gboCoShC1nmV qhKxoVMUwe/hRROk3Mkz/Uo9VMg9Vvm2XEFCglEdZGj2iwgXgXFrYndrC19MdnsF6Z67 AKb74fUpcFQzy6NyzBPyVdDT9eoPWsVICGnGH9RkUZzuMSsWFLeYpENZyAiTBGio6Pql DAOzeOfDgy/zlOLeAsDin2qe1ywd6ZtflRRUW8Uni5TUwJqBavkKPivUge87zrcmfF0k uLMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740420193; x=1741024993; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=z/H/eT26mOgQNU3BllgWxVbfjIUbjkvzBf7y7ZTGGVA=; b=mMbnD+Ko8CJRb90qZSXKlCeMwr9Z/u6vzA2wS4kCyPkPl3IdSKvu9p6mZcL7QQ0/8J /T3+bapSdHFUjKNQdFce6Bnu9yZwlEHbTF2xKljO1bSjxZyQ5voHGiVprU3wDSOAqxgO PO0anj8yKOY5G393z9IMtt0WX+e3ZYRrHLMTwRzRdZs+joPX3m3PNTUQ93PlxOa9fzCj cYiNOFw3Itac6OeouuUMaHK8kmq56XiWrPjvW57jS8xq/aJhZ8SWt4lDJwbJP06VvoTJ Nbj8vINxIGmD0zx1Z+BoO1yXAfJz+vixzUpOUyLfaU65xadX9uAp9ElCA+8/p4zK28dd 6whg== X-Gm-Message-State: AOJu0YycKuROPnbGOOzbPKVv8phN1tv86Mt0Ob95ryNVI14CVSFZBx1w bd70Np+mSAE7QunAFwKsK/rHhRAx1qsMpNTpQ5UaPgAftHFhzg6u8+hqunWWyek= X-Gm-Gg: ASbGncvSjV2KZcBxGE0JbC7kxe4eG67rtE2aSHTQ2uLZLjzyHsVeRTBlvorGYxTy0Hg 9xgNwKl2BHKBlsT1xU+ebTiIQYLP2LJ2VixPepTJzpeMbZfw2A94tMk2VIVwg2PdGlQemTCmBMV DktMopLFqq4BtcyqwAOc8ifF/Md2zyt2PjR89tOA705yay/e4K+rixFZk/+ylvfywZ65UfjVn7k /xx2Lsq5vOMxudquLIqpy2w8rP3lit+WoW5ibeTlZ7xqf2HodYkkic7YxFZopTb8560tg1r8B9A vWAHtBCM6kgHBPY9oTN2B+T2/NW0CPDQ5QTECKFiKbnV X-Google-Smtp-Source: AGHT+IGUDqNjKzzodPdbBw61i2d6PWv+5jYpVz5ZaK/dw7xN7n6YgLYt5rhN5Rse7OqlMkN3KAlLxw== X-Received: by 2002:a17:903:41cc:b0:220:d5a4:3ac with SMTP id d9443c01a7336-221a11b9809mr212857675ad.45.1740420193321; Mon, 24 Feb 2025 10:03:13 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556e15esm184834695ad.190.2025.02.24.10.03.09 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 24 Feb 2025 10:03:12 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Baolin Wang , Kalesh Singh , Matthew Wilcox , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 6/7] mm, swap: remove swap slot cache Date: Tue, 25 Feb 2025 02:02:11 +0800 Message-ID: <20250224180212.22802-7-ryncsn@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224180212.22802-1-ryncsn@gmail.com> References: <20250224180212.22802-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Queue-Id: 1D1F01C0005 X-Stat-Signature: ij4ajq4mbow4uyakh5rsz4pbth7afrtf X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1740420194-668278 X-HE-Meta: U2FsdGVkX1+RtEids9eBwa+PswH2mLH9DzSzxDCqvENvoJcKSaq9rwWbXu1rzvi1yDxGTLEulGAHzT3zUOuqpSA++fnSPWTk6AbkzjSTWBeaFAEMgSRVtK24JCBWjXcLek3lViha5DZJ5yEQtqBaItthx7p7hOtRbIs3c8w2zPTd38EaXiXA5yaSKGDLtwrlMTwPMibwXUudM4vxqPGO+4lCppISuK77yn9NWpw1vcVeaFlReFQPWpNXybCYoeFQqkKtw9U2fXBdQRZ1kl6A970N6H8nk5KaHG5Lwenp9PiYRpBrNkDq/vehf254PKbpn6D5lUPnJqGEHta8KFn1j0UeSWyeRXlhY2mowu88hhu1XliGbStwg+5ORdKjqeMfkacAQcFidy75NeyW0NmrgDl3XnFJqJ7K/bkwNI/MCTjLxhlkPpwRB6xVGSUHssmv6CkBCMBItjPqNVqnSwtmlaESxmxvNKuP7Tsl47titHmaX7Y0f8qqTjvpNy3yrX9ImKbXqhAldH3Worm40PvJlyBSEcecxlMt5gIsIsmPmkmgy3h99osUVVxVpTt6Tlci6SDtYkZd+XztxRpq/DIGSNR6wQNiweH29fLEU6y40Nvn6mhfmdQMKHnQ9U4Oe/OiIntX9yTeXXsZiPWV3sVXiXkciV65PZlvb1K+8hIQ5zaKwgmriGecXsgHvCHiWy2PyHLIrSGLfim+euXZuomSiiWh0ADlIQX9U0imqLSsiPAzS5CF9fhyD18QlMVLBIWDQFtqZgy4k8sIxJ1x/g+YFXjbVJu9JH/+OX9NDNEdMZVUI4f99y0TTmnlY1OhjyOvXMWAWRZz/hWYsbsaxN5VnFHe5Lo1xe9g01pH4jWCVjFIRsN6xvfYkHiZDYrSuMFSpfP+Qn1yKPYkdB15IoxMkP2+QnvVVMDYHDbsxjSIHJa6z2xgaIGtnW7SezO8NU1enQX8DA7ymfxe9nFBC7K S9TqsHpy QMgu2r7V7VkFzS/JWWt6kl1RyhU/T/OqyI0PCEEaVDl+wR34elXSwsRpBOyZqLbCzUussmNbpR1dE4eJIti+Xc3ByMQvHIIfl/zTmc/RASw3xhtbxG4VGr/g8bFFzI6PkHcrWj5GtqrUOrbUl6LCeSotG+NpizXMHJq8oLtmuu2CGOSTyzvzOzC9oxHKrGjzY/svJV0i6r+VRfX8w3AqQcYNIACb7/IKxuERb2PjNtl83q/cDZVUdF2DgrcHTqVGMGzkI4ZdvWseqIgfNmHRl3BHEs8FeB3TzA8Lcfet7UcJ/1HBFA2FsWHqx7oEnTo1JIH+YfeynpnpVN+/GNcTO9q2ZzUVnPnnRYpMLGBZgV+RM1JFC0r2U7nTJkDanv8iitc2v2OhvxmsPiX6aK3W268Yw/WyrDkbeBL19CtMvcz4skLED620gvOXDNCUNF404eRICVKo+fzVbczjB58WpkTTkqjgQaS1l9ze8JG0QRQdBZ9rrWEziRiCCcCHPjz01RAfapxfMiEXX6WFUe59G3giZDYT0e6RTPG48+Ol0pPENEwIs9MPMl17CP3mlc6A1HdKQIf0z+0MvIx3JioziyPXuoQ== 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: From: Kairui Song Slot cache is no longer needed now, removing it and all related code. - vm-scalability with: `usemem --init-time -O -y -x -R -31 1G`, 12G memory cgroup using simulated pmem as SWAP (32G pmem, 32 CPUs), 16 test runs for each case, measuring the total throughput: Before (KB/s) (stdev) After (KB/s) (stdev) Random (4K): 424907.60 (24410.78) 414745.92 (34554.78) Random (64K): 163308.82 (11635.72) 167314.50 (18434.99) Sequential (4K, !-R): 6150056.79 (103205.90) 6321469.06 (115878.16) The performance changes are below noise level. - Build linux kernel with make -j96, using 4K folio with 1.5G memory cgroup limit and 64K folio with 2G memory cgroup limit, on top of tmpfs, 12 test runs, measuring the system time: Before (s) (stdev) After (s) (stdev) make -j96 (4K): 6445.69 (61.95) 6408.80 (69.46) make -j96 (64K): 6841.71 (409.04) 6437.99 (435.55) Similar to above, 64k mTHP case showed a slight improvement. Signed-off-by: Kairui Song --- include/linux/swap.h | 2 - include/linux/swap_slots.h | 28 ---- mm/Makefile | 2 +- mm/swap_slots.c | 295 ------------------------------------- mm/swap_state.c | 8 +- mm/swapfile.c | 193 +++++++++--------------- 6 files changed, 71 insertions(+), 457 deletions(-) delete mode 100644 include/linux/swap_slots.h delete mode 100644 mm/swap_slots.c diff --git a/include/linux/swap.h b/include/linux/swap.h index 374bffc87427..a0a262bcaf41 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -465,7 +465,6 @@ void free_pages_and_swap_cache(struct encoded_page **, int); extern atomic_long_t nr_swap_pages; extern long total_swap_pages; extern atomic_t nr_rotate_swap; -extern bool has_usable_swap(void); /* Swap 50% full? Release swapcache more aggressively.. */ static inline bool vm_swap_full(void) @@ -489,7 +488,6 @@ extern void swap_shmem_alloc(swp_entry_t, int); extern int swap_duplicate(swp_entry_t); extern int swapcache_prepare(swp_entry_t entry, int nr); extern void swap_free_nr(swp_entry_t entry, int nr_pages); -extern void swapcache_free_entries(swp_entry_t *entries, int n); extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); int swap_type_of(dev_t device, sector_t offset); int find_first_swap(dev_t *device); diff --git a/include/linux/swap_slots.h b/include/linux/swap_slots.h deleted file mode 100644 index 840aec3523b2..000000000000 --- a/include/linux/swap_slots.h +++ /dev/null @@ -1,28 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _LINUX_SWAP_SLOTS_H -#define _LINUX_SWAP_SLOTS_H - -#include -#include -#include - -#define SWAP_SLOTS_CACHE_SIZE SWAP_BATCH -#define THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE (5*SWAP_SLOTS_CACHE_SIZE) -#define THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE (2*SWAP_SLOTS_CACHE_SIZE) - -struct swap_slots_cache { - bool lock_initialized; - struct mutex alloc_lock; /* protects slots, nr, cur */ - swp_entry_t *slots; - int nr; - int cur; - int n_ret; -}; - -void disable_swap_slots_cache_lock(void); -void reenable_swap_slots_cache_unlock(void); -void enable_swap_slots_cache(void); - -extern bool swap_slot_cache_enabled; - -#endif /* _LINUX_SWAP_SLOTS_H */ diff --git a/mm/Makefile b/mm/Makefile index 4510a9869e77..e7f6bbf8ae5f 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -75,7 +75,7 @@ ifdef CONFIG_MMU obj-$(CONFIG_ADVISE_SYSCALLS) += madvise.o endif -obj-$(CONFIG_SWAP) += page_io.o swap_state.o swapfile.o swap_slots.o +obj-$(CONFIG_SWAP) += page_io.o swap_state.o swapfile.o obj-$(CONFIG_ZSWAP) += zswap.o obj-$(CONFIG_HAS_DMA) += dmapool.o obj-$(CONFIG_HUGETLBFS) += hugetlb.o diff --git a/mm/swap_slots.c b/mm/swap_slots.c deleted file mode 100644 index 9c7c171df7ba..000000000000 --- a/mm/swap_slots.c +++ /dev/null @@ -1,295 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * Manage cache of swap slots to be used for and returned from - * swap. - * - * Copyright(c) 2016 Intel Corporation. - * - * Author: Tim Chen - * - * We allocate the swap slots from the global pool and put - * it into local per cpu caches. This has the advantage - * of no needing to acquire the swap_info lock every time - * we need a new slot. - * - * There is also opportunity to simply return the slot - * to local caches without needing to acquire swap_info - * lock. We do not reuse the returned slots directly but - * move them back to the global pool in a batch. This - * allows the slots to coalesce and reduce fragmentation. - * - * The swap entry allocated is marked with SWAP_HAS_CACHE - * flag in map_count that prevents it from being allocated - * again from the global pool. - * - * The swap slots cache is protected by a mutex instead of - * a spin lock as when we search for slots with scan_swap_map, - * we can possibly sleep. - */ - -#include -#include -#include -#include -#include -#include -#include - -static DEFINE_PER_CPU(struct swap_slots_cache, swp_slots); -static bool swap_slot_cache_active; -bool swap_slot_cache_enabled; -static bool swap_slot_cache_initialized; -static DEFINE_MUTEX(swap_slots_cache_mutex); -/* Serialize swap slots cache enable/disable operations */ -static DEFINE_MUTEX(swap_slots_cache_enable_mutex); - -static void __drain_swap_slots_cache(void); - -#define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled) - -static void deactivate_swap_slots_cache(void) -{ - mutex_lock(&swap_slots_cache_mutex); - swap_slot_cache_active = false; - __drain_swap_slots_cache(); - mutex_unlock(&swap_slots_cache_mutex); -} - -static void reactivate_swap_slots_cache(void) -{ - mutex_lock(&swap_slots_cache_mutex); - swap_slot_cache_active = true; - mutex_unlock(&swap_slots_cache_mutex); -} - -/* Must not be called with cpu hot plug lock */ -void disable_swap_slots_cache_lock(void) -{ - mutex_lock(&swap_slots_cache_enable_mutex); - swap_slot_cache_enabled = false; - if (swap_slot_cache_initialized) { - /* serialize with cpu hotplug operations */ - cpus_read_lock(); - __drain_swap_slots_cache(); - cpus_read_unlock(); - } -} - -static void __reenable_swap_slots_cache(void) -{ - swap_slot_cache_enabled = has_usable_swap(); -} - -void reenable_swap_slots_cache_unlock(void) -{ - __reenable_swap_slots_cache(); - mutex_unlock(&swap_slots_cache_enable_mutex); -} - -static bool check_cache_active(void) -{ - long pages; - - if (!swap_slot_cache_enabled) - return false; - - pages = get_nr_swap_pages(); - if (!swap_slot_cache_active) { - if (pages > num_online_cpus() * - THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE) - reactivate_swap_slots_cache(); - goto out; - } - - /* if global pool of slot caches too low, deactivate cache */ - if (pages < num_online_cpus() * THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE) - deactivate_swap_slots_cache(); -out: - return swap_slot_cache_active; -} - -static int alloc_swap_slot_cache(unsigned int cpu) -{ - struct swap_slots_cache *cache; - swp_entry_t *slots; - - /* - * Do allocation outside swap_slots_cache_mutex - * as kvzalloc could trigger reclaim and folio_alloc_swap, - * which can lock swap_slots_cache_mutex. - */ - slots = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t), - GFP_KERNEL); - if (!slots) - return -ENOMEM; - - mutex_lock(&swap_slots_cache_mutex); - cache = &per_cpu(swp_slots, cpu); - if (cache->slots) { - /* cache already allocated */ - mutex_unlock(&swap_slots_cache_mutex); - - kvfree(slots); - - return 0; - } - - if (!cache->lock_initialized) { - mutex_init(&cache->alloc_lock); - cache->lock_initialized = true; - } - cache->nr = 0; - cache->cur = 0; - cache->n_ret = 0; - /* - * We initialized alloc_lock and free_lock earlier. We use - * !cache->slots or !cache->slots_ret to know if it is safe to acquire - * the corresponding lock and use the cache. Memory barrier below - * ensures the assumption. - */ - mb(); - cache->slots = slots; - mutex_unlock(&swap_slots_cache_mutex); - return 0; -} - -static void drain_slots_cache_cpu(unsigned int cpu, bool free_slots) -{ - struct swap_slots_cache *cache; - - cache = &per_cpu(swp_slots, cpu); - if (cache->slots) { - mutex_lock(&cache->alloc_lock); - swapcache_free_entries(cache->slots + cache->cur, cache->nr); - cache->cur = 0; - cache->nr = 0; - if (free_slots && cache->slots) { - kvfree(cache->slots); - cache->slots = NULL; - } - mutex_unlock(&cache->alloc_lock); - } -} - -static void __drain_swap_slots_cache(void) -{ - unsigned int cpu; - - /* - * This function is called during - * 1) swapoff, when we have to make sure no - * left over slots are in cache when we remove - * a swap device; - * 2) disabling of swap slot cache, when we run low - * on swap slots when allocating memory and need - * to return swap slots to global pool. - * - * We cannot acquire cpu hot plug lock here as - * this function can be invoked in the cpu - * hot plug path: - * cpu_up -> lock cpu_hotplug -> cpu hotplug state callback - * -> memory allocation -> direct reclaim -> folio_alloc_swap - * -> drain_swap_slots_cache - * - * Hence the loop over current online cpu below could miss cpu that - * is being brought online but not yet marked as online. - * That is okay as we do not schedule and run anything on a - * cpu before it has been marked online. Hence, we will not - * fill any swap slots in slots cache of such cpu. - * There are no slots on such cpu that need to be drained. - */ - for_each_online_cpu(cpu) - drain_slots_cache_cpu(cpu, false); -} - -static int free_slot_cache(unsigned int cpu) -{ - mutex_lock(&swap_slots_cache_mutex); - drain_slots_cache_cpu(cpu, true); - mutex_unlock(&swap_slots_cache_mutex); - return 0; -} - -void enable_swap_slots_cache(void) -{ - mutex_lock(&swap_slots_cache_enable_mutex); - if (!swap_slot_cache_initialized) { - int ret; - - ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache", - alloc_swap_slot_cache, free_slot_cache); - if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating " - "without swap slots cache.\n", __func__)) - goto out_unlock; - - swap_slot_cache_initialized = true; - } - - __reenable_swap_slots_cache(); -out_unlock: - mutex_unlock(&swap_slots_cache_enable_mutex); -} - -/* called with swap slot cache's alloc lock held */ -static int refill_swap_slots_cache(struct swap_slots_cache *cache) -{ - if (!use_swap_slot_cache) - return 0; - - cache->cur = 0; - if (swap_slot_cache_active) - cache->nr = get_swap_pages(SWAP_SLOTS_CACHE_SIZE, - cache->slots, 0); - - return cache->nr; -} - -swp_entry_t folio_alloc_swap(struct folio *folio) -{ - swp_entry_t entry; - struct swap_slots_cache *cache; - - entry.val = 0; - - if (folio_test_large(folio)) { - if (IS_ENABLED(CONFIG_THP_SWAP)) - get_swap_pages(1, &entry, folio_order(folio)); - goto out; - } - - /* - * Preemption is allowed here, because we may sleep - * in refill_swap_slots_cache(). But it is safe, because - * accesses to the per-CPU data structure are protected by the - * mutex cache->alloc_lock. - * - * The alloc path here does not touch cache->slots_ret - * so cache->free_lock is not taken. - */ - cache = raw_cpu_ptr(&swp_slots); - - if (likely(check_cache_active() && cache->slots)) { - mutex_lock(&cache->alloc_lock); - if (cache->slots) { -repeat: - if (cache->nr) { - entry = cache->slots[cache->cur]; - cache->slots[cache->cur++].val = 0; - cache->nr--; - } else if (refill_swap_slots_cache(cache)) { - goto repeat; - } - } - mutex_unlock(&cache->alloc_lock); - if (entry.val) - goto out; - } - - get_swap_pages(1, &entry, 0); -out: - if (mem_cgroup_try_charge_swap(folio, entry)) { - put_swap_folio(folio, entry); - entry.val = 0; - } - return entry; -} diff --git a/mm/swap_state.c b/mm/swap_state.c index 50840a2887a5..2b5744e211cd 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include "internal.h" @@ -447,13 +446,8 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, /* * Just skip read ahead for unused swap slot. - * During swap_off when swap_slot_cache is disabled, - * we have to handle the race between putting - * swap entry in swap cache and marking swap slot - * as SWAP_HAS_CACHE. That's done in later part of code or - * else swap_off will be aborted if we return NULL. */ - if (!swap_entry_swapped(si, entry) && swap_slot_cache_enabled) + if (!swap_entry_swapped(si, entry)) goto put_and_return; /* diff --git a/mm/swapfile.c b/mm/swapfile.c index 7caaaea95408..1ba916109d99 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -37,7 +37,6 @@ #include #include #include -#include #include #include #include @@ -885,6 +884,13 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o struct swap_cluster_info *ci; unsigned int offset, found = 0; + /* + * Swapfile is not block device so unable + * to allocate large entries. + */ + if (order && !(si->flags & SWP_BLKDEV)) + return 0; + if (!(si->flags & SWP_SOLIDSTATE)) { /* Serialize HDD SWAP allocation for each device. */ spin_lock(&si->global_cluster_lock); @@ -1148,43 +1154,6 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, swap_usage_sub(si, nr_entries); } -static int scan_swap_map_slots(struct swap_info_struct *si, - unsigned char usage, int nr, - swp_entry_t slots[], int order) -{ - unsigned int nr_pages = 1 << order; - int n_ret = 0; - - if (order > 0) { - /* - * Should not even be attempting large allocations when huge - * page swap is disabled. Warn and fail the allocation. - */ - if (!IS_ENABLED(CONFIG_THP_SWAP) || - nr_pages > SWAPFILE_CLUSTER) { - VM_WARN_ON_ONCE(1); - return 0; - } - - /* - * Swapfile is not block device so unable - * to allocate large entries. - */ - if (!(si->flags & SWP_BLKDEV)) - return 0; - } - - while (n_ret < nr) { - unsigned long offset = cluster_alloc_swap_entry(si, order, usage); - - if (!offset) - break; - slots[n_ret++] = swp_entry(si->type, offset); - } - - return n_ret; -} - static bool get_swap_device_info(struct swap_info_struct *si) { if (!percpu_ref_tryget_live(&si->users)) @@ -1205,16 +1174,13 @@ static bool get_swap_device_info(struct swap_info_struct *si) * Fast path try to get swap entries with specified order from current * CPU's swap entry pool (a cluster). */ -static int swap_alloc_fast(swp_entry_t entries[], +static int swap_alloc_fast(swp_entry_t *entry, unsigned char usage, - int order, int n_goal) + int order) { struct swap_cluster_info *ci; struct swap_info_struct *si; - unsigned int offset, found; - int n_ret = 0; - - n_goal = min(n_goal, SWAP_BATCH); + unsigned int offset, found = SWAP_ENTRY_INVALID; /* * Once allocated, swap_info_struct will never be completely freed, @@ -1223,44 +1189,48 @@ static int swap_alloc_fast(swp_entry_t entries[], si = __this_cpu_read(percpu_swap_cluster.si[order]); offset = __this_cpu_read(percpu_swap_cluster.offset[order]); if (!si || !offset || !get_swap_device_info(si)) - return 0; + return false; - while (offset) { - ci = lock_cluster(si, offset); - if (!cluster_is_usable(ci, order)) - break; + ci = lock_cluster(si, offset); + if (cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset = cluster_offset(si, ci); found = alloc_swap_scan_cluster(si, ci, offset, order, usage); - if (!found) - break; - entries[n_ret++] = swp_entry(si->type, found); - if (n_ret == n_goal) - break; - offset = __this_cpu_read(percpu_swap_cluster.offset[order]); + if (found) + *entry = swp_entry(si->type, found); + } else { + unlock_cluster(ci); } put_swap_device(si); - return n_ret; + return !!found; } -int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) +swp_entry_t folio_alloc_swap(struct folio *folio) { - int order = swap_entry_order(entry_order); - unsigned long size = 1 << order; + unsigned int order = folio_order(folio); + unsigned int size = 1 << order; struct swap_info_struct *si, *next; - int n_ret = 0; + swp_entry_t entry = {}; + unsigned long offset; int node; + if (order) { + /* + * Should not even be attempting large allocations when huge + * page swap is disabled. Warn and fail the allocation. + */ + if (!IS_ENABLED(CONFIG_THP_SWAP) || size > SWAPFILE_CLUSTER) { + VM_WARN_ON_ONCE(1); + return entry; + } + } + /* Fast path using percpu cluster */ local_lock(&percpu_swap_cluster.lock); - n_ret = swap_alloc_fast(swp_entries, - SWAP_HAS_CACHE, - order, n_goal); - if (n_ret == n_goal) - goto out; + if (swap_alloc_fast(&entry, SWAP_HAS_CACHE, order)) + goto out_alloced; - n_goal = min_t(int, n_goal - n_ret, SWAP_BATCH); /* Rotate the device and switch to a new cluster */ spin_lock(&swap_avail_lock); start_over: @@ -1269,19 +1239,14 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); spin_unlock(&swap_avail_lock); if (get_swap_device_info(si)) { - /* - * For order 0 allocation, try best to fill the request - * as it's used by slot cache. - * - * For mTHP allocation, it always have n_goal == 1, - * and falling a mTHP swapin will just make the caller - * fallback to order 0 allocation, so just bail out. - */ - n_ret += scan_swap_map_slots(si, SWAP_HAS_CACHE, n_goal, - swp_entries + n_ret, order); + offset = cluster_alloc_swap_entry(si, order, SWAP_HAS_CACHE); put_swap_device(si); - if (n_ret || size > 1) - goto out; + if (offset) { + entry = swp_entry(si->type, offset); + goto out_alloced; + } + if (order) + goto out_failed; } spin_lock(&swap_avail_lock); @@ -1300,10 +1265,20 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_order) goto start_over; } spin_unlock(&swap_avail_lock); -out: +out_failed: + local_unlock(&percpu_swap_cluster.lock); + return entry; + +out_alloced: local_unlock(&percpu_swap_cluster.lock); - atomic_long_sub(n_ret * size, &nr_swap_pages); - return n_ret; + if (mem_cgroup_try_charge_swap(folio, entry)) { + put_swap_folio(folio, entry); + entry.val = 0; + } else { + atomic_long_sub(size, &nr_swap_pages); + } + + return entry; } static struct swap_info_struct *_swap_info_get(swp_entry_t entry) @@ -1599,25 +1574,6 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) unlock_cluster(ci); } -void swapcache_free_entries(swp_entry_t *entries, int n) -{ - int i; - struct swap_cluster_info *ci; - struct swap_info_struct *si = NULL; - - if (n <= 0) - return; - - for (i = 0; i < n; ++i) { - si = _swap_info_get(entries[i]); - if (si) { - ci = lock_cluster(si, swp_offset(entries[i])); - swap_entry_range_free(si, ci, entries[i], 1); - unlock_cluster(ci); - } - } -} - int __swap_count(swp_entry_t entry) { struct swap_info_struct *si = swp_swap_info(entry); @@ -1858,6 +1814,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr) swp_entry_t get_swap_page_of_type(int type) { struct swap_info_struct *si = swap_type_to_swap_info(type); + unsigned long offset; swp_entry_t entry = {0}; if (!si) @@ -1865,8 +1822,13 @@ swp_entry_t get_swap_page_of_type(int type) /* This is called for allocating swap entry, not cache */ if (get_swap_device_info(si)) { - if ((si->flags & SWP_WRITEOK) && scan_swap_map_slots(si, 1, 1, &entry, 0)) - atomic_long_dec(&nr_swap_pages); + if (si->flags & SWP_WRITEOK) { + offset = cluster_alloc_swap_entry(si, 0, 1); + if (offset) { + entry = swp_entry(si->type, offset); + atomic_long_dec(&nr_swap_pages); + } + } put_swap_device(si); } fail: @@ -2627,21 +2589,6 @@ static void reinsert_swap_info(struct swap_info_struct *si) spin_unlock(&swap_lock); } -static bool __has_usable_swap(void) -{ - return !plist_head_empty(&swap_active_head); -} - -bool has_usable_swap(void) -{ - bool ret; - - spin_lock(&swap_lock); - ret = __has_usable_swap(); - spin_unlock(&swap_lock); - return ret; -} - /* * Called after clearing SWP_WRITEOK, ensures cluster_alloc_range * see the updated flags, so there will be no more allocations. @@ -2732,8 +2679,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) wait_for_allocation(p); - disable_swap_slots_cache_lock(); - set_current_oom_origin(); err = try_to_unuse(p->type); clear_current_oom_origin(); @@ -2741,12 +2686,9 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) if (err) { /* re-insert swap space back into swap_list */ reinsert_swap_info(p); - reenable_swap_slots_cache_unlock(); goto out_dput; } - reenable_swap_slots_cache_unlock(); - /* * Wait for swap operations protected by get/put_swap_device() * to complete. Because of synchronize_rcu() here, all swap @@ -3495,8 +3437,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) putname(name); if (inode) inode_unlock(inode); - if (!error) - enable_swap_slots_cache(); return error; } @@ -3892,6 +3832,11 @@ static void free_swap_count_continuations(struct swap_info_struct *si) } #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP) +static bool __has_usable_swap(void) +{ + return !plist_head_empty(&swap_active_head); +} + void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp) { struct swap_info_struct *si, *next; From patchwork Mon Feb 24 18:02:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13988668 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 CC6FDC021A4 for ; Mon, 24 Feb 2025 18:03:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C72D6B0092; Mon, 24 Feb 2025 13:03:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 579C56B0093; Mon, 24 Feb 2025 13:03:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C9646B0095; Mon, 24 Feb 2025 13:03:22 -0500 (EST) 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 18ACF6B0092 for ; Mon, 24 Feb 2025 13:03:22 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 92F111A0179 for ; Mon, 24 Feb 2025 18:03:21 +0000 (UTC) X-FDA: 83155610202.03.0F7587D Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf02.hostedemail.com (Postfix) with ESMTP id B1BFA8003D for ; Mon, 24 Feb 2025 18:03:19 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hitmdhfv; spf=pass (imf02.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740420199; a=rsa-sha256; cv=none; b=IbbdfuzrEQKE4mpEYR4PgORrg6G3QKZbpZDIUu9HVoZqcDAyzH8Xr54MFMxWn4LZ5wrYKX nYDmR88NXiulSY186nGj5ry+314tD6TtG2jgOB1qjGf5z3/PEaZC+jgu/pZU/lmfOjz37u LJ1IriL3f2xdkrtBk/zpdRUDonkjBvg= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hitmdhfv; spf=pass (imf02.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740420199; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0F3kezvsTBdwcMP4/p3aFmc/96fOBXv0a/r8BX7g49c=; b=5h9GFLO2sw6Lk4S25qYo6d48s1ms5gER4aR6UL4iTiVhG+fCBOdFckrvvRHSi00JRk6m8v u7/0ExIkUC2oFScMZIG2Nmu9P62aYucKgK3cT/n9GSoxVxRqpg/oeo4fdH8KXYDiKJflZf a28rMgO1s/8hwNAp4gAnsHDblvOWh48= Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-220c92c857aso81046375ad.0 for ; Mon, 24 Feb 2025 10:03:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740420198; x=1741024998; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=0F3kezvsTBdwcMP4/p3aFmc/96fOBXv0a/r8BX7g49c=; b=hitmdhfvzaX4NidpfB9day6aynUP8yAPPkC4DosGIMHlDyIdwhPQ8NoiHjS2DwNOn7 p360bHlVeUCpGDQv9jLQPaZaNxlZhJITGaUr5q7v/ZG2JcqOOKJtadjuzN2Qepui/4I/ l42y1lWJAOAaWI6GvtASChnAi1LQ1K+w3ono4BAxChBBEfQ78pN7r2sr/hm/YFeGEBfy 7a1SKsJaOQJYfmS6ZT9pNklp0f6sdRb2s7EkbFQhLpX/KT58PeZ16cjI9vkt5lwwTSVj hziw4TtRQnqJ16Kw6hX8LQgi1RLWdD9bq+V0YQvC0DpnuigcmDQXW+6s9uc3t6RMbQ8k wBWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740420198; x=1741024998; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=0F3kezvsTBdwcMP4/p3aFmc/96fOBXv0a/r8BX7g49c=; b=B2biTgX783v6FY2TrAOTZRAA79VDzPKp5l5780sdT4ZhWUacPuvFjUpHs5Kn80h13J OgprDE2M15Ud1TjHwKBHIZWSfFbSpX2AJyc++e5SwsarXeHRlKkBPSewApq8aVIw5x+x wh2lKmci1Oi5eag6mSwYXu6o9l6J0qhUoNop+LmxsmPCVk23YK7uyMa9G2F7VzfIXxUq rleUQJvm9dTHcR7XZ69AcMqWumg5PctbWL99j4My6YmE2ONGI5u6NfCJr6zIswRCJQmj t7gVEP3KjfpsmIVL/ZQf+1fB734j7dqgzJe3i1lGfD5vlwiGc1JKtgPE6AfE2O0d2WyZ uQKA== X-Gm-Message-State: AOJu0YziUaxx//kLNUZRd2iCDKNOzshgVnNi7zB/joBD4gDA+fK4XKnn iHWs2kXiVLp7q6xkubkG4FmzOZFc0C+4w+vATOAAHaIVDR/WnpLBH+9x4Z6H1+w= X-Gm-Gg: ASbGncvli08VakST2Y8Tntb5mqzGr8XZEJ/9F8trHC5YMe9tdVETtgKBEoV/f1T4Tik gFm4ZFD2a0F70n+WB3zkTJFiT3O7qXTfE6LtJ63O0SnU3jJstklGFNQbCw+zOS4KAYRFqtcAg0r 9LTL+KxV7cya/O5wOO1sLjs98hGAIIcaW80kmQ7DmX7q/is8gzlpZK344g7naF5nnITQMJuX2X4 WOairKCcyZFFEdao27qKtp9JVbGQLj7WSzwQ9czG0nAgGKosm7CY7Uk2GjamDmBZlhlmV/J/sZX 6iBsXGHzdTas4SoI0OqCG7EZwhZkxguDS/55jsJhqzLZ X-Google-Smtp-Source: AGHT+IFXam9uJQZb5hs3Hxkm0TWEoFuaGqLoECsuP+xjMxnP1x5Le2+i2A4Hq3TcsdGNfTd3wjKQnQ== X-Received: by 2002:a17:902:fc4e:b0:21f:4c8b:c4da with SMTP id d9443c01a7336-2219fff87cbmr242938105ad.18.1740420197211; Mon, 24 Feb 2025 10:03:17 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.88]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-220d556e15esm184834695ad.190.2025.02.24.10.03.13 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 24 Feb 2025 10:03:16 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Baolin Wang , Kalesh Singh , Matthew Wilcox , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 7/7] mm, swap: simplify folio swap allocation Date: Tue, 25 Feb 2025 02:02:12 +0800 Message-ID: <20250224180212.22802-8-ryncsn@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224180212.22802-1-ryncsn@gmail.com> References: <20250224180212.22802-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Queue-Id: B1BFA8003D X-Stat-Signature: ur86aurp1c8nw96udcqmeg1c5k96rjec X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1740420199-426762 X-HE-Meta: U2FsdGVkX195+tdJaOnZ0TKb/8U+e1FDVIDBrXc2s8oe1l/Q6NdIpc6+I4xaLttWnkzI+bQl4GkxJ75SU/q/Hp9AG5tAst85O459ANmhxCa7BF3BUPV87hdXL3UeEYURhorwkE6guQGGuhsFp4e02/oXdZzDbZ4hHP76mXEIPrAJ4A5UdXO2fagiPhUc1VwERsDUJE1uapcQUch1mVJUGAbwsl3PHWDGB6UHcWx79HvDkh9OvdPlXtmwqHQtBqOqF8rOw8SutgHaNgLSbjwrqwSpoGDStSs+EHftAJ1sNvGipSKgwyXEtsQRMHmUY4Q0koiOTAYQLWIjAk9SOUcJ6qpswBX11i4Xa/JS0y7+qfYtUvj/j/R+yB9vnxYFmUV5Ai/f7M1pX3mgtkdm/mpM6mIq+4z7reNbX0BBB2T32suZo1ukDLLlDTT3WmobtclyTb/UEpepYy3H+K8tU6VByt9SKyLa1vx/7JH0uf/cjfzBw6fzh2MS5WG/28D7voO7WMmlvgrjoegEGFx1ctHgy50kDBFKUE5Tj01i5ciltgfl5GcPtnqMoy8c0XYiY/6B0YMLJJBecbqjan28EA2Lmmui9bzfTytcCUfJRyV8/MCDzTGV/6RAkdLV03zzoyLIoKlKQBcf6cJ0qPY2E6rZJtgB3Ec/2W8f47t1SeNa19rl35nzpAgp3Zyc88vW8FwUEDaXDfAHKWahjIXdXLXRHBUhNIXSPShs1lb6qxN5pLbHnlbtSYEWpa+gEtwxd0fMR2yv1YPgILHhEXDiR94PHHkI6QHCssm42UgL+8eSbARYW0tEL1N9+KMOgU04GnHxZ4uYmlpgHuwBLggl+t+satuEf8z8//mWBknc5sZvdFPaLBsIZZr1+9/dIvKr5HV32PWl/ckRLXWbPw3OVv5PWBVIZYiQkpnFatFXOaR+7PjA9ci/c5pW0sxutw2XuDXINH50PhLuR0sZTNGf6rS DtmVyuZp l05Rs0NiDR/rpp3qOgfm2/WJp54OU7qjs2Exf1HN1cSNPbzoxF7zlJGmSx+Mn8peBYItmkifk00tXW4Ety2jNCnZae86c0eAVXKMTPGzSdzvt5nKWwoGM2W00c43h5qzJv/UD1GkjInHddIlCsssdXLrCtnR6yjyHTSGzbyidzLH21oymayz6cBr14BzSHGzVnO2YB0/WJuTlUbRaDQVqRW+GDlDdDgQi/75DpQug7FIELZCnnJx8jv5Sg8WfwVcWFQWMSeJsImG2o29l2zDxKFwUTlimHuVXSZnUy3s+Qq21b0Vhg5Lk3OVZ2YrBDfTgKd9hxwGrPRQLFte8xCJz6dmBuXY9etkcAZLFzWqK9dVDto+tK5SICBXOG7fNMkSIK+JS+tW7zSbfRYMOCmzkQX+CdqbOwhe5EdZ27CvdWVKwNtU72QHN0ZGSn6uI/pT+SAGC/US6ob8D9POKMrBYcUQaowM2gI1IkUsP/KoXKAepU0GEr04wfWgHO8f1GQrhmpMyRtsXVZb7Lu22ELAfd/gzhXCmTVXVbLX/G+wAg6aNX5rvo8utRxgUp54VaIYD2XyH 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: From: Kairui Song With slot cache gone, clean up the allocation helpers even more. folio_alloc_swap will be the only entry for allocation and adding the folio to swap cache (except suspend), making it opposite of folio_free_swap. Signed-off-by: Kairui Song --- include/linux/swap.h | 8 ++-- mm/shmem.c | 21 +++------ mm/swap.h | 6 --- mm/swap_state.c | 57 ---------------------- mm/swapfile.c | 110 ++++++++++++++++++++++++++++--------------- mm/vmscan.c | 16 ++++++- 6 files changed, 94 insertions(+), 124 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index a0a262bcaf41..3a68da686c4e 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -478,7 +478,7 @@ static inline long get_nr_swap_pages(void) } extern void si_swapinfo(struct sysinfo *); -swp_entry_t folio_alloc_swap(struct folio *folio); +int folio_alloc_swap(struct folio *folio, gfp_t gfp_mask); bool folio_free_swap(struct folio *folio); void put_swap_folio(struct folio *folio, swp_entry_t entry); extern swp_entry_t get_swap_page_of_type(int); @@ -587,11 +587,9 @@ static inline int swp_swapcount(swp_entry_t entry) return 0; } -static inline swp_entry_t folio_alloc_swap(struct folio *folio) +static int folio_alloc_swap(struct folio *folio, gfp_t gfp_mask) { - swp_entry_t entry; - entry.val = 0; - return entry; + return -EINVAL; } static inline bool folio_free_swap(struct folio *folio) diff --git a/mm/shmem.c b/mm/shmem.c index 45dbcb69da0c..aad02132b75a 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1546,7 +1546,6 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc) struct inode *inode = mapping->host; struct shmem_inode_info *info = SHMEM_I(inode); struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); - swp_entry_t swap; pgoff_t index; int nr_pages; bool split = false; @@ -1628,14 +1627,6 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc) folio_mark_uptodate(folio); } - swap = folio_alloc_swap(folio); - if (!swap.val) { - if (nr_pages > 1) - goto try_split; - - goto redirty; - } - /* * Add inode to shmem_unuse()'s list of swapped-out inodes, * if it's not already there. Do it now before the folio is @@ -1648,20 +1639,20 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc) if (list_empty(&info->swaplist)) list_add(&info->swaplist, &shmem_swaplist); - if (add_to_swap_cache(folio, swap, - __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, - NULL) == 0) { + if (!folio_alloc_swap(folio, __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN)) { shmem_recalc_inode(inode, 0, nr_pages); - swap_shmem_alloc(swap, nr_pages); - shmem_delete_from_page_cache(folio, swp_to_radix_entry(swap)); + swap_shmem_alloc(folio->swap, nr_pages); + shmem_delete_from_page_cache(folio, swp_to_radix_entry(folio->swap)); mutex_unlock(&shmem_swaplist_mutex); BUG_ON(folio_mapped(folio)); return swap_writepage(&folio->page, wbc); } + list_del_init(&info->swaplist); mutex_unlock(&shmem_swaplist_mutex); - put_swap_folio(folio, swap); + if (nr_pages > 1) + goto try_split; redirty: folio_mark_dirty(folio); if (wbc->for_reclaim) diff --git a/mm/swap.h b/mm/swap.h index ad2f121de970..0abb68091b4f 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -50,7 +50,6 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry) } void show_swap_cache_info(void); -bool add_to_swap(struct folio *folio); void *get_shadow_from_swap_cache(swp_entry_t entry); int add_to_swap_cache(struct folio *folio, swp_entry_t entry, gfp_t gfp, void **shadowp); @@ -163,11 +162,6 @@ struct folio *filemap_get_incore_folio(struct address_space *mapping, return filemap_get_folio(mapping, index); } -static inline bool add_to_swap(struct folio *folio) -{ - return false; -} - static inline void *get_shadow_from_swap_cache(swp_entry_t entry) { return NULL; diff --git a/mm/swap_state.c b/mm/swap_state.c index 2b5744e211cd..68fd981b514f 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -166,63 +166,6 @@ void __delete_from_swap_cache(struct folio *folio, __lruvec_stat_mod_folio(folio, NR_SWAPCACHE, -nr); } -/** - * add_to_swap - allocate swap space for a folio - * @folio: folio we want to move to swap - * - * Allocate swap space for the folio and add the folio to the - * swap cache. - * - * Context: Caller needs to hold the folio lock. - * Return: Whether the folio was added to the swap cache. - */ -bool add_to_swap(struct folio *folio) -{ - swp_entry_t entry; - int err; - - VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); - VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio); - - entry = folio_alloc_swap(folio); - if (!entry.val) - return false; - - /* - * XArray node allocations from PF_MEMALLOC contexts could - * completely exhaust the page allocator. __GFP_NOMEMALLOC - * stops emergency reserves from being allocated. - * - * TODO: this could cause a theoretical memory reclaim - * deadlock in the swap out path. - */ - /* - * Add it to the swap cache. - */ - err = add_to_swap_cache(folio, entry, - __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN, NULL); - if (err) - goto fail; - /* - * Normally the folio will be dirtied in unmap because its - * pte should be dirty. A special case is MADV_FREE page. The - * page's pte could have dirty bit cleared but the folio's - * SwapBacked flag is still set because clearing the dirty bit - * and SwapBacked flag has no lock protected. For such folio, - * unmap will not set dirty bit for it, so folio reclaim will - * not write the folio out. This can cause data corruption when - * the folio is swapped in later. Always setting the dirty flag - * for the folio solves the problem. - */ - folio_mark_dirty(folio); - - return true; - -fail: - put_swap_folio(folio, entry); - return false; -} - /* * This must be called only on folios that have * been verified to be in the swap cache and locked. diff --git a/mm/swapfile.c b/mm/swapfile.c index 1ba916109d99..628f67974a7c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1174,9 +1174,9 @@ static bool get_swap_device_info(struct swap_info_struct *si) * Fast path try to get swap entries with specified order from current * CPU's swap entry pool (a cluster). */ -static int swap_alloc_fast(swp_entry_t *entry, - unsigned char usage, - int order) +static bool swap_alloc_fast(swp_entry_t *entry, + unsigned char usage, + int order) { struct swap_cluster_info *ci; struct swap_info_struct *si; @@ -1206,47 +1206,31 @@ static int swap_alloc_fast(swp_entry_t *entry, return !!found; } -swp_entry_t folio_alloc_swap(struct folio *folio) +/* Rotate the device and switch to a new cluster */ +static bool swap_alloc_slow(swp_entry_t *entry, + unsigned char usage, + int order) { - unsigned int order = folio_order(folio); - unsigned int size = 1 << order; - struct swap_info_struct *si, *next; - swp_entry_t entry = {}; - unsigned long offset; int node; + unsigned long offset; + struct swap_info_struct *si, *next; - if (order) { - /* - * Should not even be attempting large allocations when huge - * page swap is disabled. Warn and fail the allocation. - */ - if (!IS_ENABLED(CONFIG_THP_SWAP) || size > SWAPFILE_CLUSTER) { - VM_WARN_ON_ONCE(1); - return entry; - } - } - - /* Fast path using percpu cluster */ - local_lock(&percpu_swap_cluster.lock); - if (swap_alloc_fast(&entry, SWAP_HAS_CACHE, order)) - goto out_alloced; - - /* Rotate the device and switch to a new cluster */ + node = numa_node_id(); spin_lock(&swap_avail_lock); start_over: - node = numa_node_id(); plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { + /* Rotate the device and switch to a new cluster */ plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); spin_unlock(&swap_avail_lock); if (get_swap_device_info(si)) { offset = cluster_alloc_swap_entry(si, order, SWAP_HAS_CACHE); put_swap_device(si); if (offset) { - entry = swp_entry(si->type, offset); - goto out_alloced; + *entry = swp_entry(si->type, offset); + return true; } if (order) - goto out_failed; + return false; } spin_lock(&swap_avail_lock); @@ -1265,20 +1249,68 @@ swp_entry_t folio_alloc_swap(struct folio *folio) goto start_over; } spin_unlock(&swap_avail_lock); -out_failed: + return false; +} + +/** + * folio_alloc_swap - allocate swap space for a folio + * @folio: folio we want to move to swap + * @gfp: gfp mask for shadow nodes + * + * Allocate swap space for the folio and add the folio to the + * swap cache. + * + * Context: Caller needs to hold the folio lock. + * Return: Whether the folio was added to the swap cache. + */ +int folio_alloc_swap(struct folio *folio, gfp_t gfp) +{ + unsigned int order = folio_order(folio); + unsigned int size = 1 << order; + swp_entry_t entry = {}; + + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio); + + /* + * Should not even be attempting large allocations when huge + * page swap is disabled. Warn and fail the allocation. + */ + if (order && (!IS_ENABLED(CONFIG_THP_SWAP) || size > SWAPFILE_CLUSTER)) { + VM_WARN_ON_ONCE(1); + return -EINVAL; + } + + local_lock(&percpu_swap_cluster.lock); + if (swap_alloc_fast(&entry, SWAP_HAS_CACHE, order)) + goto out_alloced; + if (swap_alloc_slow(&entry, SWAP_HAS_CACHE, order)) + goto out_alloced; local_unlock(&percpu_swap_cluster.lock); - return entry; + return -ENOMEM; out_alloced: local_unlock(&percpu_swap_cluster.lock); - if (mem_cgroup_try_charge_swap(folio, entry)) { - put_swap_folio(folio, entry); - entry.val = 0; - } else { - atomic_long_sub(size, &nr_swap_pages); - } + if (mem_cgroup_try_charge_swap(folio, entry)) + goto out_free; - return entry; + /* + * XArray node allocations from PF_MEMALLOC contexts could + * completely exhaust the page allocator. __GFP_NOMEMALLOC + * stops emergency reserves from being allocated. + * + * TODO: this could cause a theoretical memory reclaim + * deadlock in the swap out path. + */ + if (add_to_swap_cache(folio, entry, gfp | __GFP_NOMEMALLOC, NULL)) + goto out_free; + + atomic_long_sub(size, &nr_swap_pages); + return 0; + +out_free: + put_swap_folio(folio, entry); + return -ENOMEM; } static struct swap_info_struct *_swap_info_get(swp_entry_t entry) diff --git a/mm/vmscan.c b/mm/vmscan.c index fcca38bc640f..be00af3763b5 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1289,7 +1289,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, split_folio_to_list(folio, folio_list)) goto activate_locked; } - if (!add_to_swap(folio)) { + if (folio_alloc_swap(folio, __GFP_HIGH | __GFP_NOWARN)) { int __maybe_unused order = folio_order(folio); if (!folio_test_large(folio)) @@ -1305,9 +1305,21 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, } #endif count_mthp_stat(order, MTHP_STAT_SWPOUT_FALLBACK); - if (!add_to_swap(folio)) + if (folio_alloc_swap(folio, __GFP_HIGH | __GFP_NOWARN)) goto activate_locked_split; } + /* + * Normally the folio will be dirtied in unmap because its + * pte should be dirty. A special case is MADV_FREE page. The + * page's pte could have dirty bit cleared but the folio's + * SwapBacked flag is still set because clearing the dirty bit + * and SwapBacked flag has no lock protected. For such folio, + * unmap will not set dirty bit for it, so folio reclaim will + * not write the folio out. This can cause data corruption when + * the folio is swapped in later. Always setting the dirty flag + * for the folio solves the problem. + */ + folio_mark_dirty(folio); } }