From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9850CC433FE for ; Thu, 17 Nov 2022 10:23:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239500AbiKQKXX (ORCPT ); Thu, 17 Nov 2022 05:23:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229931AbiKQKXV (ORCPT ); Thu, 17 Nov 2022 05:23:21 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E8F524D5FA for ; Thu, 17 Nov 2022 02:23:20 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0FE1913D5; Thu, 17 Nov 2022 02:23:27 -0800 (PST) Received: from [10.57.69.157] (unknown [10.57.69.157]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 396103F73B; Thu, 17 Nov 2022 02:23:19 -0800 (PST) Message-ID: Date: Thu, 17 Nov 2022 10:23:13 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [RFC PATCH] arm64: mm: Add invalidate back in arch_sync_dma_for_device when FROM_DEVICE Content-Language: en-GB To: Ard Biesheuvel , Nanyong Sun Cc: will@kernel.org, catalin.marinas@arm.com, sstabellini@kernel.org, jgross@suse.com, oleksandr_tyshchenko@epam.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, wangkefeng.wang@huawei.com References: <20221117072100.2720512-1-sunnanyong@huawei.com> From: Robin Murphy In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022-11-17 08:24, Ard Biesheuvel wrote: > 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. It also commonly goes wrong the other way round when the drivers are correct but DT/ACPI failed to indicate a coherent device as such. If writes from the device actually snoop, they hit the still-present cache line, which then gets invalidated by unmap/sync_for_cpu and the new data is lost. Robin.