From patchwork Sat Feb 29 17:08:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11413959 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 840BD924 for ; Sat, 29 Feb 2020 17:08:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3586A2468A for ; Sat, 29 Feb 2020 17:08:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="b2gE7a1j" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3586A2468A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 44F176B0005; Sat, 29 Feb 2020 12:08:47 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3FFAD6B0006; Sat, 29 Feb 2020 12:08:47 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 315776B0007; Sat, 29 Feb 2020 12:08:47 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0122.hostedemail.com [216.40.44.122]) by kanga.kvack.org (Postfix) with ESMTP id 191256B0005 for ; Sat, 29 Feb 2020 12:08:47 -0500 (EST) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id BA5714FFA for ; Sat, 29 Feb 2020 17:08:46 +0000 (UTC) X-FDA: 76543799052.02.crow42_1260e380bc31c X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,darrick.wong@oracle.com,,RULES_HIT:30012:30054:30056:30064:30070,0,RBL:156.151.31.86:@oracle.com:.lbl8.mailshell.net-64.10.201.10 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: crow42_1260e380bc31c X-Filterd-Recvd-Size: 6716 Received: from userp2130.oracle.com (userp2130.oracle.com [156.151.31.86]) by imf08.hostedemail.com (Postfix) with ESMTP for ; Sat, 29 Feb 2020 17:08:46 +0000 (UTC) Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 01TH3XjC135390; Sat, 29 Feb 2020 17:08:43 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : mime-version : content-type; s=corp-2020-01-29; bh=Aziub7tqyinjoq6gQPjP+xzoq0HBrul3VA7U0lGzdxA=; b=b2gE7a1ju958Q/zu/uaHVrnhznRlU2qCYwMa8fm+5EmYsrrQC+K5lnE8rO2FwirYHMRC S118iU/lAulJxEdWQ+NsUYw6xWF5MtfrXJPA0r7y4+mnDLSAfsg/zTnD64dQcu+HVZxc MDHF93YpH8paDPcGtd6o6jrPdag5sSU23pNZy3EN+1Lqu5XXSqtDoEeRpg4LpoKaDbJZ /Lwe8RgrIMEEOV+BKHVDfccsCUDbAJSzm1h2jwag+hat+emHP9oENhMulbfdVXBZxQZt H2lceYI2f0773+8DnV7Ciqdhdjl1Ari2dz6Hns/8UJIj6CjXMxspJxNjJduxDQzhLAvI vg== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2130.oracle.com with ESMTP id 2yffcu1hmr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 29 Feb 2020 17:08:43 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 01TH2RkW101511; Sat, 29 Feb 2020 17:08:42 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserp3020.oracle.com with ESMTP id 2yffs6y8gn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 29 Feb 2020 17:08:42 +0000 Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 01TH8R4u030776; Sat, 29 Feb 2020 17:08:27 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sat, 29 Feb 2020 09:08:27 -0800 Date: Sat, 29 Feb 2020 09:08:25 -0800 From: "Darrick J. Wong" To: linux-pm@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: domenico.andreoli@linux.com, mkleinsoft@gmail.com, hch@lst.de, akpm@linux-foundation.org, rjw@rjwysocki.net, len.brown@intel.com, pavel@ucw.cz Subject: [PATCH] hibernate: unlock swap bdev for writing when uswsusp is active Message-ID: <20200229170825.GX8045@magnolia> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9546 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 spamscore=0 bulkscore=0 mlxscore=0 adultscore=0 suspectscore=2 phishscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2001150001 definitions=main-2002290131 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9546 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxscore=0 bulkscore=0 adultscore=0 suspectscore=2 spamscore=0 malwarescore=0 impostorscore=0 priorityscore=1501 mlxlogscore=999 lowpriorityscore=0 clxscore=1011 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2001150001 definitions=main-2002290131 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: From: Darrick J. Wong It turns out that there /is/ one use case for programs being able to write to swap devices, and that is the userspace hibernation code. The uswsusp ioctls allow userspace to lease parts of swap devices, so turn S_SWAPFILE off when invoking suspend. Fixes: 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices") Reported-by: Domenico Andreoli Reported-by: Marian Klein Tested-by: Marian Klein Signed-off-by: Darrick J. Wong Reported-by: Domenico Andreoli Reported-by: Marian Klein Signed-off-by: Darrick J. Wong --- include/linux/swap.h | 1 + kernel/power/user.c | 11 ++++++++++- mm/swapfile.c | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 1e99f7ac1d7e..add93e205850 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -458,6 +458,7 @@ 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 **); +extern void swap_relockall(void); 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/user.c b/kernel/power/user.c index 77438954cc2b..b11f7037ce5e 100644 --- a/kernel/power/user.c +++ b/kernel/power/user.c @@ -271,6 +271,8 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd, break; } error = hibernation_restore(data->platform_support); + if (!error) + swap_relockall(); break; case SNAPSHOT_FREE: @@ -372,10 +374,17 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd, */ swdev = new_decode_dev(swap_area.dev); if (swdev) { + struct block_device *bd; + offset = swap_area.offset; - data->swap = swap_type_of(swdev, offset, NULL); + data->swap = swap_type_of(swdev, offset, &bd); if (data->swap < 0) error = -ENODEV; + + inode_lock(bd->bd_inode); + bd->bd_inode->i_flags &= ~S_SWAPFILE; + inode_unlock(bd->bd_inode); + bdput(bd); } else { data->swap = -1; error = -EINVAL; diff --git a/mm/swapfile.c b/mm/swapfile.c index b2a2e45c9a36..439bfb7263d3 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1799,6 +1799,32 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) return -ENODEV; } +/* Re-lock swap devices after resuming from userspace suspend. */ +void swap_relockall(void) +{ + int type; + + spin_lock(&swap_lock); + for (type = 0; type < nr_swapfiles; type++) { + struct swap_info_struct *sis = swap_info[type]; + struct block_device *bdev = bdgrab(sis->bdev); + + /* + * uswsusp only knows how to suspend to block devices, so we + * can skip swap files. + */ + if (!(sis->flags & SWP_WRITEOK) || + !(sis->flags & SWP_BLKDEV)) + continue; + + inode_lock(bdev->bd_inode); + bdev->bd_inode->i_flags |= S_SWAPFILE; + inode_unlock(bdev->bd_inode); + bdput(bdev); + } + spin_unlock(&swap_lock); +} + /* * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev * corresponding to given index in swap_info (swap type).