From patchwork Fri Nov 22 10:26:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 180017 Delivered-To: patch@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp60586ilf; Fri, 22 Nov 2019 03:11:42 -0800 (PST) X-Google-Smtp-Source: APXvYqw0tlppteLDjChWIRpo8YA0XaeRJmGj/TjWY5034WiPxku7/NKmvrjGC3NRngTOewmB75xt X-Received: by 2002:a05:6402:1543:: with SMTP id p3mr356922edx.304.1574421102187; Fri, 22 Nov 2019 03:11:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574421102; cv=none; d=google.com; s=arc-20160816; b=Ky2c5bmADA3lRmf8PkcA6MDvqmL6ESGJNrgFsTSSSoLDY0ZcTux8t6sV0PxjdVRDJa JqZqmly/uxRTbgARRz028n2XgLbFi1vnIBlEIWz2ygOaCaULrCtgqTTyTtEyr/+24e2h mmXGDypBR2l7FblKiucEXyMkVBSbinuFo8deDreK9ocrNrTQLcVkttTcpiHmk/rEegPl n1gAHeph0TuCH7S0HIxHO+EaUQQW/go5qdiW9qoZuMXkMIaHj9/3hSXtN/A5dr5cBB7G Zu2SRjFplke20mIt3XZJTDnp/6N+c1XKPL8YFXg2ZnDrop5v1f8aLJUT7AnWNq/8PESX A8pQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sgzwA2ud0aFMcLu6Lazkg0rW9dYEzUTwpTLJ+RkVfKk=; b=mcDzYC7r6tJcJZlblA4CuPv2qGfqDGeTeakY0nCerYaRsQE3eJeI8QvexlwIKlqWLS iteoLX18C7+pgqUEJbojgPx+t+/5OilDAvum8H06kFIeJYcJAHCquTuID9X+yx4OjZNr 0kpzTzkkOGTnlF/1JboY+frX6T3be04n6ulYiWjM0lJzGue14yerx/+pEgs6wqAp88P3 LXOzfMziSiwxFx3SsatH5z0OaOhQGl7ldey9NKJ/oXsac4/sD5MJgctW8ha9oZ255nsw yEdVgzqs0RxWumy16yZVmbNMJcYfuSygKGYYTaBp/HNPqyCnF9cbEa/dI6ZxXjAdqJ/g AAZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2FEWZjyj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j6si3794744ejb.419.2019.11.22.03.11.41; Fri, 22 Nov 2019 03:11:42 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2FEWZjyj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730898AbfKVK7V (ORCPT + 26 others); Fri, 22 Nov 2019 05:59:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:50146 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730637AbfKVK7R (ORCPT ); Fri, 22 Nov 2019 05:59:17 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3972C20706; Fri, 22 Nov 2019 10:59:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1574420356; bh=iOCWkkQyNZ9HFAZK0cqFlOadkurC8YTumm+nF+4sP6Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2FEWZjyj+SNARVnVXICeEUnQ+ERB3V+T6Au0izBmeatzTFyiwfr//UlKiEX0eB32J qXcYRVFAOTlCcxXXZ5stTA9JgSsnFSxixQHOTDIL0bmxD+0gubPRKA6ZLcyaCAkz6q bSrxsDeYR7chZZl6NlrjPW2sVRS6hhRhfOBEHeqs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhen Lei , Will Deacon , Sasha Levin Subject: [PATCH 4.19 035/220] iommu/arm-smmu-v3: Fix unexpected CMD_SYNC timeout Date: Fri, 22 Nov 2019 11:26:40 +0100 Message-Id: <20191122100914.880014065@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191122100912.732983531@linuxfoundation.org> References: <20191122100912.732983531@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhen Lei [ Upstream commit 0f02477d16980938a84aba8688a4e3a303306116 ] The condition break condition of: (int)(VAL - sync_idx) >= 0 in the __arm_smmu_sync_poll_msi() polling loop requires that sync_idx must be increased monotonically according to the sequence of the CMDs in the cmdq. However, since the msidata is populated using atomic_inc_return_relaxed() before taking the command-queue spinlock, then the following scenario can occur: CPU0 CPU1 msidata=0 msidata=1 insert cmd1 insert cmd0 smmu execute cmd1 smmu execute cmd0 poll timeout, because msidata=1 is overridden by cmd0, that means VAL=0, sync_idx=1. This is not a functional problem, since the caller will eventually either timeout or exit due to another CMD_SYNC, however it's clearly not what the code is supposed to be doing. Fix it, by incrementing the sequence count with the command-queue lock held, allowing us to drop the atomic operations altogether. Signed-off-by: Zhen Lei [will: dropped the specialised cmd building routine for now] Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- drivers/iommu/arm-smmu-v3.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) -- 2.20.1 diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c index 40fbf20d69e5a..2ab7100bcff12 100644 --- a/drivers/iommu/arm-smmu-v3.c +++ b/drivers/iommu/arm-smmu-v3.c @@ -567,7 +567,7 @@ struct arm_smmu_device { int gerr_irq; int combined_irq; - atomic_t sync_nr; + u32 sync_nr; unsigned long ias; /* IPA */ unsigned long oas; /* PA */ @@ -964,14 +964,13 @@ static int __arm_smmu_cmdq_issue_sync_msi(struct arm_smmu_device *smmu) struct arm_smmu_cmdq_ent ent = { .opcode = CMDQ_OP_CMD_SYNC, .sync = { - .msidata = atomic_inc_return_relaxed(&smmu->sync_nr), .msiaddr = virt_to_phys(&smmu->sync_count), }, }; - arm_smmu_cmdq_build_cmd(cmd, &ent); - spin_lock_irqsave(&smmu->cmdq.lock, flags); + ent.sync.msidata = ++smmu->sync_nr; + arm_smmu_cmdq_build_cmd(cmd, &ent); arm_smmu_cmdq_insert_cmd(smmu, cmd); spin_unlock_irqrestore(&smmu->cmdq.lock, flags); @@ -2196,7 +2195,6 @@ static int arm_smmu_init_structures(struct arm_smmu_device *smmu) { int ret; - atomic_set(&smmu->sync_nr, 0); ret = arm_smmu_init_queues(smmu); if (ret) return ret;