Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1397756rdb; Mon, 2 Oct 2023 08:22:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGZlVPcRXQ1Ia8VN3Se0oMSdS+RystvGUNhHSP9X4gEH5y++qyN9AHy/qXtNwqcl5B9zi3c X-Received: by 2002:a05:6a00:1352:b0:68a:6d1a:b812 with SMTP id k18-20020a056a00135200b0068a6d1ab812mr14911143pfu.9.1696260123696; Mon, 02 Oct 2023 08:22:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696260123; cv=none; d=google.com; s=arc-20160816; b=Cyyo++aZLKo7o7KiQxflwD5fayDvDw1JQLmG84hRvHsFU6yBqOqQGAIHmgpdt4gZJh HGK5zbBs1TsxosVLSGHojbeBwvjuf7wt83z/Xxu5IBoANksUMmsK+e+qc93W42P52Mvu wehPNvITj5hfhOk34FJhRsLf9soPna48oDsx86p2Ml4Msm5LsWm1e2Eewa6RaHh8Y17A ZuROe/eH+wa4Tra2/S9gk/VHN6LZmryDdzqxVkVKGdc+TqCRrpO2QGRw21qWJWl87a3x sRHAJzBN+Inrq2oR6JJnv8V98Y/LA8Hi8F+2vMvP4ms2vy8MM6COVEvYsFLLa1LnYTzp WMyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=7C/ye+sVylN14XrBKQucHOYEDuIPDcYNH5y6lYVp1i4=; fh=VNNeuvOaOX9vs8hTHNRc07SbRSyWfXwwxmrpu40yE1U=; b=uvETa7DTCIAvtj3J8MSZkLaqbtPZ1EEedzlF1fO1Vjlk9YlNLlg25PsudMtru1cpi0 7C+YB8HA2S5R6VUrstPlBqQHkjWy1FpKy7ic+kPawkp2q9vmsy1Ti6Bs1Oxo2jcD511J 8CvNaVh6ZmcN5uvlk/uen8LNIBcfRvv943kwWqV8srq9cZddtpxultCmP39lg/BKJlUO AgZRwNzwiP1vcZJ2cbuhWZTgaY4SZfElrO3LL+62nvb5xhRTy2ScCVU5fmsB8f8zh5x2 frF6RIGXMiQZnyaITw3aUakB0SbWOMiNyahsQzgsrc1U/VIRcWxdYuXRl9wlqYe6rv3U R7Og== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id n13-20020a65488d000000b0055be9543340si26407547pgs.872.2023.10.02.08.22.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 08:22:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 4DF6780BD503; Mon, 2 Oct 2023 08:09:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237935AbjJBPI7 (ORCPT + 99 others); Mon, 2 Oct 2023 11:08:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237812AbjJBPI5 (ORCPT ); Mon, 2 Oct 2023 11:08:57 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F0053B7 for ; Mon, 2 Oct 2023 08:08:53 -0700 (PDT) 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 19935C15; Mon, 2 Oct 2023 08:09:32 -0700 (PDT) Received: from [10.1.196.40] (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1AB5D3F762; Mon, 2 Oct 2023 08:08:43 -0700 (PDT) Message-ID: <478adaab-75fe-4d67-10e5-b45bcb2e08d0@arm.com> Date: Mon, 2 Oct 2023 16:08:32 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH v1 1/1] ARM: Select DMA_DIRECT_REMAP to fix restricted DMA Content-Language: en-GB To: Jim Quinlan Cc: Linus Walleij , Christoph Hellwig , bcm-kernel-feedback-list@broadcom.com, jim2101024@gmail.com, Russell King , Arnd Bergmann , Geert Uytterhoeven , "Russell King (Oracle)" , Andrew Morton , Jonathan Corbet , Thomas Gleixner , Sebastian Reichel , "Mike Rapoport (IBM)" , Eric DeVolder , Nathan Chancellor , "Kirill A. Shutemov" , Christophe Leroy , "moderated list:ARM PORT" , open list , Claire Chang References: <20230926175208.9298-1-james.quinlan@broadcom.com> <20230926175208.9298-2-james.quinlan@broadcom.com> From: Robin Murphy In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 02 Oct 2023 08:09:13 -0700 (PDT) On 02/10/2023 1:33 pm, Jim Quinlan wrote: > On Thu, Sep 28, 2023 at 11:47 AM Robin Murphy wrote: >> >> On 28/09/2023 1:07 pm, Jim Quinlan wrote: >>> On Wed, Sep 27, 2023 at 7:10 PM Linus Walleij wrote: >>>> >>>> Hi Jim, >>>> >>>> thanks for your patch! >>>> >>>> On Tue, Sep 26, 2023 at 7:52 PM Jim Quinlan wrote: >>>> >>>>> Without this commit, the use of dma_alloc_coherent() while >>>>> using CONFIG_DMA_RESTRICTED_POOL=y breaks devices from working. >>>>> For example, the common Wifi 7260 chip (iwlwifi) works fine >>>>> on arm64 with restricted memory but not on arm, unless this >>>>> commit is applied. >>>>> >>>>> Signed-off-by: Jim Quinlan >>>> >>>> (...) >>>>> + select DMA_DIRECT_REMAP >>>> >>>> Christoph invented that symbol so he can certainly >>>> explain what is missing to use this on ARM. >>>> >>>> This looks weird to me, because: >>>>> git grep atomic_pool_init >>>> arch/arm/mm/dma-mapping.c:static int __init atomic_pool_init(void) >>>> kernel/dma/pool.c:static int __init dma_atomic_pool_init(void) >>>> >>>> Now you have two atomic DMA pools in the kernel, >>>> and a lot more than that is duplicated. I'm amazed that it >>>> compiles at all. >>>> >>>> Clearly if you want to do this, surely the ARM-specific >>>> arch/arm/mm/dma-mapping.c and arch/arm/mm/dma-mapping-nommu.c >>>> needs to be removed at the same time? >>>> >>>> However I don't think it's that simple, because Christoph would surely >>>> had done this a long time ago if it was that simple. >>> >>> Hello Linus, >>> >>> Yes, this is the reason I used "RFC" as the fix looked too easy to be viable :-) >>> I debugged it enough to see that the host driver's >>> writes to the dma_alloc_coherent() region were not appearing in >>> memory, and that >>> led me to DMA_DIRECT_REMAP. >> >> Oh, another thing - the restricted-dma-pool is really only for streaming >> DMA - IIRC there can be cases where the emergency fallback of trying to >> allocate out of the bounce buffer won't work properly. Are you also >> using an additional shared-dma-pool carveout to satisfy the coherent >> allocations, per the DT binding? > > Hello Robin, > Sorry for the delay. We use "restricted DMA" as a poor person's IOMMU; we can > restrict the DMA memory of a device to a narrow region, and our memory > bus HW has > "checkers' to enforce said region for a specific memory client, e.g. PCIe. > > We can confirm the assignment of restricted DMA in the bootlog when the device > is probed: > > iwlwifi 0001:01:00.0: assigned reserved memory node pcieSR1@4a000000 > iwlwifi 0001:01:00.0: enabling device (0000 -> 0002) > > As far as your other question, why don't I just post our relevant DT [1]. OK, I spent a while reminding myself of the restricted DMA code, and I'm at least 95% confident that I now recall the relevant details... Restricted DMA has never actually been supported on ARM, or various other platforms which the config doesn't do a great job of reflecting. ARM does not fully use dma-direct, and its arch_dma_alloc() implementation doesn't understand how to call the swiotlb_alloc() fallback path. TBH I'm now rather puzzled that you get any semblance of working at all, since AFAICS what ARM's arch_dma_alloc() should end up doing is giving you a non-cacheable remap as expected, but of some regular kernel memory outside the restricted address range, which oughtn't to work at all if something is actually restricting device accesses. Secondly, the case I was half-remembering above is that the aforementioned fallback path fundamentally *only* works for non-coherent devices where dma_alloc_coherent() calls are in non-blocking context. The only way to make atomic coherent allocations come from the restricted range is to set up part of it as a per-device coherent pool, via an additional reserved-memory region as mentioned. Thanks, Robin. > > Regards, > Jim Quinlan > Broardcom STB/CM > > [1] > memory { > device_type = "memory"; > reg = <0x0 0x40000000 0x1 0x0>; > }; > > reserved-memory { > #address-cells = <0x2>; > #size-cells = <0x2>; > ranges; > /* ... */ > > pcieSR1@4a000000 { > linux,phandle = <0x2a>; > phandle = <0x2a>; > compatible = "restricted-dma-pool"; > reserved-names = "pcieSR1"; > reg = <0x0 0x4a000000 0x0 0x2400000>; > }; > }; > pcie@8b20000 { > /* ... */ > pci@0,0 { > /* ... */ > pci-ep@0,0 { > memory-region = <0x2a>; > reg = <0x10000 0x0 0x0 0x0 0x0>; > }; > }; > }; > > > > >> >> Thanks, >> Robin.