From: Steven Price <steven.price@arm.com>
To: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>,
dri-devel@lists.freedesktop.org
Cc: Rob Herring <robh@kernel.org>,
Tomeu Vizoso <tomeu.vizoso@collabora.com>,
David Airlie <airlied@linux.ie>, Daniel Vetter <daniel@ffwll.ch>,
linux-kernel@vger.kernel.org,
Chris Morgan <macromorgan@hotmail.com>,
stable@vger.kernel.org
Subject: Re: [PATCH 1/3] drm/panfrost: Simplify lock_region calculation
Date: Mon, 23 Aug 2021 10:40:44 +0100 [thread overview]
Message-ID: <192e5a1b-2caf-11a8-f090-ec5649ea16b5@arm.com> (raw)
In-Reply-To: <20210820213117.13050-2-alyssa.rosenzweig@collabora.com>
On 20/08/2021 22:31, Alyssa Rosenzweig wrote:
> In lock_region, simplify the calculation of the region_width parameter.
> This field is the size, but encoded as log2(ceil(size)) - 1.
> log2(ceil(size)) may be computed directly as fls(size - 1). However, we
> want to use the 64-bit versions as the amount to lock can exceed
> 32-bits.
>
> This avoids undefined behaviour when locking all memory (size ~0),
> caught by UBSAN.
It might have been useful to mention what it is that UBSAN specifically
picked up (it took me a while to spot) - but anyway I think there's a
bigger issue with it being completely wrong when size == ~0 (see below).
> Signed-off-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
> Reported-and-tested-by: Chris Morgan <macromorgan@hotmail.com>
> Cc: <stable@vger.kernel.org>
However, I've confirmed this returns the same values and is certainly
more simple, so:
Reviewed-by: Steven Price <steven.price@arm.com>
> ---
> drivers/gpu/drm/panfrost/panfrost_mmu.c | 19 +++++--------------
> 1 file changed, 5 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/gpu/drm/panfrost/panfrost_mmu.c b/drivers/gpu/drm/panfrost/panfrost_mmu.c
> index 0da5b3100ab1..f6e02d0392f4 100644
> --- a/drivers/gpu/drm/panfrost/panfrost_mmu.c
> +++ b/drivers/gpu/drm/panfrost/panfrost_mmu.c
> @@ -62,21 +62,12 @@ static void lock_region(struct panfrost_device *pfdev, u32 as_nr,
> {
> u8 region_width;
> u64 region = iova & PAGE_MASK;
> - /*
> - * fls returns:
> - * 1 .. 32
> - *
> - * 10 + fls(num_pages)
> - * results in the range (11 .. 42)
> - */
> -
> - size = round_up(size, PAGE_SIZE);
This seems to be the first issue - ~0 will be 'rounded up' to 0.
>
> - region_width = 10 + fls(size >> PAGE_SHIFT);
fls(0) == 0, so region_width == 10.
> - if ((size >> PAGE_SHIFT) != (1ul << (region_width - 11))) {
Presumably here's where UBSAN objects - we're shifting by a negative
value, which even it it happens to works means the lock region is tiny
and certainly not what was intended! It might well be worth a:
Fixes: f3ba91228e8e ("drm/panfrost: Add initial panfrost driver")
Note for anyone following along at (working-from-) home: although this
code was cargo culted from kbase - kbase is fine because it takes a pfn
and doesn't do the round_up() stage.
Which also exposes the second bug (fixed in patch 2): a size_t isn't big
enough on 32 bit platforms (all Midgard/Bifrost GPUs have a VA size
bigger than 32 bits). Again kbase gets away with a u32 because it's a pfn.
There is potentially a third bug which kbase only recently attempted to
fix. The lock address is effectively rounded down by the hardware (the
bottom bits are ignored). So if you have mask=(1<<region_width)-1 but
(iova & mask) != ((iova + size) & mask) then you are potentially failing
to lock the end of the intended region. kbase has added some code to
handle this:
> /* Round up if some memory pages spill into the next region. */
> region_frame_number_start = pfn >> (lockaddr_size_log2 - PAGE_SHIFT);
> region_frame_number_end =
> (pfn + num_pages - 1) >> (lockaddr_size_log2 - PAGE_SHIFT);
>
> if (region_frame_number_start < region_frame_number_end)
> lockaddr_size_log2 += 1;
I guess we should too?
Steve
> - /* not pow2, so must go up to the next pow2 */
> - region_width += 1;
> - }
> + /* The size is encoded as ceil(log2) minus(1), which may be calculated
> + * with fls. The size must be clamped to hardware bounds.
> + */
> + size = max_t(u64, size, PAGE_SIZE);
> + region_width = fls64(size - 1) - 1;
> region |= region_width;
>
> /* Lock the region that needs to be updated */
>
next prev parent reply other threads:[~2021-08-23 9:40 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-20 21:31 [PATCH 0/3] drm/panfrost: Bug fixes for lock_region Alyssa Rosenzweig
2021-08-20 21:31 ` [PATCH 1/3] drm/panfrost: Simplify lock_region calculation Alyssa Rosenzweig
2021-08-23 9:40 ` Steven Price [this message]
2021-08-23 21:09 ` Alyssa Rosenzweig
2021-08-23 21:54 ` Steven Price
2021-08-20 21:31 ` [PATCH 2/3] drm/panfrost: Use u64 for size in lock_region Alyssa Rosenzweig
2021-08-23 9:40 ` Steven Price
2021-08-23 21:11 ` Alyssa Rosenzweig
2021-08-23 21:57 ` Steven Price
2021-08-20 21:31 ` [PATCH 3/3] drm/panfrost: Clamp lock region to Bifrost minimum Alyssa Rosenzweig
2021-08-23 9:40 ` Steven Price
2021-08-23 21:13 ` Alyssa Rosenzweig
2021-08-23 22:02 ` Steven Price
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=192e5a1b-2caf-11a8-f090-ec5649ea16b5@arm.com \
--to=steven.price@arm.com \
--cc=airlied@linux.ie \
--cc=alyssa.rosenzweig@collabora.com \
--cc=daniel@ffwll.ch \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=macromorgan@hotmail.com \
--cc=robh@kernel.org \
--cc=stable@vger.kernel.org \
--cc=tomeu.vizoso@collabora.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®