Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp3624858ybg; Fri, 25 Oct 2019 06:46:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqx1u9ScQtsTgVC0iqsjdnp679sHzzcpPl2tdNsrmbtMXH6TeHfVlzoC/hEIJiZslUzbdfMg X-Received: by 2002:a50:9713:: with SMTP id c19mr4081575edb.206.1572011184802; Fri, 25 Oct 2019 06:46:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572011184; cv=none; d=google.com; s=arc-20160816; b=C6GuXffY+quLBfo/HFWjTimg2nDlf17HkRHZL5foN4o5C3Qa+Csbb7RpStB+WyO0kG RXNg/9E1gQPgij4lw2Hu5oVbcFDbgkdrD8eyihrwYwhRyBjeM0GQgo+u051vtZ7kVOzp 5egEpV6P4lyBCbGeJdSV2HM7zhZpeIdZEpwa60PCBNknFutKvxff1dFk/Mz6PJ+p5KBg fHOTOug84wC57M5w1pdLmsNokFMAfPQDk0eB1A1Ti2ZLuOoQwsHVCgQiQ6V/AMFT3sVz htkvdxrSUFIvMpPFE3W7DsIhCEO/joHGFGMId5UJZ8Db72KIZBe1440ENvuDwESHvyiF nNBw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=rgR8iSUbBvZJUDYblMNNLznvyJfzdNXdD9UJJw1C60w=; b=tTplzMOSVXlzbkMCyVz9f4qE4F1ZsrGHi15Q9+GBFE8KX1DehSLy6TsWcoUMlLivlk 5+Nvofx21Kvli4MUhaUasyP8g9SpV+LEJ65vXlH4//82xTZTjLMJz6jcz0N3tn9ypkAb PkdWAJfN0ZInVDtib2anJq/Ka8QH15nLNt0hXgB4NCLKIbe1k8X2w/7vMCI8ocFe4goz 69fnEIogbvId03B4TKCXMgxzIF6UkXwiWF0eJGrT7qTV393z6XmCppDHBoOY6+tQ36VJ him9p4PDwHyT35igp4k5FJqK4vrg6h8+FHVayzMWGctVfIQ0L9M94V/qJxDcmFfKCpGe 4bbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BsCVKoAv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b9si1344919eda.115.2019.10.25.06.46.01; Fri, 25 Oct 2019 06:46:24 -0700 (PDT) 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=@redhat.com header.s=mimecast20190719 header.b=BsCVKoAv; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2502208AbfJXMyY (ORCPT + 99 others); Thu, 24 Oct 2019 08:54:24 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:30899 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729336AbfJXMyX (ORCPT ); Thu, 24 Oct 2019 08:54:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571921662; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=rgR8iSUbBvZJUDYblMNNLznvyJfzdNXdD9UJJw1C60w=; b=BsCVKoAv34cpXgvuEQ7woXJE/Tg0Pc5eNKdUrpNe64sb4ECAibVDZ2ed4SWwf23VhACFKl 03k6g+xmD/iKUQykqQPb0NjQITZcNTbz5YRB0m1mBRZF7lU2CuHQihj8EhGTWWCxri5czS Sxb2CG1Tr4f6E1pXPo2jaY/mSbrlVdI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-224-eym6RqyEMIuvi5I7I7KVNA-1; Thu, 24 Oct 2019 08:54:19 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A7A7E107AD31; Thu, 24 Oct 2019 12:54:18 +0000 (UTC) Received: from localhost.localdomain (unknown [10.43.17.109]) by smtp.corp.redhat.com (Postfix) with ESMTP id AEF305D70E; Thu, 24 Oct 2019 12:54:17 +0000 (UTC) From: Denys Vlasenko To: Tom Lendacky , Joerg Roedel Cc: Denys Vlasenko , linux-kernel@vger.kernel.org Subject: [PATCH] iommu/amd: Do not re-fetch iommu->cmd_buf_tail Date: Thu, 24 Oct 2019 14:54:10 +0200 Message-Id: <20191024125410.19224-1-dvlasenk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-MC-Unique: eym6RqyEMIuvi5I7I7KVNA-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The compiler is not smart enough to realize that iommu->cmd_buf_tail can't be modified across memcpy: 41 8b 45 74 mov 0x74(%r13),%eax # iommu->cmd_buf_tail 44 8d 78 10 lea 0x10(%rax),%r15d # +=3D sizeof(*cmd) 41 81 e7 ff 1f 00 00 and $0x1fff,%r15d # %=3D CMD_BUFFER_SIZE 49 03 45 68 add 0x68(%r13),%rax # target =3D iommu->cmd_buf += iommu->cmd_buf_tail 45 89 7d 74 mov %r15d,0x74(%r13) # store to iommu->cmd_buf_tai= l 49 8b 34 24 mov (%r12),%rsi # memcpy 49 8b 7c 24 08 mov 0x8(%r12),%rdi # memcpy 48 89 30 mov %rsi,(%rax) # memcpy 48 89 78 08 mov %rdi,0x8(%rax) # memcpy 49 8b 55 38 mov 0x38(%r13),%rdx # iommu->mmio_base 41 8b 45 74 mov 0x74(%r13),%eax # redundant load of iommu->cm= d_buf_tail ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 89 82 08 20 00 00 mov %eax,0x2008(%rdx) # writel CC: Tom Lendacky CC: Joerg Roedel CC: linux-kernel@vger.kernel.org Signed-off-by: Denys Vlasenko --- drivers/iommu/amd_iommu.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c index dd555078258c..34c497c4b0a7 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c @@ -876,17 +876,18 @@ static void copy_cmd_to_buffer(struct amd_iommu *iomm= u, =09=09=09 struct iommu_cmd *cmd) { =09u8 *target; - -=09target =3D iommu->cmd_buf + iommu->cmd_buf_tail; - -=09iommu->cmd_buf_tail +=3D sizeof(*cmd); -=09iommu->cmd_buf_tail %=3D CMD_BUFFER_SIZE; +=09u32 tail; =20 =09/* Copy command to buffer */ +=09tail =3D iommu->cmd_buf_tail; +=09target =3D iommu->cmd_buf + tail; =09memcpy(target, cmd, sizeof(*cmd)); =20 +=09tail =3D (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE; +=09iommu->cmd_buf_tail =3D tail; + =09/* Tell the IOMMU about it */ -=09writel(iommu->cmd_buf_tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); +=09writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); } =20 static void build_completion_wait(struct iommu_cmd *cmd, u64 address) --=20 2.21.0