Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1012129ybh; Tue, 21 Jul 2020 13:30:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXjlQyLUYi7CdbwXML/sy6WIVD/6F8Y8ZiUDIfE0hChzUGE4TBS1xa24RzjhBMipX7cOeJ X-Received: by 2002:a17:906:a055:: with SMTP id bg21mr26698405ejb.516.1595363439278; Tue, 21 Jul 2020 13:30:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595363439; cv=none; d=google.com; s=arc-20160816; b=EyEnkUQJhuAtgZlcXxCcYuOKl0pDfUtOndvCJXYMiFS/VJ++Ss2LPF32hdZUMPZWrI GQuuIfwLcD7dS6pHHNziynxrYH6A3mHtAsOisL58NQu2/k9si+VrgkFFN+RtPScKQhMK ufSgkO5lwa9XB/OQZBcE0bF1dJdzAKv5t2nsWakgbms/auajZKlyyy9wXJQLSb3tVrGi HfLyGu29E83LZcFH3lu1/CJa1f8ScdXR0Wd7Wc8rbSqXCiOxDgmx+WCILJrxBc7jJxiv Uk2unqtpIrgSMXQtlHu6v8Msi5CU+MbLiKCwercXfhTi+kDfleGU6KvOB5+Y/NJx0gyx +sNQ== 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=KLCByWF4yLz2SmZ5rErGQplCKH4CPMVPAEbJkDh8hXg=; b=h2u0ijxjLXm/i1Foj0iZIXDN47H3sgy6DuVnqZj8+jimoYqiQg1AG9yIRdXkTceaDv 3dklgDmU2wTccOdiBGw2ZhJiEJZlkZ3pSaesN8AU1SeEpbgeySUhc8lDGHraw48Bft26 AP8HR3m1czjJuzLGyyjLKtPqdVPLX4E0qWbuK+VJ+Y0co80a4W8T4hxbrV3MfZLGR9zI fWkPxdXJV2+4ACn4URaC1YlOVUZK1QpMb2yaVh7H9HK+At7KqApMc9VggF+xAhUDELFW XQ9515tA3sUIBEVPUj0aM0dJ9PDsUjs3v/2fG+2NucVT3dzwrvLTxbP6plyMubSHctTw zX9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Q1goVVRy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w18si12840248ejk.216.2020.07.21.13.30.16; Tue, 21 Jul 2020 13:30:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Q1goVVRy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730281AbgGUU1C (ORCPT + 99 others); Tue, 21 Jul 2020 16:27:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729928AbgGUU1A (ORCPT ); Tue, 21 Jul 2020 16:27:00 -0400 Received: from mail-vk1-xa41.google.com (mail-vk1-xa41.google.com [IPv6:2607:f8b0:4864:20::a41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 659B9C061794 for ; Tue, 21 Jul 2020 13:27:00 -0700 (PDT) Received: by mail-vk1-xa41.google.com with SMTP id t187so45848vke.5 for ; Tue, 21 Jul 2020 13:27:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=KLCByWF4yLz2SmZ5rErGQplCKH4CPMVPAEbJkDh8hXg=; b=Q1goVVRy0qIKIyOTfwTHQM8HUc5zxIwUwDM+oHWmOwuqPRrbRupgAs25ujIwSPdoz5 1M6Zj7yl2+wNwXwZqkRzsITMucTg+XuAkgiInPP/IjjvqA8bHsxKyojhuHc+3CbEYWsS nkc34p1lczVQ3GD1B7C3Ty7SgYCP5T9b0dvGU= 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=KLCByWF4yLz2SmZ5rErGQplCKH4CPMVPAEbJkDh8hXg=; b=IJPr4VOP7//hGv+zwGv4prHsDHbX7t1H9dCoc8T7+6dQUSUe7aLNdMSU/63btyO/9K pq4ftb8e4izHXUmYTnu4lpl8yO5OK37BIq4eT6902DeGk0fRxfu3KniOfTRctgnDlrP3 fOqsQQ7fp/HJqP23yoPRaC/l3B496BhOkNZJi2ctHbAMf9n2BXd8m/iq+w5PL49vfYf3 x42e2NidsxTWN1oWYT1zvkLYHNo3pSbSbNLIpucA/eUmBla39cjhmWI/KkHC/dVvGPfQ guaD2g0kjb/TZu9ELNdpd9cie2til2TxtjkJ+P50YhGNwFtperHD/jHhsUXeWhpBV4n6 ga4w== X-Gm-Message-State: AOAM530crc509H8QFCkg3F2tj2Jar0DOXkn18gATfoVap59heFYTLj9s 9DOjrL0fu6zV1aPpHYmwFdUdycBz8tI= X-Received: by 2002:a1f:f4cb:: with SMTP id s194mr21313774vkh.48.1595363218825; Tue, 21 Jul 2020 13:26:58 -0700 (PDT) Received: from mail-ua1-f50.google.com (mail-ua1-f50.google.com. [209.85.222.50]) by smtp.gmail.com with ESMTPSA id a203sm3344432vka.44.2020.07.21.13.26.57 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Jul 2020 13:26:58 -0700 (PDT) Received: by mail-ua1-f50.google.com with SMTP id n4so6657989uae.5 for ; Tue, 21 Jul 2020 13:26:57 -0700 (PDT) X-Received: by 2002:a9f:3dc6:: with SMTP id e6mr22533941uaj.104.1595363217358; Tue, 21 Jul 2020 13:26:57 -0700 (PDT) MIME-Version: 1.0 References: <20200720172448.1.I7efdf6efaa6edadbb690196cd4fbe3392a582c89@changeid> <159531115483.3847286.18280088484118119899@swboyd.mtv.corp.google.com> <159531527579.3847286.1254956818647049462@swboyd.mtv.corp.google.com> <159535775253.3847286.5195740102798837524@swboyd.mtv.corp.google.com> In-Reply-To: <159535775253.3847286.5195740102798837524@swboyd.mtv.corp.google.com> From: Doug Anderson Date: Tue, 21 Jul 2020 13:26:44 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] i2c: i2c-qcom-geni: Fix DMA transfer race To: Stephen Boyd Cc: Wolfram Sang , Sai Prakash Ranjan , Rajendra Nayak , Akash Asthana , Alok Chauhan , Andy Gross , Bjorn Andersson , Wolfram Sang , linux-arm-msm , linux-i2c@vger.kernel.org, LKML 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 Hi, On Tue, Jul 21, 2020 at 11:55 AM Stephen Boyd wrote: > > Quoting Doug Anderson (2020-07-21 09:18:35) > > On Tue, Jul 21, 2020 at 12:08 AM Stephen Boyd wrote: > > > > > > Quoting Stephen Boyd (2020-07-20 22:59:14) > > > > > > > > I worry that we also need a dmb() here to make sure the dma buffer is > > > > properly mapped before this write to the device is attempted. But it may > > > > only matter to be before the I2C_READ. > > > > > > > > > > I'm suggesting this patch instead where we make geni_se_setup_m_cmd() > > > use a writel() so that it has the proper barrier semantics to wait for > > > the other memory writes that happened in program order before this point > > > to complete before the device is kicked to do a read or a write. > > > > Are you saying that dma_map_single() isn't guaranteed to have a > > barrier or something? I tried to do some searching and found a thread > > [1] where someone tried to add a barrierless variant of them. To me > > that means that the current APIs have barriers. > > > > ...or is there something else you're worried about? > > I'm not really thinking about dma_map_single() having a barrier or not. > The patch you mention is from 2010. Many things have changed in the last > decade. Does it have barrier semantics? The presence of a patch on the > mailing list doesn't mean much. Yes, it's pretty old, but if you follow the thread and look at the patch I'm fairly certain it's still relevant. Specifically, following one thread of dma_map_single() on arm64: dma_map_single() -> dma_map_single_attrs() --> dma_map_page_attrs() ---> dma_direct_map_page() ----> arch_sync_dma_for_device() -----> __dma_map_area() ------> __dma_inv_area() which has a "dsb" I'm sure there are lots of other possible paths, but one thing pointed out by following that path is 'DMA_ATTR_SKIP_CPU_SYNC'. The documentation of that option talks about the normal flow. It says that in the normal flow that dma_map_{single,page,sg} will synchronize. We are in the normal flow here. As far as I understand, the whole point of dma_map_single() is to take a given buffer and get it all ready so that if a device does DMA on it right after the function exits that it's all set. > Specifically I'm looking at "KERNEL I/O BARRIER EFFECTS" of > Documentation/memory-barriers.txt and noticing that this driver is using > relaxed IO accessors meaning that the reads and writes aren't ordered > with respect to other memory accesses. They're only ordered to > themselves within the same device. I'm concerned that the CPU will issue > the IO access to start the write DMA operation before the buffer is > copied over due to out of order execution. I'm not an expert either, but it really looks like dma_map_single() does all that we need it to. > I'm not an expert in this area, but this is why we ask driver authors to > use the non-relaxed accessors because they have the appropriate > semantics built in to make them easy to reason about. They do what they > say when they say to do it. I'm all for avoiding using the relaxed variants too except if it's been shown to be a performance problem. The one hesitation I have, though, is that I've spent time poking a bunch at the geni SPI driver. We do _a lot_ of very small SPI transfers on our system. For each of these it's gotta setup a lot of commands. When I was poking I definitely noticed the difference between writel() and writel_relaxed(). If we can save a few microseconds on each one of these transfers it's probably worth it since it's effectively in the inner loop of some transfers. One option I thought of was to track the mode (DMA vs. FIFO) and only do writel() for DMA mode. If you're not convinced by my arguments about dma_map_single(), would you be good with just doing the non-relaxed version if we're in DMA mode? -Doug