xref: /linux/mm/swap_table.h (revision 8804d970fab45726b3c7cd7f240b31122aa94219)
18578e0c0SKairui Song /* SPDX-License-Identifier: GPL-2.0 */
28578e0c0SKairui Song #ifndef _MM_SWAP_TABLE_H
38578e0c0SKairui Song #define _MM_SWAP_TABLE_H
48578e0c0SKairui Song 
507adc4cfSKairui Song #include <linux/rcupdate.h>
607adc4cfSKairui Song #include <linux/atomic.h>
78578e0c0SKairui Song #include "swap.h"
88578e0c0SKairui Song 
907adc4cfSKairui Song /* A typical flat array in each cluster as swap table */
1007adc4cfSKairui Song struct swap_table {
1107adc4cfSKairui Song 	atomic_long_t entries[SWAPFILE_CLUSTER];
1207adc4cfSKairui Song };
1307adc4cfSKairui Song 
14*f83938e4SKairui Song #define SWP_TABLE_USE_PAGE (sizeof(struct swap_table) == PAGE_SIZE)
15*f83938e4SKairui Song 
168578e0c0SKairui Song /*
178578e0c0SKairui Song  * A swap table entry represents the status of a swap slot on a swap
188578e0c0SKairui Song  * (physical or virtual) device. The swap table in each cluster is a
198578e0c0SKairui Song  * 1:1 map of the swap slots in this cluster.
208578e0c0SKairui Song  *
218578e0c0SKairui Song  * Each swap table entry could be a pointer (folio), a XA_VALUE
228578e0c0SKairui Song  * (shadow), or NULL.
238578e0c0SKairui Song  */
248578e0c0SKairui Song 
258578e0c0SKairui Song /*
268578e0c0SKairui Song  * Helpers for casting one type of info into a swap table entry.
278578e0c0SKairui Song  */
null_to_swp_tb(void)288578e0c0SKairui Song static inline unsigned long null_to_swp_tb(void)
298578e0c0SKairui Song {
308578e0c0SKairui Song 	BUILD_BUG_ON(sizeof(unsigned long) != sizeof(atomic_long_t));
318578e0c0SKairui Song 	return 0;
328578e0c0SKairui Song }
338578e0c0SKairui Song 
folio_to_swp_tb(struct folio * folio)348578e0c0SKairui Song static inline unsigned long folio_to_swp_tb(struct folio *folio)
358578e0c0SKairui Song {
368578e0c0SKairui Song 	BUILD_BUG_ON(sizeof(unsigned long) != sizeof(void *));
378578e0c0SKairui Song 	return (unsigned long)folio;
388578e0c0SKairui Song }
398578e0c0SKairui Song 
shadow_swp_to_tb(void * shadow)408578e0c0SKairui Song static inline unsigned long shadow_swp_to_tb(void *shadow)
418578e0c0SKairui Song {
428578e0c0SKairui Song 	BUILD_BUG_ON((BITS_PER_XA_VALUE + 1) !=
438578e0c0SKairui Song 		     BITS_PER_BYTE * sizeof(unsigned long));
448578e0c0SKairui Song 	VM_WARN_ON_ONCE(shadow && !xa_is_value(shadow));
458578e0c0SKairui Song 	return (unsigned long)shadow;
468578e0c0SKairui Song }
478578e0c0SKairui Song 
488578e0c0SKairui Song /*
498578e0c0SKairui Song  * Helpers for swap table entry type checking.
508578e0c0SKairui Song  */
swp_tb_is_null(unsigned long swp_tb)518578e0c0SKairui Song static inline bool swp_tb_is_null(unsigned long swp_tb)
528578e0c0SKairui Song {
538578e0c0SKairui Song 	return !swp_tb;
548578e0c0SKairui Song }
558578e0c0SKairui Song 
swp_tb_is_folio(unsigned long swp_tb)568578e0c0SKairui Song static inline bool swp_tb_is_folio(unsigned long swp_tb)
578578e0c0SKairui Song {
588578e0c0SKairui Song 	return !xa_is_value((void *)swp_tb) && !swp_tb_is_null(swp_tb);
598578e0c0SKairui Song }
608578e0c0SKairui Song 
swp_tb_is_shadow(unsigned long swp_tb)618578e0c0SKairui Song static inline bool swp_tb_is_shadow(unsigned long swp_tb)
628578e0c0SKairui Song {
638578e0c0SKairui Song 	return xa_is_value((void *)swp_tb);
648578e0c0SKairui Song }
658578e0c0SKairui Song 
668578e0c0SKairui Song /*
678578e0c0SKairui Song  * Helpers for retrieving info from swap table.
688578e0c0SKairui Song  */
swp_tb_to_folio(unsigned long swp_tb)698578e0c0SKairui Song static inline struct folio *swp_tb_to_folio(unsigned long swp_tb)
708578e0c0SKairui Song {
718578e0c0SKairui Song 	VM_WARN_ON(!swp_tb_is_folio(swp_tb));
728578e0c0SKairui Song 	return (void *)swp_tb;
738578e0c0SKairui Song }
748578e0c0SKairui Song 
swp_tb_to_shadow(unsigned long swp_tb)758578e0c0SKairui Song static inline void *swp_tb_to_shadow(unsigned long swp_tb)
768578e0c0SKairui Song {
778578e0c0SKairui Song 	VM_WARN_ON(!swp_tb_is_shadow(swp_tb));
788578e0c0SKairui Song 	return (void *)swp_tb;
798578e0c0SKairui Song }
808578e0c0SKairui Song 
818578e0c0SKairui Song /*
828578e0c0SKairui Song  * Helpers for accessing or modifying the swap table of a cluster,
838578e0c0SKairui Song  * the swap cluster must be locked.
848578e0c0SKairui Song  */
__swap_table_set(struct swap_cluster_info * ci,unsigned int off,unsigned long swp_tb)858578e0c0SKairui Song static inline void __swap_table_set(struct swap_cluster_info *ci,
868578e0c0SKairui Song 				    unsigned int off, unsigned long swp_tb)
878578e0c0SKairui Song {
8807adc4cfSKairui Song 	atomic_long_t *table = rcu_dereference_protected(ci->table, true);
8907adc4cfSKairui Song 
9007adc4cfSKairui Song 	lockdep_assert_held(&ci->lock);
918578e0c0SKairui Song 	VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER);
9207adc4cfSKairui Song 	atomic_long_set(&table[off], swp_tb);
938578e0c0SKairui Song }
948578e0c0SKairui Song 
__swap_table_xchg(struct swap_cluster_info * ci,unsigned int off,unsigned long swp_tb)958578e0c0SKairui Song static inline unsigned long __swap_table_xchg(struct swap_cluster_info *ci,
968578e0c0SKairui Song 					      unsigned int off, unsigned long swp_tb)
978578e0c0SKairui Song {
9807adc4cfSKairui Song 	atomic_long_t *table = rcu_dereference_protected(ci->table, true);
9907adc4cfSKairui Song 
10007adc4cfSKairui Song 	lockdep_assert_held(&ci->lock);
1018578e0c0SKairui Song 	VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER);
1028578e0c0SKairui Song 	/* Ordering is guaranteed by cluster lock, relax */
10307adc4cfSKairui Song 	return atomic_long_xchg_relaxed(&table[off], swp_tb);
1048578e0c0SKairui Song }
1058578e0c0SKairui Song 
__swap_table_get(struct swap_cluster_info * ci,unsigned int off)1068578e0c0SKairui Song static inline unsigned long __swap_table_get(struct swap_cluster_info *ci,
1078578e0c0SKairui Song 					     unsigned int off)
1088578e0c0SKairui Song {
10907adc4cfSKairui Song 	atomic_long_t *table;
11007adc4cfSKairui Song 
1118578e0c0SKairui Song 	VM_WARN_ON_ONCE(off >= SWAPFILE_CLUSTER);
11207adc4cfSKairui Song 	table = rcu_dereference_check(ci->table, lockdep_is_held(&ci->lock));
11307adc4cfSKairui Song 
11407adc4cfSKairui Song 	return atomic_long_read(&table[off]);
11507adc4cfSKairui Song }
11607adc4cfSKairui Song 
swap_table_get(struct swap_cluster_info * ci,unsigned int off)11707adc4cfSKairui Song static inline unsigned long swap_table_get(struct swap_cluster_info *ci,
11807adc4cfSKairui Song 					unsigned int off)
11907adc4cfSKairui Song {
12007adc4cfSKairui Song 	atomic_long_t *table;
12107adc4cfSKairui Song 	unsigned long swp_tb;
12207adc4cfSKairui Song 
12307adc4cfSKairui Song 	rcu_read_lock();
12407adc4cfSKairui Song 	table = rcu_dereference(ci->table);
12507adc4cfSKairui Song 	swp_tb = table ? atomic_long_read(&table[off]) : null_to_swp_tb();
12607adc4cfSKairui Song 	rcu_read_unlock();
12707adc4cfSKairui Song 
12807adc4cfSKairui Song 	return swp_tb;
1298578e0c0SKairui Song }
1308578e0c0SKairui Song #endif
131