From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335222; b=DM6HE4YQNWOBUQrD33RJ6dc5oJPrq+crfJCbf8Bmiwt+jzAbYHARQP1uHtp5coTYR4s9 tmzCi621lPWVKZCjlV7xCFmEgqU1U5GiTGZD2r4rl66r2eaVsz27yRKOkvJ2etxvFuPGS Of6exkuHhxgceiR+n81aPxp/Ca/AC7odsLa2MdKzLTBowluEskGrmcPDPq+j9/y8T1CAN 2C96fElbkzHjLpZIeVOy2x4coSk3dwathfNJblftSq+3SsragdHUO5Zta0CIseeBW3G3o ljOT8VO2KGXc/iwSDzaaZP6Q+ssPxW4PK3Uihb1WjgxSj5ybOweqwpVqFYLhifGQrsw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335222; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=Xu2m9EQvnNuXoUj+o8hs+pPH1MXlfFZTwWe4F6NHtPw=; b=Ob8W8eftJz4GnpQJrTDU3pgAcwX+/lBuraP8rUBhssFiW17RBplQQ5Ib1W50DPTTP9ia bMdlsJenEMPVfuz1jJ+bKir5lKSKPeRutsVxw/jRsKZfoy2kI/JXDqN6EpNtFlBCKzItM m16uXNzbm+iLak5JeVxVrudRRCAypbWqOY7rmGd6l3DNhK1YxFy84flsh38lm7JH28Lf+ Cnn9RgAMcpnZOML4J8D+jUv3QM2LQiEEY8ZXuYEPpAAPVbq6hS5d5FLGA/g/U2leAzUxq pTMI8KRxhhJ/3Va/Fkg8sF9dg1/Nc9c2YvIaaRQfGnY9cIiLccDkquBJ6nnmm1L5QHA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Phdx63Im; arc=none smtp.remote-ip=209.85.128.174 Received: from mail-yw1-f174.google.com (mail-yw1-f174.google.com [209.85.128.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CDE6606A6A4 for ; Thu, 3 Dec 2026 22:00:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9CDE6606A6A4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Phdx63Im Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-8b60552bebaso2711057b3.3 for ; Thu, 03 Dec 2026 14:00:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335221; x=1796940021; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Xu2m9EQvnNuXoUj+o8hs+pPH1MXlfFZTwWe4F6NHtPw=; b=Phdx63Im60C/rK4bOD+guoXT4cImOLYlSomKyKLKsDAyssznbWwDwGEx4wRuCIsWlG sHTz54fT1TArZuBxFwpQXfIzaqeECvo7E3eCpUPAuO+KtMQYYZWoPzPs4AuQkcKrBrhI /a9JOf5IivBqnl+Ij0PxkpY0hZXoS6055EjQDM8ikzjQxCAlglhpNj03CifulDCz502E xLxmI9xb7YiQGnnJnqnsnfoAfBE+hbDY12sgmR4ajf/yV8if0/xyW8Mvodvm+0VskGtJ OynvdgiGHFRwS64vinRhf4X3FpQV6zU5q9ryqQ8VwitnG4cnytinBDlk2X2hrhiYQyHV Y3ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335221; x=1796940021; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Xu2m9EQvnNuXoUj+o8hs+pPH1MXlfFZTwWe4F6NHtPw=; b=YMxEBm8KVWKbk+oR+j1qpZ4Ipg53IZq8xzAZOnFur7oIB5KQVv7mkLBtUKnDv8YIq0 JmOqWMQI1ewLgKsYboh7KjKNeC3AjR5tV5dlQplcWR+4B45qG8Ftpt/d5QAIURnJfqGo SA98f7/VpfSnDk6xCxiYL2mhLcu4uaOKSq0N3WJmXbGCwjfO7tx4NK2NSsGMEKqguNaz eNdbWfajjpukOTiLwawG9qPpd9BtZZqK3kIVtfpd8jugc2x9NAav/etvVXz0Pt0dLw5J /IY2OGN2IUbyrS1SKXSaLI0/Xu4yHoF1KmaL5wa3aP9Sy2Clw36pUuOpt/iWByneWYxf VzZA== X-Forwarded-Encrypted: i=1; AL+hErFUj3QjVHgfFvTkLpphUJPiZ9pI1yDj1/siJrME0Yy+yxwVZCoogzeOyc97xe+vCqaSKaBTD7wvTVU=@vger.kernel.org X-Gm-Message-State: APuU4Z9dAsImwsA5JIG6ikFFsHdsQ5z1PRIZrjqTgVBe2Jt74xkFEqTR tWCDrKorvTUSQlpjX+t9B/4z8IYYDd8RdR8ncDhp3AYpXKBKmrdCbw7a X-Gm-Gg: AZFdTZdPgXT9m3bLQt2HwD02MW0N5wKR/T7xNBUOd3b4zLL3Wr1JLTr9LsWIGSIynwa /37kKqRPa/o9ny2BUvbbUhX3ZT9EyHW9Sjd2Cc5xMTNHu3lg9yg+6DDhaKI32utXitYqy9F9wmb edDd+mHE6cP4K53xaJi079+9Zh3Wd01vCbQetPwQh7XWeNTBEIpsVsO4DBNxvdTafOsZAuB48dK 2/n+R5y/+RPn2xAYvysE/fIjy4PMFxKKW3b3urC0LpzBBX8a3TBQD10zFLeZIGDyPh2+1s8w8Bi zlViUeOvByacXopm59GhV+ihcNQBY8+QvSCLKFDxK5bgI3qPZaW8vbmx55QZ3SZhdGbyeJrf+EK Ks0ck2i8t96sb7xx5Sf1C1F1m0XyOpX0+RUBpkPLTniHu5p73C6bk8sLC1gcOqI9pPJMXSeLVsX Azy2WEtuo5QsWLokSluAfKbP3FysNz5yZ+IKdU+fuf2ooVosEX/G6n6jgE908oldKmsletmOjjL nlnQdS6XgM5qP7DpxkgXErs/74XGH9S5ib9UlZu0xhUWOJW6U03KTtG8EHlyVv600PPfmSvJjP7 Z6deF7ycgjc1U3D7l3QNeB+43BLoVWDv7KcXNiynhPbxgypimtnBDGH2pFaEsbnEz54Q6S0jRAA jbBOE8Ss/FVHjZ6GMsuxPI+LVrilmBTjhgTE5Lirg7Ki++pgZlZLtoKugUw== X-Received: by 2002:a05:690c:4cbc:b0:8b3:35d2:c49e with SMTP id 00721157ae682-8b660e157d9mr1273117b3.68.1796335220506; Thu, 03 Dec 2026 14:00:20 -0800 (PST) Received: from localhost ([2a03:2880:10ff:d::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660dfde4dsm956277b3.40.2026.12.03.14.00.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:19 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Baoquan He , Barry Song , Youngjun Park , Johannes Weiner , Yosry Ahmed , Chengming Zhou , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baolin Wang , Michal Hocko , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Hugh Dickins , Jonathan Corbet , Shuah Khan , Randy Dunlap , Roman Gushchin , Muchun Song , Arnd Bergmann , Karl Mehltretter , Dapeng Mi , Marco Elver , Ethan Nelson-Moore , "Borislav Petkov (AMD)" , Jeff Layton , linux-doc@vger.kernel.org, cgroups@vger.kernel.org, Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= Subject: [PATCH v7 05/20] mm/swap: return physical entries from the swap allocator Date: Thu, 3 Dec 2026 13:59:55 -0800 Message-ID: <20261203220010.1302207-6-nphamcs@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The physical allocator reports whether the folio entered swapcache. A folio that already occupies a virtual entry needs the newly allocated physical entry, so its swapcache state cannot report the allocation result. Return the allocated entry from the fast and scanning allocation paths. Share their existing allocation and discard retry in folio_alloc_phys_swap(), which the native folio_alloc_swap() path calls immediately. Keep cache installation inside __swap_cluster_alloc_entries(). The folio metadata and cache pointer retain their existing publication and locking. Signed-off-by: Nhat Pham --- mm/swapfile.c | 59 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index aa08111bea62..fa315389399b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1545,12 +1545,14 @@ 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 bool swap_alloc_fast(struct folio *folio) +static swp_entry_t swap_alloc_fast(struct folio *folio) { unsigned int order = folio_order(folio); struct swap_cluster_info *ci; struct swap_info_struct *si; - unsigned int offset; + unsigned long offset, found = 0; + + lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock); /* * Once allocated, swap_info_struct will never be completely freed, @@ -1559,25 +1561,28 @@ static bool swap_alloc_fast(struct folio *folio) 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 false; + return (swp_entry_t){}; ci = swap_cluster_lock(si, offset); if (ci && cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset = cluster_offset(si, ci); - alloc_swap_scan_cluster(si, ci, folio, offset); + found = alloc_swap_scan_cluster(si, ci, folio, offset); } else if (ci) { swap_cluster_unlock(ci); } put_swap_device(si); - return folio_test_swapcache(folio); + if (found) + return swp_entry(si->type, found); + return (swp_entry_t){}; } /* Rotate the device and switch to a new cluster */ -static void swap_alloc_slow(struct folio *folio) +static swp_entry_t swap_alloc_slow(struct folio *folio) { struct swap_info_struct *si, *next; + unsigned long found; spin_lock(&swap_avail_lock); start_over: @@ -1586,12 +1591,12 @@ static void swap_alloc_slow(struct folio *folio) plist_requeue(&si->avail_list, &swap_avail_head); spin_unlock(&swap_avail_lock); if (get_swap_device_info(si)) { - cluster_alloc_swap_entry(si, folio); + found = cluster_alloc_swap_entry(si, folio); put_swap_device(si); - if (folio_test_swapcache(folio)) - return; + if (found) + return swp_entry(si->type, found); if (folio_test_large(folio)) - return; + return (swp_entry_t){}; } spin_lock(&swap_avail_lock); @@ -1609,6 +1614,7 @@ static void swap_alloc_slow(struct folio *folio) goto start_over; } spin_unlock(&swap_avail_lock); + return (swp_entry_t){}; } /* @@ -1959,6 +1965,23 @@ static bool vswap_alloc(struct folio *folio) return false; } +static swp_entry_t folio_alloc_phys_swap(struct folio *folio) +{ + swp_entry_t entry; + +again: + local_lock(&percpu_swap_cluster.lock); + entry = swap_alloc_fast(folio); + if (!entry.val) + entry = swap_alloc_slow(folio); + local_unlock(&percpu_swap_cluster.lock); + + if (!entry.val && !folio_order(folio) && swap_sync_discard()) + goto again; + + return entry; +} + /** * folio_alloc_swap - allocate swap space for a folio * @folio: folio we want to move to swap @@ -2000,20 +2023,8 @@ int folio_alloc_swap(struct folio *folio) } } - if (may_vswap && vswap_alloc(folio)) - goto done; - -again: - local_lock(&percpu_swap_cluster.lock); - if (!swap_alloc_fast(folio)) - swap_alloc_slow(folio); - local_unlock(&percpu_swap_cluster.lock); - -done: - if (!order && unlikely(!folio_test_swapcache(folio))) { - if (swap_sync_discard()) - goto again; - } + if (!may_vswap || !vswap_alloc(folio)) + folio_alloc_phys_swap(folio); /* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */ if (unlikely(mem_cgroup_try_charge_swap(folio))) { -- 2.53.1-Meta