From nobody Fri Dec 19 18:43:37 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEFC6768E1 for ; Mon, 2 Sep 2024 02:31:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725244297; cv=none; b=nZFovHQhfxNy0lfEExdPY1ygTGHb5kYpFreGD/lYqpZ32Ft6nQAEC6XTq3yB1nAtuLbOdqDtnsAObVrWl6iJQWK+UMoeaVwcMNVrc8DKz9Yi4pFoZstsNN91mkWk83TEg0dGlT11dDvRrnfmEoICyEgVKon/z3GjSsipJON42q4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725244297; c=relaxed/simple; bh=JAHOTQH4PiZazv2tg8j+ODrfqTu/cLl55/IpDQrDQD8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=d+CggYsnf4+n585DjeP82jhv2CF+lrUHMYZiBIAi1wjnGqERVwip2gaHt/Jd+pZLwNDuFMxIpMJZj2G5D7HwRMHCzC+biSOzpuNBHkZjvu0qEwjbTv39z6+C1uPafFde1fCLLYyeunEeodBVDS2pDXK2QXM/jTPRYCNfO3BDN2M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=IIFi8Zac; arc=none smtp.client-ip=192.198.163.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="IIFi8Zac" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725244296; x=1756780296; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JAHOTQH4PiZazv2tg8j+ODrfqTu/cLl55/IpDQrDQD8=; b=IIFi8ZacrwohrhgGRVRpPHXyENZ8rjbJU7MPwGnAYNO57viqtkb08ndE lO9qUPQzBznOU17FGXJ/NVOb4VOBKISYBITER+Ynft9pFW8wN4J+7neoW a3kC949m5ynkATG+h8LsldIlbquefd1qwwJ1qWUGFa4sFOykEoaTt1r2u BZoahXmz7OkTb+D7e0ujiQbxE/kZcZfPynPjq0wghsoGXDZYpKDlNY59J mTCj/bPFt44gC1ezVVZQ6YocY80XICeH+0wzMprN4VeE4Yd1+i2XCV+de sQQ5izRyVmOFEqTxeEuyFS+rvXq9cqHoBxrMJKSF5VbZ0pF8F5pRwgh2I Q==; X-CSE-ConnectionGUID: Le81Iso0REOCCf8SP2HW1g== X-CSE-MsgGUID: sPguVFELS5+lOEqsLUCxfg== X-IronPort-AV: E=McAfee;i="6700,10204,11182"; a="23994319" X-IronPort-AV: E=Sophos;i="6.10,195,1719903600"; d="scan'208";a="23994319" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2024 19:31:35 -0700 X-CSE-ConnectionGUID: LIYITmiyQ4Krj157cBc4kA== X-CSE-MsgGUID: CNIssNR0TlqE6y8Dt7EolA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,195,1719903600"; d="scan'208";a="69359334" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orviesa004.jf.intel.com with ESMTP; 01 Sep 2024 19:31:33 -0700 From: Lu Baolu To: Joerg Roedel , Will Deacon Cc: Tina Zhang , Sanjay K Kumar , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH 08/14] iommu/vt-d: Fix potential lockup if qi_submit_sync called with 0 count Date: Mon, 2 Sep 2024 10:27:18 +0800 Message-Id: <20240902022724.67059-9-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240902022724.67059-1-baolu.lu@linux.intel.com> References: <20240902022724.67059-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sanjay K Kumar If qi_submit_sync() is invoked with 0 invalidation descriptors (for instance, for DMA draining purposes), we can run into a bug where a submitting thread fails to detect the completion of invalidation_wait. Subsequently, this led to a soft lockup. Currently, there is no impact by this bug on the existing users because no callers are submitting invalidations with 0 descriptors. This fix will enable future users (such as DMA drain) calling qi_submit_sync() with 0 count. Suppose thread T1 invokes qi_submit_sync() with non-zero descriptors, while concurrently, thread T2 calls qi_submit_sync() with zero descriptors. Both threads then enter a while loop, waiting for their respective descriptors to complete. T1 detects its completion (i.e., T1's invalidation_wait status changes to QI_DONE by HW) and proceeds to call reclaim_free_desc() to reclaim all descriptors, potentially including adjacent ones of other threads that are also marked as QI_DONE. During this time, while T2 is waiting to acquire the qi->q_lock, the IOMMU hardware may complete the invalidation for T2, setting its status to QI_DONE. However, if T1's execution of reclaim_free_desc() frees T2's invalidation_wait descriptor and changes its status to QI_FREE, T2 will not observe the QI_DONE status for its invalidation_wait and will indefinitely remain stuck. This soft lockup does not occur when only non-zero descriptors are submitted.In such cases, invalidation descriptors are interspersed among wait descriptors with the status QI_IN_USE, acting as barriers. These barriers prevent the reclaim code from mistakenly freeing descriptors belonging to other submitters. Considered the following example timeline: T1 T2 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D ID1 WD1 while(WD1!=3DQI_DONE) unlock lock WD1=3DQI_DONE* WD2 while(WD2!=3DQI_DONE) unlock lock WD1=3D=3DQI_DONE? ID1=3DQI_DONE WD2=3DDONE* reclaim() ID1=3DFREE WD1=3DFREE WD2=3DFREE unlock soft lockup! T2 never sees QI_DONE in WD2 Where: ID =3D invalidation descriptor WD =3D wait descriptor * Written by hardware The root of the problem is that the descriptor status QI_DONE flag is used for two conflicting purposes: 1. signal a descriptor is ready for reclaim (to be freed) 2. signal by the hardware that a wait descriptor is complete The solution (in this patch) is state separation by using QI_FREE flag for #1. Once a thread's invalidation descriptors are complete, their status would be set to QI_FREE. The reclaim_free_desc() function would then only free descriptors marked as QI_FREE instead of those marked as QI_DONE. This change ensures that T2 (from the previous example) will correctly observe the completion of its invalidation_wait (marked as QI_DONE). Signed-off-by: Sanjay K Kumar Signed-off-by: Jacob Pan Reviewed-by: Kevin Tian Link: https://lore.kernel.org/r/20240728210059.1964602-1-jacob.jun.pan@linu= x.intel.com Signed-off-by: Lu Baolu --- drivers/iommu/intel/dmar.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c index 1c8d3141cb55..01e157d89a16 100644 --- a/drivers/iommu/intel/dmar.c +++ b/drivers/iommu/intel/dmar.c @@ -1204,9 +1204,7 @@ static void free_iommu(struct intel_iommu *iommu) */ static inline void reclaim_free_desc(struct q_inval *qi) { - while (qi->desc_status[qi->free_tail] =3D=3D QI_DONE || - qi->desc_status[qi->free_tail] =3D=3D QI_ABORT) { - qi->desc_status[qi->free_tail] =3D QI_FREE; + while (qi->desc_status[qi->free_tail] =3D=3D QI_FREE && qi->free_tail != =3D qi->free_head) { qi->free_tail =3D (qi->free_tail + 1) % QI_LENGTH; qi->free_cnt++; } @@ -1463,8 +1461,16 @@ int qi_submit_sync(struct intel_iommu *iommu, struct= qi_desc *desc, raw_spin_lock(&qi->q_lock); } =20 - for (i =3D 0; i < count; i++) - qi->desc_status[(index + i) % QI_LENGTH] =3D QI_DONE; + /* + * The reclaim code can free descriptors from multiple submissions + * starting from the tail of the queue. When count =3D=3D 0, the + * status of the standalone wait descriptor at the tail of the queue + * must be set to QI_FREE to allow the reclaim code to proceed. + * It is also possible that descriptors from one of the previous + * submissions has to be reclaimed by a subsequent submission. + */ + for (i =3D 0; i <=3D count; i++) + qi->desc_status[(index + i) % QI_LENGTH] =3D QI_FREE; =20 reclaim_free_desc(qi); raw_spin_unlock_irqrestore(&qi->q_lock, flags); --=20 2.34.1