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 X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 68105C432BE for ; Wed, 25 Aug 2021 09:04:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 44AFC610CD for ; Wed, 25 Aug 2021 09:04:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239421AbhHYJEx (ORCPT ); Wed, 25 Aug 2021 05:04:53 -0400 Received: from foss.arm.com ([217.140.110.172]:46236 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232302AbhHYJEv (ORCPT ); Wed, 25 Aug 2021 05:04:51 -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 CDFB131B; Wed, 25 Aug 2021 02:04:05 -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 802A73F66F; Wed, 25 Aug 2021 02:04:04 -0700 (PDT) Subject: Re: [PATCH v2 1/4] drm/panfrost: Simplify lock_region calculation To: Alyssa Rosenzweig , dri-devel@lists.freedesktop.org Cc: Rob Herring , Tomeu Vizoso , David Airlie , Daniel Vetter , linux-kernel@vger.kernel.org, Chris Morgan , stable@vger.kernel.org References: <20210824173028.7528-1-alyssa.rosenzweig@collabora.com> <20210824173028.7528-2-alyssa.rosenzweig@collabora.com> From: Steven Price Message-ID: <698bbb98-5fd8-d6cd-b8cd-0ff29573314c@arm.com> Date: Wed, 25 Aug 2021 10:03:59 +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-2-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: > In lock_region, simplify the calculation of the region_width parameter. > This field is the size, but encoded as ceil(log2(size)) - 1. > ceil(log2(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 (and completely wrong) behaviour when locking all > memory (size ~0). In this case, the old code would "round up" ~0 to the > nearest page, overflowing to 0. Since fls(0) == 0, this would calculate > a region width of 10 + 0 = 10. But then the code would shift by > (region_width - 11) = -1. As shifting by a negative number is undefined, > UBSAN flags the bug. Of course, even if it were defined the behaviour is > wrong, instead of locking all memory almost none would get locked. > > The new form of the calculation corrects this special case and avoids > the undefined behaviour. > > Signed-off-by: Alyssa Rosenzweig > Reported-and-tested-by: Chris Morgan > Fixes: f3ba91228e8e ("drm/panfrost: Add initial panfrost driver") > Cc: Reviewed-by: Steven Price > --- > 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); > > - region_width = 10 + fls(size >> PAGE_SHIFT); > - if ((size >> PAGE_SHIFT) != (1ul << (region_width - 11))) { > - /* 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 */ >