Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1801925pxb; Fri, 20 Aug 2021 14:33:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6XiwRhedKN36VktIWhgHPhZ2i4x2Tp3p2gJlVvmLk9OEHb8nl38faookxMNIxSMwbmtO6 X-Received: by 2002:a17:906:4310:: with SMTP id j16mr24039334ejm.182.1629495212659; Fri, 20 Aug 2021 14:33:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629495212; cv=none; d=google.com; s=arc-20160816; b=w0TkDKQhIkxIJYJJzyajRbU+4E0pQVuTNewKIqF3Zihfk+TCjC3DmN0v37ONfoUzFb 4Ff8MX6exCx/QyaMBdCvaEXRN80FTJd6DRb4EPDlRNKzU9TaYLejyECGP8Ec+zGq1CYj Axk2vEAF5auK7cQTAIP1c2jcehra+km00bNzeOCt8Bns91958LfvXCUrpHiZkwiF5WBy 98YFLbxiBnMyDGosvCTLD4eQYF2kiN1NVYNEck37eP0y8UKGmwgxiukSNxSgwUcysJs1 8YcWOcLTkTOEmFSJcXOmpqY02s3X5HCgUr0OCHKRESr1jc/+JSeBflbI/9+/xLDV8TYb dJdQ== 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 :message-id:date:subject:cc:to:from; bh=xoM2Hyb2e5hkSjFmb+pGka0zdLJrr+rks52RhK35B3o=; b=Vhny8Zm5sHmfH45rDe8VaxAt2GqqqRbk2N92nndKjHS/+4iRwghIhOaSFf/Rz8ciWk bqlFGBQQzq++fsBOSPAAqofeKxjnczLB/qqQkQ7jSNPG/zG8AglF4TbZ05jKGrm8hfmr 1SkltK7chMkTTcdWjaWjlj1h04M64lMoQyzUCPYlPHqxorYDpfb6aESTVDdCfJ68zoZr Karq1KlKLDoV4ZO6RCSI8/I7XNR5/9LSzCvyj+jwXOAjrI2ljzABDu391n8aXxgSXLIV b0PPsAqBBXbiV9N+i10rYh0H023mQy+BFAW3krS6XeWq9WVDDHxR4u/gxHEYqX3gSes+ T0ww== 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 v15si495533edc.457.2021.08.20.14.33.01; Fri, 20 Aug 2021 14:33:32 -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 S240792AbhHTVcS (ORCPT + 99 others); Fri, 20 Aug 2021 17:32:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231761AbhHTVcP (ORCPT ); Fri, 20 Aug 2021 17:32:15 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A802C061575 for ; Fri, 20 Aug 2021 14:31:37 -0700 (PDT) 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 C7CEF1F44A8F; Fri, 20 Aug 2021 22:31:33 +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 Subject: [PATCH 0/3] drm/panfrost: Bug fixes for lock_region Date: Fri, 20 Aug 2021 17:31:14 -0400 Message-Id: <20210820213117.13050-1-alyssa.rosenzweig@collabora.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Chris Morgan reported UBSAN errors in panfrost and tracked them down to the size computation in lock_region. This calculation is overcomplicated (seemingly cargo culted from kbase) and can be simplified with kernel helpers and some mathematical identities. The first patch in the series rewrites the calculation in a form avoiding undefined behaviour; Chris confirms it placates UBSAN. While researching this function, I noticed a pair of other potential bugs: Bifrost can lock more than 4GiB at a time, but must lock at least 32KiB at a time. The latter patches in the series handle these cases. The size computation was unit-tested in userspace. Relevant code below, just missing some copypaste definitions for fls64/clamp/etc: #define MIN_LOCK (1ULL << 12) #define MAX_LOCK (1ULL << 48) struct { uint64_t size; uint8_t encoded; } tests[] = { /* Clamping */ { 0, 11 }, { 1, 11 }, { 2, 11 }, { 4095, 11 }, /* Power of two */ { 4096, 11 }, /* Round up */ { 4097, 12 }, { 8192, 12 }, { 16384, 13 }, { 16385, 14 }, /* Maximum */ { ~0ULL, 47 }, }; static uint8_t region_width(uint64_t size) { size = clamp(size, MIN_LOCK, MAX_LOCK); return fls64(size - 1) - 1; } int main(int argc, char **argv) { for (unsigned i = 0; i < ARRAY_SIZE(tests); ++i) { uint64_t test = tests[i].size; uint8_t expected = tests[i].encoded; uint8_t actual = region_width(test); assert(expected == actual); } } Alyssa Rosenzweig (3): drm/panfrost: Simplify lock_region calculation drm/panfrost: Use u64 for size in lock_region drm/panfrost: Clamp lock region to Bifrost minimum drivers/gpu/drm/panfrost/panfrost_mmu.c | 31 +++++++++--------------- drivers/gpu/drm/panfrost/panfrost_regs.h | 2 ++ 2 files changed, 13 insertions(+), 20 deletions(-) -- 2.30.2