From nobody Fri Apr 3 01:24:29 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DFBFC313524; Mon, 16 Feb 2026 14:58:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771253888; cv=none; b=qdsUO3YlwuEvY8fTQgfjojZ+Q5tH7Jse5EMgf/lN/ZGxzXKcQIUUSYJ9JKUDgEurBeCLF3+BP8Tz6sc3bB56yGMUSLiFQNWjSgODmGuEJ/zA+He6HtLz9d4sMpa9ceXoo88Rk5mvGzT5yxYc8gBm4joZ+exhYoOdMuR5t0Ink0k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771253888; c=relaxed/simple; bh=myZyOnHzp3lPkDV1e/C2uP7wr+40p+PY2MQZBb28QNM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IAht66NYJZBQ/JuqFO/aTlRGFpwggXyqxJSZOWIM8VPn5z49v9UwEu+CUaGn5Bcd3Ef/+/D8j6nECqOwU/xaq40Wg7lgaEV1fh4Tc2PhBjjgyQgsvMxFUd+sy/fUv4BiCbyA24r/8g9n8aVUBdxelTvTPaDb2egm4q2ebTH2edE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=l+Gzv+/s; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="l+Gzv+/s" Received: by smtp.kernel.org (Postfix) with ESMTPS id BB19EC2BC86; Mon, 16 Feb 2026 14:58:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771253887; bh=myZyOnHzp3lPkDV1e/C2uP7wr+40p+PY2MQZBb28QNM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=l+Gzv+/sQQ4gp2ucvDPntyFzntvJ9KJ0ebymysuwtN+qX7m7zMEXm3MMn/AalPv+E jSEzV2n9MICzI1NIzmO8o78n297/bVF7g40z1thhSU9uTSz8wEluVywhNKGPPO5nLu +KctywZ7ENltWgIDQXhQOB+taPHiBPtSZ+u6TqrY+egBC/xfSohDZIAuQXrJQH1EIW wTCqr2oMh0vRwdhZ2cnoou0aHVW4h4dlvVHBBIZYIHhrn9lyzLmx/N2tcpyEfAS1BZ rC3zOMX3m/ctsyCQfph+WAuANsaHTb3fEWJwS7nJx0llHjelyCJ+1x9AMT8UCIOtRT tschDypbqiasA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id A823BE81A28; Mon, 16 Feb 2026 14:58:07 +0000 (UTC) From: Kairui Song via B4 Relay Date: Mon, 16 Feb 2026 22:58:03 +0800 Subject: [PATCH v4 2/3] mm, swap: reduce indention for hibernate allocation helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260216-hibernate-perf-v4-2-1ba9f0bf1ec9@tencent.com> References: <20260216-hibernate-perf-v4-0-1ba9f0bf1ec9@tencent.com> In-Reply-To: <20260216-hibernate-perf-v4-0-1ba9f0bf1ec9@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Carsten Grohmann , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org, "open list:SUSPEND TO RAM" , Carsten Grohmann , Kairui Song X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1771253885; l=2613; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=NAvqyk1BCM82J8HW6Gnh4CX79o/hdmbrD/B6Y3rHXiw=; b=k/nFxKLOf037gv1sMhovQ1z7Lzzq5b7SFtNWk9LCk4plm6mzv2JlgbNEHqsWpL6ecmjVkQy7s lmvDhThxszvBx4B7SxU8s36kT/fNDml41igu0SWj6EFXOnsA1XxehU2 X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song It doesn't have to check the device flag, as the allocator will also check the device flag and refuse to allocate if the device is not writable. This might cause a trivial waste of CPU cycles of hibernate allocation raced with swapoff, but that is very unlikely to happen. Removing the check on the common path should be more helpful. Signed-off-by: Kairui Song --- mm/swapfile.c | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 32e0e7545ab8..ea63885f344a 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1931,35 +1931,30 @@ swp_entry_t swap_alloc_hibernation_slot(int type) struct swap_cluster_info *ci; swp_entry_t entry =3D {0}; =20 - if (!si) - goto fail; - - /* This is called for allocating swap entry, not cache */ - if (get_swap_device_info(si)) { - if (si->flags & SWP_WRITEOK) { - /* - * Try the local cluster first if it matches the device. If - * not, try grab a new cluster and override local cluster. - */ - local_lock(&percpu_swap_cluster.lock); - pcp_si =3D this_cpu_read(percpu_swap_cluster.si[0]); - pcp_offset =3D this_cpu_read(percpu_swap_cluster.offset[0]); - if (pcp_si =3D=3D si && pcp_offset) { - ci =3D swap_cluster_lock(si, pcp_offset); - if (cluster_is_usable(ci, 0)) - offset =3D alloc_swap_scan_cluster(si, ci, NULL, pcp_offset); - else - swap_cluster_unlock(ci); - } - if (!offset) - offset =3D cluster_alloc_swap_entry(si, NULL); - local_unlock(&percpu_swap_cluster.lock); - if (offset) - entry =3D swp_entry(si->type, offset); - } - put_swap_device(si); + /* Return empty entry if device is not usable (swapoff or full) */ + if (!si || !get_swap_device_info(si)) + return entry; + /* + * Try the local cluster first if it matches the device. If + * not, try grab a new cluster and override local cluster. + */ + local_lock(&percpu_swap_cluster.lock); + pcp_si =3D this_cpu_read(percpu_swap_cluster.si[0]); + pcp_offset =3D this_cpu_read(percpu_swap_cluster.offset[0]); + if (pcp_si =3D=3D si && pcp_offset) { + ci =3D swap_cluster_lock(si, pcp_offset); + if (cluster_is_usable(ci, 0)) + offset =3D alloc_swap_scan_cluster(si, ci, NULL, pcp_offset); + else + swap_cluster_unlock(ci); } -fail: + if (offset =3D=3D SWAP_ENTRY_INVALID) + offset =3D cluster_alloc_swap_entry(si, NULL); + local_unlock(&percpu_swap_cluster.lock); + if (offset) + entry =3D swp_entry(si->type, offset); + put_swap_device(si); + return entry; } =20 --=20 2.52.0