Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp569183ybt; Wed, 8 Jul 2020 06:42:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8nu4EZhUWkZrPqwEdzvFJM/w2HPDC0FJA9VNPV9kQX1+omidjrN6fqyaOwb9TzE546Jwz X-Received: by 2002:a17:906:f202:: with SMTP id gt2mr51297582ejb.70.1594215750882; Wed, 08 Jul 2020 06:42:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594215750; cv=none; d=google.com; s=arc-20160816; b=qqyI2CWophENwrIi0hbKVSyOF1uzyUsI/YT3HuDtaAkfqMUtX3Cic/EEysHS8ePMK9 f0Xx1UpW/inqQTO9x3aQqYfDmscY3mwdQGqfcB4vEOzkoCxv26jjYbTB2feAg18Qo7xF fsvmaWlY8yuyCJ1nj4AfkM2luTM6i1HEI89hlq1jg7PZ8vR2ztPIOACcEdpViHlPjRaP ook4ozKKZUga4hMKS/9LogWueTTfowc1qws7Gv3Am7FS2j8ZspjHU+hcAj48qfbkTo6p YyuKLth48wD9CSJMxSeASqs1tbtzN9YlDRJnd/egTXStTyzyvuW/nGyBY6QaE1NuyJu0 XqSw== 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=8p2MeO53N4LFHo14H/Wf4TMxCwYMC//FC4iUC98Jggc=; b=urYfdapYrGGQaMSqWTE3+skgWCwF8K3Hvi0hRNW+6bMSvE14eahyPpk7kygm3Vf6iR opUK5cl/XRkwLmzwjyIl055KdqiPpCaFMPqoDoo+bXGkxHVVOLZtaFaYinMjNySGSiUu GlSP0eWfnuR+ywnsO3ls50lvcGmGb2NI3yOFbMAGugfQ43Xe1PDt/9QOv5nDnQE+HJAg 0Y5jbg0SmnWOp9Ak1C09dyVUJM/aujTb9keQLaH46AYVoNuo90ELo+Err0cJwduVdeGo JLXXVNbgHQQRjFpBYdZ3V9oFjfe2FfgLIkvKI7ENXImdgpAI4CFDg53igAASQVtKdBI4 4taA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ntI6XtkL; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v26si5820edx.137.2020.07.08.06.42.07; Wed, 08 Jul 2020 06:42:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-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=@kernel.org header.s=default header.b=ntI6XtkL; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728932AbgGHNlh (ORCPT + 99 others); Wed, 8 Jul 2020 09:41:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:39720 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728210AbgGHNlh (ORCPT ); Wed, 8 Jul 2020 09:41:37 -0400 Received: from mail-oo1-f50.google.com (mail-oo1-f50.google.com [209.85.161.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B996F206E9 for ; Wed, 8 Jul 2020 13:41:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594215696; bh=8p2MeO53N4LFHo14H/Wf4TMxCwYMC//FC4iUC98Jggc=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ntI6XtkLpHlx8H1tAwp/+6rhq5WmAK4k8P5i01wDnr54TXDRHdDE3wtR02kP+ePPw Gpwm+g3Cn36x8ULkHyKCR3lkDAKydFMuq6xBQisumIIWq8oB4XtbPHgVPTqswrORVr NBgLty0oZ5hpzj0qBdOuyd1hBqPkzM+hL3cKJhug= Received: by mail-oo1-f50.google.com with SMTP id z127so5302006ooa.3 for ; Wed, 08 Jul 2020 06:41:36 -0700 (PDT) X-Gm-Message-State: AOAM531G20wJ8uNtvFmwcsk8u5JHl37kgGhIWyUchORR0oU4B7Ayvght c9yDjRGea4axJAINPzkCtFM4k2NIPP2YMscq1X8= X-Received: by 2002:a4a:de8d:: with SMTP id v13mr28486733oou.45.1594215696103; Wed, 08 Jul 2020 06:41:36 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ard Biesheuvel Date: Wed, 8 Jul 2020 16:41:24 +0300 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: question regarding crypto driver DMA issue To: "Van Leeuwen, Pascal" Cc: "linux-crypto@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Wed, 8 Jul 2020 at 16:35, Van Leeuwen, Pascal wrote: > > Hi Ard, > > Thanks for responding! > > > > For the situation where this problem is occuring, the actual buffers are stored inside > > > the ahash_req structure. So my question is: is there any reason why this structure may > > > not be DMA-able on some systems? (as I have a hunch that may be the problem ...) > > > > > > > If DMA is non-coherent, and the ahash_req structure is also modified > > by the CPU while it is mapped for DMA, you are likely to get a > > conflict. > > > Ah ... I get it. If I dma_map TO_DEVICE then all relevant cachelines are flushed, then > if the CPU accesses any other data sharing those cachelines, they get read back into > the cache. Any subsequent access of the actual result will then read stale data from > the cache. > > > It should help if you align the DMA-able fields sufficiently, and make > > sure you never touch them while they are mapped for writing by the > > device. > > > Yes, I guess that is the only way. I also toyed with the idea of using dedicated properly > dma_alloc'ed buffers with pointers in the ahash_request structure, but I don't see how > I can allocate per-request buffers as there is no callback to the driver on req creation. > > So ... is there any magical way within the Linux kernel to cacheline-align members of > a structure? Considering cacheline size is very system-specific? > You can use __cacheline_aligned as a modifier on struct members that are accessed by the device. However, this is a typical value, not a worst case value, and since this is taken into account at compile time, you really need a worst case value. On arm64, the maximum CWG (Cache Writeback Granule) value is 2k, which is a bit excessive, so it might help to do this at runtime. One thing you might do is increase the reqsize at TFM init time (in which case you could also check whether the device is cache coherent for DMA), and have a helper that gives you the address of the sub-struct inside the request struct based on the current cache alignment.