Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp665093ybl; Wed, 11 Dec 2019 05:42:00 -0800 (PST) X-Google-Smtp-Source: APXvYqwGN7zekjNayzIuaxNhwmFEryOJ5tm8oxtrgvXXq2YQ0GN05ycwL/FCPs5RdV93fB2oOaHF X-Received: by 2002:a05:6808:7da:: with SMTP id f26mr2640665oij.73.1576071720047; Wed, 11 Dec 2019 05:42:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576071720; cv=none; d=google.com; s=arc-20160816; b=AsWDaUOqShg5ebk5Y6tffFMrzKG76NqP81wDrjlCclmIHIV2W8KcBTDJjQQhO+Ptyp MJmzUUrl1RHHMrr8bjtsE8x8NZRm9GW0XjT7CcD0UIrcausSFXk56asyxyaA0eq4nHzc QxK3PcrAcUAvF6lo5m29uwY35HgSOhxhf1evO5fC+peoItMxfbSFcnHi+BA6raSlY6sF gecQMfxbuo7KzrBU8h6NxOOCJpKIqEMBTHgb7KUNt3hJ1Xl4C1xNt3Lhf6jvpmmiSt4K D++Cx145QAkQKdissH4HM15aU72EqypGioeppI6VGAPl+lLo6TVFZE62ggZVomGUlXik uJdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=bX0CyfVm5sI2R+ZhkRyOsPhvsUNQZkDoRab1+PG2NkY=; b=X/ulDmKRpTHz8UGao2nW0gUhk3raa5k0m2I0GHLcRuIay15zyFHU8TEm4B4x3C2Iu3 RWnW/lopl+NMyB96mAZ04dmogpN8FL4cYeq4Y1IrlBCvmknFDtki67Y1QPEmv2C+trQq oknzwHVhDzCp1YEIyDFX8V/LGnC92ulYHZ8+Buj8OBjAZ48agx5KeO6uwvVbERMIPSoN cCAGTf2kvh7Gbnok67bOo/1w910O9InnjjeJG0J1TT3Txx1fvjTtL0xlxio2oX0umPjF 8iZ8ygu/MUf07m6sgIyhMT7XyBywq00vIApVLAL8+DEH3jv9hPtUkSwKANONdF+OGRhj GqkQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s17si1157357otq.78.2019.12.11.05.41.47; Wed, 11 Dec 2019 05:42:00 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729730AbfLKNlI (ORCPT + 99 others); Wed, 11 Dec 2019 08:41:08 -0500 Received: from foss.arm.com ([217.140.110.172]:58606 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729719AbfLKNlH (ORCPT ); Wed, 11 Dec 2019 08:41:07 -0500 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 089E01FB; Wed, 11 Dec 2019 05:41:07 -0800 (PST) Received: from [10.1.196.37] (e121345-lin.cambridge.arm.com [10.1.196.37]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 142BA3F6CF; Wed, 11 Dec 2019 05:41:02 -0800 (PST) Subject: Re: [PATCH] ARM: dma-api: fix max_pfn off-by-one error in __dma_supported() To: Chen-Yu Tsai , Russell King Cc: stable@vger.kernel.org, Chen-Yu Tsai , Christoph Hellwig , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org References: <20191211104152.26496-1-wens@kernel.org> From: Robin Murphy Message-ID: <28dfaeab-73cd-041b-9894-776064d13245@arm.com> Date: Wed, 11 Dec 2019 13:40:58 +0000 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20191211104152.26496-1-wens@kernel.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/12/2019 10:41 am, Chen-Yu Tsai wrote: > From: Chen-Yu Tsai > > max_pfn, as set in arch/arm/mm/init.c: > > static void __init find_limits(unsigned long *min, > unsigned long *max_low, > unsigned long *max_high) > { > *max_low = PFN_DOWN(memblock_get_current_limit()); > *min = PFN_UP(memblock_start_of_DRAM()); > *max_high = PFN_DOWN(memblock_end_of_DRAM()); > } > > with memblock_end_of_DRAM() pointing to the next byte after DRAM. As > such, max_pfn points to the PFN after the end of DRAM. > > Thus when using max_pfn to check DMA masks, we should subtract one > when checking DMA ranges against it. > > Commit 8bf1268f48ad ("ARM: dma-api: fix off-by-one error in > __dma_supported()") fixed the same issue, but missed this spot. > > This issue was found while working on the sun4i-csi v4l2 driver on the > Allwinner R40 SoC. On Allwinner SoCs, DRAM is offset at 0x40000000, > and we are starting to use of_dma_configure() with the "dma-ranges" > property in the device tree to have the DMA API handle the offset. > > In this particular instance, dma-ranges was set to the same range as > the actual available (2 GiB) DRAM. The following error appeared when > the driver attempted to allocate a buffer: > > sun4i-csi 1c09000.csi: Coherent DMA mask 0x7fffffff (pfn 0x40000-0xc0000) > covers a smaller range of system memory than the DMA zone pfn 0x0-0xc0001 > sun4i-csi 1c09000.csi: dma_alloc_coherent of size 307200 failed > > Fixing the off-by-one error makes things work. > > Fixes: 11a5aa32562e ("ARM: dma-mapping: check DMA mask against available memory") > Fixes: 9f28cde0bc64 ("ARM: another fix for the DMA mapping checks") > Fixes: ab746573c405 ("ARM: dma-mapping: allow larger DMA mask than supported") > Cc: > Signed-off-by: Chen-Yu Tsai > --- > arch/arm/mm/dma-mapping.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c > index e822af0d9219..f4daafdbac56 100644 > --- a/arch/arm/mm/dma-mapping.c > +++ b/arch/arm/mm/dma-mapping.c > @@ -227,12 +227,12 @@ static int __dma_supported(struct device *dev, u64 mask, bool warn) > * Translate the device's DMA mask to a PFN limit. This > * PFN number includes the page which we can DMA to. > */ > - if (dma_to_pfn(dev, mask) < max_dma_pfn) { > + if (dma_to_pfn(dev, mask) < max_dma_pfn - 1) { I think this correction actually wants to happen a couple of lines up in the definition: unsigned long max_dma_pfn = min(max_pfn, arm_dma_pfn_limit); max_pfn is indeed an exclusive limit, but AFAICS arm_dma_pfn_limit is inclusive, so none of these "+1"s and "-1"s can be entirely right for both cases. Robin. > if (warn) > dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n", > mask, > dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1, > - max_dma_pfn + 1); > + max_dma_pfn); > return 0; > } > >