Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp243331pxb; Wed, 25 Aug 2021 02:06:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz5H2iGiyEbmFcrGkMdtA7Va+YEhjntqbjWJUak3XdndCgcGhOkSr0dSESie4VpLladK/zO X-Received: by 2002:a6b:28b:: with SMTP id 133mr34912852ioc.107.1629882372819; Wed, 25 Aug 2021 02:06:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629882372; cv=none; d=google.com; s=arc-20160816; b=Gg0OcgbMqCEBtj1QT5dY5QnueQnYFj2cX7DHqHjpYTgNMWBwBD2TdvhnRp4dsLfq2P 6RY7SW9VM3caefIWHm1rvA8ToNeRtluc8SunYHcpj5i5olRlLTAahOT7oMsTdebtUhyY YsQT4xKe4t7n7oCbmasQ1MZEHngwLfY7CyIfBzQ9hbVe4l8CljxMDTlw4FIWaR1K8deY wZmU3x9AQIgQpzI5hik2/5+yiNKMEzWxeC5mxfRjFFxMs3I605GiG38Jd+hZzPYkhhsv JMdo1Io/KKGrTwBueVuJjBd6YUq4zIfxeHgqeD3xb3S3j98nXRw3rokWFihVwycF10Qw /6RA== 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=QQOROxb/puF1U8/lWGhekXywH0Ee1QBMEqY8TiIrjPo=; b=NMXGe1MmiFdpntkBKpAuu122ROuzV2MG5GzQXiFEMZaML0hy16SlxWSpHEDq9uSM4G 44dw2/0CivfX5061/frojjpGDgENhxhdFpApeWS+oh3kboDOo8VTcyjrySSq1VW/jRBT GYEx7D+rYtVKwD/WEx8GFdJd8lYkvUeI4obE4nmNoYXvabNarGD5B3sFSmH1xBt0OPPE lYnHGDTpk793rxF/Qplfamz/LTVWOEkZdu2aIBOgjWFkwBdEDcfYf9fn7PRgepQhTvRg 4JCzQCqqgNj/STDTz+x25Zuujk4Nmj574FFBy6xQ27ytEL08UCS1f94iEa2hJJ6vo2zW mEqw== 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 n6si2807480ilk.152.2021.08.25.02.06.01; Wed, 25 Aug 2021 02:06:12 -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 S239403AbhHYJEw (ORCPT + 99 others); Wed, 25 Aug 2021 05:04:52 -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 */ >