From patchwork Fri Nov 4 18:17:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 80910 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp110784qge; Fri, 4 Nov 2016 11:17:20 -0700 (PDT) X-Received: by 10.98.28.79 with SMTP id c76mr28754750pfc.8.1478283440718; Fri, 04 Nov 2016 11:17:20 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id wi8si11951970pab.108.2016.11.04.11.17.20 for ; Fri, 04 Nov 2016 11:17:20 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-scsi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@sandiskcorp.onmicrosoft.com; spf=pass (google.com: best guess record for domain of linux-scsi-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-scsi-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S938608AbcKDSRT (ORCPT ); Fri, 4 Nov 2016 14:17:19 -0400 Received: from mail-sn1nam01on0072.outbound.protection.outlook.com ([104.47.32.72]:43679 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S938605AbcKDSRS (ORCPT ); Fri, 4 Nov 2016 14:17:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sandiskcorp.onmicrosoft.com; s=selector1-sandisk-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=wUEo8pGDsF7+nnJIwGS48GHKB1Pw7x0WAAypAnQalps=; b=f4fCxr1aFHrHwgKfAOfwOlJlUF6CNZXWUPbcdJlw9kXCXoNFYBd/ZAMLn4KJcTM85IXtltN+4z0idUzV7gHbgRcT/PgTv1FL7T2qaSpK7KgoNOrLhP9t8S9htyJbBvk24RN+GgFXlJ6nsnHwVgjliW3pToXUkSnVTVyNklx2Sk4= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Bart.VanAssche@sandisk.com; Received: from exp-402881.sandisk.com (198.233.217.214) by BLUPR02MB1684.namprd02.prod.outlook.com (10.162.213.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.693.12; Fri, 4 Nov 2016 18:17:14 +0000 Subject: Re: [PATCH] Avoid that SCSI device removal through sysfs triggers a deadlock To: James Bottomley , "Martin K. Petersen" References: <14379fd1-c9bd-ad75-ca7c-0632f3e3c5d1@sandisk.com> <1477706936.2850.27.camel@linux.vnet.ibm.com> <96320196-4074-c970-97f0-bffb74713990@sandisk.com> <1478267240.3287.8.camel@linux.vnet.ibm.com> CC: Hannes Reinecke , Johannes Thumshirn , Sagi Grimberg , "linux-scsi@vger.kernel.org" From: Bart Van Assche Message-ID: Date: Fri, 4 Nov 2016 12:17:03 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <1478267240.3287.8.camel@linux.vnet.ibm.com> X-Originating-IP: [198.233.217.214] X-ClientProxiedBy: MWHPR07CA0027.namprd07.prod.outlook.com (10.169.230.13) To BLUPR02MB1684.namprd02.prod.outlook.com (10.162.213.26) X-MS-Office365-Filtering-Correlation-Id: a74673d1-febc-45b8-4f99-08d404dece32 X-Microsoft-Exchange-Diagnostics: 1; BLUPR02MB1684; 2:9UKR7qUoxK9Jj1e8z+BpuV+HB+mkoDE2QO77z6dPoaRCPVfNToj1dDmlNJpyvcTeaE3sCi7so+H7SuRXs1shN97rmGksPyDFd/CbySTQW6WcBSV8ntPQ7etVjiAHF+70+8X6Ls+0/gzm2oTWPDay6NQ8Dl90HeO7m0IB/JmzEMpP/5BbDEc0WThPEJ5P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l7STqBiYz8MTk=; 20:6IWdJ6cVqH+gk9DwrV3lf3L6R3P7x2hPRSLknJF3CQL4x9s4Uq81TJvwjSXb8k4kEyidGT6/ME1t4OUrmZFCZB3gMd2VSgEg8jx+wO4MKdtuOzAGbD1kTg3UZjxfarAklSwyhZV6FXMjAjVvfxtGSGTKKzljgidfVw6c/0rCIshU8GuJ9ffMF36eNGwCH8IRf5X4t0yoS2n+Ks0P55aa/GCm9RHQkO+Yw+Tk4YmKKhjiGrV3RfG0zFt3E01BG/jvpA3sr2XtGOwuLyF8dyz4JJa5iD00Q635QaUBTKfT/71kq1fsTFxlzZlLy+WNoGLWJ55Hg5kdP7ee9JNuZGiGL8ZdRRj6TT1E7sWU95LkxfqgRMKl8liRal9Sic6w5VndZqs8g7QQjAoBSvRfaFrDne+ETWxm29RD2HgsmnSpPXj1Tha1h4br4zNmpo5WZXRCqWF5QKwJQT2oTZntPdr/q6GDaSQvNwKMlKH7ww1zQ2qoY/5qHndQ7dZqixgAirFg X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(102415321)(6040176)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(6055026); SRVR:BLUPR02MB1684; BCL:0; PCL:0; RULEID:; SRVR:BLUPR02MB1684; X-Microsoft-Exchange-Diagnostics: 1; BLUPR02MB1684; 4:bp/AhFSPMUylvcK5lItborhdGBD0sWVaJwEuyagluFJG7KZORFwM3bI9s6e7XA+Sso3+1rFm8X0CxD27bTDm6HC8yvIto9lDYibANMvmL1Bh7M9/R2hNHwXS7IBQLZqQsYvWTnC2q/fUjjdz3K99G8Wu4hsb0FhwAqXCwsnHjR0U3ZZQQDDJCjNRxsj0U4x0CTsqC2U/EKOv6FOWmBDBzYB8G6a84o55aLuyptku4nHpAN2/S6Hcgwj2bsS8EvVwy4GqoETA8piHDg1yE4FD7DCWdy2DwYqYdyVHms5l0tWbXuOdlm2n/0z/GAkudXCsfmBgihVixHRhLEyfLn+Cf6qV86uZBkEbOHxJSM8ZxGIgDcu32OO1mAZvwSbTAWtN4O3ZEqKh/VYUiU/tmuEFqNEZhqoYCgv94WUR3r3IvOD0t4f3vKwaJ6jM5GotsCaD X-Forefront-PRVS: 01165471DB X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(377454003)(52314003)(189002)(199003)(24454002)(106356001)(5660300001)(512874002)(53416004)(19580395003)(31686004)(105586002)(54356999)(42186005)(76176999)(8676002)(4610100001)(2476003)(101416001)(4001350100001)(50986999)(568964002)(33646002)(270700001)(5001770100001)(3846002)(77096005)(69596002)(586003)(93886004)(6116002)(65826007)(4326007)(86362001)(6666003)(64126003)(2906002)(36756003)(2950100002)(92566002)(31696002)(81156014)(81166006)(7846002)(97736004)(66066001)(65956001)(189998001)(305945005)(84326002)(83506001)(65806001)(68736007)(5000100001)(4810100001)(5890100001)(7736002); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR02MB1684; H:exp-402881.sandisk.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: sandisk.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BLUPR02MB1684; 23:txCX+svf7zNmSINe8HixWpPYgPk7N/lPawAFeaRL6?= =?us-ascii?Q?3rj4oTgsjxSjtXB30WTmxRPSkXOjnpM4IGeJvVOgp2YJUjSyptu/aNwy6VdR?= =?us-ascii?Q?6zFLgqoHJSftJJOE8/ZYuEAbGUk6Lea2NClXIn5WP0lEfaS7gLqwcP8ujCpL?= =?us-ascii?Q?cX48YZujeSzlXzH7Twv2D31EcPFm9ntqiXB/yxws2U7cvWkXTDjPQBMu4sQn?= =?us-ascii?Q?Z2GnQuZU1yBWuegqt9z6hjppP9csAZW6AXw0SX1EzjYvCbbMeH5wgBHofaYN?= =?us-ascii?Q?SryDEkLjybBJacy9qHe8U1hOKAbFTrkA0C3yDye9erknnjiq3Smz/6yLfhzS?= =?us-ascii?Q?2vWVenc1fGRKlZs19Jm8r9X0j1cVPJhLhKdgK0hr461jf9yTp+Oe3EhgHV0+?= =?us-ascii?Q?Zz46H1l98tG7Szq6wwHoQaIV2W1b4AulsX1iP6laL5IAg0KvUJKxrAEGWarg?= =?us-ascii?Q?n31HBiEjJyH3gXvJp7L2mNFND267DkMGd+rJOepVoL3wfz+R6o+ZZnp9UiAF?= =?us-ascii?Q?l741uDFtIBxJn7n4oBiHw0DrexUyjNmc0hlNqBzWl26XxhBuogN5FK7YWerK?= =?us-ascii?Q?lb2SCYMv2DaKenwvcyjpBKJFU/LsPaMlKfd7EH3XsEyda9slWsYOk4M3haEL?= =?us-ascii?Q?NBoR3h43kNszi7jwqRilTiM3380gapWngU+06wXITeS2shY8aukfpdUVjz/O?= =?us-ascii?Q?vHISyIP1jH0gFA2X7Cz0AZ9EDqbVWlRdQPgxpBvgardHp5olD/eAxoE+4bH1?= =?us-ascii?Q?S/hrUZwzjAL2TFxsBaj7zEnefua5oBQEX8tTSHM+8VkK1S350HjxoX8wKo/b?= =?us-ascii?Q?S1g2QbipZb7eftTEi+AdN9HNHWMbzBzmuW2iZZe+73sdwxTI9pvD1WSyr5d9?= =?us-ascii?Q?5E6vW7mDwsR1PF8eJrKTcHF8incBozmoL/jed2asZ9G7LDmHyF4uZFifqIwF?= =?us-ascii?Q?h9usgwcI8ifo20oRRFecPTxT/ARYdfh1HVPajCTSi8Lku1ABj3chSUAGvMJK?= =?us-ascii?Q?n2GZuOReINV3iNI86KN7q2HunXoEvBu8aKt2TJHCN+Lf1oydiVnEkS5w3lAJ?= =?us-ascii?Q?FUpaAtt5z2O4Xe1sVlAXnOfYivCjb9Ubc9u4pxa5U8nx8vWcVPGDEpI4jXod?= =?us-ascii?Q?EiQBC54CaCmg+BC4dwfg9u2vIscLfzKeC3KIUNADg+qRgHv7VijoYqnE1gU6?= =?us-ascii?Q?JWK1FfY/aRXpICHaw/7C2FcYFvn0xUzEnFIUGnXG8OAonSmJJCJ65JLzyOiu?= =?us-ascii?Q?o5jE/NPAB+++kVr2Si7Kf+qLCzET6xCsKrIG/qjeQ/D9lbNaSapo9MVi3eau?= =?us-ascii?Q?WyqJRiW0C5dN9H30v1xFxqIf5uB/TtDQn93tyrzVFHsUL6mlrEaT6LTQijNW?= =?us-ascii?Q?am+3Is/HNBbC5gh6TTaWjxQGK3Ki5GSit2duQYFse5zuVoKkS2lRPJuMjtM6?= =?us-ascii?Q?DuO0sTtVn8AYpFz5z+S2dQsaA1VS5Ep4haBnHz6PWTZNWhWkjOo?= X-Microsoft-Exchange-Diagnostics: 1; BLUPR02MB1684; 6:aa/j4K5qfy4aQHwDr/pfHNZKJfSeVFoJTqnsN4d0uvzjg76yYJRdC50Bvh1JI67wL2wQwCt2Mo0nedvUAXT3CKgecmHyvwTcT3ggTQYGBzgqmz5mQBD4fvVTT+UKor7h8LUAt5+wQDTCHEsq4s5BTmAyQShPezhVI/6sr6Esg6hiwgPcwSSuQnM3c2J78idsrBD1Y23zI2xeGUOcu5Vi5gaZ9neCV9oC9oPcUvS4L25dgyDr1VYBlistHclnKI/MMaPakF/dOIPGC6Tyqv5fpTL5WC1/j9f80tMY3cL6lVUGa/11Qw9lw7RcTDY57VDdybAUf9wfP/XPYcZAJE2N1dHUITJBm9w4nTb9L+lK8v4=; 5:QWYgA2ZqMomjIjtEhKBiHFLG0zIr49L9hUhc19eP8XgIkUtlLK0KiCViJpoT6yAExGvs9lXQ5J32F8UP8sC067D4fdTT65QTgRplx27xFvS4SVH9jT13o5ae2o0LHyFPGslKKVWJRABXopVoL3A9ZA==; 24:xSlFjIvmkcH2Osuaq47aQXzdt6a56Ot8xnGMJXaji50dPw1+GM0tV2qmeqXxbSmKu4I4tsqvoG/4RRgmqd/o5vk6Wa3IFZka0z/EaIxVusI= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BLUPR02MB1684; 7:Lo4I/qSamPOxTY6wpa6nNI6JG2Cv4RjGGFRlc+V68aXzlVMhAlepBrgkaV5PzSR9kh6Kfz7YtXOw51Y5R5Ze8RMjF8syIND+UdeGFnm+jqB4sXAdd1iA/bbzWbyXhSHG5p9JC71P38vixH5vwZG/ixG0Nzo2KwEYLlZo04OWXltfKCL8tT7D9ny7Q0DH6OtZWJYTV4LPnRhZYNXpv60CUK5OCvcXiA3tWNRY9TtV+rB29eTwBK2Uu14z55kfAAKgJzjKsmABIclXV3wGCjufuC+epFtcYHZaJXn5PUgf4WX9QlQ1Kge+Y82hsqal3g8v/05CXKBbZFPlKfWQJ3yQNcp+LRGymE9n2mdDRenLUVs=; 20:q68RVSp8I8HE4ZHodOXNlDVKfb5nUTYruqvhaO65WBk6UBHPGUdKrC08t4t1s7wF9NRHo0PSzHvwPJvbagqmv16VNhUQEFgmeTWBXFWOvNr5a1sk+vynaTmZxzJB5GYTzG6IDqs4mhMKV/vw1Yd95BF1ihpeZ5Tk4MkpGaLAAZ2UWFrGyPu5OqB3ynsdMrk8GodyueWVBWyFEjEQCSziF7uNA4uvXScEh1GXrZmdNo7v5QMwsbeKFa1pytYv6asS X-OriginatorOrg: sandisk.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Nov 2016 18:17:14.1605 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR02MB1684 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org On 11/04/2016 07:47 AM, James Bottomley wrote: > You know after > > if (device_remove_file_self(dev, attr)) > > returns true that s_active is held and also that KERNFS_SUICIDAL is set > on the node, so the non-self remove paths can simply check for this > flag and return without descending into __kernfs_remove(), which would > mean they never take s_active. That means we never get the inversion. Hello James, The lock inversion is not triggered by the non-self remove paths but by the self-remove path. Anyway, can you have a look at the two attached patches? Thanks, Bart. >From f092fc1d40f3746c417e979985346ba169af7a6d Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Fri, 4 Nov 2016 10:07:23 -0600 Subject: [PATCH 2/2] Avoid that SCSI device removal through sysfs triggers a deadlock The SCSI core holds scan_mutex around SCSI device addition and removal operations. sysfs serializes attribute read and write operations against attribute removal through s_active. Avoid that grabbing scan_mutex during self-removal of a SCSI device triggers a deadlock by re-grabbing s_active after self-removal has finished instead of after kernfs_remove_self() has finished. Signed-off-by: Bart Van Assche --- fs/kernfs/dir.c | 19 +++++++++++++------ fs/kernfs/file.c | 7 +++++++ include/linux/kernfs.h | 1 + 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index cf4c636..abd3481 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -426,6 +426,8 @@ void kernfs_put_active(struct kernfs_node *kn) if (unlikely(!kn)) return; + WARN_ON_ONCE(kn->flags & KERNFS_BROKE_A_P); + if (kernfs_lockdep(kn)) rwsem_release(&kn->dep_map, 1, _RET_IP_); v = atomic_dec_return(&kn->active); @@ -1314,6 +1316,9 @@ void kernfs_unbreak_active_protection(struct kernfs_node *kn) * kernfs_remove_self - remove a kernfs_node from its own method * @kn: the self kernfs_node to remove * + * If kernfs_remove_self() sets KERNFS_BROKE_A_P the caller must invoke + * kernfs_unbreak_active_protection(). + * * The caller must be running off of a kernfs operation which is invoked * with an active reference - e.g. one of kernfs_ops. This can be used to * implement a file operation which deletes itself. @@ -1354,6 +1359,7 @@ bool kernfs_remove_self(struct kernfs_node *kn) */ if (!(kn->flags & KERNFS_SUICIDAL)) { kn->flags |= KERNFS_SUICIDAL; + kn->flags |= KERNFS_BROKE_A_P; __kernfs_remove(kn); kn->flags |= KERNFS_SUICIDED; ret = true; @@ -1375,13 +1381,14 @@ bool kernfs_remove_self(struct kernfs_node *kn) finish_wait(waitq, &wait); WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb)); ret = false; - } - /* - * This must be done while holding kernfs_mutex; otherwise, waiting - * for SUICIDED && deactivated could finish prematurely. - */ - kernfs_unbreak_active_protection(kn); + /* + * This must be done while holding kernfs_mutex; otherwise, + * waiting for SUICIDED && deactivated could finish + * prematurely. + */ + kernfs_unbreak_active_protection(kn); + } mutex_unlock(&kernfs_mutex); return ret; diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index da58ea4..be2e540 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -319,6 +319,13 @@ static ssize_t kernfs_fop_write(struct file *file, const char __user *user_buf, else len = -EINVAL; + mutex_lock(&kernfs_mutex); + if (kn->flags & KERNFS_BROKE_A_P) { + kernfs_unbreak_active_protection(kn); + kn->flags &= ~KERNFS_BROKE_A_P; + } + mutex_unlock(&kernfs_mutex); + kernfs_put_active(kn); mutex_unlock(&of->mutex); diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 7056238..34de7f1 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -46,6 +46,7 @@ enum kernfs_node_flag { KERNFS_SUICIDAL = 0x0400, KERNFS_SUICIDED = 0x0800, KERNFS_EMPTY_DIR = 0x1000, + KERNFS_BROKE_A_P = 0x2000, }; /* @flags for kernfs_create_root() */ -- 2.10.1