Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp583863iog; Mon, 13 Jun 2022 08:35:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxuLh/BDkT9qehLjutY980sd00J62z5EEYDgdQFcGB4dZJWuElMXp6c6PV9IYOk5Q97P7qY X-Received: by 2002:a17:906:779a:b0:715:790f:715c with SMTP id s26-20020a170906779a00b00715790f715cmr329701ejm.707.1655134514241; Mon, 13 Jun 2022 08:35:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655134514; cv=none; d=google.com; s=arc-20160816; b=PPEUKYdYRll37GWJy6u5Yt1Ul3dV8SSJ0W+y2RifPPq+d122ccSyZ+rNw+qhhrm+Xi cLkNcWSwlKyT+CUZho8BMMNgf+MX1ZtkPl1+LSWS2klWNKBaYZKbqskPwmoxYwySuQPN UtnnDzL9gofLRvZ3C+iPiRRvV6vIqR/3zOLlLfHwTqH5ALhM0ZLpbfpxuvW6GMbmg60B EraZJ1PwaZZHV8sGrrYnU6GYLhztEoktd/uhzrYECAdvqgV1QUDchNWfsy7At5W/Fj3D s1XKSVnxbevSlqM38/V8Jyq8MuhS7G06YkMmrSiZ2dR9Z6eEGXyJtlo65MAmrua/L+OC SQcA== 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=Y947jdBBkH8vtzZxb9RHfJ+PYh+nuM8BQErRebvch3w=; b=c3K6QVvwciWPfpAT55O4O9cSN2GPhGpGBmwp3k8adiK3JX6karSbtLYDNKNYZ+YJ1M Mqk4yuD5VqVWV9cZskzanbU/i38Adm4LqBvruwC2M+jbX3rjdbQ5/NDf+tCapBs6bGwK c7fuKzE7ThN3wQKCpMFW5h7q2ShCcpfy95/Wy1yYxZWQ3TboBMQmTvnSqsNrwrTZge+0 JFOOX9p2BbZOJlrBZ8zSqAv6Y/dP7G7UnETNPl4St/JsObDloH+7rm9QlUkhgo+WMtAT yBdUCaf98N3wDSDQ3YPOImqQYkwvqiIGSk6Vs2slR89VX6MtRGXZxmXkNnQwebLxdlk6 Fk/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xYiU9jCD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z27-20020a17090674db00b00711ca15cd1dsi7359229ejl.180.2022.06.13.08.34.49; Mon, 13 Jun 2022 08:35:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xYiU9jCD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S1376948AbiFMNXy (ORCPT + 99 others); Mon, 13 Jun 2022 09:23:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376980AbiFMNTq (ORCPT ); Mon, 13 Jun 2022 09:19:46 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3F6E692BD; Mon, 13 Jun 2022 04:23:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B0BA460F8C; Mon, 13 Jun 2022 11:22:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF94BC34114; Mon, 13 Jun 2022 11:22:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655119347; bh=dGSOQsi//NDJceKRzTdm937m9iBGzCOIme+ahMgLDsw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xYiU9jCD5KyOXqAgz+Ip009SuJsWW0+Dg4wB3MA4DFD+KreZFfC5SGLlFSe3OO+uR xfOwGyB0xmJqgLw8oCm7v14Aw/7LEh907lgjKm2mNG3Ko6ryqMS8+RARzy0OYab3g9 qlvarXyFZEiJFFdHzseXovX1Tvq22NkcitbdWLzI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Donald Buczek , Guoqing Jiang , Song Liu , Sasha Levin Subject: [PATCH 5.15 193/247] md: protect md_unregister_thread from reentrancy Date: Mon, 13 Jun 2022 12:11:35 +0200 Message-Id: <20220613094928.804432025@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094922.843438024@linuxfoundation.org> References: <20220613094922.843438024@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Guoqing Jiang [ Upstream commit 1e267742283a4b5a8ca65755c44166be27e9aa0f ] Generally, the md_unregister_thread is called with reconfig_mutex, but raid_message in dm-raid doesn't hold reconfig_mutex to unregister thread, so md_unregister_thread can be called simulitaneously from two call sites in theory. Then after previous commit which remove the protection of reconfig_mutex for md_unregister_thread completely, the potential issue could be worse than before. Let's take pers_lock at the beginning of function to ensure reentrancy. Reported-by: Donald Buczek Signed-off-by: Guoqing Jiang Signed-off-by: Song Liu Signed-off-by: Sasha Levin --- drivers/md/md.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/drivers/md/md.c b/drivers/md/md.c index b553654cbebc..bf1c5c0e472e 100644 --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -7942,17 +7942,22 @@ EXPORT_SYMBOL(md_register_thread); void md_unregister_thread(struct md_thread **threadp) { - struct md_thread *thread = *threadp; - if (!thread) - return; - pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); - /* Locking ensures that mddev_unlock does not wake_up a + struct md_thread *thread; + + /* + * Locking ensures that mddev_unlock does not wake_up a * non-existent thread */ spin_lock(&pers_lock); + thread = *threadp; + if (!thread) { + spin_unlock(&pers_lock); + return; + } *threadp = NULL; spin_unlock(&pers_lock); + pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); kthread_stop(thread->tsk); kfree(thread); } -- 2.35.1