Commit 3bd0f0c763e497c8674b28e3df2732f48683dabd

Authored by Suresh Jayaraman
Committed by Jens Axboe
1 parent a112a71d45

swapfile: avoid NULL pointer dereference in swapon when s_bdev is NULL

While testing Swap over NFS patchset, I noticed an oops that was triggered
during swapon. Investigating further, the NULL pointer deference is due to the
SSD device check/optimization in the swapon code that assumes s_bdev could never
be NULL.

inode->i_sb->s_bdev could be NULL in a few cases. For e.g. one such case is
loopback NFS mount, there could be others as well. Fix this by ensuring s_bdev
is not NULL before we try to deference s_bdev.

Signed-off-by: Suresh Jayaraman <sjayaraman@suse.de>
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>

Showing 1 changed file with 7 additions and 5 deletions Side-by-side Diff

... ... @@ -1974,12 +1974,14 @@
1974 1974 goto bad_swap;
1975 1975 }
1976 1976  
1977   - if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
1978   - p->flags |= SWP_SOLIDSTATE;
1979   - p->cluster_next = 1 + (random32() % p->highest_bit);
  1977 + if (p->bdev) {
  1978 + if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
  1979 + p->flags |= SWP_SOLIDSTATE;
  1980 + p->cluster_next = 1 + (random32() % p->highest_bit);
  1981 + }
  1982 + if (discard_swap(p) == 0)
  1983 + p->flags |= SWP_DISCARDABLE;
1980 1984 }
1981   - if (discard_swap(p) == 0)
1982   - p->flags |= SWP_DISCARDABLE;
1983 1985  
1984 1986 mutex_lock(&swapon_mutex);
1985 1987 spin_lock(&swap_lock);