Received: by 2002:a05:6520:4211:b029:f4:110d:56bc with SMTP id o17csp2112631lkv; Thu, 20 May 2021 03:42:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/CadSnX5u9OBsOHMDDdJKdWt9ylrx7OlURaOqDetUP0Oqn/0rT60CQodnXQCP6W6TkWra X-Received: by 2002:a5d:848a:: with SMTP id t10mr4855974iom.68.1621507356122; Thu, 20 May 2021 03:42:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621507356; cv=none; d=google.com; s=arc-20160816; b=Mm+G9n6/KJ7g2LMldREdFpzG/ae0zv/vaQOUfjTD50EDfgef3o1R3jFuwQz9iWMC4/ x4HKOe2gfjxVZs6Zcto7gUms+j1AylRNWOuwnK878dCDCg8uMIxYGI/oMBUqyWi0bhpj O8urwdHicXTquI68YnXWELCJ9Obf9tupxB5vtSfQtUY4MkLqCzWRJ7y0nwsyPBHFjh1U xaP9whxsC2uDblIYD6kBramsBm/FimAD/77+lZtTByIH9/P33qVlmQxAw6DdxhV96eIF L7SH1D2p6NPkB5nD+LVeH+28A5Ffh1PFGLqhFrPb9f42ND5HN6wLpR0ouk74Navh2qPX 4wow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XdqwUPI9BUAeWOq7sMwKrHWBqFYUk1k3KoqZy/yPRKE=; b=PWrjpR4422UTJd3O3aZnjwaKNEfdgAvUf69c2ZZ+frsErRd+dQ8d9JBCUufnqmMW0G Fe3lxQ+aH43ftViB5mHMVlMdJ2D7n+/crd3t1cY1A8KtRNm4EOseWytoxq2CUJqCMp2W 7ARB98ya8hqofoBzS/BtCkhsgovYhsKn4oNvbp9ZyZQBWzHhyH4XkBWqVdx1Ynj/CyH7 1jFlpY7p5weINHgsUIBH2PpDTgWa5nn38bBoeb8hqWdPAHkfjDZAzUu8ArxyMQfxT2gw 0bHdebXC9WjW1j8iekUXgdIFPSmHGpkC455j65iwVxU9efTbsJnmP52aWwoSpEP6h8zw bthA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QI5Vzu1t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o21si2161482jat.63.2021.05.20.03.41.54; Thu, 20 May 2021 03:42:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QI5Vzu1t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238180AbhETKji (ORCPT + 99 others); Thu, 20 May 2021 06:39:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:54242 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236802AbhETKYc (ORCPT ); Thu, 20 May 2021 06:24:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8B8D661465; Thu, 20 May 2021 09:49:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621504169; bh=rAj/I9XrTXvfC4kGNOHuq0EJVGXRQGbCyUpSWlKL1ew=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QI5Vzu1toseJ0o3ji23/AypPNTWPPNnUZIhKq/0LGtotXlqwOkBsnEhlqKws9D1af PrtYUqrWKBY35JKLwXyzo0KV+BLdQ2MuQAwZXC93sD3Rv6RSIbTU2V6XlqLNIfkXTH w7qkeZU5qsIptEEmY/lJBbt+IdZ7NhpJlfhafhSw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org Subject: [PATCH 4.14 122/323] md: split mddev_find Date: Thu, 20 May 2021 11:20:14 +0200 Message-Id: <20210520092124.289317712@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Christoph Hellwig commit 65aa97c4d2bfd76677c211b9d03ef05a98c6d68e upstream. Split mddev_find into a simple mddev_find that just finds an existing mddev by the unit number, and a more complicated mddev_find that deals with find or allocating a mddev. This turns out to fix this bug reported by Zhao Heming. ----------------------------- snip ------------------------------ commit d3374825ce57 ("md: make devices disappear when they are no longer needed.") introduced protection between mddev creating & removing. The md_open shouldn't create mddev when all_mddevs list doesn't contain mddev. With currently code logic, there will be very easy to trigger soft lockup in non-preempt env. --- drivers/md/md.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -570,6 +570,22 @@ EXPORT_SYMBOL_GPL(mddev_init); static struct mddev *mddev_find(dev_t unit) { + struct mddev *mddev; + + if (MAJOR(unit) != MD_MAJOR) + unit &= ~((1 << MdpMinorShift) - 1); + + spin_lock(&all_mddevs_lock); + mddev = mddev_find_locked(unit); + if (mddev) + mddev_get(mddev); + spin_unlock(&all_mddevs_lock); + + return mddev; +} + +static struct mddev *mddev_find_or_alloc(dev_t unit) +{ struct mddev *mddev, *new = NULL; if (unit && MAJOR(unit) != MD_MAJOR) @@ -5276,7 +5292,7 @@ static int md_alloc(dev_t dev, char *nam * writing to /sys/module/md_mod/parameters/new_array. */ static DEFINE_MUTEX(disks_mutex); - struct mddev *mddev = mddev_find(dev); + struct mddev *mddev = mddev_find_or_alloc(dev); struct gendisk *disk; int partitioned; int shift; @@ -6126,11 +6142,9 @@ static void autorun_devices(int part) md_probe(dev, NULL, NULL); mddev = mddev_find(dev); - if (!mddev || !mddev->gendisk) { - if (mddev) - mddev_put(mddev); + if (!mddev) break; - } + if (mddev_lock(mddev)) pr_warn("md: %s locked, cannot run\n", mdname(mddev)); else if (mddev->raid_disks || mddev->major_version