mm: fix the page_swap_info() BUG_ON check

Commit 62c230bc1790 ("mm: add support for a filesystem to activate
swap files and use direct_IO for writing swap pages") replaced the
swap_aops dirty hook from __set_page_dirty_no_writeback() with

For normal cases without these special SWP flags code path falls back to
__set_page_dirty_no_writeback() so the behaviour is expected to be the
same as before.

But swap_set_page_dirty() makes use of the page_swap_info() helper to
get the swap_info_struct to check for the flags like SWP_FILE,
SWP_BLKDEV etc as desired for those features.  This helper has
BUG_ON(!PageSwapCache(page)) which is racy and safe only for the
set_page_dirty_lock() path.

For the set_page_dirty() path which is often needed for cases to be
called from irq context, kswapd() can toggle the flag behind the back
while the call is getting executed when system is low on memory and
heavy swapping is ongoing.

This ends up with undesired kernel panic.

This patch just moves the check outside the helper to its users
appropriately to fix kernel panic for the described path.  Couple of
users of helpers already take care of SwapCache condition so I skipped

Signed-off-by: Santosh Shilimkar <>
Cc: Mel Gorman <>
Cc: Joe Perches <>
Cc: Peter Zijlstra <>
Cc: Rik van Riel <>
Cc: David S. Miller <>
Cc: Jens Axboe <>
Cc: Michal Hocko <>
Cc: Hugh Dickins <>
Cc: Al Viro <>
Cc: <>	[4.7.x]
Signed-off-by: Andrew Morton <>
Signed-off-by: Linus Torvalds <>
2 files changed