Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2653282pxa; Mon, 17 Aug 2020 15:35:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzE0zbD3czCGwb46hGevFKaVU8iFgWgud8luZbV9/JB9ok8y8QGAyRHQ8u1VZD4F/Q4QuAB X-Received: by 2002:a17:906:d930:: with SMTP id rn16mr17955686ejb.330.1597703746818; Mon, 17 Aug 2020 15:35:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597703746; cv=none; d=google.com; s=arc-20160816; b=LsS5dqNWNHEADVHZ4lissIaLmIqHV9Okb/BQxxKbdvgHDtAMkhKb95lX6yepuKX7Oo qpMWSLYXsnJ6h4GJ+C9R+CwQzr1t5FpEcyelz6XM5eYZ9dWPbOQvarF6E41lZ9Iz6hgw dc0zAR+PzttVgVib4l5sXtKpa8nhCQujVknh6oXOGqHrUisPhGdcqXCpgKKaS3bjCGGN KRabwBJ5uvTZR5qrUJicvOreE4O2/smV+G2s0pTDbUdsG+oBfIqOMiPMUT/jyoD6JDCy o9uEv95yQVfiU0zliaLpzSRRYVTtuMb7vrfE2Bf4PblwjJ9k3OF+MjHm6Vl8CnGxE3xw o/HA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Pc4Y7J7KnhhsIj6IBwUdummOB59KvVeEMStg5+73jRM=; b=wgGuaJhcCR/Qo2y/XozS1U1gkaERvPj8IwWfIZpA02wBN0SMcCTER/ryW2sMYxJ3Xc BRbePXrAsvYnIVNSUlrZoBkOrPxGnIktLge7cDdFLoG/RgK2SOuiWGpAlVGqCyuXGQ2T UZ9DWkj777LdZn0yZ2jSnrnu3UE4uDXqCpYtrNP2bYS8xLOLJQvAzZ+yCFtydZzS/1n0 aPG6qpVZtgPST07tBYbJc07FYUFcx1bKFiHYt2+Q5xmvHPbSMpn6p8k9JJAfl0qotrq7 429QIr8xB0HWfVxMKK1HMUh8y+IMaDtEHsKBQtD6f9YkSKcCg/7+7mQG9vnJ9ELesViI r/mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mET30cob; 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=fail (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 i6si11981833ejc.494.2020.08.17.15.35.23; Mon, 17 Aug 2020 15:35:46 -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=@kernel.org header.s=default header.b=mET30cob; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389606AbgHQRQe (ORCPT + 99 others); Mon, 17 Aug 2020 13:16:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:49406 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388773AbgHQQOK (ORCPT ); Mon, 17 Aug 2020 12:14:10 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5513B20657; Mon, 17 Aug 2020 16:13:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597680837; bh=QThXa8R90gwPqc9NmLyMGfrCPwZTks6xICMX/6G+Z8M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mET30cobCqnqdSwOnjt8kPCGRwMI98fmB9vEE5d7606sQft2lKCj9iIjgT6ct3+ck 8DXo6Y7emstGynopsTd0EgaXHZSwglvabQawd6L09nz/rs8hKr5XELSJyvq2auutCR 1w1MazSk+pxhLfCbGH+uSdyQvFK0fBErge0SsmIU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bjorn Helgaas , Xiang Zheng , Heyi Guo , Biaoxiang Ye , Sasha Levin Subject: [PATCH 4.19 078/168] PCI: Fix pci_cfg_wait queue locking problem Date: Mon, 17 Aug 2020 17:16:49 +0200 Message-Id: <20200817143737.609130037@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200817143733.692105228@linuxfoundation.org> References: <20200817143733.692105228@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bjorn Helgaas [ Upstream commit 2a7e32d0547f41c5ce244f84cf5d6ca7fccee7eb ] The pci_cfg_wait queue is used to prevent user-space config accesses to devices while they are recovering from reset. Previously we used these operations on pci_cfg_wait: __add_wait_queue(&pci_cfg_wait, ...) __remove_wait_queue(&pci_cfg_wait, ...) wake_up_all(&pci_cfg_wait) The wake_up acquires the wait queue lock, but the add and remove do not. Originally these were all protected by the pci_lock, but cdcb33f98244 ("PCI: Avoid possible deadlock on pci_lock and p->pi_lock"), moved wake_up_all() outside pci_lock, so it could race with add/remove operations, which caused occasional kernel panics, e.g., during vfio-pci hotplug/unplug testing: Unable to handle kernel read from unreadable memory at virtual address ffff802dac469000 Resolve this by using wait_event() instead of __add_wait_queue() and __remove_wait_queue(). The wait queue lock is held by both wait_event() and wake_up_all(), so it provides mutual exclusion. Fixes: cdcb33f98244 ("PCI: Avoid possible deadlock on pci_lock and p->pi_lock") Link: https://lore.kernel.org/linux-pci/79827f2f-9b43-4411-1376-b9063b67aee3@huawei.com/T/#u Based-on: https://lore.kernel.org/linux-pci/20191210031527.40136-1-zhengxiang9@huawei.com/ Based-on-patch-by: Xiang Zheng Signed-off-by: Bjorn Helgaas Tested-by: Xiang Zheng Cc: Heyi Guo Cc: Biaoxiang Ye Signed-off-by: Sasha Levin --- drivers/pci/access.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/pci/access.c b/drivers/pci/access.c index a3ad2fe185b9c..3c8ffd62dc006 100644 --- a/drivers/pci/access.c +++ b/drivers/pci/access.c @@ -204,17 +204,13 @@ EXPORT_SYMBOL(pci_bus_set_ops); static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait); static noinline void pci_wait_cfg(struct pci_dev *dev) + __must_hold(&pci_lock) { - DECLARE_WAITQUEUE(wait, current); - - __add_wait_queue(&pci_cfg_wait, &wait); do { - set_current_state(TASK_UNINTERRUPTIBLE); raw_spin_unlock_irq(&pci_lock); - schedule(); + wait_event(pci_cfg_wait, !dev->block_cfg_access); raw_spin_lock_irq(&pci_lock); } while (dev->block_cfg_access); - __remove_wait_queue(&pci_cfg_wait, &wait); } /* Returns 0 on success, negative values indicate error. */ -- 2.25.1