Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2151799pxb; Sun, 17 Apr 2022 11:40:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz+HmpF51XtPGHMoG6NUDZ6crbqcKBLG/l4wqzct15eG4IWqA9FTCJzaYuOUO0z0Gen/aTR X-Received: by 2002:a17:90a:2809:b0:1cb:99a8:ffcd with SMTP id e9-20020a17090a280900b001cb99a8ffcdmr9252430pjd.7.1650220856277; Sun, 17 Apr 2022 11:40:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650220856; cv=none; d=google.com; s=arc-20160816; b=m/x8plkjhqfMb5w4N5NeQ0fb8hcUMXkkGzNfay9BcN2bxO+G6ZKhnv33nHwWHl07iJ 3iQjFRCVFRKVFFsyxIZSS60hr8dNX1yPmRToQIGM2atES+fOdYNjAWlkdCEwCDyZi59W lkDcz3wSc/AnQB0QJR5nrvBBjdGsYARNJkpGxE5ZoffmbvJWnWGA/HQU7aNaHOBbgNg8 QvULICfpNJkWdUf0DzP6zl/s+7AiPboVNU85eykwbR4t0ZTBjvkJ80BF1uRnHD5wVXlo u2RtZyDGSqEBhVuHt74aQkH5/3DLVj/3frtPqssC3B+C1Y1267IhYV3dBVJo2TFwOzVU J7Ag== 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=C07mwXZ0BGCwWHknFOq1ri7NeCU1ZxPlhz2oo5FY53M=; b=VYQTg6xadMeIfY84zy3x3Z8Cn3x8ECVkrBOHlSP2Aw2hlTg3Wymh/w3o6k/0hsOJXe 3uZf+oE7c3VP1CxE66rPccLQzZXkHI9b/7N2Extzd+PSbVvI1Xdngz9EZCioJ/RBqkIJ gjOyAhemyzFlNqjatQ+uByHcwCUFmoAkE9S7BsIbfbIbqrBbWJYjCloskZZJgn5za3Ag hy18FG5oWo/N86to0QJ78hf6yWUyezoYbEZZkuWc73q9aB2qgt6CGo7z6q3hm5lQFCn2 CVfJcZ//VJqxF43vENXsQxJA6xyiWZALxEIhFj1dtyT8MYFaSkqN3JzjEd6Vb2zg+eNG J/eA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZN7F9fih; 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 q24-20020a170902bd9800b00158d36a8114si6754396pls.77.2022.04.17.11.40.23; Sun, 17 Apr 2022 11:40:56 -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=ZN7F9fih; 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 S229975AbiDQIwY (ORCPT + 99 others); Sun, 17 Apr 2022 04:52:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231579AbiDQIwX (ORCPT ); Sun, 17 Apr 2022 04:52:23 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8E303B3; Sun, 17 Apr 2022 01:49:47 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 3C94A61179; Sun, 17 Apr 2022 08:49:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B42AC385AC; Sun, 17 Apr 2022 08:49:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1650185386; bh=iqaxMFM3cYKwlA/IAnWiV8E2ISGqOdaBcPl+s4Z4EFo=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ZN7F9fihzfdvl59d2QDDnMTqqiMdPQukHbVsuA4xlXA02NG5NGlUkX3J48W6tpMop 2hh72D8MYPw57+Pjz+LXmfAUQDqjGaMmCGb8MwCH5NyuHMXwNG98uL+nRZKlbEvHl8 1KB8bBu/vtAbdTEHRrR7g4CoWBrnhEBCV02R/pJeeX/yp8aPs+pJSlUpMqujEuGRW8 /p67rTsYW5PC7nidCw4TOsuPUmylmswjeXnOjB/W0PQL/yv2g7/KhGKb8MCvbupc/A kJO20SKbTjlWDW6aWYHDEsN6QzkV3XGnD9om6jP3jYU58hQ6q+zXzphAuWuoFIVRym oXwGj0j6NUEQA== Received: by mail-vk1-f179.google.com with SMTP id c4so5098847vkq.9; Sun, 17 Apr 2022 01:49:46 -0700 (PDT) X-Gm-Message-State: AOAM531owDALx8vB7RyNPZV+hYpaqKyxxHUJieBc3SYjTNJ4zQJdxPrS +eUaQtlPeWL+/PFo8ByBNLwNyGPRsZf/3pdtdCI= X-Received: by 2002:a1f:1b07:0:b0:348:f10f:82b2 with SMTP id b7-20020a1f1b07000000b00348f10f82b2mr1461848vkb.8.1650185385515; Sun, 17 Apr 2022 01:49:45 -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: Sun, 17 Apr 2022 16:49:34 +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.1 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 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=A9crit= : > > > > On 4/16/22 2:35 AM, Corentin Labbe wrote: > > > > > Le Fri, Apr 15, 2022 at 09:19:23PM -0500, Samuel Holland a =C3=A9= crit : > > > > >> 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 my sv= pbmt series > > > > >>>> and thus also depends on Atish's isa-extension parsing series. > > > > >>>> > > > > >>>> It implements using the cache-management instructions from the= Zicbom- > > > > >>>> extension to handle cache flush, etc actions on platforms need= ing them. > > > > >>>> > > > > >>>> SoCs using cpu cores from T-Head like the Allwinne D1 implemen= t a > > > > >>>> different set of cache instructions. But while they are differ= ent, > > > > >>>> instructions they provide the same functionality, so a variant= can > > > > >>>> easly hook into the existing alternatives mechanism on those. > > > > >>>> > > > > >>>> > > > > >>> > > > > >>> Hello > > > > >>> > > > > >>> I am testing https://github.com/smaeul/linux.git branch:origin/= 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 overr= an destination buffer". > > > > >>> In fact the buffer is not overran by device but by dma_map_sing= le() 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_DEVICE); > > > > >> > > > > >> This function (through dma_direct_map_page()) ends up calling > > > > >> arch_sync_dma_for_device(..., ..., DMA_FROM_DEVICE), which inval= idates the CPU's > > > > >> cache. This is the same thing other architectures do (at least a= rm, arm64, > > > > >> openrisc, and powerpc). So this appears to be working as intende= d. > > > > > > > > > > This behavour is not present at least on ARM and ARM64. > > > > > The sample code I provided does not corrupt the buffer on them. > > > > > > > > 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 bytes = were still in > > > > a dirty cache line. The cache topology and implementation is totall= y different > > > > across the SoCs, so this is not too surprising. > > > > > > > > Semantically, dma_map_single(..., DMA_FROM_DEVICE) means you are do= ing a > > > > unidirectional DMA transfer from the device into that buffer. So th= e 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 DMA_B= IDIRECTIONAL. > > > > > > > > Regards, > > > > Samuel > > > > > > +CC crypto mailing list + maintainer > > > > > > My problem is that crypto selftest, for each buffer where I need to d= o a cipher operation, > > > concat a poison buffer to check that device does write beyond buffer. > > > > > > But the dma_map_sg(FROM_DEVICE) corrupts this poison buffer and crypt= o selftests fails thinking my device did a buffer overrun. > > > > > > So you mean that on SoC D1, this crypto API check strategy is impossi= ble ? > > > > I think you could try to replace all CLEAN & INVAL ops with FLUSH ops > > 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-noncoher= ent.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, size_t= size, enum dma_data_dire > ALT_CMO_OP(CLEAN, (unsigned long)phys_to_virt(paddr), siz= e); > break; > case DMA_FROM_DEVICE: > - ALT_CMO_OP(INVAL, (unsigned long)phys_to_virt(paddr), siz= e); > + ALT_CMO_OP(FLUSH, (unsigned long)phys_to_virt(paddr), siz= e); > break; > case DMA_BIDIRECTIONAL: > ALT_CMO_OP(FLUSH, (unsigned long)phys_to_virt(paddr), siz= e); > > > 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. > > Thanks --=20 Best Regards Guo Ren ML: https://lore.kernel.org/linux-csky/