Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1804087pxb; Fri, 20 Aug 2021 14:37:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJys+YDY3hD3L2QU8yGXGGUQvquU4q64rMCYVCfhR67zLGUjYNJXXJEv8wVv5AK/jC0eUOGf X-Received: by 2002:a17:906:84a:: with SMTP id f10mr23002984ejd.386.1629495464582; Fri, 20 Aug 2021 14:37:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629495464; cv=none; d=google.com; s=arc-20160816; b=BHvhHPncL5U1hWyQ7m3NxCfGaBol1DExxD+wiBv0Iyr2F1mW/wm0WtDpHpPoDzmSF1 +GeqyV+cKTQGM+HAVgiztBTaRUtYhVLEhN45jyASeQBjHHjtTmq/dABups23+WHGvFAx 0cz2LWf0yFdkrVh+NvX3YFqC11vUVIYiDhdWU9L5tOEPZsM4g3YRdWkilmv4zQzVh3AD odTVgJxW8g5n4oeN1bHEGAw0cQYUZyl14eKnjtOC4u6hLgO3uGDTOalnHR7+2CloaIY4 b0vbpwqhy5sPR5GQa9EiZJ3b3GikTd1/rzdfTBOEl6EobeFZJHMzGSmNl1u6ZapLOLnA Wkgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=jrGy2DJsSPOfJZgopcbt2NXltZR0xTlX5IlRGepVuN4=; b=quuayLJQaIgP30g3cewvIPvkQI247ycabG/03P37RrP0j5zm6ekwUSdWrycVW2GVx9 gltcaa/iFyrRF1EWru5HFLk/FPWu93dWi0kQrPS38wIgz1w/AbzDC8GpzzudZzMXUYLR L0ztbTRaRUYVwtHGuCHeeB7bzCMLnNbC0WMQJ9x0jwuUCDLjvVf9KhhJczlIW7OUohz+ 0G9afRJJPS2SFXX7JAZAkDzvg7MTArpNeltudzNcQUBcmJlqC0EmQZxkyZaiS62BKThh dkiN6UrUJzq0sBkEHRPNpJjI/ycTUkLXJP3CjYff3pEm/IRZfxr1iGlUUsqcJC+zJGMZ WvLw== 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=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id du15si7641123ejc.498.2021.08.20.14.37.15; Fri, 20 Aug 2021 14:37:44 -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=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240840AbhHTVcT (ORCPT + 99 others); Fri, 20 Aug 2021 17:32:19 -0400 Received: from bhuna.collabora.co.uk ([46.235.227.227]:46134 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240695AbhHTVcS (ORCPT ); Fri, 20 Aug 2021 17:32:18 -0400 Received: from localhost.localdomain (unknown [IPv6:2600:8800:8c06:1000::c8f3]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: alyssa) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id D758B1F44A90; Fri, 20 Aug 2021 22:31:35 +0100 (BST) From: Alyssa Rosenzweig To: dri-devel@lists.freedesktop.org Cc: Rob Herring , Tomeu Vizoso , Steven Price , Alyssa Rosenzweig , David Airlie , Daniel Vetter , linux-kernel@vger.kernel.org, Chris Morgan , stable@vger.kernel.org Subject: [PATCH 1/3] drm/panfrost: Simplify lock_region calculation Date: Fri, 20 Aug 2021 17:31:15 -0400 Message-Id: <20210820213117.13050-2-alyssa.rosenzweig@collabora.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210820213117.13050-1-alyssa.rosenzweig@collabora.com> References: <20210820213117.13050-1-alyssa.rosenzweig@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Alyssa Rosenzweig Reported-and-tested-by: Chris Morgan Cc: --- 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 */ -- 2.30.2