From patchwork Thu Jan 5 15:31:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhen Ren X-Patchwork-Id: 9499223 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5D86C606B4 for ; Thu, 5 Jan 2017 15:33:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 531562808C for ; Thu, 5 Jan 2017 15:33:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 46A7D283E8; Thu, 5 Jan 2017 15:33:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from userp1040.oracle.com (userp1040.oracle.com [156.151.31.81]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id AAB8F2808C for ; Thu, 5 Jan 2017 15:33:35 +0000 (UTC) Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v05FXQiH010593 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 5 Jan 2017 15:33:26 GMT Received: from oss.oracle.com (oss-old-reserved.oracle.com [137.254.22.2]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id v05FX8Y5017465 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 5 Jan 2017 15:33:08 GMT Received: from localhost ([127.0.0.1] helo=lb-oss.oracle.com) by oss.oracle.com with esmtp (Exim 4.63) (envelope-from ) id 1cPA2O-0005iZ-7T; Thu, 05 Jan 2017 07:33:08 -0800 Received: from userv0022.oracle.com ([156.151.31.74]) by oss.oracle.com with esmtp (Exim 4.63) (envelope-from ) id 1cPA28-0005hp-Vh for ocfs2-devel@oss.oracle.com; Thu, 05 Jan 2017 07:32:53 -0800 Received: from aserp1030.oracle.com (aserp1030.oracle.com [141.146.126.68]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id v05FWqtb023286 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 5 Jan 2017 15:32:52 GMT Received: from userp2040.oracle.com (userp2040.oracle.com [156.151.31.90]) by aserp1030.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v05FWpN7009014 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Thu, 5 Jan 2017 15:32:52 GMT Received: from pps.filterd (userp2040.oracle.com [127.0.0.1]) by userp2040.oracle.com (8.16.0.17/8.16.0.17) with SMTP id v05FW3hG004235 for ; Thu, 5 Jan 2017 15:32:51 GMT Authentication-Results: oracle.com; spf=pass smtp.mailfrom=zren@suse.com Received: from prv3-mh.provo.novell.com (victor.provo.novell.com [137.65.250.26]) by userp2040.oracle.com with ESMTP id 27sn88evdf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 05 Jan 2017 15:32:51 +0000 Received: from laptop.ha.suse (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (TLS encrypted); Thu, 05 Jan 2017 08:32:34 -0700 From: Eric Ren To: ocfs2-devel@oss.oracle.com Date: Thu, 5 Jan 2017 23:31:02 +0800 Message-Id: <1483630262-22227-3-git-send-email-zren@suse.com> X-Mailer: git-send-email 2.6.6 In-Reply-To: <1483630262-22227-1-git-send-email-zren@suse.com> References: <1483630262-22227-1-git-send-email-zren@suse.com> X-PDR: PASS X-ServerName: victor.provo.novell.com X-Proofpoint-SPF-Result: pass X-Proofpoint-SPF-Record: v=spf1 include:microfocus.com ~all X-Proofpoint-Virus-Version: vendor=nai engine=5800 definitions=8399 signatures=670794 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1701050241 Cc: linux-kernel@vger.kernel.org Subject: [Ocfs2-devel] [PATCH 2/2] ocfs2: fix deadlocks when taking inode lock at vfs entry points X-BeenThere: ocfs2-devel@oss.oracle.com X-Mailman-Version: 2.1.9 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ocfs2-devel-bounces@oss.oracle.com Errors-To: ocfs2-devel-bounces@oss.oracle.com X-Source-IP: aserv0022.oracle.com [141.146.126.234] X-Virus-Scanned: ClamAV using ClamSMTP Commit 743b5f1434f5 ("ocfs2: take inode lock in ocfs2_iop_set/get_acl()") results in a deadlock, as the author "Tariq Saeed" realized shortly after the patch was merged. The discussion happened here (https://oss.oracle.com/pipermail/ocfs2-devel/2015-September/011085.html). The reason why taking cluster inode lock at vfs entry points opens up a self deadlock window, is explained in the previous patch of this series. So far, we have seen two different code paths that have this issue. 1. do_sys_open may_open inode_permission ocfs2_permission ocfs2_inode_lock() <=== take PR generic_permission get_acl ocfs2_iop_get_acl ocfs2_inode_lock() <=== take PR 2. fchmod|fchmodat chmod_common notify_change ocfs2_setattr <=== take EX posix_acl_chmod get_acl ocfs2_iop_get_acl <=== take PR ocfs2_iop_set_acl <=== take EX Fixes them by adding the tracking logic (in the previous patch) for these funcs above, ocfs2_permission(), ocfs2_iop_[set|get]_acl(), ocfs2_setattr(). Signed-off-by: Eric Ren --- fs/ocfs2/acl.c | 39 ++++++++++++++++++++++++++++++++++----- fs/ocfs2/file.c | 44 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/fs/ocfs2/acl.c b/fs/ocfs2/acl.c index bed1fcb..c539890 100644 --- a/fs/ocfs2/acl.c +++ b/fs/ocfs2/acl.c @@ -284,16 +284,31 @@ int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type) { struct buffer_head *bh = NULL; int status = 0; - - status = ocfs2_inode_lock(inode, &bh, 1); + int arg_flags = 0, has_locked; + struct ocfs2_holder oh; + struct ocfs2_lock_res *lockres; + + lockres = &OCFS2_I(inode)->ip_inode_lockres; + has_locked = (ocfs2_is_locked_by_me(lockres) != NULL); + if (has_locked) + arg_flags = OCFS2_META_LOCK_GETBH; + status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags); if (status < 0) { if (status != -ENOENT) mlog_errno(status); return status; } + if (!has_locked) + ocfs2_add_holder(lockres, &oh); + status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL); - ocfs2_inode_unlock(inode, 1); + + if (!has_locked) { + ocfs2_remove_holder(lockres, &oh); + ocfs2_inode_unlock(inode, 1); + } brelse(bh); + return status; } @@ -303,21 +318,35 @@ struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type) struct buffer_head *di_bh = NULL; struct posix_acl *acl; int ret; + int arg_flags = 0, has_locked; + struct ocfs2_holder oh; + struct ocfs2_lock_res *lockres; osb = OCFS2_SB(inode->i_sb); if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL)) return NULL; - ret = ocfs2_inode_lock(inode, &di_bh, 0); + + lockres = &OCFS2_I(inode)->ip_inode_lockres; + has_locked = (ocfs2_is_locked_by_me(lockres) != NULL); + if (has_locked) + arg_flags = OCFS2_META_LOCK_GETBH; + ret = ocfs2_inode_lock_full(inode, &di_bh, 0, arg_flags); if (ret < 0) { if (ret != -ENOENT) mlog_errno(ret); return ERR_PTR(ret); } + if (!has_locked) + ocfs2_add_holder(lockres, &oh); acl = ocfs2_get_acl_nolock(inode, type, di_bh); - ocfs2_inode_unlock(inode, 0); + if (!has_locked) { + ocfs2_remove_holder(lockres, &oh); + ocfs2_inode_unlock(inode, 0); + } brelse(di_bh); + return acl; } diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c index c488965..62be75d 100644 --- a/fs/ocfs2/file.c +++ b/fs/ocfs2/file.c @@ -1138,6 +1138,9 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) handle_t *handle = NULL; struct dquot *transfer_to[MAXQUOTAS] = { }; int qtype; + int arg_flags = 0, had_lock; + struct ocfs2_holder oh; + struct ocfs2_lock_res *lockres; trace_ocfs2_setattr(inode, dentry, (unsigned long long)OCFS2_I(inode)->ip_blkno, @@ -1173,13 +1176,20 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) } } - status = ocfs2_inode_lock(inode, &bh, 1); + lockres = &OCFS2_I(inode)->ip_inode_lockres; + had_lock = (ocfs2_is_locked_by_me(lockres) != NULL); + if (had_lock) + arg_flags = OCFS2_META_LOCK_GETBH; + status = ocfs2_inode_lock_full(inode, &bh, 1, arg_flags); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto bail_unlock_rw; } - inode_locked = 1; + if (!had_lock) { + ocfs2_add_holder(lockres, &oh); + inode_locked = 1; + } if (size_change) { status = inode_newsize_ok(inode, attr->ia_size); @@ -1260,7 +1270,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) bail_commit: ocfs2_commit_trans(osb, handle); bail_unlock: - if (status) { + if (status && inode_locked) { + ocfs2_remove_holder(lockres, &oh); ocfs2_inode_unlock(inode, 1); inode_locked = 0; } @@ -1278,8 +1289,10 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) if (status < 0) mlog_errno(status); } - if (inode_locked) + if (inode_locked) { + ocfs2_remove_holder(lockres, &oh); ocfs2_inode_unlock(inode, 1); + } brelse(bh); return status; @@ -1321,20 +1334,31 @@ int ocfs2_getattr(struct vfsmount *mnt, int ocfs2_permission(struct inode *inode, int mask) { int ret; + int has_locked; + struct ocfs2_holder oh; + struct ocfs2_lock_res *lockres; if (mask & MAY_NOT_BLOCK) return -ECHILD; - ret = ocfs2_inode_lock(inode, NULL, 0); - if (ret) { - if (ret != -ENOENT) - mlog_errno(ret); - goto out; + lockres = &OCFS2_I(inode)->ip_inode_lockres; + has_locked = (ocfs2_is_locked_by_me(lockres) != NULL); + if (!has_locked) { + ret = ocfs2_inode_lock(inode, NULL, 0); + if (ret) { + if (ret != -ENOENT) + mlog_errno(ret); + goto out; + } + ocfs2_add_holder(lockres, &oh); } ret = generic_permission(inode, mask); - ocfs2_inode_unlock(inode, 0); + if (!has_locked) { + ocfs2_remove_holder(lockres, &oh); + ocfs2_inode_unlock(inode, 0); + } out: return ret; }