Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2186812pxb; Mon, 23 Aug 2021 14:11:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyHqy8RUOdPOJ4H6Cs8v1vQh5/iAfyLassYinfz/LXXwubzpNM8R8i0noFkOySN2JhwSDQB X-Received: by 2002:a05:6e02:20cd:: with SMTP id 13mr24056164ilq.156.1629753103084; Mon, 23 Aug 2021 14:11:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629753103; cv=none; d=google.com; s=arc-20160816; b=VqTUH1gvlo8Y7sMFEQftKKbH/pRZ0FufpoXMVFM7N6ycK22rNbyTwKDF8w+jKkno84 gINqt6rDVgMIKlUKwYFDzcfIcwOn934IZJUl8ECDHWOzBtwRY4ioPpstAkCw+dxzs9zp 31Rabgp9tu6r9VUe34Bkq8F3c+9VbAPFwsT6ngoLU4ldWOc8gddV/bzgOrFPs+wCYKRi elzZDebq+3cM0ydayINHvQ4KaeDc1ZKdgLNz4A/lxRSr5DHX3CbqwUNrd94MsrhbRwSK VytNS661lQaDoEWNU8zcsRH65GTajNVJOIax7OxFGKTn8XAZgyC75kQ0CgYPnmiH3NZT qUtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=8yzBKX5HOtHlg/3eq6ByGhWYmZADLI4oEtIgRlLNqaQ=; b=sY8aABzsDXalGNBjFzhVFaw5HiuFW5z6CiWy5CElZyIHu3GqBOeWE90nouhAEkXnWn qyoHGJvj5Gf5V3Hh5M7ZkAajRr7WbZvujpEv9MwBYAMEHD5xjwHiU1TkAig4ojeZ89Ic tRlLaPnrea336pmU9BaMC4fJ/ed47iEd4Xq+NZnX/EbHzjVw/XOteuJEDNyjiojbVMjk sft7uLhj1tSwhKlH4cYpeeMpnJSovwftkkXH/sRmu93x37oEdDUBBvk72lAHftdIc2iS Ftv1XdfxyDTfWlDjDR4pWZ7lRZ66zq3Cuk4gMd1+7ilP3WhWsCF1rbqvpEqfMuVk/BRS 9xWw== 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 a14si10783607iod.86.2021.08.23.14.11.20; Mon, 23 Aug 2021 14:11:43 -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 S232747AbhHWVKE (ORCPT + 99 others); Mon, 23 Aug 2021 17:10:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232503AbhHWVKD (ORCPT ); Mon, 23 Aug 2021 17:10:03 -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 3DDCBC061575; Mon, 23 Aug 2021 14:09:20 -0700 (PDT) Received: from maud (unknown [IPv6:2600:8800:8c06:1000::c8f3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: alyssa) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 336701F425BC; Mon, 23 Aug 2021 22:09:14 +0100 (BST) Date: Mon, 23 Aug 2021 17:09:08 -0400 From: Alyssa Rosenzweig To: Steven Price Cc: Alyssa Rosenzweig , dri-devel@lists.freedesktop.org, Rob Herring , Tomeu Vizoso , David Airlie , Daniel Vetter , linux-kernel@vger.kernel.org, Chris Morgan , stable@vger.kernel.org Subject: Re: [PATCH 1/3] drm/panfrost: Simplify lock_region calculation Message-ID: References: <20210820213117.13050-1-alyssa.rosenzweig@collabora.com> <20210820213117.13050-2-alyssa.rosenzweig@collabora.com> <192e5a1b-2caf-11a8-f090-ec5649ea16b5@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=unknown-8bit Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <192e5a1b-2caf-11a8-f090-ec5649ea16b5@arm.com> 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. > > 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). Indeed. I've updated the commit message in v2 to explain what goes wrong (your analysis was spot on, but a mailing list message is more ephermal than a commit message). I'll send out v2 tomorrow assuming nobody objects to v1 in the mean time. Thanks for the review. > 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< (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? Oh, I missed this one. Guess we have 4 bugs with this code instead of just 3, yikes. How could such a short function be so deeply and horribly broken? ???? Should I add a fourth patch to the series to fix this?