Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp242851pxb; Wed, 25 Aug 2021 02:05:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwrmSR3AT8Vpu8yUJ7H4KizbFIvecsg9Gqoiph5tJGL5yxjWYms6pxCdkqU/rSYojkmS1qN X-Received: by 2002:a02:866b:: with SMTP id e98mr39429743jai.48.1629882321784; Wed, 25 Aug 2021 02:05:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629882321; cv=none; d=google.com; s=arc-20160816; b=Vv7aEl3+GwNkNSekcDmXkRSwZvUJkD36KpEYHJp6PrTu3uC9EXeRJ0M4XFMmkhjDJC qtaSJeW4aicZpYVXE0IoYDDLTvOAjOEahw5wbztre1/vSZtc8bFqf4pBFDCceWw5n+7d v7s5yU/Q8iNwoIUv6ZfRwG4Su1E2mRLLVRi+LiJeJN+pPGjP33VQ2gIn7fJUQ+r/piMx qAVQ1gGv+DHGRwTUxRXSje31uuLmsXzRO49sVgy1UX6murN5lOB0aZbo4DeMbqLBh41r EpH35TObMI7kisLOrYw2bBjUdPWPjJBZVJbIjBLm7bo6WCBA4wv4YLkmSx6olxzbtF00 nW1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=c9QlCD04ZmmdrdTvW/aMGNn+2aMwsuGo3+ERxure5HA=; b=U91UYS6LjFhCHZavTiH3sz9wbV//cLwyJHDeP3hapDy06F/eWYeYF3RaBbFBqpJZll +QnR71eBufYahR/49YPXuenOHSNrPTxJa21+0wysrFk5RBXXaFYD3/a9QNoYb7sQ2sWH BEvp0dUNSl0iAP8rTjCOvmDy/HBwC0WC0olpY/tprUYH03TbU5fj+6blugSi8B+JPkD8 RiF25ac1nkPTi7r06IJ+Py63h8W45izkmL42ehdNdCqO54Lc0g/KA9eMvH49YXvskor1 mk7sYb+bFR9disI9dC0B8mkGgL9lbbRL6ah7GEDif3eR2F0XBnRMERwuuuhCzaq+j6YC DqUA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x10si16631113ilg.50.2021.08.25.02.05.10; Wed, 25 Aug 2021 02:05:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239451AbhHYJE6 (ORCPT + 99 others); Wed, 25 Aug 2021 05:04:58 -0400 Received: from foss.arm.com ([217.140.110.172]:46250 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239464AbhHYJE4 (ORCPT ); Wed, 25 Aug 2021 05:04:56 -0400 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 639BF31B; Wed, 25 Aug 2021 02:04:11 -0700 (PDT) Received: from [192.168.1.179] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 51B5F3F66F; Wed, 25 Aug 2021 02:04:10 -0700 (PDT) Subject: Re: [PATCH v2 4/4] drm/panfrost: Handle non-aligned lock addresses To: Alyssa Rosenzweig , dri-devel@lists.freedesktop.org Cc: Rob Herring , Tomeu Vizoso , David Airlie , Daniel Vetter , linux-kernel@vger.kernel.org References: <20210824173028.7528-1-alyssa.rosenzweig@collabora.com> <20210824173028.7528-5-alyssa.rosenzweig@collabora.com> From: Steven Price Message-ID: <6fe675c4-d22b-22da-ba3c-f6d33419b9ed@arm.com> Date: Wed, 25 Aug 2021 10:04:01 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210824173028.7528-5-alyssa.rosenzweig@collabora.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 24/08/2021 18:30, Alyssa Rosenzweig wrote: > When locking memory, the base address is rounded down to the nearest > page. The current code does not adjust the size in this case, > truncating the lock region: > > Input: [----size----] > Round: [----size----] > > To fix the truncation, extend the lock region by the amount rounded off. > > Input: [----size----] > Round: [-------size------] > > This bug is difficult to hit under current assumptions: since the size > of the lock region is stored as a ceil(log2), the truncation must cause > us to cross a power-of-two boundary. This is possible, for example if > the caller tries to lock 65535 bytes starting at iova 0xCAFE0010. The > existing code rounds down the iova to 0xCAFE0000 and rounds up the lock > region to 65536 bytes, locking until 0xCAFF0000. This fails to lock the > last 15 bytes. > > In practice, the current callers pass PAGE_SIZE aligned inputs, avoiding > the bug. Therefore this doesn't need to be backported. Still, that's a > happy accident and not a precondition of lock_region, so we let's do the > right thing to future proof. Actually it's worse than that due to the hardware behaviour, the spec states (for LOCKADDR_BASE): > Only the upper bits of the address are used. The address is aligned to a > multiple of the region size, so a variable number of low-order bits are > ignored, depending on the selected region size. It is recommended that software > ensures that these low bits in the address are cleared, to avoid confusion. It appears that indeed this has caused confusion ;) So for a simple request like locking from 0xCAFE0000 - 0xCB010000 (size = 0x30000) the region width gets rounded up (to 0x40000) which causes the start address to be effectively rounded down (by the hardware) to 0xCAFC0000 and we fail to lock 0xCB000000-0xCB010000. Interestingly (unless my reading of this is wrong) that means to lock 0xFFFF0000-0x100010000 (i.e. crossing the 4GB boundary) requires locking *at least* 0x00000000-0x200000000 (i.e. locking the first 8GB). This appears to be broken in kbase (which actually does zero out the low bits of the address) - I've raised a bug internally so hopefully someone will tell me if I've read the spec completely wrong here. Steve > Signed-off-by: Alyssa Rosenzweig > Reported-by: Steven Price > --- > drivers/gpu/drm/panfrost/panfrost_mmu.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/drivers/gpu/drm/panfrost/panfrost_mmu.c b/drivers/gpu/drm/panfrost/panfrost_mmu.c > index dfe5f1d29763..14be32497ec3 100644 > --- a/drivers/gpu/drm/panfrost/panfrost_mmu.c > +++ b/drivers/gpu/drm/panfrost/panfrost_mmu.c > @@ -63,6 +63,9 @@ static void lock_region(struct panfrost_device *pfdev, u32 as_nr, > u8 region_width; > u64 region = iova & PAGE_MASK; > > + /* After rounding the address down, extend the size to lock the end. */ > + size += (region - iova); > + > /* The size is encoded as ceil(log2) minus(1), which may be calculated > * with fls. The size must be clamped to hardware bounds. > */ >