From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id E3466803F7 for ; Tue, 10 Nov 2026 19:20:04 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AAJInrl018400; Tue, 10 Nov 2026 22:18:49 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AAJInrl018400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=default; t=1794338331; bh=17ELMak9WAQ+3r2U3snJ0f7E4WAi/U/TVtccbOihtU0=; h=From:To:Date:In-Reply-To:References:Cc:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe; b=Ag6GdbBFy+RUbeecgBCcfde+Fvhy0EnDUlLFBkTHXTZ2z7tfhtslvQlmQsIQjDHTU aHWcVp0i0sBQj+6c8t0Wc+oXUcds1n2XBydoyJaJsUC+HOEsyR47Z3wK5xOdEDaHc7 XnwrbwyugMGGFIGL+JntKhxKU4WVJ+7gmseYDQhU2ENMF0D6UPr4y2Wh3gJc17gJml eC4k3xqEfFctIvSzHbyeiCgDNzqCyAMbC38wsuouNkPdRkxVvJdVbSRSWVaWTKlklA qw9GNwT3zprLXqCSCyn6hbs4JzopVvgxYHHAnsoiP7syNB77Q5MXTJJBrjRWBInLUx MfQTNP9Ne8AJQ== Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AAJIhm6018380 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Tue, 10 Nov 2026 22:18:43 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AAJIhm6018380 Authentication-Results: mail.openvz.org; dkim=pass (2048-bit key) header.d=openvz.org header.i=@openvz.org header.b="KAD1yPWL" Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-48e0461e040so1877952f8f.0 for ; Tue, 10 Nov 2026 11:18:43 -0800 (PST) X-Gm-Message-State: APuU4Z+/CKjdAzSUWptLqvDuoBrqpcevjNZ9OhiRUVbrzRh9FI408bA8 ac2qoZFzvhbzXcHDCJ8GZi5Qe8piQd5OZnKrWyneJaoZ/NUd0oA+xf4eYDX2mwg3GQTxCw0Wa3s Pcx+ZLyNKzq9Nx0bVNpUUjoFRw1mmbscq5+Dsj3a4qruogvouhZfzlg== X-Gm-Gg: AYBFou0jdnErbY6UvUKe6detYutsGXReh/TFOwZdGbFoIoDyKIdOcwLpXCgPEW4rsyI f0EkAJLwnwMccvaW1ySF+Z005nbdU7F0q8bvqVUyBixDpl1cKbtXmu6dxcD9B+IrBtoZho+uRTm 3/4Lt/Gp8kBaoRieiH0So5sZXA9rPzQ1ijsc0RaHYVegEKMlpkz9t5891cYDEx9xI9bpq/GyD/y /k6TB7rIsuBx4i5YY8UatVMElsB3Bp38MpRVknYumizF1mOyH5QSzFOOtUj7sF3m4SD4mMGeZPF doG+3Yxl5w== X-Received: by 2002:a05:600c:81c5:b0:4a1:d087:f6ab with SMTP id 5b1f17b1804b1-4ba09376cc2mr1817095e9.18.1794338323020; Tue, 10 Nov 2026 11:18:43 -0800 (PST) X-Received: by 2002:a05:600c:81c5:b0:4a1:d087:f6ab with SMTP id 5b1f17b1804b1-4ba09376cc2mr1816885e9.18.1794338322646; Tue, 10 Nov 2026 11:18:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794338322; cv=none; d=google.com; s=arc-20260327; b=mkYDxgq6RzDdALiMjOkCSsVs9FXJWuWMo0FCy4i5XYzEHk/o6gbwNRB4XDL7D8PAZq Rg+dImviG+cgn3f1OjuD/CNxW/4SbZ7EKZW+X/pJrFKQ/LMFPJnIWvWZNs/RPBtraZHS QvLZM+upWP++aKw2A/7T4WyVvG8jFvnpb0QwhLv4JwP23yJXfiPcmPxZtL31cfBxNAgF fXI5O4lBGoxlXeIF0mdQ7LpbVc7rxALongSRDxDNab0UPBgjudkH6b7gRIeeFnOnC1sJ fJa2s1vEXWKA9kvuozfs79DcB1utNuhlL+lKMJMsQ4ECwU9nC5eob7xLwZN9JvmXqrtD Im3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=yAhlEUH9fTL7zquVJTX4PqXNcShVsi/4OL4g3e/Itlc=; fh=ozrYJQ1t3QIP6s65KBq5xgJxiFM2qf+5wwn3ABTusyU=; b=DQ4XmWLHsBpMH+Luen9mrHF0WSZwayKHwnNFUYZ2/wQW0AUuRLEQTjtpdrh4+Bd09w 7u8YeBNeQFO3M5tpSITzgMzudQYMaTvvDZ34q1uNABvdGNmftwiDLb2StEltL25cF1iQ TvSt9wrDd5tnUPu4cyVaBKNkM7LMPIHdK5mTgR3n6mlzRnnmbJjFzaGTHaw7KCeV52vV /hxsIlgGrSldz45mpr+n/qH3gF3YKAURoErabG+MnGkk9jH66GHkJ1mNhhRmnSmt5WE8 S53w8Tp96QPyI3PD2UAeWqzyoP8dZMrEdF0IOGpIPpPl6TVg1Zu9dM2Y3OWZgTF6pEof a9JA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@openvz.org header.s=google header.b=KAD1yPWL; spf=softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:4f78:6441:d0f1:22af as permitted sender) smtp.mailfrom=den@openvz.org; dara=neutral header.i=@openvz.org Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id 5b1f17b1804b1-4ba01ff40ccsor8590895e9.12.2026.11.10.11.18.42 for (Google Transport Security); Tue, 10 Nov 2026 11:18:42 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:4f78:6441:d0f1:22af as permitted sender) client-ip=2a06:5b06:b600:300:4f78:6441:d0f1:22af; Authentication-Results: mx.google.com; dkim=pass header.i=@openvz.org header.s=google header.b=KAD1yPWL; spf=softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:4f78:6441:d0f1:22af as permitted sender) smtp.mailfrom=den@openvz.org; dara=neutral header.i=@openvz.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=google; t=1794338322; x=1794943122; darn=openvz.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=yAhlEUH9fTL7zquVJTX4PqXNcShVsi/4OL4g3e/Itlc=; b=KAD1yPWLJNfUihjLhUlT3kXNmo8Z3c8J77oOAYZL9TpwgVXJTvERW5Jhowfxh4FL5V Qyo8ZzzuUIlBgF2+/vZL+YhtOILFE91a6Fk3svUlO4IONzY5PB30vXQpsX6JR7kOwYhZ NkqirO0GXNcWip63pzONPM5/bKw0rXIuDIIwnweXVa64zMKPzYeas3j2/G0PZRbFOohl l8aBST1Freo74g02wmw4vW23d7CI8nSP0JAjjzm1TQhMCLEwRRsbvmWJmXzkEhnHTD1b muEDgtmP1Y4K2liTxVeNRzLgKm4fJBkxPDa+9fH6Corsv/V1PWJ8Kkw1LZtOLiciakB2 fqlA== X-Received: by 2002:a05:600c:1c0c:b0:4a2:1655:87db with SMTP id 5b1f17b1804b1-4ba091a5bcbmr3142165e9.0.1794338322173; Tue, 10 Nov 2026 11:18:42 -0800 (PST) Received: from athena.sw.ru ([2a06:5b06:b600:300:4f78:6441:d0f1:22af]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4ba01fea745sm80156815e9.10.2026.11.10.11.18.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Nov 2026 11:18:41 -0800 (PST) From: "Denis V. Lunev" To: khorenko@virtuozzo.com Date: Tue, 10 Nov 2026 20:18:36 +0100 Message-ID: <20261110191836.3000352-4-den@openvz.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261110191836.3000352-1-den@openvz.org> References: <20261110191836.3000352-1-den@openvz.org> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.1 3/3] ms/iommu/amd: Wait for completion instead of returning early in iommu_completion_wait() X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org From: Guanghui Feng need_sync is a per-IOMMU flag shared by all domains and devices behind that IOMMU. It is set whenever a command is queued with sync == true and cleared when a completion-wait (CWAIT) command is queued. However, a cleared need_sync only means that a covering CWAIT has been queued, not that all previously queued commands have actually completed in hardware. iommu_completion_wait() read need_sync locklessly and returned early when it was false. This breaks the "block until all previously queued commands have completed" contract in a multi-CPU scenario: CPU2: queue inv-B => need_sync = true CPU1: queue CWAIT(N); need_sync = false; then wait_on_sem(N) CPU2: read need_sync == false => return 0 (no wait!) CPU2 returns without waiting for any sequence number even though its inv-B may not have completed yet (CWAIT(N), queued after inv-B, has not been signaled). CPU2 then proceeds to, for example, free page-table pages while the IOMMU can still walk stale translations, opening a use-after-free window. This is a logical race in the meaning of the flag, not a memory-visibility issue, so barriers alone do not help. Fix it without losing the optimization of avoiding redundant CWAIT commands: take iommu->lock before testing need_sync, and when it is false do not return early but wait for the last allocated sequence number (cmd_sem_val). Since need_sync == false implies no sync command was queued after the last CWAIT, that CWAIT is FIFO-ordered after every not-yet-completed command, so waiting for its sequence number guarantees all prior commands (possibly queued by another CPU) have completed. The common path with pending work is unchanged and no extra hardware command is issued. Signed-off-by: Guanghui Feng Fixes: 815b33fdc279 ("x86/amd-iommu: Cleanup completion-wait handling") Reviewed-by: Vasant Hegde Signed-off-by: Will Deacon (cherry picked from commit 1e75a8255f11c81fb07e81e5029cfd75804350a0) https://virtuozzo.atlassian.net/browse/VSTOR-154424 Feature: fix ms/iommu Signed-off-by: Denis V. Lunev --- drivers/iommu/amd/iommu.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 373596fa07c0..91cf277f1293 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -1408,11 +1408,23 @@ static int iommu_completion_wait(struct amd_iommu *iommu) int ret; u64 data; - if (!iommu->need_sync) - return 0; - raw_spin_lock_irqsave(&iommu->lock, flags); + if (!iommu->need_sync) { + /* + * No command has been queued since the last completion-wait. + * A concurrent CPU may have already queued that CWAIT and + * cleared need_sync; need_sync == false only means a covering + * CWAIT is queued, not that all prior commands have completed. + * Wait for the last allocated sequence number so that any + * command queued before this call (possibly on another CPU) + * is guaranteed to have completed before returning. + */ + data = iommu->cmd_sem_val; + raw_spin_unlock_irqrestore(&iommu->lock, flags); + return wait_on_sem(iommu, data); + } + data = get_cmdsem_val(iommu); build_completion_wait(&cmd, iommu, data); @@ -1422,9 +1434,7 @@ static int iommu_completion_wait(struct amd_iommu *iommu) if (ret) return ret; - ret = wait_on_sem(iommu, data); - - return ret; + return wait_on_sem(iommu, data); } static void domain_flush_complete(struct protection_domain *domain) -- 2.53.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel