From patchwork Tue Feb 4 09:59:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naohiro Aota X-Patchwork-Id: 11364333 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 7B13113A4 for ; Tue, 4 Feb 2020 10:01:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3A232217BA for ; Tue, 4 Feb 2020 10:01:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=wdc.com header.i=@wdc.com header.b="j0fsfjOM" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3A232217BA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 598666B0006; Tue, 4 Feb 2020 05:01:23 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5212B6B0007; Tue, 4 Feb 2020 05:01:23 -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 4112F6B0008; Tue, 4 Feb 2020 05:01:23 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0241.hostedemail.com [216.40.44.241]) by kanga.kvack.org (Postfix) with ESMTP id 237B36B0006 for ; Tue, 4 Feb 2020 05:01:23 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id D5327180AD802 for ; Tue, 4 Feb 2020 10:01:22 +0000 (UTC) X-FDA: 76452002004.17.turn66_461084e9ebc63 X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,prvs=2961137da=naohiro.aota@wdc.com,::linux-fsdevel@vger.kernel.org:linux-block@vger.kernel.org:akpm@linux-foundation.org:hch@infradead.org:darrick.wong@oracle.com:naohiro.aota@wdc.com,RULES_HIT:4423:30029:30045:30051:30054:30056:30070:30075,0,RBL:216.71.153.141:@wdc.com:.lbl8.mailshell.net-62.18.0.100 64.10.201.10,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:248,LUA_SUMMARY:none X-HE-Tag: turn66_461084e9ebc63 X-Filterd-Recvd-Size: 6818 Received: from esa3.hgst.iphmx.com (esa3.hgst.iphmx.com [216.71.153.141]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Tue, 4 Feb 2020 10:01:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1580810482; x=1612346482; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=ycNjUinQ2vTFmehHApNSB68O+VxNJ/WdRiNvk28URno=; b=j0fsfjOMfWwu80Xr9ChJDluLfJ2di7ktiMUqbcr1M+NDg13pJlbB+bJo kffy2ne/QHQE3g56MQU5wFN7diqBAAxkkVndiUoPXoZSHmRGwuh7jXfXz IZtdwemcSFZ4zgrq0o8vU41Go8RcSMW5u7owT85B63dK2V5MWECDHZJnF ZC80RvxlLi2X7YFJY1qgUH2KBuIEjVcrSyb9wgifPbBGd3Z39o38lQrf7 UrhdGu/4bDnVFtiWsbagdsURXZ0+LqFI8qcmJRosNbJB6S7dkvVLarIK/ 4ZSc9+LCj2MgQPxILY4uqGWix5UGOj/tfTDSCjM/odgaN1KUTeNtgn+ux Q==; IronPort-SDR: 66Hx/zkt5mKhdIpGbkyfUu+oxMk4WWuYNQG2hAWf5rjF0DSA3Y2njGFW+Qk47bbTjKL86aAC9m UoJ/dSXzdzmYkfbqVelN7Vsq2ado7UfT3sXmhF1xhMtbnHeyFPkx2Pg5BQoq58XuRlf4hYYGBZ LRKKwb+sXHpQDmm2IfP/ouKRQtuoWiJDCQbsHJRbvmOuPU/Q3Dnr2U40QRUWpjla5vXGWudC8d prAT1AfZeeSaULJvYOmdurasVIs1d17BjdNXtQC31ZYwUAnzPnkbVHkcnZY2ZCQELwvKedhQN3 mQs= X-IronPort-AV: E=Sophos;i="5.70,401,1574092800"; d="scan'208";a="133405076" Received: from uls-op-cesaip01.wdc.com (HELO uls-op-cesaep01.wdc.com) ([199.255.45.14]) by ob1.hgst.iphmx.com with ESMTP; 04 Feb 2020 18:01:20 +0800 IronPort-SDR: E7SPJGTbwBvu4h63Sdv+GFN8mLGY1y1P66HnH5/CVS30V7T6UK83DvQSk822CH7+zyhSdElD8H CrO3cSdz8o5pWnG11oqnF2iCk8MLCW+jDjBDVznPfSOlem6h9be5yM10vFRHEZrmDDDFjeDAI6 osP0yAVwVGVfj3p59bLZcWii2FHSv+pnU35CcPrBsq/TTFjRcEwCbCYYDlsRNgtIeD5mzvKziq Jb9QZ07zh6JtcciA/krkYoHHzuqUKTqsEX/Pn+yUMt+ps6Lorp2cIpAMwztIg9cIDRY6woNnjV S6c+Lp+tyWwXUCgGxMzeSYNx Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep01.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Feb 2020 01:54:22 -0800 IronPort-SDR: LEtxxlxr9gYJqYRD/PQ4/XS1ngt1jBOnY/DRDWX8M3ICedVSCnkU3g5I5As6/2Z8mmzUSoH8p/ uTYBhW7EHNnZvdws/30MMJ57HWoWDhkjbcjV0yI2TnGElgBQNte7fh8t21H8vfK2J7d/TVCmRu tyzGVhva2peocJNwnHMOeWTjt0KNadUtoX9wAMqnD+9p7v3ekYu43LckfbAFuBk6BeFMdyxm1p AbUTdpCEXu6Z7R1Pt7bLxD1NPgpWcxNdY6tCU+ezUIpBKbTk1eD2Y3/3CPozbU/8dDgTI7py6Y d+c= WDCIronportException: Internal Received: from naota.dhcp.fujisawa.hgst.com ([10.149.52.155]) by uls-op-cesaip01.wdc.com with ESMTP; 04 Feb 2020 02:01:19 -0800 From: Naohiro Aota To: linux-mm@kvack.org Cc: linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, Andrew Morton , Christoph Hellwig , "Darrick J . Wong" , Naohiro Aota Subject: [PATCH] mm, swap: unlock inode in error path of claim_swapfile Date: Tue, 4 Feb 2020 18:59:43 +0900 Message-Id: <20200204095943.727666-1-naohiro.aota@wdc.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 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: claim_swapfile() currently keeps the inode locked when it is successful, or the file is already swapfile (with -EBUSY). And, on the other error cases, it does not lock the inode. This inconsistency of the lock state and return value is quite confusing and actually causing a bad unlock balance as below in the "bad_swap" section of __do_sys_swapon(). This commit fixes this issue by unlocking the inode on the error path. It also reverts blocksize and releases bdev, so that the caller can safely forget about the inode. ===================================== WARNING: bad unlock balance detected! 5.5.0-rc7+ #176 Not tainted ------------------------------------- swapon/4294 is trying to release lock (&sb->s_type->i_mutex_key) at: [] __do_sys_swapon+0x94b/0x3550 but there are no more locks to release! other info that might help us debug this: no locks held by swapon/4294. stack backtrace: CPU: 5 PID: 4294 Comm: swapon Not tainted 5.5.0-rc7-BTRFS-ZNS+ #176 Hardware name: ASUS All Series/H87-PRO, BIOS 2102 07/29/2014 Call Trace: dump_stack+0xa1/0xea ? __do_sys_swapon+0x94b/0x3550 print_unlock_imbalance_bug.cold+0x114/0x123 ? __do_sys_swapon+0x94b/0x3550 lock_release+0x562/0xed0 ? kvfree+0x31/0x40 ? lock_downgrade+0x770/0x770 ? kvfree+0x31/0x40 ? rcu_read_lock_sched_held+0xa1/0xd0 ? rcu_read_lock_bh_held+0xb0/0xb0 up_write+0x2d/0x490 ? kfree+0x293/0x2f0 __do_sys_swapon+0x94b/0x3550 ? putname+0xb0/0xf0 ? kmem_cache_free+0x2e7/0x370 ? do_sys_open+0x184/0x3e0 ? generic_max_swapfile_size+0x40/0x40 ? do_syscall_64+0x27/0x4b0 ? entry_SYSCALL_64_after_hwframe+0x49/0xbe ? lockdep_hardirqs_on+0x38c/0x590 __x64_sys_swapon+0x54/0x80 do_syscall_64+0xa4/0x4b0 entry_SYSCALL_64_after_hwframe+0x49/0xbe RIP: 0033:0x7f15da0a0dc7 Fixes: 1638045c3677 ("mm: set S_SWAPFILE on blockdev swap devices") Signed-off-by: Naohiro Aota --- mm/swapfile.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index bb3261d45b6a..dd5d7fa42282 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2886,24 +2886,37 @@ static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) p->old_block_size = block_size(p->bdev); error = set_blocksize(p->bdev, PAGE_SIZE); if (error < 0) - return error; + goto err; /* * Zoned block devices contain zones that have a sequential * write only restriction. Hence zoned block devices are not * suitable for swapping. Disallow them here. */ - if (blk_queue_is_zoned(p->bdev->bd_queue)) - return -EINVAL; + if (blk_queue_is_zoned(p->bdev->bd_queue)) { + error = -EINVAL; + goto err; + } p->flags |= SWP_BLKDEV; } else if (S_ISREG(inode->i_mode)) { p->bdev = inode->i_sb->s_bdev; } inode_lock(inode); - if (IS_SWAPFILE(inode)) - return -EBUSY; + if (IS_SWAPFILE(inode)) { + inode_unlock(inode); + error = -EBUSY; + goto err; + } return 0; + +err: + if (S_ISBLK(inode->i_mode)) { + set_blocksize(p->bdev, p->old_block_size); + blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); + } + + return error; } @@ -3157,10 +3170,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) mapping = swap_file->f_mapping; inode = mapping->host; - /* If S_ISREG(inode->i_mode) will do inode_lock(inode); */ + /* do inode_lock(inode); */ error = claim_swapfile(p, inode); - if (unlikely(error)) + if (unlikely(error)) { + inode = NULL; goto bad_swap; + } /* * Read the swap header.