From nobody Wed Feb 11 08:12:12 2026 Received: from mail-ot1-f45.google.com (mail-ot1-f45.google.com [209.85.210.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E6A7378D73 for ; Sun, 8 Feb 2026 21:59:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770587966; cv=none; b=iFsrtPBxSuTDBNOnSFqnmK5xK1BWxiffqDk2fnvjetpywCRysOHg+kf3EvxQGjiyLGnuVe8SWOa9zXm3balOmvePB4iob1eYfKuBNXli/+RY3oVwCfS8U/o5dr7/xsmIDBrqzVLJJ74g5gX80gZRFQB9T0NC/AFIt7FXynC7NBI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770587966; c=relaxed/simple; bh=VEosGorbCN3vXTzEpm7A1BGYOplMIFk+kHkexBvQWFc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Wc6aTEipKkdHWRSNF4bN9TUd7H1FsCe4z1EltkJVy89XKlordKW0WXKwzvyXs0FvJimjm0x2VYAoo8mWc/8AJD8Gm0sfKqSehM7B3wf9XUu/M75vq9BPfwT6dMn4+rGAfUgNK2+IF/lrcZQxgxs4ciEKq5/BJlS5glQW85+biQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dDXXA8RL; arc=none smtp.client-ip=209.85.210.45 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.b="dDXXA8RL" Received: by mail-ot1-f45.google.com with SMTP id 46e09a7af769-7d148dd3421so871239a34.0 for ; Sun, 08 Feb 2026 13:59:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770587965; x=1771192765; 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; bh=c90iUpuT0uTrBpR1ziQiggNR562GhjEZFfLgw7gtP4Q=; b=dDXXA8RLnhOvLHRBEyN3N+J0sOH+ta9LNuqDprGnUFouUVmgUQj3NxdKS6uO2ZGxMj hrjVXCFNMW4g96g3RnnYUeWellgHeBbxa7LiO1HfoMy7zJxh60yvIAP//1rK6gWnnCXD e72QWQsjRJbRdFmKyxKtFbqdgFV1hG+SMOW5RB/3u2I1z55lFXK2qB86YyYa6v12s5V3 gtSiPWeGIMvsH93TdEe6IijIZbQd83ZUFxqp8eXo6yAKTQYje5HAd+T8BMGSQ3P3Gb8m 2kA1DunEe5KRYSxSKRuqsSgdimftZv1g7kQZHIL8oofd4S6zN9KuihZqaVKRB57NH74C L3ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770587965; x=1771192765; 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; bh=c90iUpuT0uTrBpR1ziQiggNR562GhjEZFfLgw7gtP4Q=; b=izJtuODownPQLjs0nbODQpjnMo/61OlqJo9TvO+wc1yv5//7ZB2Z3WJZh1kiTsc9Uu ZVB2hUqXUvLLPiQB+lJuAikxZlriZ+guK5TIpURFbJ7v8ajF0lc7I+OOgevYHMzE+CEI oDO+H2hFz4vd7Y1JH/DKLdwN7k8pazxgeBjJB/A0/OBXYVdYxg6KR1QtkpEY6S27jEsI D0jKn9EV/uS7gob5hoXLSETDtPvdlc+v5xR9NtldC7TqutUoufnD/h1pKujc/wG5e7qC GKhjFImvC54yalF4B++G2rAYIt0kdwMRkzmAdYZu7n+vz3c/Jugox0AYEQqY21u+ImIA V1Dg== X-Forwarded-Encrypted: i=1; AJvYcCVdukNQna5fE1xk88qx8hSeVfEV/+GqhV/aNefalTwkWXoj11kuzA1KhrD6jWI9np42ANGmxlTiduusVM8=@vger.kernel.org X-Gm-Message-State: AOJu0YzngY03T1drP1ImY5DazsGKTWckrSvDIuuGiOz3BmvZbVRGp2KZ ZR7z641boIjITiFadyGYOATuTOnv2AWmziGEcWIa8KcAGts22Ij2hIwm X-Gm-Gg: AZuq6aL9hXwePZ6/eKhoS9FpXQA53fpEPyccS+/FBMQ7GKQtDRjJmgaggXZaVe4r2yA Lp0VKVoiudPJdXOxMYrQz3eyYZ94eZpiTdmwmiQZdbQULKOwvWjMxVaiABe2xakV+5No4VbcJ6i 6nvGe3gYI1M/8kCWhV1r3OduB+k9GH+5pjm6krGoYDhGrbBoNZMGy9m1rlXuSqpeYO1fNjEfGRa 0Cqpmp2HtofpDsxqkUTQqa3BbVQzO3GZ+AhkjcxSWanHlSgAvvBWV8zk72qvTc5J3s6213TXp59 bUAd83TZNzhmfnp9vYZ4oETIv/y/hXokIb+hHqIVC8Z1tWpLmSnUMybSyUeMF2j4w9eJkOn9No4 LaUZnYAN+E8QMWosLZV9xMUAkjFntMfdVSWyAHFb6q0aPCDY5GrJmE4P0vURfaOJrrO/LOZjVz3 7GpC4lra2tfqsUz7mpFInEmgzaiFQapdbI2Q== X-Received: by 2002:a05:6830:6f47:b0:7d1:8ad3:ce7 with SMTP id 46e09a7af769-7d4643e7eb1mr4865896a34.1.1770587965213; Sun, 08 Feb 2026 13:59:25 -0800 (PST) Received: from localhost ([2a03:2880:10ff:40::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7d46479954bsm6335454a34.21.2026.02.08.13.59.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Feb 2026 13:59:24 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, hughd@google.com, yosry.ahmed@linux.dev, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, len.brown@intel.com, chengming.zhou@linux.dev, kasong@tencent.com, chrisl@kernel.org, huang.ying.caritas@gmail.com, ryan.roberts@arm.com, shikemeng@huaweicloud.com, viro@zeniv.linux.org.uk, baohua@kernel.org, bhe@redhat.com, osalvador@suse.de, lorenzo.stoakes@oracle.com, christophe.leroy@csgroup.eu, pavel@kernel.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-pm@vger.kernel.org, peterx@redhat.com, riel@surriel.com, joshua.hahnjy@gmail.com, npache@redhat.com, gourry@gourry.net, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, rafael@kernel.org, jannh@google.com, pfalcato@suse.de, zhengqi.arch@bytedance.com Subject: [PATCH v3 20/20] swapfile: replace the swap map with bitmaps Date: Sun, 8 Feb 2026 13:58:33 -0800 Message-ID: <20260208215839.87595-21-nphamcs@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260208215839.87595-1-nphamcs@gmail.com> References: <20260208215839.87595-1-nphamcs@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that we have moved the swap count state to virtual swap layer, each swap map entry only has 3 possible states: free, allocated, and bad. Replace the swap map with 2 bitmaps (one for allocated state and one for bad state), saving 6 bits per swap entry. Signed-off-by: Nhat Pham --- include/linux/swap.h | 3 +- mm/swapfile.c | 81 +++++++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index a30d382fb5ee1..a02ce3fb2358b 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -259,7 +259,8 @@ struct swap_info_struct { struct plist_node list; /* entry in swap_active_head */ signed char type; /* strange name for an index */ unsigned int max; /* extent of the swap_map */ - unsigned char *swap_map; /* vmalloc'ed array of usage counts */ + unsigned long *swap_map; /* bitmap for allocated state */ + unsigned long *bad_map; /* bitmap for bad state */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ struct list_head free_clusters; /* free clusters list */ struct list_head full_clusters; /* full clusters list */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 9478707ce3ffa..b7661ffa312be 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -760,25 +760,19 @@ static bool cluster_reclaim_range(struct swap_info_st= ruct *si, struct swap_cluster_info *ci, unsigned long start, unsigned long end) { - unsigned char *map =3D si->swap_map; unsigned long offset =3D start; int nr_reclaim; =20 spin_unlock(&ci->lock); do { - switch (READ_ONCE(map[offset])) { - case 0: + if (!test_bit(offset, si->swap_map)) { offset++; - break; - case SWAP_MAP_ALLOCATED: + } else { nr_reclaim =3D __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); if (nr_reclaim > 0) offset +=3D nr_reclaim; else goto out; - break; - default: - goto out; } } while (offset < end); out: @@ -787,11 +781,7 @@ static bool cluster_reclaim_range(struct swap_info_str= uct *si, * Recheck the range no matter reclaim succeeded or not, the slot * could have been be freed while we are not holding the lock. */ - for (offset =3D start; offset < end; offset++) - if (READ_ONCE(map[offset])) - return false; - - return true; + return find_next_bit(si->swap_map, end, start) >=3D end; } =20 static bool cluster_scan_range(struct swap_info_struct *si, @@ -800,15 +790,16 @@ static bool cluster_scan_range(struct swap_info_struc= t *si, bool *need_reclaim) { unsigned long offset, end =3D start + nr_pages; - unsigned char *map =3D si->swap_map; - unsigned char count; =20 if (cluster_is_empty(ci)) return true; =20 for (offset =3D start; offset < end; offset++) { - count =3D READ_ONCE(map[offset]); - if (!count) + /* Bad slots cannot be used for allocation */ + if (test_bit(offset, si->bad_map)) + return false; + + if (!test_bit(offset, si->swap_map)) continue; =20 if (swap_cache_only(si, offset)) { @@ -841,7 +832,7 @@ static bool cluster_alloc_range(struct swap_info_struct= *si, struct swap_cluster if (cluster_is_empty(ci)) ci->order =3D order; =20 - memset(si->swap_map + start, usage, nr_pages); + bitmap_set(si->swap_map, start, nr_pages); swap_range_alloc(si, nr_pages); ci->count +=3D nr_pages; =20 @@ -1404,7 +1395,7 @@ static struct swap_info_struct *_swap_info_get(swp_sl= ot_t slot) offset =3D swp_slot_offset(slot); if (offset >=3D si->max) goto bad_offset; - if (data_race(!si->swap_map[swp_slot_offset(slot)])) + if (data_race(!test_bit(offset, si->swap_map))) goto bad_free; return si; =20 @@ -1518,8 +1509,7 @@ static void swap_slots_free(struct swap_info_struct *= si, swp_slot_t slot, unsigned int nr_pages) { unsigned long offset =3D swp_slot_offset(slot); - unsigned char *map =3D si->swap_map + offset; - unsigned char *map_end =3D map + nr_pages; + unsigned long end =3D offset + nr_pages; =20 /* It should never free entries across different clusters */ VM_BUG_ON(ci !=3D __swap_offset_to_cluster(si, offset + nr_pages - 1)); @@ -1527,10 +1517,8 @@ static void swap_slots_free(struct swap_info_struct = *si, VM_BUG_ON(ci->count < nr_pages); =20 ci->count -=3D nr_pages; - do { - VM_BUG_ON(!swap_is_last_ref(*map)); - *map =3D 0; - } while (++map < map_end); + VM_BUG_ON(find_next_zero_bit(si->swap_map, end, offset) < end); + bitmap_clear(si->swap_map, offset, nr_pages); =20 swap_range_free(si, offset, nr_pages); =20 @@ -1741,9 +1729,7 @@ unsigned int count_swap_pages(int type, int free) static bool swap_slot_allocated(struct swap_info_struct *si, unsigned long offset) { - unsigned char count =3D READ_ONCE(si->swap_map[offset]); - - return count && swap_count(count) !=3D SWAP_MAP_BAD; + return test_bit(offset, si->swap_map); } =20 /* @@ -2064,7 +2050,7 @@ static int setup_swap_extents(struct swap_info_struct= *sis, sector_t *span) } =20 static void setup_swap_info(struct swap_info_struct *si, int prio, - unsigned char *swap_map, + unsigned long *swap_map, struct swap_cluster_info *cluster_info) { si->prio =3D prio; @@ -2092,7 +2078,7 @@ static void _enable_swap_info(struct swap_info_struct= *si) } =20 static void enable_swap_info(struct swap_info_struct *si, int prio, - unsigned char *swap_map, + unsigned long *swap_map, struct swap_cluster_info *cluster_info) { spin_lock(&swap_lock); @@ -2185,7 +2171,8 @@ static void flush_percpu_swap_cluster(struct swap_inf= o_struct *si) SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) { struct swap_info_struct *p =3D NULL; - unsigned char *swap_map; + unsigned long *swap_map; + unsigned long *bad_map; struct swap_cluster_info *cluster_info; struct file *swap_file, *victim; struct address_space *mapping; @@ -2280,6 +2267,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) p->swap_file =3D NULL; swap_map =3D p->swap_map; p->swap_map =3D NULL; + bad_map =3D p->bad_map; + p->bad_map =3D NULL; maxpages =3D p->max; cluster_info =3D p->cluster_info; p->max =3D 0; @@ -2290,7 +2279,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) mutex_unlock(&swapon_mutex); kfree(p->global_cluster); p->global_cluster =3D NULL; - vfree(swap_map); + kvfree(swap_map); + kvfree(bad_map); free_cluster_info(cluster_info, maxpages); =20 inode =3D mapping->host; @@ -2638,18 +2628,20 @@ static unsigned long read_swap_header(struct swap_i= nfo_struct *si, =20 static int setup_swap_map(struct swap_info_struct *si, union swap_header *swap_header, - unsigned char *swap_map, + unsigned long *swap_map, + unsigned long *bad_map, unsigned long maxpages) { unsigned long i; =20 - swap_map[0] =3D SWAP_MAP_BAD; /* omit header page */ + set_bit(0, bad_map); /* omit header page */ + for (i =3D 0; i < swap_header->info.nr_badpages; i++) { unsigned int page_nr =3D swap_header->info.badpages[i]; if (page_nr =3D=3D 0 || page_nr > swap_header->info.last_page) return -EINVAL; if (page_nr < maxpages) { - swap_map[page_nr] =3D SWAP_MAP_BAD; + set_bit(page_nr, bad_map); si->pages--; } } @@ -2753,7 +2745,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) int nr_extents; sector_t span; unsigned long maxpages; - unsigned char *swap_map =3D NULL; + unsigned long *swap_map =3D NULL, *bad_map =3D NULL; struct swap_cluster_info *cluster_info =3D NULL; struct folio *folio =3D NULL; struct inode *inode =3D NULL; @@ -2849,16 +2841,24 @@ SYSCALL_DEFINE2(swapon, const char __user *, specia= lfile, int, swap_flags) maxpages =3D si->max; =20 /* OK, set up the swap map and apply the bad block list */ - swap_map =3D vzalloc(maxpages); + swap_map =3D kvcalloc(BITS_TO_LONGS(maxpages), sizeof(long), GFP_KERNEL); if (!swap_map) { error =3D -ENOMEM; goto bad_swap_unlock_inode; } =20 - error =3D setup_swap_map(si, swap_header, swap_map, maxpages); + bad_map =3D kvcalloc(BITS_TO_LONGS(maxpages), sizeof(long), GFP_KERNEL); + if (!bad_map) { + error =3D -ENOMEM; + goto bad_swap_unlock_inode; + } + + error =3D setup_swap_map(si, swap_header, swap_map, bad_map, maxpages); if (error) goto bad_swap_unlock_inode; =20 + si->bad_map =3D bad_map; + if (si->bdev && bdev_stable_writes(si->bdev)) si->flags |=3D SWP_STABLE_WRITES; =20 @@ -2952,7 +2952,10 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) si->swap_file =3D NULL; si->flags =3D 0; spin_unlock(&swap_lock); - vfree(swap_map); + if (swap_map) + kvfree(swap_map); + if (bad_map) + kvfree(bad_map); if (cluster_info) free_cluster_info(cluster_info, maxpages); if (inced_nr_rotate_swap) --=20 2.47.3