Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3560847pxb; Mon, 24 Jan 2022 12:14:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJzd2PUaYne+gbVRlvTiH6m9Dm7qrw4Z/dhO5yMFDxkblOP8FySJG9HI3dFv5e6MoyaG4de4 X-Received: by 2002:a63:6f4d:: with SMTP id k74mr13161655pgc.208.1643055277223; Mon, 24 Jan 2022 12:14:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643055277; cv=none; d=google.com; s=arc-20160816; b=BIYC+TwpWgOKI/mOF4x5HL5P2nZsAMdHPohLc51LTKoH/4H1jdduBoXw3/Kxu7Bd5Q RmvsxAuSxkNgsDxUcqXLs9npEmhtGOTsKU3SIhSrLvDXFl6YdTh5mosUi9ZIIdxTqr0f lhfLwmuL2M5zk6i0a20GtcgxNMbeMgHnQUbvSsrTrUdHeQRrIjOTo618GMpUPbs/ZByY /C7l0juUzk+Uk39a/TczzD3OClQTmDx5WsU4OLmtNkt2+Pt3pyzNqs5zDjgBKz8x0LWz bpXVyCTWW5aaaDWIt2hPuVzhIAl2G7AfBsPs08jdb+AszeEse4VSPE+nJDgU8dDhSdNP HkRQ== 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=FrsbHwVk83mLWF8p+FTy4Rm0t/fXl5GFnc6wCMYxCW4=; b=bnNWnXx4Dwgpk1RHLi7Z2j9vJUSR/UZECVTKFVuHCWmflDeOU4SkFVQZXiTQac+RH3 JeyK5mOYyfcucw+oINXIaQLdKC7OEKN9ziPLX0WiDjEmMAqZhhguFlxjUU6tqSQQdtGF ZOesBRMIqCjPVze3NFePg2M1NoA5j6eFRspo3qJrZ6RTv4uZnxeeaJCnyjHhuwiM2xv7 eam4+RtKSuH4TbaRlGjJLxZa7dmUNrXQofpql3s5Va9Dv/5N+xkZsheMeG8pCP+vCWYG qYqAr0rdDvMNmwY1EFgCo69BIudT9sGlfxbSiQQ9yLfenTR5GMXh31OMvj9OzI8WEcTI vkXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=laAhjQCy; 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 be11si13792370plb.94.2022.01.24.12.14.24; Mon, 24 Jan 2022 12:14:37 -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=laAhjQCy; 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 S1345582AbiAXTRn (ORCPT + 99 others); Mon, 24 Jan 2022 14:17:43 -0500 Received: from ams.source.kernel.org ([145.40.68.75]:34358 "EHLO ams.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345654AbiAXTGY (ORCPT ); Mon, 24 Jan 2022 14:06:24 -0500 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 ams.source.kernel.org (Postfix) with ESMTPS id E94EAB81239; Mon, 24 Jan 2022 19:06:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1AC80C340E5; Mon, 24 Jan 2022 19:06:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643051178; bh=RtIo5JOYlq8YpCO9arDpjnQVZP64mqVCmyYXczOJcOs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=laAhjQCyDu7VdK3of0ECehIzhzjoN2P9sXRZaZBS8PTowl7q4NKKBSVdxNWWyF0mf cWH6H9YbVJTxMoCwgA+q95H1SVlO6tKkKVwZPmDpePcgWSEvvYGriZtrZzwLHWjPYT 4IFvrQgGw+7UUGJ/KjSKk0SkQI3wQDWwGt/LHY6c= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, John Garry , Xiongfeng Wang , Robin Murphy , Joerg Roedel , Sasha Levin Subject: [PATCH 4.14 080/186] iommu/iova: Fix race between FQ timeout and teardown Date: Mon, 24 Jan 2022 19:42:35 +0100 Message-Id: <20220124183939.695751989@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183937.101330125@linuxfoundation.org> References: <20220124183937.101330125@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: Xiongfeng Wang [ Upstream commit d7061627d701c90e1cac1e1e60c45292f64f3470 ] It turns out to be possible for hotplugging out a device to reach the stage of tearing down the device's group and default domain before the domain's flush queue has drained naturally. At this point, it is then possible for the timeout to expire just before the del_timer() call in free_iova_flush_queue(), such that we then proceed to free the FQ resources while fq_flush_timeout() is still accessing them on another CPU. Crashes due to this have been observed in the wild while removing NVMe devices. Close the race window by using del_timer_sync() to safely wait for any active timeout handler to finish before we start to free things. We already avoid any locking in free_iova_flush_queue() since the FQ is supposed to be inactive anyway, so the potential deadlock scenario does not apply. Fixes: 9a005a800ae8 ("iommu/iova: Add flush timer") Reviewed-by: John Garry Signed-off-by: Xiongfeng Wang [ rm: rewrite commit message ] Signed-off-by: Robin Murphy Link: https://lore.kernel.org/r/0a365e5b07f14b7344677ad6a9a734966a8422ce.1639753638.git.robin.murphy@arm.com Signed-off-by: Joerg Roedel Signed-off-by: Sasha Levin --- drivers/iommu/iova.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index 2c97d2552c5bd..ebee9e191b3ee 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -68,8 +68,7 @@ static void free_iova_flush_queue(struct iova_domain *iovad) if (!has_iova_flush_queue(iovad)) return; - if (timer_pending(&iovad->fq_timer)) - del_timer(&iovad->fq_timer); + del_timer_sync(&iovad->fq_timer); fq_destroy_all_entries(iovad); -- 2.34.1