The patch titled Subject: swap: divide-by-zero when zero length swap file on ssd has been added to the -mm tree. Its filename is swap-divide-by-zero-when-zero-length-swap-file-on-ssd.patch This patch should soon appear at http://ozlabs.org/~akpm/mmots/broken-out/swap-divide-by-zero-when-zero-length-swap-file-on-ssd.patch and later at http://ozlabs.org/~akpm/mmotm/broken-out/swap-divide-by-zero-when-zero-length-swap-file-on-ssd.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: "Tom Abraham" <tabraham@xxxxxxxx> Subject: swap: divide-by-zero when zero length swap file on ssd Calling swapon() on a zero length swap file on SSD can lead to a divide-by-zero. Although creating such files isn't possible with mkswap and they woud be considered invalid, it would be better for the swapon code to be more robust and handle this condition gracefully (return -EINVAL). Especially since the fix is small and straight-forward. To help with wear leveling on SSD, the swapon syscall calculates a random position in the swap file using modulo p->highest_bit, which is set to maxpages - 1 in read_swap_header. If the swap file is zero length, read_swap_header sets maxpages=1 and last_page=0, resulting in p->highest_bit=0 and we divide-by-zero when we modulo p->highest_bit in swapon syscall. This can be prevented by having read_swap_header return zero if last_page is zero. Link: http://lkml.kernel.org/r/5AC747C1020000A7001FA82C@xxxxxxxxxxxxxxxxxxxxxxx Reviewed-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> Cc: Randy Dunlap <rdunlap@xxxxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- mm/swapfile.c | 4 ++++ 1 file changed, 4 insertions(+) diff -puN mm/swapfile.c~swap-divide-by-zero-when-zero-length-swap-file-on-ssd mm/swapfile.c --- a/mm/swapfile.c~swap-divide-by-zero-when-zero-length-swap-file-on-ssd +++ a/mm/swapfile.c @@ -2961,6 +2961,10 @@ static unsigned long read_swap_header(st maxpages = swp_offset(pte_to_swp_entry( swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; last_page = swap_header->info.last_page; + if (!last_page) { + pr_warn("Empty swap-file\n"); + return 0; + } if (last_page > maxpages) { pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", maxpages << (PAGE_SHIFT - 10), _ Patches currently in -mm which might be from tabraham@xxxxxxxx are swap-divide-by-zero-when-zero-length-swap-file-on-ssd.patch -- To unsubscribe from this list: send the line "unsubscribe mm-commits" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html