Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3603847pxb; Mon, 24 Jan 2022 13:18:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJw9Wap/C5PKASUbMUlPa/yAw2WF1zGr+0vMHx8VdFP9H1H7V+9AGElIWdTe8SzlzNGmDQ5/ X-Received: by 2002:a05:6a00:1945:b0:4c3:39cc:52c6 with SMTP id s5-20020a056a00194500b004c339cc52c6mr15280044pfk.18.1643059095943; Mon, 24 Jan 2022 13:18:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643059095; cv=none; d=google.com; s=arc-20160816; b=Qk+hz2fI1b1pkzQ3WjgOoo9To/Sm9KAEpyM2WYCtDZAr1CmDWNjC6m6w3yLRRgh61n HbHFuAt0eZDFQJMw6LYdcbmqWYFxke2LFmjZYfwB2wdCq5vrFvWMYN1H+aezmSOULYrp n5fE2MPOhfcDQEvcjIYjNJSAvLOFsBZP5/xFesll7veNyJDWD2y8mG9TlzDng1uYW4bR BgtFMrFcuu78b/dKNDF0JoMW4fkh3di2XC5qWbvVPQCKuhkjOMn0zzy6EkSpxDc8+M09 Cf1/TuseRtEKO0wnc3Ic59pJ05q1EI/MiJRePhkfRH2tK/LvSBT3OfMhYGEfAwVq+Vp3 EHsQ== 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=oKtYo1z5+NCEQyNhiCptlG756cRYvPbpvtWapXVR+7g=; b=sne5LjLcCCYofkLGeG6XJAxqwiWocB3UmXqPswBciCQT0BNw/a9eyhDTtH1MC7TPC+ Qs38FSOpR3/EqUOQslzp71plnq3FdbVRSKDpC/I4ZaenO/mnXmmjqXNAMgUiMoGV5uv+ ZD3PFPWdm+C9y0oIcUAmT0KFRgAFDhd62F3lsO2Y20PsGeqgDdbdy7bJABpJGwATNDD0 dz3toJpOjPvQZUhjLhigFze7Ud8QVf6dAxmp6yhtOZwvvkhXTbWGxzHFBQs8hZT9K7pU qsRzYnvCoVqMcR/BVgcj+gsY4xJB2riQdosgJ0AbHI4YeZ2+KvW6tCsWFjGavgzyFSvr JaHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1oHilC+k; 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 j185si942203pge.274.2022.01.24.13.18.03; Mon, 24 Jan 2022 13:18:15 -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=1oHilC+k; 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 S1442317AbiAXUyQ (ORCPT + 99 others); Mon, 24 Jan 2022 15:54:16 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:55290 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379434AbiAXUbE (ORCPT ); Mon, 24 Jan 2022 15:31:04 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 404C361531; Mon, 24 Jan 2022 20:31:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1881AC340E5; Mon, 24 Jan 2022 20:31:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643056262; bh=NArxHwZeGD7GaMmhx/LlhECXgL7KMKQlYAHAYYPDj34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1oHilC+k+FDN9vqtX2PFQNYE2h52GDNDIfZok3r8L9OAi3wsY8zO80p9huOQ/RD3T CAGpX7NXH6BwClE4hrTMPzk4417c2XRK+W7+kv7ifV/tasXu0TrIRgVX138qnZ3BMc PVlcPxCeABCTnI0rSkHwUGQtQWOWZnE7hsIi6Fxo= 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.15 425/846] iommu/iova: Fix race between FQ timeout and teardown Date: Mon, 24 Jan 2022 19:39:02 +0100 Message-Id: <20220124184115.637840607@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184100.867127425@linuxfoundation.org> References: <20220124184100.867127425@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 9e8bc802ac053..920fcc27c9a1e 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -83,8 +83,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