Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp851122ybh; Wed, 22 Jul 2020 15:11:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxsDVWv0SnoomAF3f1ehJFhZeoJfAKULxmW7jQDcYSVCv/zCzMucXS69knlH9FeLadxCdX9 X-Received: by 2002:a17:907:2112:: with SMTP id qn18mr1552602ejb.356.1595455914014; Wed, 22 Jul 2020 15:11:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595455914; cv=none; d=google.com; s=arc-20160816; b=fUAmVv6wSNqbWe57ygiqOSD/FtHDzXEqrR1u5dfDuLSbRvuiBhJFl09RL1RpRa+nos tH6NEpmT0iN52kpZEKPmoUrktMwRAqYS39c6KdXnq4CiMwu8rGniYNGCnfBf3pnY6UyA KZ+iK1tNJcVcGJrVL1Rq46fdPpd7SlFth7IL3N00UmbpVfUD6E1KbMZYgrctpwElEzCi 8kzANdWGKGB5jx4KSHzjOAx2eg8XLpI14cZ8mIC1A3/E4bJzqJ62XXebFSoJr3hmDqee SopY1rAsJy0XAwSy1NLfeBxuthM1oewuN66p7NhLhJptFHCv1L+oaEzma0MJbYiBwCZR MHtw== 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=wUNWDv0pMvOxp3f1lq5ubWVgLTQ75vGUx1UyIToohCs=; b=YtUtTJTbc0YLjB3cJVtLC5xFRuH9UDEANhm+VUxvpa7jhLrhRis2UVPafUT5Qnbgpl F6RvS/NgKdHg9wZXGk9wfGYffxAx+HwLHeNNqPHk9W4ecBDGHJrxeI1YrrtvcP0VkClG 79lVEnGvpRv7Wf3IZAKhFX9d7Rtq7xTYk2Vc3OBpJ78gbgpQqnPQMyQ5xUd5nSE22edO BrIEhZNmV8zJsmfRbigEP2xR7jo+IKEtIASD+dfT2+RFPPR4W67oHwzN/p9DtJT0RNfP lSSeJ16NLgyS5Jm5qTmuMOA+cfOamTLR0yL+cpQtS7zjHG8fFFreTFxLvy+UIDR/iqD+ +vNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=kqsyj1xN; 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 qp26si775454ejb.118.2020.07.22.15.11.29; Wed, 22 Jul 2020 15:11:54 -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=kqsyj1xN; 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 S1730837AbgGVWIa (ORCPT + 99 others); Wed, 22 Jul 2020 18:08:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726462AbgGVWI3 (ORCPT ); Wed, 22 Jul 2020 18:08:29 -0400 Received: from mail-vk1-xa43.google.com (mail-vk1-xa43.google.com [IPv6:2607:f8b0:4864:20::a43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DDE9C0619DC for ; Wed, 22 Jul 2020 15:08:29 -0700 (PDT) Received: by mail-vk1-xa43.google.com with SMTP id g22so930734vke.9 for ; Wed, 22 Jul 2020 15:08:29 -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=wUNWDv0pMvOxp3f1lq5ubWVgLTQ75vGUx1UyIToohCs=; b=kqsyj1xN2xk8GBmQL+ZMGDx/S5h87V9fnQpibUyeZjaONvTDcRgLHjE8PqbBx7DGFp Ahbis0yqSTcab9Ul9KNZ7DuQFFmo5+Xvc9Gf6pZeeZTMcywy8m10G6WwlwxkSaH/M+cM R4EjCh92aMjx/SlIcpDp8HZuUkRo4KKJkUJKk= 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=wUNWDv0pMvOxp3f1lq5ubWVgLTQ75vGUx1UyIToohCs=; b=ElZLJSI1abekP4en64bei6TAJS5r+mitZpe9fm5McBy/m8ExndWR5BvEbzBNZTrwoa WPwL0rfgvt7dN1WSc6guJ79fwZ4+md1GZxvBLsbz1XhZj7hCW327pJSaED1UVXlADIvV qYSvwaeQrUu3JUdaqPk9oVexBk9mYvATB1A6eSaT/PjRaxEgqGlUsXpnhwdMzPQGpSRx rJnihBBNUMVIvYtoAktgR8HKLvWzIAeSYzuCOLhGSxyo88ukivrSUTnv1cFd5gtFhGPR tXb1r1SURe++8X/bm3Cfgqfe5CffxvIPXZ+fuqN8ze4of8si6ZgGQ1+kgi8JRHmWUxTa vaEg== X-Gm-Message-State: AOAM532e2GEre5PW5eWFzz58Jxjun9pnds8/PjZHonL+jUwQmOmZ1Bq+ P+odQVgTtn9RkQ+Ppb95iubmL8GQw4E= X-Received: by 2002:a1f:16c3:: with SMTP id 186mr1650345vkw.16.1595455708293; Wed, 22 Jul 2020 15:08:28 -0700 (PDT) Received: from mail-ua1-f42.google.com (mail-ua1-f42.google.com. [209.85.222.42]) by smtp.gmail.com with ESMTPSA id f15sm152745vsa.28.2020.07.22.15.08.26 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Jul 2020 15:08:26 -0700 (PDT) Received: by mail-ua1-f42.google.com with SMTP id r63so1156524uar.9 for ; Wed, 22 Jul 2020 15:08:26 -0700 (PDT) X-Received: by 2002:ab0:150c:: with SMTP id o12mr1758711uae.90.1595455706030; Wed, 22 Jul 2020 15:08:26 -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: From: Doug Anderson Date: Wed, 22 Jul 2020 15:08:14 -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 1:26 PM Doug Anderson wrote: > > 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? OK, so I did some quick benchmarking and I couldn't find any performance regression with just always using writel() here. Even if dma_map_single() does guarantee that things are synced: * There's no guarantee that all geni users will use dma_map_{xxx}. * As Stephen says, the writel() is easier to reason about. The change to a writel() is a bit orthogonal to the issue being discussed here, though and it wouldn't make sense to have one patch touch both the geni headers and also the i2c code. Thus, I have sent v2 without it (just with the other fixes that Stephen requested) and also sent out a separate patch to change from writel_relaxed() to writel(). Breadcrumbs: [PATCH v2] i2c: i2c-qcom-geni: Fix DMA transfer race https://lore.kernel.org/r/20200722145948.v2.1.I7efdf6efaa6edadbb690196cd4fbe3392a582c89@changeid/ [PATCH] soc: qcom-geni-se: Don't use relaxed writes when writing commands https://lore.kernel.org/r/20200722150113.1.Ia50ab5cb8a6d3a73d302e6bdc25542d48ffd27f4@changeid/ As mentioned after the cut in the i2c change, I have kept people's tested/reviewed tags for v2. -Doug