Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1728679rwb; Thu, 17 Nov 2022 00:44:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf72iG4LuUxCr749mT0v2g30eWXxvoUaIBssL9f6noN5BjEigACKkCXgbaOCbSMtn8BQ/V5T X-Received: by 2002:aa7:cb49:0:b0:468:f307:3014 with SMTP id w9-20020aa7cb49000000b00468f3073014mr239780edt.321.1668674646353; Thu, 17 Nov 2022 00:44:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668674646; cv=none; d=google.com; s=arc-20160816; b=mxdyPr/bKCJs56FcK2g0U7YmBC7TUAAwsl5yHj8dl85uOnHPNqOUK5pTgKKG7r8lUb 9KJzQI9HCS9MBu+NQdTfnMhkswZlu3f8Zj1tJVUPhxoUmIoJ6+6HOXZfnsvG4oVEPu34 kD4pw6jD436nCpnCdf1sqdL/4LdC5/sdmm9ITzIfIIrPHzbdaVEn3Ko00E8W2q8Mz+Ne Ckr1HFdr5z9JcYe7kfd1z2+x3kbUmgDooLIN/SAWTgdTZ6Ab4NtDS4oCMGLG6VtVcg71 yawEHYVv43g6yZgw4GkbdA+nO9m7LH9suq0pPB03BtTp25nHLJ51g2yyL1KuHy2m5i6W UL8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Tc6Lq1Zf0jcqiA4pGqNL4hULPTJ7TyWJhHbki6opuFc=; b=MQ1nSwuPMGG+w0x9hN6cyYvosEn5gQ7K2Nnki8Vvi5uaccT6WGXoCHci3la9M34RTY GogD6mH2mwm3sJxTuLWDGe7Ky7yNhwFwtyDzdqYipqAwI0se5RPhMXjILpK69nR5c76/ 5NcEU7Fg5slHfPMEMVTUuT+0KrxzGlBUa6YADip4SoasfMtcmO8TnZl/macmTIqCo7vf tMXZWOpUDfReoF6vR7Az8HRoahWiFfQCCfogqfSJUrTCKqIXezPqAfGiILN3QUQzimQU rNsq35MrETVC04FvLKD511yDLn7BeAkX0NfWi0vrEH4ToyMGNsh5c/FL697pcnx5frd0 NkTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LfTwAska; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-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 et19-20020a170907295300b0078d9cec6a5asi105216ejc.191.2022.11.17.00.43.44; Thu, 17 Nov 2022 00:44:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-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=LfTwAska; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-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 S239470AbiKQIYs (ORCPT + 92 others); Thu, 17 Nov 2022 03:24:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239414AbiKQIYp (ORCPT ); Thu, 17 Nov 2022 03:24:45 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83F2F6DCF8 for ; Thu, 17 Nov 2022 00:24:44 -0800 (PST) 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 1F91C620FE for ; Thu, 17 Nov 2022 08:24:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F2FBC433C1 for ; Thu, 17 Nov 2022 08:24:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668673483; bh=ILRX15KGqyKu8+u5JNGQ2tQtPbAfM+RA/0Wr4Ct0E5g=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=LfTwAska/uNn2Ts5/GBMxSUyEAwR85VG0H0k5urLduOIjpWzihUcd1wVwh8Gvxsud Zbx+3CdONTPpzbi5lyhKwKDepg2j05zcvooUed0D56pQru/FdAclZB7BS3gnr42UCM oP7x6rkKmJIzrrnT3QdkjnLD8ueperIcqsc8j7neOhi9MAWlx4ENrryoQcHjiiSPBO j/4QYwsspLZYM+IHAM5OiQ5gGrXviptuP98z2T0KRRtxiYdSwM/nFLZuCHyS9qShko 0eVNlXNi+HU5GiUGsxzTtOSPPwq1RxU4WeCXIfpOKtkrjUQrDVKTfisT5+V/xfgo6m YS8itPG7owZmw== Received: by mail-lj1-f169.google.com with SMTP id x21so1725532ljg.10 for ; Thu, 17 Nov 2022 00:24:43 -0800 (PST) X-Gm-Message-State: ANoB5pnN7RVMFb1oGWmRWngSm+o8FohH0YIoZ1AqzeqKf9YPUsL8FakJ 1QlVDXeBpQNu7MSsiX5Vl36NqESL98Te5q+if9o= X-Received: by 2002:a05:651c:1601:b0:277:3a1:e86d with SMTP id f1-20020a05651c160100b0027703a1e86dmr695741ljq.152.1668673481480; Thu, 17 Nov 2022 00:24:41 -0800 (PST) MIME-Version: 1.0 References: <20221117072100.2720512-1-sunnanyong@huawei.com> In-Reply-To: <20221117072100.2720512-1-sunnanyong@huawei.com> From: Ard Biesheuvel Date: Thu, 17 Nov 2022 09:24:29 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH] arm64: mm: Add invalidate back in arch_sync_dma_for_device when FROM_DEVICE To: Nanyong Sun Cc: will@kernel.org, catalin.marinas@arm.com, sstabellini@kernel.org, robin.murphy@arm.com, jgross@suse.com, oleksandr_tyshchenko@epam.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, wangkefeng.wang@huawei.com Content-Type: text/plain; charset="UTF-8" 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 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-kernel@vger.kernel.org On Thu, 17 Nov 2022 at 07:33, Nanyong Sun wrote: > > The commit c50f11c6196f ("arm64: mm: Don't invalidate FROM_DEVICE > buffers at start of DMA transfer") replaces invalidate with clean > when DMA_FROM_DEVICE, this changes the behavior of functions like > dma_map_single() and dma_sync_single_for_device(*, *, *, DMA_FROM_DEVICE), > then it may make some drivers works unwell because the implementation > of these DMA APIs lose the original cache invalidation. > > Situation 1: ... > Situation 2: > After backporting the above commit, we find a network card driver go > wrong with cache inconsistency when doing DMA transfer: CPU got the > stale data in cache when reading DMA data received from device. I suppose this means those drivers may lack dma_sync_single_for_cpu() calls after the inbound transfers complete, and are instead relying on the cache invalidation performed before the transfer to make the DMA'd data visible to the CPU. This is buggy and fragile, and should be fixed in any case. There is no guarantee that the CPU will not preload parts of the buffer into the cache while DMA is in progress, so the invalidation must occur strictly after the device has finished transferring the data. > A similar phenomenon happens on sata disk drivers, it involves > mainline modules like libata, scsi, ahci etc, and is hard to find > out which line of code results in the error. > Could you identify the actual hardware and drivers that you are observing the issue on? Claiming that everything is broken is not very helpful in narrowing it down (although I am not saying you are wrong :-)) > It seems that some dirvers may go wrong and have to match the > implementation changes of the DMA APIs, and it would be confused > because the behavior of these DMA APIs on arm64 are different > from other archs. > > Add invalidate back in arch_sync_dma_for_device() to keep drivers > compatible by replace dcache_clean_poc with dcache_clean_inval_poc > when DMA_FROM_DEVICE. > So notably, the patch in question removes cache invalidation *without* clean, and what you are adding here is clean+invalidate. (Invalidation without clean may undo the effect of, e.g., the memzero() of a secret in memory, and so it is important that we don't add that back if we can avoid it) Since we won't lose the benefits of that change, incorporating invalidation at this point should be fine: clean+invalidate shouldn't be more expensive than clean, and [correctly written] drivers will invalidate those lines anyway, as the data has to come from DRAM in any case. So unless fixing the drivers in question is feasible, this change seems reasonable to me. > Fixes: c50f11c6196f ("arm64: mm: Don't invalidate FROM_DEVICE buffers at start of DMA transfer") > Signed-off-by: Nanyong Sun > --- > arch/arm64/mm/dma-mapping.c | 5 ++++- > 1 file changed, 4 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c > index 3cb101e8cb29..07f6a3089c64 100644 > --- a/arch/arm64/mm/dma-mapping.c > +++ b/arch/arm64/mm/dma-mapping.c > @@ -18,7 +18,10 @@ void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, > { > unsigned long start = (unsigned long)phys_to_virt(paddr); > > - dcache_clean_poc(start, start + size); > + if (dir == DMA_FROM_DEVICE) > + dcache_clean_inval_poc(start, start + size); > + else > + dcache_clean_poc(start, start + size); > } > > void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, > -- > 2.25.1 >