Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3bd0f0c7 authored by Suresh Jayaraman's avatar Suresh Jayaraman Committed by Jens Axboe
Browse files

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: default avatarSuresh Jayaraman <sjayaraman@suse.de>
Signed-off-by: default avatarJens Axboe <jens.axboe@oracle.com>
parent a112a71d
Loading
Loading
Loading
Loading
+7 −5
Original line number Original line Diff line number Diff line
@@ -1974,12 +1974,14 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
		goto bad_swap;
		goto bad_swap;
	}
	}


	if (p->bdev) {
		if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
		if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
			p->flags |= SWP_SOLIDSTATE;
			p->flags |= SWP_SOLIDSTATE;
			p->cluster_next = 1 + (random32() % p->highest_bit);
			p->cluster_next = 1 + (random32() % p->highest_bit);
		}
		}
		if (discard_swap(p) == 0)
		if (discard_swap(p) == 0)
			p->flags |= SWP_DISCARDABLE;
			p->flags |= SWP_DISCARDABLE;
	}


	mutex_lock(&swapon_mutex);
	mutex_lock(&swapon_mutex);
	spin_lock(&swap_lock);
	spin_lock(&swap_lock);