Skip to content

Commit 910321e

Browse files
Hugh Dickinstorvalds
authored andcommitted
swap: revert special hibernation allocation
Please revert 2.6.36-rc commit d2997b1 "hibernation: freeze swap at hibernation". It complicated matters by adding a second swap allocation path, just for hibernation; without in any way fixing the issue that it was intended to address - page reclaim after fixing the hibernation image might free swap from a page already imaged as swapcache, letting its swap be reallocated to store a different page of the image: resulting in data corruption if the imaged page were freed as clean then swapped back in. Pages freed to si->swap_map were still in danger of being reallocated by the alternative allocation path. I guess it inadvertently fixed slow SSD swap allocation for hibernation, as reported by Nigel Cunningham: by missing out the discards that occur on the usual swap allocation path; but that was unintentional, and needs a separate fix. Signed-off-by: Hugh Dickins <hughd@google.com> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: "Rafael J. Wysocki" <rjw@sisk.pl> Cc: Ondrej Zary <linux@rainbow-software.org> Cc: Andrea Gelmini <andrea.gelmini@gmail.com> Cc: Balbir Singh <balbir@in.ibm.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Nigel Cunningham <nigel@tuxonice.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ac8456d commit 910321e

5 files changed

Lines changed: 26 additions & 84 deletions

File tree

‎include/linux/swap.h‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ extern long nr_swap_pages;
315315
extern long total_swap_pages;
316316
extern void si_swapinfo(struct sysinfo *);
317317
extern swp_entry_t get_swap_page(void);
318+
extern swp_entry_t get_swap_page_of_type(int);
318319
extern int valid_swaphandles(swp_entry_t, unsigned long *);
319320
extern int add_swap_count_continuation(swp_entry_t, gfp_t);
320321
extern void swap_shmem_alloc(swp_entry_t);
@@ -331,13 +332,6 @@ extern int reuse_swap_page(struct page *);
331332
extern int try_to_free_swap(struct page *);
332333
struct backing_dev_info;
333334

334-
#ifdef CONFIG_HIBERNATION
335-
void hibernation_freeze_swap(void);
336-
void hibernation_thaw_swap(void);
337-
swp_entry_t get_swap_for_hibernation(int type);
338-
void swap_free_for_hibernation(swp_entry_t val);
339-
#endif
340-
341335
/* linux/mm/thrash.c */
342336
extern struct mm_struct *swap_token_mm;
343337
extern void grab_swap_token(struct mm_struct *);

‎kernel/power/hibernate.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ int hibernation_snapshot(int platform_mode)
338338
goto Close;
339339

340340
suspend_console();
341-
hibernation_freeze_swap();
342341
saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
343342
error = dpm_suspend_start(PMSG_FREEZE);
344343
if (error)

‎kernel/power/snapshot.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,6 @@ void swsusp_free(void)
10861086
buffer = NULL;
10871087
alloc_normal = 0;
10881088
alloc_highmem = 0;
1089-
hibernation_thaw_swap();
10901089
}
10911090

10921091
/* Helper functions used for the shrinking of memory. */

‎kernel/power/swap.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ sector_t alloc_swapdev_block(int swap)
136136
{
137137
unsigned long offset;
138138

139-
offset = swp_offset(get_swap_for_hibernation(swap));
139+
offset = swp_offset(get_swap_page_of_type(swap));
140140
if (offset) {
141141
if (swsusp_extents_insert(offset))
142-
swap_free_for_hibernation(swp_entry(swap, offset));
142+
swap_free(swp_entry(swap, offset));
143143
else
144144
return swapdev_block(swap, offset);
145145
}
@@ -163,7 +163,7 @@ void free_all_swap_pages(int swap)
163163
ext = container_of(node, struct swsusp_extent, node);
164164
rb_erase(node, &swsusp_extents);
165165
for (offset = ext->start; offset <= ext->end; offset++)
166-
swap_free_for_hibernation(swp_entry(swap, offset));
166+
swap_free(swp_entry(swap, offset));
167167

168168
kfree(ext);
169169
}

‎mm/swapfile.c‎

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ long nr_swap_pages;
4747
long total_swap_pages;
4848
static int least_priority;
4949

50-
static bool swap_for_hibernation;
51-
5250
static const char Bad_file[] = "Bad swap file entry ";
5351
static const char Unused_file[] = "Unused swap file entry ";
5452
static const char Bad_offset[] = "Bad swap offset entry ";
@@ -453,8 +451,6 @@ swp_entry_t get_swap_page(void)
453451
spin_lock(&swap_lock);
454452
if (nr_swap_pages <= 0)
455453
goto noswap;
456-
if (swap_for_hibernation)
457-
goto noswap;
458454
nr_swap_pages--;
459455

460456
for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
@@ -487,6 +483,28 @@ swp_entry_t get_swap_page(void)
487483
return (swp_entry_t) {0};
488484
}
489485

486+
/* The only caller of this function is now susupend routine */
487+
swp_entry_t get_swap_page_of_type(int type)
488+
{
489+
struct swap_info_struct *si;
490+
pgoff_t offset;
491+
492+
spin_lock(&swap_lock);
493+
si = swap_info[type];
494+
if (si && (si->flags & SWP_WRITEOK)) {
495+
nr_swap_pages--;
496+
/* This is called for allocating swap entry, not cache */
497+
offset = scan_swap_map(si, 1);
498+
if (offset) {
499+
spin_unlock(&swap_lock);
500+
return swp_entry(type, offset);
501+
}
502+
nr_swap_pages++;
503+
}
504+
spin_unlock(&swap_lock);
505+
return (swp_entry_t) {0};
506+
}
507+
490508
static struct swap_info_struct *swap_info_get(swp_entry_t entry)
491509
{
492510
struct swap_info_struct *p;
@@ -746,74 +764,6 @@ int mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep)
746764
#endif
747765

748766
#ifdef CONFIG_HIBERNATION
749-
750-
static pgoff_t hibernation_offset[MAX_SWAPFILES];
751-
/*
752-
* Once hibernation starts to use swap, we freeze swap_map[]. Otherwise,
753-
* saved swap_map[] image to the disk will be an incomplete because it's
754-
* changing without synchronization with hibernation snap shot.
755-
* At resume, we just make swap_for_hibernation=false. We can forget
756-
* used maps easily.
757-
*/
758-
void hibernation_freeze_swap(void)
759-
{
760-
int i;
761-
762-
spin_lock(&swap_lock);
763-
764-
printk(KERN_INFO "PM: Freeze Swap\n");
765-
swap_for_hibernation = true;
766-
for (i = 0; i < MAX_SWAPFILES; i++)
767-
hibernation_offset[i] = 1;
768-
spin_unlock(&swap_lock);
769-
}
770-
771-
void hibernation_thaw_swap(void)
772-
{
773-
spin_lock(&swap_lock);
774-
if (swap_for_hibernation) {
775-
printk(KERN_INFO "PM: Thaw Swap\n");
776-
swap_for_hibernation = false;
777-
}
778-
spin_unlock(&swap_lock);
779-
}
780-
781-
/*
782-
* Because updateing swap_map[] can make not-saved-status-change,
783-
* we use our own easy allocator.
784-
* Please see kernel/power/swap.c, Used swaps are recorded into
785-
* RB-tree.
786-
*/
787-
swp_entry_t get_swap_for_hibernation(int type)
788-
{
789-
pgoff_t off;
790-
swp_entry_t val = {0};
791-
struct swap_info_struct *si;
792-
793-
spin_lock(&swap_lock);
794-
795-
si = swap_info[type];
796-
if (!si || !(si->flags & SWP_WRITEOK))
797-
goto done;
798-
799-
for (off = hibernation_offset[type]; off < si->max; ++off) {
800-
if (!si->swap_map[off])
801-
break;
802-
}
803-
if (off < si->max) {
804-
val = swp_entry(type, off);
805-
hibernation_offset[type] = off + 1;
806-
}
807-
done:
808-
spin_unlock(&swap_lock);
809-
return val;
810-
}
811-
812-
void swap_free_for_hibernation(swp_entry_t ent)
813-
{
814-
/* Nothing to do */
815-
}
816-
817767
/*
818768
* Find the swap type that corresponds to given device (if any).
819769
*

0 commit comments

Comments
 (0)