From patchwork Wed Mar 29 06:55:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13191949 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 57EA6C761AF for ; Wed, 29 Mar 2023 06:55:56 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 258868826D; Wed, 29 Mar 2023 06:55:54 +0000 (UTC) Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by gabe.freedesktop.org (Postfix) with ESMTPS id 396D110EA43 for ; Wed, 29 Mar 2023 06:55:52 +0000 (UTC) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-536a4eba107so146591307b3.19 for ; Tue, 28 Mar 2023 23:55:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680072951; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=Yb6T5Nix/3hrpi04qTRDKmlC5J5N+Z2zQezjqCwHQSc=; b=I70iBO8V621lm+2OBg9UTLn3NwNxaOjH81/kUFD8B1L+O1fwBhekEA+10JCdKiD3tA kECwcwUJvSNC88bYchl5stnbjXXBAo59zC+OgGa1lyXqpdfSBAdKUplbtKbZ6b4sx+Io fOQ63v3lHvtOi4jdjxoivD4NQGgJEUDTIXFSDL+0qLQm7CfslCBvEOlc6kmoSJRnTYhx pEEhi7f4mATzlqPFiW1w4VEErXrG7EzcULjUI2XpAcYsYU34pW9Tn9qbtYIEjkN0lEZr djC+4bkDVcIBW8KqxkB9oMzR05gvvb+gdeHsobNc5dq91YSjLozfTnVS9sd8c8258dDQ ayIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680072951; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Yb6T5Nix/3hrpi04qTRDKmlC5J5N+Z2zQezjqCwHQSc=; b=kHvmUITVCjMGI6zck8LM9nEfisfms+bdx89NOcWoECfDb0z0ZubIxnVWHwUIjHnsDa utAATVHjFDa+Y2UX1J0s2tlfeTbT04kRS7MePuF3K/qrsnf2NkTPh1DVG/9CT+9OaWTg HYHJuNcBlww9f/3YxKldQ89eb1nxJBdHXkb+LSgwNKJH3//+Fl1rIofTKNeX3KSTJkTW XH0VDWnccx/prwRCVMvbhYNBiCXKNxVNaQN/gTwyf0I53eGE58yvyr/flsn7kW2tSy5N 87UUDLZF/mEuX6VfqoaAIL0q7rcLGGA6/XQFHSfgkmD2fEJ+DdGFT8pXFaZ0wt/FxvtC yGyg== X-Gm-Message-State: AAQBX9eGpMhLXOHUqVdM5ot8mrNuiQ7N4c0A+LB9A5+cwbnh0zN1KBE0 TgH4JeV+BVJYTtPNC7W2QPXbYBDap1Ck7A== X-Google-Smtp-Source: AKy350bNaANkNE4LgBYqCLQFYMuy5mciHvuGb6vV8PhgtV00DdwwHufAEgSeA7hH9hghIP+DtRXhzI8HE8iwTQ== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:6902:1003:b0:b1d:5061:98e3 with SMTP id w3-20020a056902100300b00b1d506198e3mr12429289ybt.6.1680072951072; Tue, 28 Mar 2023 23:55:51 -0700 (PDT) Date: Wed, 29 Mar 2023 14:55:32 +0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230329065532.2122295-1-davidgow@google.com> Subject: [PATCH 1/2] drm: buddy_allocator: Fix buddy allocator init on 32-bit systems From: David Gow To: " =?utf-8?q?Lu=C3=ADs_Mendes?= " , " =?utf-8?q?Christian_K=C3=B6nig?= " , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , " =?utf-8?q?Ma=C3=ADra_Canal?= " , Arthur Grillo X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dri-devel@lists.freedesktop.org, David Gow , linux-kernel@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The drm buddy allocator tests were broken on 32-bit systems, as rounddown_pow_of_two() takes a long, and the buddy allocator handles 64-bit sizes even on 32-bit systems. This can be reproduced with the drm_buddy_allocator KUnit tests on i386: ./tools/testing/kunit/kunit.py run --arch i386 \ --kunitconfig ./drivers/gpu/drm/tests drm_buddy (It results in kernel BUG_ON() when too many blocks are created, due to the block size being too small.) This was independently uncovered (and fixed) by Luís Mendes, whose patch added a new u64 variant of rounddown_pow_of_two(). This version instead recalculates the size based on the order. Reported-by: Luís Mendes Link: https://lore.kernel.org/lkml/CAEzXK1oghXAB_KpKpm=-CviDQbNaH0qfgYTSSjZgvvyj4U78AA@mail.gmail.com/T/ Signed-off-by: David Gow Acked-by: Christian König for the series. --- drivers/gpu/drm/drm_buddy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c index 3d1f50f481cf..7098f125b54a 100644 --- a/drivers/gpu/drm/drm_buddy.c +++ b/drivers/gpu/drm/drm_buddy.c @@ -146,8 +146,8 @@ int drm_buddy_init(struct drm_buddy *mm, u64 size, u64 chunk_size) unsigned int order; u64 root_size; - root_size = rounddown_pow_of_two(size); - order = ilog2(root_size) - ilog2(chunk_size); + order = ilog2(size) - ilog2(chunk_size); + root_size = chunk_size << order; root = drm_block_alloc(mm, NULL, order, offset); if (!root) From patchwork Wed Mar 29 06:55:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13191950 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 36EEAC6FD18 for ; Wed, 29 Mar 2023 06:56:12 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4A50B10E4C2; Wed, 29 Mar 2023 06:56:11 +0000 (UTC) Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by gabe.freedesktop.org (Postfix) with ESMTPS id 59D5A10E257 for ; Wed, 29 Mar 2023 06:56:09 +0000 (UTC) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54574d6204aso145489207b3.15 for ; Tue, 28 Mar 2023 23:56:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680072968; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+6FGLc7DezyggwEDA6SlL49TwBy1ESwTS9CYC+SnZVM=; b=ip2ObvbwjKAThNEYJ+mAP6NxZUtLziQmG7qqimp6FSTliXsWvpp5OlH/S6qdrm6D4K IZicU6MzuIcQCE61oioHol6VvVAaXHIE2dhWe8vuYyDhNoqEF4mloHHEnoPtfVHcGjfO wyOpfLQ6H4o5KUW557F4zn4wpZzfuWs34QzeytMTnFRjkNgloAfzhWoWJ4+dReISEOZP 1rYuW6kB2TWKMSZvrnsbDg6JDBJqvZGSBzqMLAdDCWB8oHH9sNVSUcHox19tuqORYQv+ VXEem2/SMiKw6ug/GxM5zdBr9xY2Tv/ZorcJKFJg0KtMQRKjxshwFRGO2BOugKdYpD2h yDJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680072968; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+6FGLc7DezyggwEDA6SlL49TwBy1ESwTS9CYC+SnZVM=; b=MNIp9/XbLRdBsvaL0N0L+Hjm/LaEocj5DPx3V9OhcF4TfDjbEbAGfRtG1xnp8PSyS6 9iCiejX6LstiCGGDVq2gAZLaWh4D24UjnyMuNNUgjdliO+g45nGh09QF1EJwN03wCazU DYfP3Bd/GvT3p1flbmMEONUSwzG6cB/CglebH3gr7x6Utdyo5YDHLh9vGqibghjaEhR4 k6ZcujvLU/o0SnJYZ7+u0ZwbXuVdJ3EGR+NYJjtw2VyKcYovJaQIAnJAuoSHt5nBcvSi yooeOVzN3+I1Nk6kbczvF+UmQ8X7ayChgmzSPK7kPgWgHuZ/3a8HS5EzRKVRqlhn24Ky gAOw== X-Gm-Message-State: AAQBX9fA06A72uL1lh3GW4uLTbcfbzTR252b8h4gf+eij68zA5MhLTr+ +FQvZWVDciioFdZQllRRs/MyjbcuaElOrg== X-Google-Smtp-Source: AKy350aW0oXIdid/PamI5UXVN98qdXztLtQJ5ZEohZbEr95Se93u+cliygWLulbEfIGKqnK6l7npl+MIXUEaIA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:6902:1547:b0:b46:4a5e:365f with SMTP id r7-20020a056902154700b00b464a5e365fmr12420681ybu.8.1680072968620; Tue, 28 Mar 2023 23:56:08 -0700 (PDT) Date: Wed, 29 Mar 2023 14:55:34 +0800 In-Reply-To: <20230329065532.2122295-1-davidgow@google.com> Mime-Version: 1.0 References: <20230329065532.2122295-1-davidgow@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230329065532.2122295-2-davidgow@google.com> Subject: [PATCH 2/2] drm: test: Fix 32-bit issue in drm_buddy_test From: David Gow To: " =?utf-8?q?Lu=C3=ADs_Mendes?= " , " =?utf-8?q?Christian_K=C3=B6nig?= " , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , " =?utf-8?q?Ma=C3=ADra_Canal?= " , Arthur Grillo X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dri-devel@lists.freedesktop.org, David Gow , linux-kernel@vger.kernel.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The drm_buddy_test KUnit tests verify that returned blocks have sizes which are powers of two using is_power_of_2(). However, is_power_of_2() operations on a 'long', but the block size is a u64. So on systems where long is 32-bit, this can sometimes fail even on correctly sized blocks. This only reproduces randomly, as the parameters passed to the buddy allocator in this test are random. The seed 0xb2e06022 reproduced it fine here. For now, just hardcode an is_power_of_2() implementation using x & (x - 1). Signed-off-by: David Gow Reviewed-by: Maíra Canal --- There are actually a couple of is_power_of_2_u64() implementations already around in: - drivers/gpu/drm/i915/i915_utils.h - fs/btrfs/misc.h (called is_power_of_two_u64) So the ideal thing would be to consolidate these in one place. --- drivers/gpu/drm/tests/drm_buddy_test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/tests/drm_buddy_test.c b/drivers/gpu/drm/tests/drm_buddy_test.c index f8ee714df396..09ee6f6af896 100644 --- a/drivers/gpu/drm/tests/drm_buddy_test.c +++ b/drivers/gpu/drm/tests/drm_buddy_test.c @@ -89,7 +89,8 @@ static int check_block(struct kunit *test, struct drm_buddy *mm, err = -EINVAL; } - if (!is_power_of_2(block_size)) { + /* We can't use is_power_of_2() for a u64 on 32-bit systems. */ + if (block_size & (block_size - 1)) { kunit_err(test, "block size not power of two\n"); err = -EINVAL; }