From patchwork Mon Jan 13 17:57:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13937874 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 05D2EC02183 for ; Mon, 13 Jan 2025 18:00:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 89F7B6B007B; Mon, 13 Jan 2025 13:00:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 828166B00A0; Mon, 13 Jan 2025 13:00:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A2CB6B00A1; Mon, 13 Jan 2025 13:00:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 455056B007B for ; Mon, 13 Jan 2025 13:00:26 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E4573A0208 for ; Mon, 13 Jan 2025 18:00:25 +0000 (UTC) X-FDA: 83003193210.11.EAB03C1 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf24.hostedemail.com (Postfix) with ESMTP id D3A67180020 for ; Mon, 13 Jan 2025 18:00:23 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=I23tGI2X; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736791223; a=rsa-sha256; cv=none; b=m7E0GQ1tO+ggAqaWsQORiSaWBS3XLMEfTMZe+/RZDa07PIqsFm0PbQv2hVXx2mLx7QhGJQ SFdt6R3v7eVnxqXRifFIcnZDdLOGVZrD16V1t5ynEQBzIOfJO6LL4t6jYkGlIWdDyXJT1i G3WetfZDficWa6lmgjSHasy8xMNfK54= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=I23tGI2X; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736791223; 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=ZcBqrejXjDN/SI+43XhdviH2TahNKIN+YXabcnMV+tI=; b=McvXnXRz/TXwR0pCKq/2TzT6NE9GzEJIEw+EbdHNsw8B6wpP0Q855XrO8uTor3quXLBK/y kFXjPiXSblYlx11YE5EkLGYB+mpWDChAhmfBQMJCuCv3QG9hpryEv78JPqO6dPV4G19aZD Jrbr75zyOaY8e5IhY4zviEB17SrVoMA= Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2eeb4d643a5so8058872a91.3 for ; Mon, 13 Jan 2025 10:00:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736791222; x=1737396022; 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=ZcBqrejXjDN/SI+43XhdviH2TahNKIN+YXabcnMV+tI=; b=I23tGI2XMiubGHc+opkLv8g6JovBykU7ASxp/QtHqdRevAcemgtuW6h4cuoiw0+yQw NyNg2kR+ogZeMQfag+yXMv0iVC31oOjh9EyfDVuo8YfPXTCGbObABOvFBzL5U+eaOP6X aez7kmrQXQSeDKYSmkv97viAgQXByKJUEbDWRd8ceE2FxtUjGc+1Y/BXDGsiMKCJqJU5 R5VSsv0vc4WPdaUoYe1Gh7LRRxIlHawQMzNFPx2+wZlFtIhm7xzVqKdhFcxBjPmDA33Q pFpkyuUdOibJVvShXIHhuc5kwQDc+AuSNZIpL/QgqsOzu2rhw1ajylklV7L4ZHIDZHOn u6tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736791222; x=1737396022; 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=ZcBqrejXjDN/SI+43XhdviH2TahNKIN+YXabcnMV+tI=; b=KhFp/BYpKgLXyvsalqcZNUUuxJkFOq3Kf64r5ttLx2//Hbntrv3C/1x7oLS3DD7eyS r0XNxMmOhyU/f+UnHYWrMJWVPpUfJqTNmql6Xylkc3t/SFLhi3yknfRntdgcWU3nrceD CRhrufXcuBPiea3szbANqYQ31mZpv4/VOVdervN7Ax42PKL4QMlyEipNNsNpxS3I4VSA 4TLWvf3alF+rH6AUMdQHe1AM/iMYAvOSwbk5i3PJ67PEiR7nkXG4C939IzJrLZ3AKuk9 SNLBUBYfPk2gU89Ytm6psodG1Alo8uyR9s/f4JU7EoD2qcvkrOWrgFbMW/8bRfmSibzl Ivig== X-Gm-Message-State: AOJu0YwYBhz1T2ROamwb26O5QydoosNPV0p83IdDUb23GAyd3H3e3LFH wHC+l6rwj1LoP1sDLgpXumktmgtT41DHbdMV1NWruaWVMekI2rdBKAaLI6oLtyI= X-Gm-Gg: ASbGncu7dfTQtT/IIBlKAeElBrVIzTt1RvAkq9ysznmHZIVxZ9rgikufce5d3PHSnMc XcqbIZUZzO72p4y7Fk6swjL2LUdwejFmbrrs9b8ObZxh0JhWQCZxvibWFd2qHsA0NLChT9dGokA mIUsxchTf9EweYRWIMDRW+XmtgdAe5G3TOd+UnCKuIcqVyt4s1AQgKc2DMhSVygD9fchoA8V3ue Ki2HsQmcJu7aAZJ+nnE8/771e6ZaQoRsSCtwMfC8kY1s4ocJ300tf+S2qo9Yb5L88JEL8wO8ZfY +A== X-Google-Smtp-Source: AGHT+IF7Lv6p+QRH7O5OPr3Tvh76Vf7drCt/uBv+BS+KwCQv0OckMgkTAS/vMPDOVtUVb/e8JFn2VQ== X-Received: by 2002:a17:90b:54c4:b0:2ee:8ea0:6b9c with SMTP id 98e67ed59e1d1-2f548f33220mr37465138a91.12.1736791221839; Mon, 13 Jan 2025 10:00:21 -0800 (PST) Received: from KASONG-MC4.tencent.com ([115.171.41.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f21aba7sm57023635ad.113.2025.01.13.10.00.18 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 10:00:21 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Ryan Roberts , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v4 08/13] mm, swap: use an enum to define all cluster flags and wrap flags changes Date: Tue, 14 Jan 2025 01:57:27 +0800 Message-ID: <20250113175732.48099-9-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250113175732.48099-1-ryncsn@gmail.com> References: <20250113175732.48099-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D3A67180020 X-Stat-Signature: 9rs79niri74p3jgku9p17tcah1et9pqo X-Rspam-User: X-HE-Tag: 1736791223-95037 X-HE-Meta: U2FsdGVkX1/E8NR6jgq2ULrpqvTaN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bPftsedYCJrbtn6X1W3rVHD/0R1tmAI63bL/yKbiM17HVi7l5g/JG214HWeLIiIMXBv104JjW2hc4Zudbf6gRsEKfyv8Y1OkbGKb9ZBCHdup1gCxTKillfHwW/PJTHb6Dz8K0a2cnyHTQ14As0OtIZbs6wheL3BQvTKYOHeTyzVx/3bRbfk07MIcScjl8KD2epYc+Ds8KneZVBBFtuXMey3nNfVBRngDqY+oTbdYVHme7emvIdvHh4Cjk719CeAuoXtyhQpsy13qeMDajpPNjudhfayumA1lFy/PdfbaULIK+fFFDNREK7QMM/G1Akfb74FogmsNmiM+3Bv6wkrGQ0vxFfv6mbmAz4cs6LArWDvoLX7DBW7lT4EBOFzmD9Ls6bBk/MKrRvEqP6N/Hb6DQsbWmH/oHF9NKDBeXy/qoSJMy0NY= 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, we are only using flags to indicate which list the cluster is on. Using one bit for each list type might be a waste, as the list type grows, we will consume too many bits. Additionally, the current mixed usage of '&' and '==' is a bit confusing. Make it clean by using an enum to define all possible cluster statuses. Only an off-list cluster will have the NONE (0) flag. And use a wrapper to annotate and sanitize all flag settings and list movements. Suggested-by: Chris Li Signed-off-by: Kairui Song --- include/linux/swap.h | 17 +++++++--- mm/swapfile.c | 76 +++++++++++++++++++++++--------------------- 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 02120f1005d5..339d7f0192ff 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -257,10 +257,19 @@ struct swap_cluster_info { u8 order; struct list_head list; }; -#define CLUSTER_FLAG_FREE 1 /* This cluster is free */ -#define CLUSTER_FLAG_NONFULL 2 /* This cluster is on nonfull list */ -#define CLUSTER_FLAG_FRAG 4 /* This cluster is on nonfull list */ -#define CLUSTER_FLAG_FULL 8 /* This cluster is on full list */ + +/* All on-list cluster must have a non-zero flag. */ +enum swap_cluster_flags { + CLUSTER_FLAG_NONE = 0, /* For temporary off-list cluster */ + CLUSTER_FLAG_FREE, + CLUSTER_FLAG_NONFULL, + CLUSTER_FLAG_FRAG, + /* Clusters with flags above are allocatable */ + CLUSTER_FLAG_USABLE = CLUSTER_FLAG_FRAG, + CLUSTER_FLAG_FULL, + CLUSTER_FLAG_DISCARD, + CLUSTER_FLAG_MAX, +}; /* * The first page in the swap file is the swap header, which is always marked diff --git a/mm/swapfile.c b/mm/swapfile.c index 3898576f947a..b754c9e16c3b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -403,7 +403,7 @@ static void discard_swap_cluster(struct swap_info_struct *si, static inline bool cluster_is_free(struct swap_cluster_info *info) { - return info->flags & CLUSTER_FLAG_FREE; + return info->flags == CLUSTER_FLAG_FREE; } static inline unsigned int cluster_index(struct swap_info_struct *si, @@ -434,6 +434,28 @@ static inline void unlock_cluster(struct swap_cluster_info *ci) spin_unlock(&ci->lock); } +static void move_cluster(struct swap_info_struct *si, + struct swap_cluster_info *ci, struct list_head *list, + enum swap_cluster_flags new_flags) +{ + VM_WARN_ON(ci->flags == new_flags); + + BUILD_BUG_ON(1 << sizeof(ci->flags) * BITS_PER_BYTE < CLUSTER_FLAG_MAX); + + if (ci->flags == CLUSTER_FLAG_NONE) { + list_add_tail(&ci->list, list); + } else { + if (ci->flags == CLUSTER_FLAG_FRAG) { + VM_WARN_ON(!si->frag_cluster_nr[ci->order]); + si->frag_cluster_nr[ci->order]--; + } + list_move_tail(&ci->list, list); + } + ci->flags = new_flags; + if (new_flags == CLUSTER_FLAG_FRAG) + si->frag_cluster_nr[ci->order]++; +} + /* Add a cluster to discard list and schedule it to do discard */ static void swap_cluster_schedule_discard(struct swap_info_struct *si, struct swap_cluster_info *ci) @@ -447,10 +469,8 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si, */ memset(si->swap_map + idx * SWAPFILE_CLUSTER, SWAP_MAP_BAD, SWAPFILE_CLUSTER); - - VM_BUG_ON(ci->flags & CLUSTER_FLAG_FREE); - list_move_tail(&ci->list, &si->discard_clusters); - ci->flags = 0; + VM_BUG_ON(ci->flags == CLUSTER_FLAG_FREE); + move_cluster(si, ci, &si->discard_clusters, CLUSTER_FLAG_DISCARD); schedule_work(&si->discard_work); } @@ -458,12 +478,7 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info { lockdep_assert_held(&si->lock); lockdep_assert_held(&ci->lock); - - if (ci->flags) - list_move_tail(&ci->list, &si->free_clusters); - else - list_add_tail(&ci->list, &si->free_clusters); - ci->flags = CLUSTER_FLAG_FREE; + move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE); ci->order = 0; } @@ -479,6 +494,8 @@ static void swap_do_scheduled_discard(struct swap_info_struct *si) while (!list_empty(&si->discard_clusters)) { ci = list_first_entry(&si->discard_clusters, struct swap_cluster_info, list); list_del(&ci->list); + /* Must clear flag when taking a cluster off-list */ + ci->flags = CLUSTER_FLAG_NONE; idx = cluster_index(si, ci); spin_unlock(&si->lock); @@ -519,9 +536,6 @@ static void free_cluster(struct swap_info_struct *si, struct swap_cluster_info * lockdep_assert_held(&si->lock); lockdep_assert_held(&ci->lock); - if (ci->flags & CLUSTER_FLAG_FRAG) - si->frag_cluster_nr[ci->order]--; - /* * If the swap is discardable, prepare discard the cluster * instead of free it immediately. The cluster will be freed @@ -573,13 +587,9 @@ static void dec_cluster_info_page(struct swap_info_struct *si, return; } - if (!(ci->flags & CLUSTER_FLAG_NONFULL)) { - VM_BUG_ON(ci->flags & CLUSTER_FLAG_FREE); - if (ci->flags & CLUSTER_FLAG_FRAG) - si->frag_cluster_nr[ci->order]--; - list_move_tail(&ci->list, &si->nonfull_clusters[ci->order]); - ci->flags = CLUSTER_FLAG_NONFULL; - } + if (ci->flags != CLUSTER_FLAG_NONFULL) + move_cluster(si, ci, &si->nonfull_clusters[ci->order], + CLUSTER_FLAG_NONFULL); } static bool cluster_reclaim_range(struct swap_info_struct *si, @@ -663,11 +673,13 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster if (!(si->flags & SWP_WRITEOK)) return false; + VM_BUG_ON(ci->flags == CLUSTER_FLAG_NONE); + VM_BUG_ON(ci->flags > CLUSTER_FLAG_USABLE); + if (cluster_is_free(ci)) { - if (nr_pages < SWAPFILE_CLUSTER) { - list_move_tail(&ci->list, &si->nonfull_clusters[order]); - ci->flags = CLUSTER_FLAG_NONFULL; - } + if (nr_pages < SWAPFILE_CLUSTER) + move_cluster(si, ci, &si->nonfull_clusters[order], + CLUSTER_FLAG_NONFULL); ci->order = order; } @@ -675,14 +687,8 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster swap_range_alloc(si, nr_pages); ci->count += nr_pages; - if (ci->count == SWAPFILE_CLUSTER) { - VM_BUG_ON(!(ci->flags & - (CLUSTER_FLAG_FREE | CLUSTER_FLAG_NONFULL | CLUSTER_FLAG_FRAG))); - if (ci->flags & CLUSTER_FLAG_FRAG) - si->frag_cluster_nr[ci->order]--; - list_move_tail(&ci->list, &si->full_clusters); - ci->flags = CLUSTER_FLAG_FULL; - } + if (ci->count == SWAPFILE_CLUSTER) + move_cluster(si, ci, &si->full_clusters, CLUSTER_FLAG_FULL); return true; } @@ -821,9 +827,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o while (!list_empty(&si->nonfull_clusters[order])) { ci = list_first_entry(&si->nonfull_clusters[order], struct swap_cluster_info, list); - list_move_tail(&ci->list, &si->frag_clusters[order]); - ci->flags = CLUSTER_FLAG_FRAG; - si->frag_cluster_nr[order]++; + move_cluster(si, ci, &si->frag_clusters[order], CLUSTER_FLAG_FRAG); offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), &found, order, usage); frags++;