Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1626473ybl; Sat, 24 Aug 2019 00:59:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqxq6w8R5pvuxxH5QYOYmryJil2kbMpJB1P/62YwiTc9BGaWj9ZD3r1rOBvXz+5g9X+v4+qH X-Received: by 2002:a17:902:bb96:: with SMTP id m22mr9189431pls.158.1566633544177; Sat, 24 Aug 2019 00:59:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566633544; cv=none; d=google.com; s=arc-20160816; b=p5YOi8QuQu5U1TPJEYAAsl3fbvC4jOfBiDNL0ecbwMFACIwrWLzXGMk3cDpmTP8MIS EAYpNx6ZRKXci+oE77/S9++UBypHAHnzoamlw2d2uAYXf0iYISYxNOU40YTeaabvBNR+ EkRyPS9zs4B8mdyb5nDnM9lydsmzX94G8V44Vg686SAPJQKI7KAZg6GBs4tLVQaJWXDz eRVGdozyKoHdcbCZ+hQ7GTb6nPdmrnDR2tjp1K+ICOs5jLF0P0Mc6EA+H5woBgFJeL8A AWHTQhvOydU0yIjftJY7O7stMaE/RpXSycs0Vnjkm7v6tvDwwP77xaipVz5XrnV+bhG4 FgRw== 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:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=Rt7wtYCc1s5tor1o81jIoZmh+z8hgaSPweyus4OLaDI=; b=UF9r+b/iiyltkE0Me90XgLvvKftZO8qcIOdyVO/udsmIOehcOTvGuTXiWPKRRDuScm b70Un3glRSZraVuXm8G6NilnlflnlBezuRp/y6lPSOU9RGDxc6I9MfOxklqek9zqGfJh cF2oLQMbv+kuGgzRari3nRlrW9DCOdW4JVe0hq5bzdWzuaZWa48Vr7ceKtQLzO8wPK/M FmoaqyHQ45BtGpxzw+zcsoNV7DdS60bXa+3RawPbynB6qSO0Iko/G3yK7uOXmRu3/mBc iRDS3HE8fErVDE3zZ3XCx6JoJBVExmQRKJbgh0d9BYyO9wS3j7a4uYfoJbQDlRNDONPG jnvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tcd-ie.20150623.gappssmtp.com header.s=20150623 header.b=K3z+flYV; 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 a2si4500158pls.175.2019.08.24.00.58.36; Sat, 24 Aug 2019 00:59:04 -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=@tcd-ie.20150623.gappssmtp.com header.s=20150623 header.b=K3z+flYV; 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 S1726723AbfHXH5M (ORCPT + 99 others); Sat, 24 Aug 2019 03:57:12 -0400 Received: from mail-io1-f68.google.com ([209.85.166.68]:40276 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725974AbfHXH5M (ORCPT ); Sat, 24 Aug 2019 03:57:12 -0400 Received: by mail-io1-f68.google.com with SMTP id t6so25553391ios.7 for ; Sat, 24 Aug 2019 00:57:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tcd-ie.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Rt7wtYCc1s5tor1o81jIoZmh+z8hgaSPweyus4OLaDI=; b=K3z+flYVSNNSWoTBFmevj6fYPEM2G+divlFtL3oFGJ/e/x/iInhDr1dHK6vOFA/Sbp 0UBZ0ILyiMLYwgPhEtNbPrCfbqGmZtE/iabl81cCpUXQwu/WVEpKa5tCim0w1eCIh71t eInx/aknxqQIs4gIxsdejGNRIx8xP+U2vTrq8ZHY1WRqCjxz80WNyH+rIwQ2mT6JfE1B QwqIwtiPieUU8kHxEfuWEcHR8GIRvzO2XbMzcLUUBoaBKhzkgm+6/ek9kkwQndKDRlwN Bo6V5oJZr/cP6fVAv0g7HenkS6s1hcn7PjRTIxgVGyzPH3mpyl52vEoa/RUroG5wsa33 XzSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Rt7wtYCc1s5tor1o81jIoZmh+z8hgaSPweyus4OLaDI=; b=A/72K2chnqCF22kUAHvCXBg0NilUaRy/HoAlzHuJI6vHQfhm5BscBybgorIeKChpT+ 2xoOReNI4IlfjHNCX9Ag8dRbn5jryKxI0Jh71KgXpzWv0NrgPBI2iNBGHx+Nd7Oo0Aor CgTXgAjDhume5bOjjg9jqWaN90LIK646ODQIabsW9SZ6KfjNm5taAlWgD7itUXMCY71V JCYhgWeJSkoBYDSUzjYwt5NRFeP++v7oeti3F6A3y6Jziq8C4h9K5EEALAUnKLizCNv7 J+MJA17XwY//UQhnTEuy+hZuKjoyqUxscJWUU5+7+0ApkL9VcJMGKJdvNjve1mfCNzoz VRAw== X-Gm-Message-State: APjAAAXIxySkuVaP/LLfKsGm8OlAqDYbvRtkFpQYC8k6vkxvcqf9UpCU NeESRyk/J6rJfA+HNrn8+Krphnr6xHyAXZj2y5iqPQ== X-Received: by 2002:a6b:b8c4:: with SMTP id i187mr12749931iof.102.1566633430565; Sat, 24 Aug 2019 00:57:10 -0700 (PDT) MIME-Version: 1.0 References: <20190815110944.3579-1-murphyt7@tcd.ie> <20190815110944.3579-2-murphyt7@tcd.ie> <20190820094143.GA24154@infradead.org> In-Reply-To: <20190820094143.GA24154@infradead.org> From: Tom Murphy Date: Sat, 24 Aug 2019 08:56:59 +0100 Message-ID: Subject: Re: [PATCH V5 1/5] iommu/amd: Remove unnecessary locking from AMD iommu driver To: Christoph Hellwig Cc: iommu@lists.linux-foundation.org, Heiko Stuebner , virtualization@lists.linux-foundation.org, linux-tegra@vger.kernel.org, Thierry Reding , Will Deacon , Jean-Philippe Brucker , linux-samsung-soc@vger.kernel.org, Krzysztof Kozlowski , Jonathan Hunter , linux-rockchip@lists.infradead.org, Andy Gross , Gerald Schaefer , linux-s390@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mediatek@lists.infradead.org, Matthias Brugger , linux-arm-kernel@lists.infradead.org, David Woodhouse , Linux Kernel Mailing List , Kukjin Kim , Robin Murphy Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org >I have to admit I don't fully understand the concurrency issues here, but = neither do I understand what the mutex you removed might have helped to sta= rt with. Each range in the page tables is protected by the IO virtual address allocator. The iommu driver allocates an IOVA range using locks before it writes to a page table range. The IOVA allocator acts like a lock on a specific range of the page tables. So we can handle most of the concurrency issues in the IOVA allocator and avoid locking while writing to a range in the page tables. However because we have multiple levels of pages we might have to allocate a middle page (a PMD) which covers more than the IOVA range we have allocated. To solve this we could use locks: //pseudo code lock_page_table() if (we need to allocate middle pages) { //allocate the page //set the PMD value } unlock_page_table() but we can actually avoid having any locking by doing the following: //pseudo code if (we need to allocate middle pages) { //allocate the page //cmpxchg64 to set the PMD if it wasn't already set since we last checked if (the PMD was set while since we last checked) //free the page we just allocated } In this case we can end up doing a pointless page allocate and free but it's worth it to avoid using locks You can see this in the intel iommu code here: https://github.com/torvalds/linux/blob/9140d8bdd4c5a04abe181bb300378355d569= 90a4/drivers/iommu/intel-iommu.c#L904 >what the mutex you removed might have helped to start with. The mutex I removed is arguably completely useless. In the dma ops path we handle the IOVA allocations in the driver so we can be sure a certain range is protected by the IOVA allocator. Because the iommu ops path doesn't handle the IOVA allocations it seems reasonable to lock the page tables to avoid two writers writing to the same range at the same time. Without the lock it's complete chaos and all writers can be writing to the same range at the same time resulting in complete garbage. BUT the locking doesn't actually make any real difference. Even with locking we still have a race condition if two writers want to write to the same range at the same time, the race is just whoever gets the lock first, we still can't be sure what the result will be. So the result is still garbage, just slightly more usable garbage because at least the range is correct for one writer. It just makes no sense to ever have two writers writing to the same range and adding a lock doesn't fix that. Already the Intel iommu ops path doesn't use locks for it's page table so this isn't a new idea I'm just doing the same for the AMD iommu driver Does all that make sense? On Tue, 20 Aug 2019 at 10:41, Christoph Hellwig wrote: > > On Thu, Aug 15, 2019 at 12:09:39PM +0100, Tom Murphy wrote: > > We can remove the mutex lock from amd_iommu_map and amd_iommu_unmap. > > iommu_map doesn=E2=80=99t lock while mapping and so no two calls should= touch > > the same iova range. The AMD driver already handles the page table page > > allocations without locks so we can safely remove the locks. > > I've been looking over the code and trying to understand how the > synchronization works. I gues we the cmpxchg64 in free_clear_pte > is the important point here? I have to admit I don't fully understand > the concurrency issues here, but neither do I understand what the > mutex you removed might have helped to start with.