Skip to content

Commit 18ab4d4

Browse files
ddstreettorvalds
authored andcommitted
swap: change swap_list_head to plist, add swap_avail_head
Originally get_swap_page() started iterating through the singly-linked list of swap_info_structs using swap_list.next or highest_priority_index, which both were intended to point to the highest priority active swap target that was not full. The first patch in this series changed the singly-linked list to a doubly-linked list, and removed the logic to start at the highest priority non-full entry; it starts scanning at the highest priority entry each time, even if the entry is full. Replace the manually ordered swap_list_head with a plist, swap_active_head. Add a new plist, swap_avail_head. The original swap_active_head plist contains all active swap_info_structs, as before, while the new swap_avail_head plist contains only swap_info_structs that are active and available, i.e. not full. Add a new spinlock, swap_avail_lock, to protect the swap_avail_head list. Mel Gorman suggested using plists since they internally handle ordering the list entries based on priority, which is exactly what swap was doing manually. All the ordering code is now removed, and swap_info_struct entries and simply added to their corresponding plist and automatically ordered correctly. Using a new plist for available swap_info_structs simplifies and optimizes get_swap_page(), which no longer has to iterate over full swap_info_structs. Using a new spinlock for swap_avail_head plist allows each swap_info_struct to add or remove themselves from the plist when they become full or not-full; previously they could not do so because the swap_info_struct->lock is held when they change from full<->not-full, and the swap_lock protecting the main swap_active_head must be ordered before any swap_info_struct->lock. Signed-off-by: Dan Streetman <ddstreet@ieee.org> Acked-by: Mel Gorman <mgorman@suse.de> Cc: Shaohua Li <shli@fusionio.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Hugh Dickins <hughd@google.com> Cc: Dan Streetman <ddstreet@ieee.org> Cc: Michal Hocko <mhocko@suse.cz> Cc: Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> Cc: Weijie Yang <weijieut@gmail.com> Cc: Rik van Riel <riel@redhat.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Bob Liu <bob.liu@oracle.com> Cc: Paul Gortmaker <paul.gortmaker@windriver.com> Cc: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a75f232 commit 18ab4d4

4 files changed

Lines changed: 97 additions & 59 deletions

File tree

‎include/linux/swap.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ struct percpu_cluster {
214214
struct swap_info_struct {
215215
unsigned long flags; /* SWP_USED etc: see above */
216216
signed short prio; /* swap priority of this type */
217-
struct list_head list; /* entry in swap list */
217+
struct plist_node list; /* entry in swap_active_head */
218+
struct plist_node avail_list; /* entry in swap_avail_head */
218219
signed char type; /* strange name for an index */
219220
unsigned int max; /* extent of the swap_map */
220221
unsigned char *swap_map; /* vmalloc'ed array of usage counts */

‎include/linux/swapfile.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* want to expose them to the dozens of source files that include swap.h
77
*/
88
extern spinlock_t swap_lock;
9-
extern struct list_head swap_list_head;
9+
extern struct plist_head swap_active_head;
1010
extern struct swap_info_struct *swap_info[];
1111
extern int try_to_unuse(unsigned int, bool, unsigned long);
1212

‎mm/frontswap.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ static unsigned long __frontswap_curr_pages(void)
331331
struct swap_info_struct *si = NULL;
332332

333333
assert_spin_locked(&swap_lock);
334-
list_for_each_entry(si, &swap_list_head, list)
334+
plist_for_each_entry(si, &swap_active_head, list)
335335
totalpages += atomic_read(&si->frontswap_pages);
336336
return totalpages;
337337
}
@@ -346,7 +346,7 @@ static int __frontswap_unuse_pages(unsigned long total, unsigned long *unused,
346346
unsigned long pages = 0, pages_to_unuse = 0;
347347

348348
assert_spin_locked(&swap_lock);
349-
list_for_each_entry(si, &swap_list_head, list) {
349+
plist_for_each_entry(si, &swap_active_head, list) {
350350
si_frontswap_pages = atomic_read(&si->frontswap_pages);
351351
if (total_pages_to_unuse < si_frontswap_pages) {
352352
pages = pages_to_unuse = total_pages_to_unuse;
@@ -408,7 +408,7 @@ void frontswap_shrink(unsigned long target_pages)
408408
/*
409409
* we don't want to hold swap_lock while doing a very
410410
* lengthy try_to_unuse, but swap_list may change
411-
* so restart scan from swap_list_head each time
411+
* so restart scan from swap_active_head each time
412412
*/
413413
spin_lock(&swap_lock);
414414
ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type);

‎mm/swapfile.c‎

Lines changed: 91 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,22 @@ static const char Unused_offset[] = "Unused swap offset entry ";
6161
* all active swap_info_structs
6262
* protected with swap_lock, and ordered by priority.
6363
*/
64-
LIST_HEAD(swap_list_head);
64+
PLIST_HEAD(swap_active_head);
65+
66+
/*
67+
* all available (active, not full) swap_info_structs
68+
* protected with swap_avail_lock, ordered by priority.
69+
* This is used by get_swap_page() instead of swap_active_head
70+
* because swap_active_head includes all swap_info_structs,
71+
* but get_swap_page() doesn't need to look at full ones.
72+
* This uses its own lock instead of swap_lock because when a
73+
* swap_info_struct changes between not-full/full, it needs to
74+
* add/remove itself to/from this list, but the swap_info_struct->lock
75+
* is held and the locking order requires swap_lock to be taken
76+
* before any swap_info_struct->lock.
77+
*/
78+
static PLIST_HEAD(swap_avail_head);
79+
static DEFINE_SPINLOCK(swap_avail_lock);
6580

6681
struct swap_info_struct *swap_info[MAX_SWAPFILES];
6782

@@ -594,6 +609,9 @@ static unsigned long scan_swap_map(struct swap_info_struct *si,
594609
if (si->inuse_pages == si->pages) {
595610
si->lowest_bit = si->max;
596611
si->highest_bit = 0;
612+
spin_lock(&swap_avail_lock);
613+
plist_del(&si->avail_list, &swap_avail_head);
614+
spin_unlock(&swap_avail_lock);
597615
}
598616
si->swap_map[offset] = usage;
599617
inc_cluster_info_page(si, si->cluster_info, offset);
@@ -645,57 +663,63 @@ swp_entry_t get_swap_page(void)
645663
{
646664
struct swap_info_struct *si, *next;
647665
pgoff_t offset;
648-
struct list_head *tmp;
649666

650-
spin_lock(&swap_lock);
651667
if (atomic_long_read(&nr_swap_pages) <= 0)
652668
goto noswap;
653669
atomic_long_dec(&nr_swap_pages);
654670

655-
list_for_each(tmp, &swap_list_head) {
656-
si = list_entry(tmp, typeof(*si), list);
671+
spin_lock(&swap_avail_lock);
672+
673+
start_over:
674+
plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
675+
/* requeue si to after same-priority siblings */
676+
plist_requeue(&si->avail_list, &swap_avail_head);
677+
spin_unlock(&swap_avail_lock);
657678
spin_lock(&si->lock);
658679
if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) {
680+
spin_lock(&swap_avail_lock);
681+
if (plist_node_empty(&si->avail_list)) {
682+
spin_unlock(&si->lock);
683+
goto nextsi;
684+
}
685+
WARN(!si->highest_bit,
686+
"swap_info %d in list but !highest_bit\n",
687+
si->type);
688+
WARN(!(si->flags & SWP_WRITEOK),
689+
"swap_info %d in list but !SWP_WRITEOK\n",
690+
si->type);
691+
plist_del(&si->avail_list, &swap_avail_head);
659692
spin_unlock(&si->lock);
660-
continue;
693+
goto nextsi;
661694
}
662695

663-
/*
664-
* rotate the current swap_info that we're going to use
665-
* to after any other swap_info that have the same prio,
666-
* so that all equal-priority swap_info get used equally
667-
*/
668-
next = si;
669-
list_for_each_entry_continue(next, &swap_list_head, list) {
670-
if (si->prio != next->prio)
671-
break;
672-
list_rotate_left(&si->list);
673-
next = si;
674-
}
675-
676-
spin_unlock(&swap_lock);
677696
/* This is called for allocating swap entry for cache */
678697
offset = scan_swap_map(si, SWAP_HAS_CACHE);
679698
spin_unlock(&si->lock);
680699
if (offset)
681700
return swp_entry(si->type, offset);
682-
spin_lock(&swap_lock);
701+
pr_debug("scan_swap_map of si %d failed to find offset\n",
702+
si->type);
703+
spin_lock(&swap_avail_lock);
704+
nextsi:
683705
/*
684706
* if we got here, it's likely that si was almost full before,
685707
* and since scan_swap_map() can drop the si->lock, multiple
686708
* callers probably all tried to get a page from the same si
687-
* and it filled up before we could get one. So we need to
688-
* try again. Since we dropped the swap_lock, there may now
689-
* be non-full higher priority swap_infos, and this si may have
690-
* even been removed from the list (although very unlikely).
691-
* Let's start over.
709+
* and it filled up before we could get one; or, the si filled
710+
* up between us dropping swap_avail_lock and taking si->lock.
711+
* Since we dropped the swap_avail_lock, the swap_avail_head
712+
* list may have been modified; so if next is still in the
713+
* swap_avail_head list then try it, otherwise start over.
692714
*/
693-
tmp = &swap_list_head;
715+
if (plist_node_empty(&next->avail_list))
716+
goto start_over;
694717
}
695718

719+
spin_unlock(&swap_avail_lock);
720+
696721
atomic_long_inc(&nr_swap_pages);
697722
noswap:
698-
spin_unlock(&swap_lock);
699723
return (swp_entry_t) {0};
700724
}
701725

@@ -798,8 +822,18 @@ static unsigned char swap_entry_free(struct swap_info_struct *p,
798822
dec_cluster_info_page(p, p->cluster_info, offset);
799823
if (offset < p->lowest_bit)
800824
p->lowest_bit = offset;
801-
if (offset > p->highest_bit)
825+
if (offset > p->highest_bit) {
826+
bool was_full = !p->highest_bit;
802827
p->highest_bit = offset;
828+
if (was_full && (p->flags & SWP_WRITEOK)) {
829+
spin_lock(&swap_avail_lock);
830+
WARN_ON(!plist_node_empty(&p->avail_list));
831+
if (plist_node_empty(&p->avail_list))
832+
plist_add(&p->avail_list,
833+
&swap_avail_head);
834+
spin_unlock(&swap_avail_lock);
835+
}
836+
}
803837
atomic_long_inc(&nr_swap_pages);
804838
p->inuse_pages--;
805839
frontswap_invalidate_page(p->type, offset);
@@ -1734,40 +1768,37 @@ static void _enable_swap_info(struct swap_info_struct *p, int prio,
17341768
unsigned char *swap_map,
17351769
struct swap_cluster_info *cluster_info)
17361770
{
1737-
struct swap_info_struct *si;
1738-
17391771
if (prio >= 0)
17401772
p->prio = prio;
17411773
else
17421774
p->prio = --least_priority;
1775+
/*
1776+
* the plist prio is negated because plist ordering is
1777+
* low-to-high, while swap ordering is high-to-low
1778+
*/
1779+
p->list.prio = -p->prio;
1780+
p->avail_list.prio = -p->prio;
17431781
p->swap_map = swap_map;
17441782
p->cluster_info = cluster_info;
17451783
p->flags |= SWP_WRITEOK;
17461784
atomic_long_add(p->pages, &nr_swap_pages);
17471785
total_swap_pages += p->pages;
17481786

17491787
assert_spin_locked(&swap_lock);
1750-
BUG_ON(!list_empty(&p->list));
1751-
/*
1752-
* insert into swap list; the list is in priority order,
1753-
* so that get_swap_page() can get a page from the highest
1754-
* priority swap_info_struct with available page(s), and
1755-
* swapoff can adjust the auto-assigned (i.e. negative) prio
1756-
* values for any lower-priority swap_info_structs when
1757-
* removing a negative-prio swap_info_struct
1758-
*/
1759-
list_for_each_entry(si, &swap_list_head, list) {
1760-
if (p->prio >= si->prio) {
1761-
list_add_tail(&p->list, &si->list);
1762-
return;
1763-
}
1764-
}
17651788
/*
1766-
* this covers two cases:
1767-
* 1) p->prio is less than all existing prio
1768-
* 2) the swap list is empty
1789+
* both lists are plists, and thus priority ordered.
1790+
* swap_active_head needs to be priority ordered for swapoff(),
1791+
* which on removal of any swap_info_struct with an auto-assigned
1792+
* (i.e. negative) priority increments the auto-assigned priority
1793+
* of any lower-priority swap_info_structs.
1794+
* swap_avail_head needs to be priority ordered for get_swap_page(),
1795+
* which allocates swap pages from the highest available priority
1796+
* swap_info_struct.
17691797
*/
1770-
list_add_tail(&p->list, &swap_list_head);
1798+
plist_add(&p->list, &swap_active_head);
1799+
spin_lock(&swap_avail_lock);
1800+
plist_add(&p->avail_list, &swap_avail_head);
1801+
spin_unlock(&swap_avail_lock);
17711802
}
17721803

17731804
static void enable_swap_info(struct swap_info_struct *p, int prio,
@@ -1821,7 +1852,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
18211852

18221853
mapping = victim->f_mapping;
18231854
spin_lock(&swap_lock);
1824-
list_for_each_entry(p, &swap_list_head, list) {
1855+
plist_for_each_entry(p, &swap_active_head, list) {
18251856
if (p->flags & SWP_WRITEOK) {
18261857
if (p->swap_file->f_mapping == mapping) {
18271858
found = 1;
@@ -1841,16 +1872,21 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
18411872
spin_unlock(&swap_lock);
18421873
goto out_dput;
18431874
}
1875+
spin_lock(&swap_avail_lock);
1876+
plist_del(&p->avail_list, &swap_avail_head);
1877+
spin_unlock(&swap_avail_lock);
18441878
spin_lock(&p->lock);
18451879
if (p->prio < 0) {
18461880
struct swap_info_struct *si = p;
18471881

1848-
list_for_each_entry_continue(si, &swap_list_head, list) {
1882+
plist_for_each_entry_continue(si, &swap_active_head, list) {
18491883
si->prio++;
1884+
si->list.prio--;
1885+
si->avail_list.prio--;
18501886
}
18511887
least_priority++;
18521888
}
1853-
list_del_init(&p->list);
1889+
plist_del(&p->list, &swap_active_head);
18541890
atomic_long_sub(p->pages, &nr_swap_pages);
18551891
total_swap_pages -= p->pages;
18561892
p->flags &= ~SWP_WRITEOK;
@@ -2115,7 +2151,8 @@ static struct swap_info_struct *alloc_swap_info(void)
21152151
*/
21162152
}
21172153
INIT_LIST_HEAD(&p->first_swap_extent.list);
2118-
INIT_LIST_HEAD(&p->list);
2154+
plist_node_init(&p->list, 0);
2155+
plist_node_init(&p->avail_list, 0);
21192156
p->flags = SWP_USED;
21202157
spin_unlock(&swap_lock);
21212158
spin_lock_init(&p->lock);

0 commit comments

Comments
 (0)