Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3234750imu; Sat, 24 Nov 2018 00:44:07 -0800 (PST) X-Google-Smtp-Source: AFSGD/U6of4n5I2Huh0E/VUH9jWjOwcyJsT3b9rSbsLbscMCro4TMcMBg5xzA5S++XjHYmkTbYBo X-Received: by 2002:a63:5f41:: with SMTP id t62mr17206916pgb.76.1543049047225; Sat, 24 Nov 2018 00:44:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543049047; cv=none; d=google.com; s=arc-20160816; b=jcCz63PCFDWcvViujgWoc09sis/Tu0oZu7hS+FQGMg2/89+yIyGt/PbXToDFlOMC1b 87bPFYZ14d1QZ91TDs974W5ErK7aQMxSv/hf8m2pfzGQWCzwPRug1eM4PdxI48yWnA43 hhTzfZIBCrAn2v1tlRoqj8COz2QeqNFvAJWhZmpcPbakJ0pJG2MaDGmsDr9eIoBa4KgX 7gAJb9Fd+Ty8GWwWI+DxktIO08rJUeN8WEkz61692ser3ZGRmQZYZTbVe6EOqKaoXFPX UhgfcOcwpiUnFXtVoOz7JUiEQ7i7jz9OaiqubFYM5328HutnPNv4Ap9jpdnezx4kYcDd FGEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=c3xrEEUbHwKw2xJRMNjlf1Bgxy87ZXsSAJY1K9y7IBc=; b=HItXCHPbyaEk+hEeNoNp58JR0KeR6gg4Cafak2Z+icEisCWiX1IOuVppm9l7/eWBfI X+p1fNsOksEIYF7uZICDsnj2TOA1wmg2DVhC7NcibA52cM08Ul8lUBN5TRq7A4HMs9m9 pLEzUho2Qgj5DZEwEvjzzc4sUDRfb/Jxk0VAkafVj0jD5uSUCTIW40V+sqBrm0nHeBIt nLY7ftqK2uTbZ1vXSRpgcOYmT6WY8+xM0fvldE7IHQRvanLpyeBl+afcqp6lYiXXHw/K BU1XqTe+lh+//xu5BDGzzq6Q8eAFbt8WJR4S2MQd52+ve0g7bsXMuIMd7vxemYlgxEmp Hcmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=fCeSipX1; 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 s11si55584689pgi.324.2018.11.24.00.43.52; Sat, 24 Nov 2018 00:44:07 -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=@linux-foundation.org header.s=google header.b=fCeSipX1; 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 S2440546AbeKXDVS (ORCPT + 99 others); Fri, 23 Nov 2018 22:21:18 -0500 Received: from mail-lf1-f67.google.com ([209.85.167.67]:36918 "EHLO mail-lf1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2440517AbeKXDVS (ORCPT ); Fri, 23 Nov 2018 22:21:18 -0500 Received: by mail-lf1-f67.google.com with SMTP id p17so9104156lfh.4 for ; Fri, 23 Nov 2018 08:36:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=c3xrEEUbHwKw2xJRMNjlf1Bgxy87ZXsSAJY1K9y7IBc=; b=fCeSipX1LZaY1oBv/rcSHCsmT5Xatt45+mDPzeOLQboYdNDU0lwkF+2vo2pOxvgh14 pwSpSpK9ca8gYmWzUwy6DfrDvcLIe0UykxlDD33zaOBkuoYn75RIUyL8rRRc/01v5dYr WuSe/lxO/tels0bF05hv556UcJT5ZH28xRsog= 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; bh=c3xrEEUbHwKw2xJRMNjlf1Bgxy87ZXsSAJY1K9y7IBc=; b=RbIHLxI2ZIwu/Y3PDdY4/v35UXpKLQWimVdQ3JMDJB35SW9dF7uGGh/T59Er5uqguj qqcsPGgVOKvtjfjH4EfcqzHHv7P0VDZn2us2Q5RyBZAUJrjwdLLqPTZEyxt+M8HRi7iK V5lw1s3gwFfWoePN6C28V+M77ugn5EpIm7aBwL3ih6wpflP6KZKVWvv5gf1Y30QtK/Py ujXZK51l710K14eTXWoMm+AA23uz2ZD3uUeQMxCUsGCn+da1f++X1DGkEDcUpJofb/bZ FukaK36shmDa5Ccz7Gq29bzMonSY7wqMsvfeG0HGopwfrNoj0yUChV4elafn8Plc/uok 3Q7Q== X-Gm-Message-State: AGRZ1gKJIwMjhvhyElnO9QO1Sgn0MK8SQ00p7dVBEhVslX6VzLHZSXXU Tsn3yRjZr32eZUnW2OkGXBei+lqE7Dzu+Q== X-Received: by 2002:a19:c801:: with SMTP id y1mr9400157lff.53.1542990980008; Fri, 23 Nov 2018 08:36:20 -0800 (PST) Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com. [209.85.167.42]) by smtp.gmail.com with ESMTPSA id e5-v6sm5580049ljj.91.2018.11.23.08.36.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 23 Nov 2018 08:36:18 -0800 (PST) Received: by mail-lf1-f42.google.com with SMTP id v5so9080153lfe.7 for ; Fri, 23 Nov 2018 08:36:18 -0800 (PST) X-Received: by 2002:a19:6e0b:: with SMTP id j11mr10250691lfc.124.1542990977654; Fri, 23 Nov 2018 08:36:17 -0800 (PST) MIME-Version: 1.0 References: <02bfc577-32a5-66be-64bf-d476b7d447d2@kernel.dk> <20181121063609.GA109082@gmail.com> <48e27a3a-2bb2-ff41-3512-8aeb3fd59e57@kernel.dk> <26eff539-7de7-784c-0c88-f1d30753299d@redhat.com> <7ea44458b90b4d41a08ba9012818d273@AcuMS.aculab.com> <64fd67993af04579b5262c270a7a4694@AcuMS.aculab.com> In-Reply-To: <64fd67993af04579b5262c270a7a4694@AcuMS.aculab.com> From: Linus Torvalds Date: Fri, 23 Nov 2018 08:36:01 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] x86: only use ERMS for user copies for larger sizes To: David.Laight@aculab.com Cc: Andrew Lutomirski , dvlasenk@redhat.com, Jens Axboe , Ingo Molnar , Thomas Gleixner , Ingo Molnar , bp@alien8.de, Peter Anvin , "the arch/x86 maintainers" , Andrew Morton , Peter Zijlstra , brgerst@gmail.com, Linux List Kernel Mailing , pabeni@redhat.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Nov 23, 2018 at 2:12 AM David Laight wrote: > > I've just patched my driver and redone the test on a 4.13 (ubuntu) kernel. > Calling memcpy_fromio(kernel_buffer, PCIe_address, length) > generates a lot of single byte TLP. I just tested it too - it turns out that the __inline_memcpy() code never triggers, and "memcpy_toio()" just generates a memcpy. So that code seems entirely dead. And, in fact, the codebase I looked at was the historical one, because I had been going back and looking at the history. The modern tree *does* have the "__inline_memcpy()" function I pointed at, but it's not actually hooked up to anything! This actually has been broken for _ages_. The breakage goes back to 2010, and commit 6175ddf06b61 ("x86: Clean up mem*io functions"), and it seems nobody really ever noticed - or thought that it was ok. That commit claims that iomem has no special significance on x86, but that really really isn't true, exactly because the access size does matter. And as mentioned, the generic memory copy routines are not at all appropriate, and that has nothing to do with ERMS. Our "do it by hand" memory copy routine does things like this: .Lless_16bytes: cmpl $8, %edx jb .Lless_8bytes /* * Move data from 8 bytes to 15 bytes. */ movq 0*8(%rsi), %r8 movq -1*8(%rsi, %rdx), %r9 movq %r8, 0*8(%rdi) movq %r9, -1*8(%rdi, %rdx) retq and note how for a 8-byte copy, it will do *two* reads of the same 8 bytes, and *two* writes of the same 8 byte destination. That's perfectly ok for regular memory, and it means that the code can handle an arbitrary 8-15 byte copy without any conditionals or loop counts, but it is *not* ok for iomem. Of course, in practice it all just happens to work in almost all situations (because a lot of iomem devices simply won't care), and manual access to iomem is basically extremely rare these days anyway, but it's definitely entirely and utterly broken. End result: we *used* to do this right. For the last eight years our "memcpy_{to,from}io()" has been entirely broken, and apparently even the people who noticed oddities like David, never reported it as breakage but instead just worked around it in drivers. Ho humm. Let me write a generic routine in lib/iomap_copy.c (which already does the "user specifies chunk size" cases), and hook it up for x86. David, are you using a bus analyzer or something to do your testing? I'll have a trial patch for you asap. Linus