Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp13958353pxu; Mon, 4 Jan 2021 09:00:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJzbsPWcdgbAke2at3lrfnRGZX+8M7KbsNHs/h5wzFvqnjgxRC9dXZ42+RhqwAl2MJ55H2nt X-Received: by 2002:a05:6402:3186:: with SMTP id di6mr69081504edb.16.1609779639410; Mon, 04 Jan 2021 09:00:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609779639; cv=none; d=google.com; s=arc-20160816; b=uBNZ0VDKewAP8XjYtfet1Zs4TRaSWD+4cEl56+eCiRBmgvIqzkYpvk97ca8rfQcooh 21N9IWxFcRtpyo+VEer/Sqvxr6IY51FngBc71lSkVPiOCmVrbTPZtCcJ2D3xCpIP5xz3 Np/XRfRt6TKsr0rDbvInvEUcxYAY3SRI/NzKwVO+8KMS1EFYk+JGn4NuwFBYollWQxuO Ra8yTdhwQ5NS3wrCrAwMqBKF8P/d7yYAaEQvna2AgpEk5GlbpFz2TLdiMjQm9sPCYNRz 8UGdXXZrH8BwULJJCIdu0K9y9zoPIt9Gtvol9A/7lU2h5keXL0B1EUnCyl6sEj+YJB7c qV7Q== 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=XSeIGdlg71haZu04g8SPZ2yOpDDXDDlneXt/PXkOYH8=; b=O1Rb1hnTDUzVvcLhNiW6G62bs7WNltoMXSqZ2EgaF444zP6J+J8JINMfcmG+OLtx9+ xCW7pBhUgjroSsGrV7DKRfQzYDd1PDhmpKCOQiGdSt6MXBUiyvnN8wKvx32KRFGkuoVZ PtdFmSyigE1+eFDZRatlw6cWiHDHbmUOiMrJx8RMO0biNS33jV/vQY59D3XnlJ/aST1M MeZJj6Eq1zsyZqX1dLoUH6x19m+JsmS7ABP1exY4QqoewPpZUhssDo3VywQdRt7v36I+ wIQbnd6RbFDVhPidbcDdFFJV+qPZmdtcdH00hg+eWyceILCDfAfHm7oC/XjyifYtpTsu CmuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AQWI4SnJ; 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 1si33358893edv.426.2021.01.04.09.00.16; Mon, 04 Jan 2021 09:00:39 -0800 (PST) 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=AQWI4SnJ; 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 S1729635AbhADQI4 (ORCPT + 99 others); Mon, 4 Jan 2021 11:08:56 -0500 Received: from mail.kernel.org ([198.145.29.99]:38464 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728611AbhADQBg (ORCPT ); Mon, 4 Jan 2021 11:01:36 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id D2025224D2; Mon, 4 Jan 2021 16:01:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609776080; bh=5Gfu3L59rdjO9CtMPxwCNS4CHfJnT5MFmlDci/a6xLE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AQWI4SnJ5sDb4ZVdT2npRXnLWx6KR45BuKghllcoHZHw0siH4SRpOAv/huKLOOIdY 37ADL3k7LMBaJeSG8h4e1uN6xPg+akeI2Dh6a/iBqpGMD6rzJQFSITiPTy5GE4dKss qdnQV72cJUmEn1VX+qNBo982jlEdMw2OseFYzuIs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ming Lei , "Rafael J. Wysocki" , Christoph Hellwig , Hannes Reinecke , Jens Axboe , Alan Stern , Stanley Chu , Can Guo , Bart Van Assche , "Martin K. Petersen" Subject: [PATCH 5.10 13/63] scsi: block: Fix a race in the runtime power management code Date: Mon, 4 Jan 2021 16:57:06 +0100 Message-Id: <20210104155709.457411783@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210104155708.800470590@linuxfoundation.org> References: <20210104155708.800470590@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: Bart Van Assche commit fa4d0f1992a96f6d7c988ef423e3127e613f6ac9 upstream. With the current implementation the following race can happen: * blk_pre_runtime_suspend() calls blk_freeze_queue_start() and blk_mq_unfreeze_queue(). * blk_queue_enter() calls blk_queue_pm_only() and that function returns true. * blk_queue_enter() calls blk_pm_request_resume() and that function does not call pm_request_resume() because the queue runtime status is RPM_ACTIVE. * blk_pre_runtime_suspend() changes the queue status into RPM_SUSPENDING. Fix this race by changing the queue runtime status into RPM_SUSPENDING before switching q_usage_counter to atomic mode. Link: https://lore.kernel.org/r/20201209052951.16136-2-bvanassche@acm.org Fixes: 986d413b7c15 ("blk-mq: Enable support for runtime power management") Cc: Ming Lei Cc: Rafael J. Wysocki Cc: stable Reviewed-by: Christoph Hellwig Reviewed-by: Hannes Reinecke Reviewed-by: Jens Axboe Acked-by: Alan Stern Acked-by: Stanley Chu Co-developed-by: Can Guo Signed-off-by: Can Guo Signed-off-by: Bart Van Assche Signed-off-by: Martin K. Petersen Signed-off-by: Greg Kroah-Hartman --- block/blk-pm.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) --- a/block/blk-pm.c +++ b/block/blk-pm.c @@ -67,6 +67,10 @@ int blk_pre_runtime_suspend(struct reque WARN_ON_ONCE(q->rpm_status != RPM_ACTIVE); + spin_lock_irq(&q->queue_lock); + q->rpm_status = RPM_SUSPENDING; + spin_unlock_irq(&q->queue_lock); + /* * Increase the pm_only counter before checking whether any * non-PM blk_queue_enter() calls are in progress to avoid that any @@ -89,15 +93,14 @@ int blk_pre_runtime_suspend(struct reque /* Switch q_usage_counter back to per-cpu mode. */ blk_mq_unfreeze_queue(q); - spin_lock_irq(&q->queue_lock); - if (ret < 0) + if (ret < 0) { + spin_lock_irq(&q->queue_lock); + q->rpm_status = RPM_ACTIVE; pm_runtime_mark_last_busy(q->dev); - else - q->rpm_status = RPM_SUSPENDING; - spin_unlock_irq(&q->queue_lock); + spin_unlock_irq(&q->queue_lock); - if (ret) blk_clear_pm_only(q); + } return ret; }