Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3975739pxb; Tue, 25 Jan 2022 00:25:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJwEEH+yCecEEFbcjQbs0haRBMzfLfVNApRBpZVXPZz/ofxXbL4hQ7ju6Ajn48cKvQ9CKfSc X-Received: by 2002:a17:907:d8e:: with SMTP id go14mr10949006ejc.616.1643099152512; Tue, 25 Jan 2022 00:25:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643099152; cv=none; d=google.com; s=arc-20160816; b=ud2V5qtJEpMjEZ8B7R7fJrFsPdvHnEbHOPtukj/AMuE0vu+AfWY90VJgsUMAJpLS5h RsVa1gpBwbLkU0gWpoSi/aeiHtCQ2mE+vPXWwvqAiUpvosqO+FUVL6xFfP6tmmJn2MsF d+1kREghDpUcGzUSbHRCi7eFT1mkzyUHtWbZu3Tik2wkBROrZmjgbpMujXdFc2LLDUbk jVQaACvWngUZrgLydDI5lzvX4d96ccNtzzPZPnMWT/UZz4I5NnkIXeGNWSZgYoa0Fit+ MYA4sW0Pl8Yzpd4FmwgZkbHWCWaHLS7g089vuqXZ1PyB6CWgjImHHB23pYpNmW0vD7dz lcHQ== 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=fs36Yzz4vsESU5yv2DQzbR1fLx6nUnoMo4nQXm6l0tE=; b=Ec3ipu9M/6r+JURhnK/pmHnhiOeWD9FdNXF+iDog0xLiG8t9hud7yErqKSO1c4Awvc lMx8VBRmbAilmV3J4Ut3HCiSlYg9OXMCfT5NjsXT+ShA2HkxlLLlj0zH7a88ifuewHSa vQiz6y7X7fmUSNUp1ee0GeK1Qcp2bA61KJKy30J3yfC6up4f8sg8r1de4jE7MSSR5d6K VB7ayeqLGEFA84UoPj7Tu0BYHbNNUpWzrEyPSlUoymGVuXUyqeef5Y5iDFGOJkc8vtE/ XEWMkLDsKHxHeWFFLPavFg0J+wdVd+rODsK0McU3BsMOYQcuzfj583SitgUX9+L6VTOj lzOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gDbAAkjp; 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 11si8051853eje.487.2022.01.25.00.25.27; Tue, 25 Jan 2022 00:25:52 -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=gDbAAkjp; 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 S3421631AbiAYC1W (ORCPT + 99 others); Mon, 24 Jan 2022 21:27:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377302AbiAXUFQ (ORCPT ); Mon, 24 Jan 2022 15:05:16 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5876DC06136A; Mon, 24 Jan 2022 11:31:29 -0800 (PST) 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 E8255614BE; Mon, 24 Jan 2022 19:31:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7441C340E5; Mon, 24 Jan 2022 19:31:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643052688; bh=yI7KrNG5Of/LDobzB0QUI4SIKQIc7jK6J4lDQYu0n28=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gDbAAkjpKQ88PXM1mqh02B/Jidh6xHq9m0cczWRJkhNpSqQ7VQCv80buhE/msYS78 +rumpcLU9cBAbkVQv+1Xqv+YUeLmfel6xkNH2pIaKF4s2wEoEJdbuxCc6AD1a5kj4J Vd9DFQR14WANqlQEQLU1W3gnQFNknp4zzFVRx0Ss= 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 5.4 143/320] iommu/iova: Fix race between FQ timeout and teardown Date: Mon, 24 Jan 2022 19:42:07 +0100 Message-Id: <20220124183958.504464068@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183953.750177707@linuxfoundation.org> References: <20220124183953.750177707@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 612cbf668adf8..906582a21124d 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -64,8 +64,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