From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBCF8C433E2 for ; Thu, 17 Sep 2020 17:25:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6075C206D4 for ; Thu, 17 Sep 2020 17:25:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="aM3zXn8Y" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6075C206D4 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9D48F6B0087; Thu, 17 Sep 2020 13:25:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 985216B0088; Thu, 17 Sep 2020 13:25:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 84E938E0001; Thu, 17 Sep 2020 13:25:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0151.hostedemail.com [216.40.44.151]) by kanga.kvack.org (Postfix) with ESMTP id 6E9166B0087 for ; Thu, 17 Sep 2020 13:25:57 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 3B36B362A for ; Thu, 17 Sep 2020 17:25:57 +0000 (UTC) X-FDA: 77273231154.23.coil03_1e0e6f427124 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id 1972437604 for ; Thu, 17 Sep 2020 17:25:57 +0000 (UTC) X-HE-Tag: coil03_1e0e6f427124 X-Filterd-Recvd-Size: 7801 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf39.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Sep 2020 17:25:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=2ayaBUOXfktvOTjNfjilWVR81vP6mBqMDpKHyQ4yF8I=; b=aM3zXn8YOCPZ4UwU3qS/Z1ltMN /SlbSpP3wOE9TV9HUi/zczy+B1Wqpxs1Kv2dZfaTJNKTe7Xk3c7DhzbvdPRsBS/uWLzLA5El/Q/WJ t1JUVrNR6Z5AoWN90FYnoI1ymW3RzCjGcokmgEJkDYogCus8kJVuehHZoUR6MYONppDLwGUaCfTmB k0dFoCRUaXryzcNudIDBjkC3ivQHs7cO9vTx3o3zbR7CljH+aqrRuqd9vsoxNngBtMk6/J39LUv3v 5Tir5j2t9QyfqMPNfyA4S6DwMBFcWe2e11x/f59PWKyMZg2bnVDZaXVITFhc5QqM4F7Npwxf4uROM CZ+OyNTQ==; Received: from 089144214092.atnat0023.highway.a1.net ([89.144.214.92] helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kIxfF-0002I7-Nc; Thu, 17 Sep 2020 17:25:46 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Josef Bacik , Minchan Kim , Stefan Haberland , Jan Hoeppner , Joseph Qi , "Rafael J. Wysocki" , Pavel Machek , Len Brown , Andrew Morton , linux-kernel@vger.kernel.org, nbd@other.debian.org, linux-ide@vger.kernel.org, linux-s390@vger.kernel.org, linux-fsdevel@vger.kernel.org, ocfs2-devel@oss.oracle.com, linux-pm@vger.kernel.org, linux-mm@kvack.org, linux-block@vger.kernel.org Subject: [PATCH 12/14] mm: split swap_type_of Date: Thu, 17 Sep 2020 18:57:18 +0200 Message-Id: <20200917165720.3285256-13-hch@lst.de> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200917165720.3285256-1-hch@lst.de> References: <20200917165720.3285256-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: swap_type_of is used for two entirely different purposes: (1) check what swap type a given device/offset corresponds to (2) find the first available swap device that can be written to Mixing both in a single function creates an unreadable mess. Create two separate functions instead, and switch both to pass a dev_t instead of a struct block_device to further simplify the code. Signed-off-by: Christoph Hellwig --- include/linux/swap.h | 3 ++- kernel/power/swap.c | 17 ++++++++--------- kernel/power/user.c | 16 ++++------------ mm/swapfile.c | 39 +++++++++++++++++++++------------------ 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 661046994db4b9..4340a7b6e7a1dc 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -467,7 +467,8 @@ extern int swapcache_prepare(swp_entry_t); extern void swap_free(swp_entry_t); extern void swapcache_free_entries(swp_entry_t *entries, int n); extern int free_swap_and_cache(swp_entry_t); -extern int swap_type_of(dev_t, sector_t, struct block_device **); +int swap_type_of(dev_t device, sector_t offset); +int find_first_swap(dev_t *device); extern unsigned int count_swap_pages(int, int); extern sector_t map_swap_page(struct page *, struct block_device **); extern sector_t swapdev_block(int, pgoff_t); diff --git a/kernel/power/swap.c b/kernel/power/swap.c index 01e2858b5fe367..9d3ffbfe08dbf6 100644 --- a/kernel/power/swap.c +++ b/kernel/power/swap.c @@ -335,12 +335,17 @@ static int swsusp_swap_check(void) { int res; =20 - res =3D swap_type_of(swsusp_resume_device, swsusp_resume_block, - &hib_resume_bdev); + if (swsusp_resume_device) + res =3D swap_type_of(swsusp_resume_device, swsusp_resume_block); + else + res =3D find_first_swap(&swsusp_resume_device); if (res < 0) return res; - root_swap =3D res; + + hib_resume_bdev =3D bdget(swsusp_resume_device); + if (!hib_resume_bdev) + return -ENOMEM; res =3D blkdev_get(hib_resume_bdev, FMODE_WRITE, NULL); if (res) return res; @@ -349,12 +354,6 @@ static int swsusp_swap_check(void) if (res < 0) blkdev_put(hib_resume_bdev, FMODE_WRITE); =20 - /* - * Update the resume device to the one actually used, - * so the test_resume mode can use it in case it is - * invoked from hibernate() to test the snapshot. - */ - swsusp_resume_device =3D hib_resume_bdev->bd_dev; return res; } =20 diff --git a/kernel/power/user.c b/kernel/power/user.c index b5815685b944fe..6510a8f7687f42 100644 --- a/kernel/power/user.c +++ b/kernel/power/user.c @@ -69,8 +69,7 @@ static int snapshot_open(struct inode *inode, struct fi= le *filp) memset(&data->handle, 0, sizeof(struct snapshot_handle)); if ((filp->f_flags & O_ACCMODE) =3D=3D O_RDONLY) { /* Hibernating. The image device should be accessible. */ - data->swap =3D swsusp_resume_device ? - swap_type_of(swsusp_resume_device, 0, NULL) : -1; + data->swap =3D swap_type_of(swsusp_resume_device, 0); data->mode =3D O_RDONLY; data->free_bitmaps =3D false; error =3D __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_cal= ls); @@ -210,7 +209,6 @@ struct compat_resume_swap_area { static int snapshot_set_swap_area(struct snapshot_data *data, void __user *argp) { - struct block_device *bdev; sector_t offset; dev_t swdev; =20 @@ -237,16 +235,10 @@ static int snapshot_set_swap_area(struct snapshot_d= ata *data, * User space encodes device types as two-byte values, * so we need to recode them */ - if (!swdev) { - data->swap =3D -1; - return -EINVAL; - } - data->swap =3D swap_type_of(swdev, offset, &bdev); + data->swap =3D swap_type_of(swdev, offset); if (data->swap < 0) - return -ENODEV; - - data->dev =3D bdev->bd_dev; - bdput(bdev); + return swdev ? -ENODEV : -EINVAL; + data->dev =3D swdev; return 0; } =20 diff --git a/mm/swapfile.c b/mm/swapfile.c index 7438c4affc75fa..b90f8692074397 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1801,13 +1801,12 @@ int free_swap_and_cache(swp_entry_t entry) * * This is needed for the suspend to disk (aka swsusp). */ -int swap_type_of(dev_t device, sector_t offset, struct block_device **bd= ev_p) +int swap_type_of(dev_t device, sector_t offset) { - struct block_device *bdev =3D NULL; int type; =20 - if (device) - bdev =3D bdget(device); + if (!device) + return -1; =20 spin_lock(&swap_lock); for (type =3D 0; type < nr_swapfiles; type++) { @@ -1816,30 +1815,34 @@ int swap_type_of(dev_t device, sector_t offset, s= truct block_device **bdev_p) if (!(sis->flags & SWP_WRITEOK)) continue; =20 - if (!bdev) { - if (bdev_p) - *bdev_p =3D bdgrab(sis->bdev); - - spin_unlock(&swap_lock); - return type; - } - if (bdev =3D=3D sis->bdev) { + if (device =3D=3D sis->bdev->bd_dev) { struct swap_extent *se =3D first_se(sis); =20 if (se->start_block =3D=3D offset) { - if (bdev_p) - *bdev_p =3D bdgrab(sis->bdev); - spin_unlock(&swap_lock); - bdput(bdev); return type; } } } spin_unlock(&swap_lock); - if (bdev) - bdput(bdev); + return -ENODEV; +} =20 +int find_first_swap(dev_t *device) +{ + int type; + + spin_lock(&swap_lock); + for (type =3D 0; type < nr_swapfiles; type++) { + struct swap_info_struct *sis =3D swap_info[type]; + + if (!(sis->flags & SWP_WRITEOK)) + continue; + *device =3D sis->bdev->bd_dev; + spin_unlock(&swap_lock); + return type; + } + spin_unlock(&swap_lock); return -ENODEV; } =20 --=20 2.28.0