Received: by 10.223.185.116 with SMTP id b49csp821122wrg; Sat, 10 Feb 2018 21:10:08 -0800 (PST) X-Google-Smtp-Source: AH8x22443YKFblx6sAYLvewrRn2uVmAoaO6n7k8FQ3O7qD9Psb3i7G2+GYoUJ3smstQzccKX1tNS X-Received: by 10.98.28.210 with SMTP id c201mr7766344pfc.109.1518325808030; Sat, 10 Feb 2018 21:10:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518325807; cv=none; d=google.com; s=arc-20160816; b=oZw5jhLysyemJG65r0SWOuktcTEQE3ENsSZ6juWSAqv99vXIqaUfmjEw50A3iWNutX 1oAH9N8X1E8ci3bckoXQ6eyVfhgVfsV5Wav1Wd4PNK3UaydrBUn2qsYwN63vuFcz/Gia lIJuEfskYU31k6h4AVWI44ZmIGOyBJNml4uvOi/bB0ebValFN3iaDBvvbz1qUOAIKd7h lbBXUBOHQJxYPUBnDNLKD4qJEbySU8iHSWSx0Yr4smV3LWRaa2mK8aIZYy+JcqkSIeDE tyUdpKbky1uxbahxjtAG73GFeLudxcCPkgLuFE2WOojudHYvp+4HpCXZJzPTN4Ch8hZL jDGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=OVjKgyY9HKwHtRUplUs4Gdt/DaGS78Vy/F1jobJudRc=; b=nsC5oM+VwtqSOm4XLg5dOVDDSj0Z17ld3/L2fXUytf64iJklI4SrMEHchKHOHp7NUJ ae2yvyxzJULO6Gn8tAu9J4Tyltof2ZmPwcf7xiX5AO71ZIjswRa6Jga0bwfvbj2eAg7z zLplwBKb+5sDpdChE4lF2bS2bxDYYdJPPbLz6rN79V1swxXNd4KsqiKg6dGuSNORUAcM utQrZCIRef+fJ5q10fB0/6xuKDawrLoTqIGcxpOz2oodpuV6nBf/SA8ICogYUJ2SwyMu YNzlaEu7nb3Y/HutGXQ2RaHbk4jIi/EUWVBi0fz4YwALp9L2VFHaS/4wTlfJ4GP44DCW aotA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n71si2276641pfk.103.2018.02.10.21.09.54; Sat, 10 Feb 2018 21:10:07 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753946AbeBKFHh (ORCPT + 99 others); Sun, 11 Feb 2018 00:07:37 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:41460 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752656AbeBKEdm (ORCPT ); Sat, 10 Feb 2018 23:33:42 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ekjKe-0002hE-O1; Sun, 11 Feb 2018 04:33:40 +0000 Received: from ben by deadeye with local (Exim 4.90) (envelope-from ) id 1ekjKX-0004UA-Td; Sun, 11 Feb 2018 04:33:33 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Hou Tao" , "Mike Snitzer" Date: Sun, 11 Feb 2018 04:20:06 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.2 34/79] dm: fix race between dm_get_from_kobject() and __dm_destroy() In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.2.99-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Hou Tao commit b9a41d21dceadf8104812626ef85dc56ee8a60ed upstream. The following BUG_ON was hit when testing repeat creation and removal of DM devices: kernel BUG at drivers/md/dm.c:2919! CPU: 7 PID: 750 Comm: systemd-udevd Not tainted 4.1.44 Call Trace: [] dm_get_from_kobject+0x34/0x3a [] dm_attr_show+0x2b/0x5e [] ? mutex_lock+0x26/0x44 [] sysfs_kf_seq_show+0x83/0xcf [] kernfs_seq_show+0x23/0x25 [] seq_read+0x16f/0x325 [] kernfs_fop_read+0x3a/0x13f [] __vfs_read+0x26/0x9d [] ? security_file_permission+0x3c/0x44 [] ? rw_verify_area+0x83/0xd9 [] vfs_read+0x8f/0xcf [] ? __fdget_pos+0x12/0x41 [] SyS_read+0x4b/0x76 [] system_call_fastpath+0x12/0x71 The bug can be easily triggered, if an extra delay (e.g. 10ms) is added between the test of DMF_FREEING & DMF_DELETING and dm_get() in dm_get_from_kobject(). To fix it, we need to ensure the test of DMF_FREEING & DMF_DELETING and dm_get() are done in an atomic way, so _minor_lock is used. The other callers of dm_get() have also been checked to be OK: some callers invoke dm_get() under _minor_lock, some callers invoke it under _hash_lock, and dm_start_request() invoke it after increasing md->open_count. Signed-off-by: Hou Tao Signed-off-by: Mike Snitzer Signed-off-by: Ben Hutchings --- drivers/md/dm.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -2685,11 +2685,15 @@ struct mapped_device *dm_get_from_kobjec md = container_of(kobj, struct mapped_device, kobj_holder.kobj); - if (test_bit(DMF_FREEING, &md->flags) || - dm_deleting_md(md)) - return NULL; - + spin_lock(&_minor_lock); + if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) { + md = NULL; + goto out; + } dm_get(md); +out: + spin_unlock(&_minor_lock); + return md; }