Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2380926pxb; Sun, 17 Apr 2022 21:00:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxPi2GH1xyq4cx9OivsImT3cgQEgg+8/GiueRiEDI+fxZxn0NRae+e1S1xoG0nl+19AnxAx X-Received: by 2002:a17:907:9803:b0:6db:ab21:738e with SMTP id ji3-20020a170907980300b006dbab21738emr7284643ejc.112.1650254439082; Sun, 17 Apr 2022 21:00:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650254439; cv=none; d=google.com; s=arc-20160816; b=VreRf+ubx4M96tnvnPbKydaywI6/NqAUOl48Emii7SpfhT4DSn/7qK4yglbieoXBsJ DQloQD3LL22A6LQLTyhS3TTcPey+hM4DPasNYp+0lF29CZdlWCmfJvsAKD0Am1Y55q5+ E3YHGrMdbjO2mCRYpH4+IyXsaHXFDQ91jSS/jCQfWsXP0jfwrjC23vzp4qHkVPdho9T7 PWWAyXIHply+vFuHCvk2qJDngKFuBsqD778sEiXacO8TtO1tdMq1JNDBe0Y1OCLtgS8x AW6t/z3wT7VWhHVTm7zd63ALU3X4pUvN9YsRLd8YgyOqW0moD3zE3db0kw/IyBZNTvAI jNyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=oCkmuptR82grVIpMKm03IldT1h/BDbWqMDrJqCZ8XQg=; b=Ujx3iOzXwBtYnOTdBUmw8Yycy3P7b/PD1DV3ej24xOWfpfsU4DoDrEOQsWEnL7adHw iVxmqtrMkTyhb0bkVsGgRY05k15wqIUNp4/Ag+VRbNaK9ef0+BH4EIJcBQLUBOZBDcdD vDxyZTUxa4mh3hV3NTFlt/1KuysECHh7bs+U3C0bxYitUBqePimAZY7zDHK1JADuAfTH YOmhn9LLoKPbJzrdV24gcdwlxmxEOxuYXT7FXmSqzBdEabpiz2PUaOsbD0Y6W1UoHklY M+lFkmMJ1QL8bbz947Z6I7NWSDUmgS6eQz0xRnynx2XPT1uiBh54jRZcVAP7M7jGsDu1 b0mQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dAVhbez3; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s21-20020a170906bc5500b006e889024a70si5645117ejv.871.2022.04.17.21.00.02; Sun, 17 Apr 2022 21:00:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dAVhbez3; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231550AbiDQWxv (ORCPT + 99 others); Sun, 17 Apr 2022 18:53:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230257AbiDQWxu (ORCPT ); Sun, 17 Apr 2022 18:53:50 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7F88186D7; Sun, 17 Apr 2022 15:51:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 5E65FB80D8A; Sun, 17 Apr 2022 22:51:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F118AC385B2; Sun, 17 Apr 2022 22:51:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1650235870; bh=zDOBgnl70WIG4aT0/2q/PIkF6z5KtIxdOp/pt8dlU/Q=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=dAVhbez32nAkSLYSZrjlNemwtBpg+fQmTQ6PKE2TFlAK6tseuEvPZWPztbRhvpwl2 HfqrgMjkzPhtvCOv1yXzcskq8gQj6tBrMoZ7e++tWxqFB+kHb59nTeCNZpSVGFga7J eHozegimtOK6a72np/J9ZF8A46Vws6W7NjOfsfhSmR9pWiC9+6GA3R1NEHBl15p5Qf SqVo4ltBKnyBy6tkPgTQmFTejH21csyiN89aqhpf99lvFxLhS1+jSfdX73tUYO8Z8F ye7bLc8+SMBi6WiDnBjsLE5N/ZprELa6mAQsMTRrFlyHopPVDeDo7BwFdLOASKbeuo ze9Ht9nee0nJA== Received: by mail-ua1-f49.google.com with SMTP id j9so4284976uap.13; Sun, 17 Apr 2022 15:51:09 -0700 (PDT) X-Gm-Message-State: AOAM533WKy99ydynEnSeGLePdbvqKN8MTloriByJ1leZnpZ2utmeRmpK FvYwigELMRzVHOQleB9iBgxO6JYLQmW6GODmG+I= X-Received: by 2002:ab0:4306:0:b0:35c:df5b:d7ab with SMTP id k6-20020ab04306000000b0035cdf5bd7abmr1809316uak.83.1650235868572; Sun, 17 Apr 2022 15:51:08 -0700 (PDT) MIME-Version: 1.0 References: <20220307224620.1933061-1-heiko@sntech.de> <70da24dd-2d03-fc49-151d-daabb315a5f6@sholland.org> <849a3728-7e84-4f26-0c73-4d68eae9ae01@sholland.org> In-Reply-To: From: Guo Ren Date: Mon, 18 Apr 2022 06:50:57 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 0/2] riscv: implement Zicbom-based CMO instructions + the t-head variant To: Corentin Labbe Cc: Samuel Holland , Heiko Stuebner , Palmer Dabbelt , Paul Walmsley , linux-riscv , Linux Kernel Mailing List , Wei Fu , Atish Patra , Anup Patel , Nick Kossifidis , Christoph Muellner , Philipp Tomsich , Herbert Xu , linux-crypto@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Mon, Apr 18, 2022 at 1:35 AM Corentin Labbe wrote: > > Le Sun, Apr 17, 2022 at 04:49:34PM +0800, Guo Ren a =C3=A9crit : > > On Sun, Apr 17, 2022 at 4:45 PM Corentin Labbe > > wrote: > > > > > > Le Sun, Apr 17, 2022 at 10:17:34AM +0800, Guo Ren a =C3=A9crit : > > > > On Sun, Apr 17, 2022 at 3:32 AM Corentin Labbe > > > > wrote: > > > > > > > > > > Le Sat, Apr 16, 2022 at 12:47:29PM -0500, Samuel Holland a =C3=A9= crit : > > > > > > On 4/16/22 2:35 AM, Corentin Labbe wrote: > > > > > > > Le Fri, Apr 15, 2022 at 09:19:23PM -0500, Samuel Holland a = =C3=A9crit : > > > > > > >> On 4/15/22 6:26 AM, Corentin Labbe wrote: > > > > > > >>> Le Mon, Mar 07, 2022 at 11:46:18PM +0100, Heiko Stuebner a = =C3=A9crit : > > > > > > >>>> This series is based on the alternatives changes done in m= y svpbmt series > > > > > > >>>> and thus also depends on Atish's isa-extension parsing ser= ies. > > > > > > >>>> > > > > > > >>>> It implements using the cache-management instructions from= the Zicbom- > > > > > > >>>> extension to handle cache flush, etc actions on platforms = needing them. > > > > > > >>>> > > > > > > >>>> SoCs using cpu cores from T-Head like the Allwinne D1 impl= ement a > > > > > > >>>> different set of cache instructions. But while they are di= fferent, > > > > > > >>>> instructions they provide the same functionality, so a var= iant can > > > > > > >>>> easly hook into the existing alternatives mechanism on tho= se. > > > > > > >>>> > > > > > > >>>> > > > > > > >>> > > > > > > >>> Hello > > > > > > >>> > > > > > > >>> I am testing https://github.com/smaeul/linux.git branch:ori= gin/riscv/d1-wip which contain this serie. > > > > > > >>> > > > > > > >>> I am hitting a buffer corruption problem with DMA. > > > > > > >>> The sun8i-ce crypto driver fail self tests due to "device o= verran destination buffer". > > > > > > >>> In fact the buffer is not overran by device but by dma_map_= single() operation. > > > > > > >>> > > > > > > >>> The following small code show the problem: > > > > > > >>> > > > > > > >>> dma_addr_t dma; > > > > > > >>> u8 *buf; > > > > > > >>> #define BSIZE 2048 > > > > > > >>> #define DMASIZE 16 > > > > > > >>> > > > > > > >>> buf =3D kmalloc(BSIZE, GFP_KERNEL | GFP_DMA); > > > > > > >>> for (i =3D 0; i < BSIZE; i++) > > > > > > >>> buf[i] =3D 0xFE; > > > > > > >>> print_hex_dump(KERN_INFO, "DMATEST1:", DUMP_PREFIX_NONE, 16= , 4, buf, 256, false); > > > > > > >>> dma =3D dma_map_single(ce->dev, buf, DMASIZE, DMA_FROM_DEVI= CE); > > > > > > >> > > > > > > >> This function (through dma_direct_map_page()) ends up callin= g > > > > > > >> arch_sync_dma_for_device(..., ..., DMA_FROM_DEVICE), which i= nvalidates the CPU's > > > > > > >> cache. This is the same thing other architectures do (at lea= st arm, arm64, > > > > > > >> openrisc, and powerpc). So this appears to be working as int= ended. > > > > > > > > > > > > > > This behavour is not present at least on ARM and ARM64. > > > > > > > The sample code I provided does not corrupt the buffer on the= m. > > > > > > > > > > > > That can be explained by the 0xFE bytes having been flushed to = DRAM already in > > > > > > your ARM/ARM64 tests, whereas in your riscv64 case, the 0xFE by= tes were still in > > > > > > a dirty cache line. The cache topology and implementation is to= tally different > > > > > > across the SoCs, so this is not too surprising. > > > > > > > > > > > > Semantically, dma_map_single(..., DMA_FROM_DEVICE) means you ar= e doing a > > > > > > unidirectional DMA transfer from the device into that buffer. S= o the contents of > > > > > > the buffer are "undefined" until the DMA transfer completes. If= you are also > > > > > > writing data into the buffer from the CPU side, then you need D= MA_BIDIRECTIONAL. > > > > > > > > > > > > Regards, > > > > > > Samuel > > > > > > > > > > +CC crypto mailing list + maintainer > > > > > > > > > > My problem is that crypto selftest, for each buffer where I need = to do a cipher operation, > > > > > concat a poison buffer to check that device does write beyond buf= fer. > > > > > > > > > > But the dma_map_sg(FROM_DEVICE) corrupts this poison buffer and c= rypto selftests fails thinking my device did a buffer overrun. > > > > > > > > > > So you mean that on SoC D1, this crypto API check strategy is imp= ossible ? > > > > > > > > I think you could try to replace all CLEAN & INVAL ops with FLUSH o= ps > > > > for the testing. (All cache block-aligned data from the device for = the > > > > CPU should be invalided.) > > > > > > > > > > With: > > > diff --git a/arch/riscv/mm/dma-noncoherent.c b/arch/riscv/mm/dma-nonc= oherent.c > > > index 2c124bcc1932..608483522e05 100644 > > > --- a/arch/riscv/mm/dma-noncoherent.c > > > +++ b/arch/riscv/mm/dma-noncoherent.c > > > @@ -21,7 +21,7 @@ void arch_sync_dma_for_device(phys_addr_t paddr, si= ze_t size, enum dma_data_dire > > > ALT_CMO_OP(CLEAN, (unsigned long)phys_to_virt(paddr),= size); > > > break; > > > case DMA_FROM_DEVICE: > > > - ALT_CMO_OP(INVAL, (unsigned long)phys_to_virt(paddr),= size); > > > + ALT_CMO_OP(FLUSH, (unsigned long)phys_to_virt(paddr),= size); > > > break; > > > case DMA_BIDIRECTIONAL: > > > ALT_CMO_OP(FLUSH, (unsigned long)phys_to_virt(paddr),= size); > > > > > > > > > The crypto self test works and I got no more buffer corruption. > > No, No ... it's not a solution. That means your driver has a problem. > > From device, we only need INVAL enough. > > > > For me, my driver works fine, the problem came from dma_map_sg(), probabl= y I didnt explain right, I restart. > > Example: > crypto self test send to my driver an AES cipher operation of 16 bytes in= side a SG, but the original buffer is greater (said 32 for the example). > So the first 16 bytes are used by the SG and the last 16 bytes are a pois= oned buffer (with value 0xFE) to check driver do not write beyong the norma= l operation of 16 bytes (and beyond the SG length). > > Doing the dma_map_sg(FROM_DEVICE) on the SG corrupt the whole buffer. > My driver write normally via DMA the first 16 bytes. > Crypto API check the last bytes, no more 0xFE, so it fail believing my dr= iver wrote beyond the first 16 bytes. > > But even If I disable my hardware operation, the buffer is still corrupte= d. (See my sample code which just do dma_map/dma_unmap) > > So the problem is the dma_map(FROM_DEVICE) which change buffer content. > > So if this behavour is normal on D1 SoC, how to fix the crypto self tests= ? Actually, FLUSH is safe for all, but more expensive. Can you tell me which arm SOC are you using? And which version of linux is running on your arm SOC? --=20 Best Regards Guo Ren ML: https://lore.kernel.org/linux-csky/