Skip to content

Commit

Permalink
swap: fix swapfile read/write offset
Browse files Browse the repository at this point in the history
commit caf6912 upstream.

We're not factoring in the start of the file for where to write and
read the swapfile, which leads to very unfortunate side effects of
writing where we should not be...

Fixes: dd6bd0d ("swap: use bdev_read_page() / bdev_write_page()")
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Cc: Anthony Iliopoulos <ailiop@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
axboe authored and gregkh committed Mar 7, 2021
1 parent bebf5e8 commit 60fdcea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/linux/swap.h
Expand Up @@ -473,6 +473,7 @@ struct backing_dev_info;
extern int init_swap_address_space(unsigned int type, unsigned long nr_pages);
extern void exit_swap_address_space(unsigned int type);
extern struct swap_info_struct *get_swap_device(swp_entry_t entry);
sector_t swap_page_sector(struct page *page);

static inline void put_swap_device(struct swap_info_struct *si)
{
Expand Down
5 changes: 0 additions & 5 deletions mm/page_io.c
Expand Up @@ -260,11 +260,6 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
return ret;
}

static sector_t swap_page_sector(struct page *page)
{
return (sector_t)__page_file_index(page) << (PAGE_SHIFT - 9);
}

static inline void count_swpout_vm_event(struct page *page)
{
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Expand Down
13 changes: 13 additions & 0 deletions mm/swapfile.c
Expand Up @@ -221,6 +221,19 @@ offset_to_swap_extent(struct swap_info_struct *sis, unsigned long offset)
BUG();
}

sector_t swap_page_sector(struct page *page)
{
struct swap_info_struct *sis = page_swap_info(page);
struct swap_extent *se;
sector_t sector;
pgoff_t offset;

offset = __page_file_index(page);
se = offset_to_swap_extent(sis, offset);
sector = se->start_block + (offset - se->start_page);
return sector << (PAGE_SHIFT - 9);
}

/*
* swap allocation tell device that a cluster of swap can now be discarded,
* to allow the swap device to optimize its wear-levelling.
Expand Down

0 comments on commit 60fdcea

Please sign in to comment.