Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp626957rwb; Tue, 29 Nov 2022 03:20:46 -0800 (PST) X-Google-Smtp-Source: AA0mqf69AQ+H7J6Zg07XcHhSggg7J7tW/j9DUwoiP7yiTehLJHRd82f53QzuoxbrG4zG5Q9XhjcY X-Received: by 2002:a05:6402:370d:b0:462:1a67:75ef with SMTP id ek13-20020a056402370d00b004621a6775efmr34389518edb.16.1669720845842; Tue, 29 Nov 2022 03:20:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1669720845; cv=pass; d=google.com; s=arc-20160816; b=VcQfzto9u+JE9ZVDJXNM9TilrwMjWLUTswc1hDVPx4T8P0d9BjZIs86Qq2F9dNQAgj ckhwR3Vh8NYCp00VhZ90VV6wl7rpoM0DaDy/hJN65ShciF71iHxLX8GerQpOyUZJw7co hb+eJ5JZibQLdu6S4gLnDv2j11r1UnXhZ5X8iPUJND7WacUI3Kjq5ZeBMIWRiUwzp6Mx 7c6XDDuIV7To+0oaQc8aRRoprWBrlGkLO56dpIjvRTPhJU1ZnN9PVCSKxORk10lE+jEo F9T3K88gJwwxjNbFxZRcsQD1Gh76l79QGUVuELoL7Zy2ZaMlP4AY1hOrmBZsAfqtV7LF 9csA== ARC-Message-Signature: i=2; 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:dkim-signature; bh=7XtRd8JrTsbaoCmuxFe3286dfOo1NMfjWxA+WMCp0s0=; b=HS/QLKsAft+iDbwney1ZWB14Ud3hNSyZNvftHR5p8e3lOlMhaDk8rbnQm1eWAoUwfp 3JqXIRrvc1dyG2hOd7CjJNjJRDFuUNkJg5feS4yiWGDebLAc7NP5l9ifA6dKNsXzMirD c4HwAaUsBqD+I6rlhH/mK7R2Ef0HriFf3f4mFzErTB8A+3ItND7SFgZIskCTgyi8caZo JTUyalfhh48O2gsJjm9aUvJnYR5hYbndm0yND4AdmN03y+D24tRj0aEO+xHSMQss4d1m Y0kJRl5HZKaBgjwA7LZkMwibEkCznWf1tH/CdnkhNEcKW9bxVaLQhwK7F5OKy0bIIygM vAzQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=mpiT5Vrs; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x9-20020a05640225c900b00458d94f1a45si11488130edb.413.2022.11.29.03.20.22; Tue, 29 Nov 2022 03:20:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=mpiT5Vrs; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232234AbiK2K5V (ORCPT + 83 others); Tue, 29 Nov 2022 05:57:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229953AbiK2K5R (ORCPT ); Tue, 29 Nov 2022 05:57:17 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2074.outbound.protection.outlook.com [40.107.212.74]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 924885F872 for ; Tue, 29 Nov 2022 02:57:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QKOaZOUJDnXeFE2DRXeSyeIgVhFGC6PpSQ0z1giLLtSlJC/NaVS4dMx/uZqrh//SwCxvj1Ulm+9/b3Gg4eA3E/V4QJWO5It/udB93kBL8nsaQg/+cPFsjIz9U4NXezyqQG74GwbPOTLDu6O7Gg/wKstvEEocDZd+64jDltV3WVcaZ4x2khXLheVAaurHIPU4ovG6cwsw0uX+T4jmhkndXepSgpqoSvXkpqPga+QM8CfB0krwhoynsT+J7M5zB5sG+TRuGAIM3DLMDvBAqtplcWVmwpd+u5ANCS5HoE25QLZ/H5T8AymFVptkyYY+pPK4uyKCX+ZL6NVaKAR2AQYz4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=7XtRd8JrTsbaoCmuxFe3286dfOo1NMfjWxA+WMCp0s0=; b=lDqlku5XlAlU8nD7LZ9MuHX1YYNIfZ6L6AS8OK7CBx0mWiGVSAdQ1c6cjJ8ZvjUlPA/bp68qKsh7PKCpIIQ4vpvICxTM1Jt7btEffYmLgdinkS55Reckc2t/wkt5uUrWAhV0VMiouFTyKwyqxgJe+5S5nId91GTKv9zFsByReciRnsGKQTbQJGNqxgKHAWXz2wWQFNtnuwKsM3L5G4mJY9uVQul9Met2slLzPG/mmM9cgeEfbmBnDqdByghc8tj8BotNj3mOmUMP7YaQ1tp5rEie5i1iwQBHPFG17YMw3OqzUtSf582UBUV+TnnHKc6MyyobkDpLNGDBu7ARJhjMeA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.freedesktop.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7XtRd8JrTsbaoCmuxFe3286dfOo1NMfjWxA+WMCp0s0=; b=mpiT5VrswrfY8dV8mn57g2VUtBXWFg5Vy+rweM84VuMM7jhSArcxhHhZfsNYNYFs4nSKZD6GCjXKW1FwgbHLs/2MTYPRVuxeUj3dzv0HldeUSuuwWCP+sulWSs2a+kVL0AqCw7BdONhDYegdN4bh5l54r6dlKde5hdABYKn7L90= Received: from BL1PR13CA0356.namprd13.prod.outlook.com (2603:10b6:208:2c6::31) by IA1PR12MB6329.namprd12.prod.outlook.com (2603:10b6:208:3e5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5857.21; Tue, 29 Nov 2022 10:57:13 +0000 Received: from BL02EPF0000EE3F.namprd05.prod.outlook.com (2603:10b6:208:2c6:cafe::10) by BL1PR13CA0356.outlook.office365.com (2603:10b6:208:2c6::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5857.23 via Frontend Transport; Tue, 29 Nov 2022 10:57:13 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BL02EPF0000EE3F.mail.protection.outlook.com (10.167.241.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5857.17 via Frontend Transport; Tue, 29 Nov 2022 10:57:13 +0000 Received: from pp-server-two.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Tue, 29 Nov 2022 04:57:10 -0600 From: xinhui pan To: CC: , , , , , , , "xinhui pan" Subject: [PATCH v4] drm: Optimise for continuous memory allocation Date: Tue, 29 Nov 2022 18:56:55 +0800 Message-ID: <20221129105655.125571-1-xinhui.pan@amd.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000EE3F:EE_|IA1PR12MB6329:EE_ X-MS-Office365-Filtering-Correlation-Id: 219552b0-a752-491b-6ce5-08dad1f878c0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zM0XEtkIjUk2Ja42WyfvxeUuTHSutCiwKWGcOuIPFqLGFB9ApOm1Vli8HaCHJSvJyI99oomYzKzJUiZl6yO+aB280iCpyGaKh5v5sYsf/A/vbQ1YxBZn+2HNYFN8WN6j4j8hOVEPflw+IOMKjo21e2iv/nDVr4jPZyFjhgZGYvQeB4va9Jz08Kl891uPyL8QU7WuPO64TvvgRW8/4+n89m9GHD6kVekOUlmMS6y2Ol8ew8j5igOD0i22hCVpjmfW7hNEHvOZDvCDhe/XPL6L0IWzlis9KSL2ukQp/ftyRIgVmGgdscqzC5/nHID22Jsfpjj+kJP51WSK7WLnpWolnx2rJP0ghtHyfER/miMZFXTNPBlwEJ9Ub5PF5Y0caKsmUsZfvlxHjDNcLQXirpKaxeo1EXtl6EI5wlCYcY+CBOdzJLMfRhYXWEfrVzo/v0sWVGNrqYIQEntmj1nB/i6Q2++4xv/JnMr1cj8Vgh0+1JlP+UrRlwHyvsegqgXZYAke+LHLBR4NNaVIS+YFXn5btSOOFKLRypenK4iof9+tjCrLQE0QdKCVcFTrYeFpJezffZ/eVWaZ8G5t+V13wlzpFSdvxr9tzQOA9jRMX6D+pu8uf+10vpPf93glfaN3xtAGs4nG6R1sc0czdlGn3EZsDQD2pOYMB97DI80KzfhZBLvxgku4SG9R2oip1EtHoTyhm5ff5fDXq5hZxAT1ZaIytsIwHMDeok5u5saFICSH8J8= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(136003)(376002)(346002)(39860400002)(396003)(451199015)(40470700004)(36840700001)(46966006)(66899015)(36756003)(82310400005)(40480700001)(36860700001)(6666004)(7696005)(16526019)(426003)(47076005)(26005)(1076003)(2616005)(83380400001)(41300700001)(86362001)(8936002)(2906002)(5660300002)(356005)(81166007)(336012)(54906003)(316002)(6916009)(186003)(478600001)(70586007)(70206006)(40460700003)(82740400003)(8676002)(4326008)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Nov 2022 10:57:13.3848 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 219552b0-a752-491b-6ce5-08dad1f878c0 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000EE3F.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6329 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently drm-buddy does not have full knowledge of continuous memory. Lets consider scenario below. order 1: L R order 0: LL LR RL RR for order 1 allocation, it can offer L or R or LR+RL. For now, we only implement L or R case for continuous memory allocation. So this patch aims to implement the rest cases. Adding a new member leaf_link which links all leaf blocks in asceding order. Now we can find more than 2 sub-order blocks easier. Say, order 4 can be combined with corresponding order 4, 2+2, 1+2+1, 0+1+2+0, 0+2+1+0. Signed-off-by: xinhui pan --- change from v3: reworked totally. adding leaf_link. change from v2: search continuous block in nearby root if needed change from v1: implement top-down continuous allocation --- drivers/gpu/drm/drm_buddy.c | 108 +++++++++++++++++++++++++++++++++--- include/drm/drm_buddy.h | 1 + 2 files changed, 102 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c index 11bb59399471..8edafb99b02c 100644 --- a/drivers/gpu/drm/drm_buddy.c +++ b/drivers/gpu/drm/drm_buddy.c @@ -80,6 +80,7 @@ int drm_buddy_init(struct drm_buddy *mm, u64 size, u64 chunk_size) { unsigned int i; u64 offset; + LIST_HEAD(leaf); if (size < chunk_size) return -EINVAL; @@ -136,6 +137,7 @@ int drm_buddy_init(struct drm_buddy *mm, u64 size, u64 chunk_size) goto out_free_roots; mark_free(mm, root); + list_add_tail(&root->leaf_link, &leaf); BUG_ON(i > mm->max_order); BUG_ON(drm_buddy_block_size(mm, root) < chunk_size); @@ -147,6 +149,7 @@ int drm_buddy_init(struct drm_buddy *mm, u64 size, u64 chunk_size) i++; } while (size); + list_del(&leaf); return 0; out_free_roots: @@ -205,6 +208,9 @@ static int split_block(struct drm_buddy *mm, mark_free(mm, block->left); mark_free(mm, block->right); + list_add(&block->right->leaf_link, &block->leaf_link); + list_add(&block->left->leaf_link, &block->leaf_link); + list_del(&block->leaf_link); mark_split(block); return 0; @@ -256,6 +262,9 @@ static void __drm_buddy_free(struct drm_buddy *mm, break; list_del(&buddy->link); + list_add(&parent->leaf_link, &block->leaf_link); + list_del(&buddy->leaf_link); + list_del(&block->leaf_link); drm_block_free(mm, block); drm_block_free(mm, buddy); @@ -386,6 +395,78 @@ alloc_range_bias(struct drm_buddy *mm, return ERR_PTR(err); } +static struct drm_buddy_block * +find_continuous_blocks(struct drm_buddy *mm, + int order, + unsigned long flags, + struct drm_buddy_block **rblock) +{ + struct list_head *head = &mm->free_list[order]; + struct drm_buddy_block *free_block, *max_block = NULL, *end, *begin; + u64 pages = BIT(order + 1); + u64 cur_pages; + + list_for_each_entry(free_block, head, link) { + if (max_block) { + if (!(flags & DRM_BUDDY_TOPDOWN_ALLOCATION)) + break; + + if (drm_buddy_block_offset(free_block) < + drm_buddy_block_offset(max_block)) + continue; + } + + cur_pages = BIT(order); + begin = end = free_block; + while (true) { + struct drm_buddy_block *prev, *next; + int prev_order, next_order; + + prev = list_prev_entry(begin, leaf_link); + if (!drm_buddy_block_is_free(prev) || + drm_buddy_block_offset(prev) > + drm_buddy_block_offset(begin)) { + prev = NULL; + } + next = list_next_entry(end, leaf_link); + if (!drm_buddy_block_is_free(next) || + drm_buddy_block_offset(next) < + drm_buddy_block_offset(end)) { + next = NULL; + } + if (!prev && !next) + break; + + prev_order = prev ? drm_buddy_block_order(prev) : -1; + next_order = next ? drm_buddy_block_order(next) : -1; + if (next_order >= prev_order) { + BUG_ON(drm_buddy_block_offset(end) + + drm_buddy_block_size(mm, end) != + drm_buddy_block_offset(next)); + end = next; + cur_pages += BIT(drm_buddy_block_order(next)); + } + if (prev_order >= next_order) { + BUG_ON(drm_buddy_block_offset(prev) + + drm_buddy_block_size(mm, prev) != + drm_buddy_block_offset(begin)); + begin = prev; + cur_pages += BIT(drm_buddy_block_order(prev)); + } + if (pages == cur_pages) + break; + BUG_ON(pages < cur_pages); + } + + if (pages > cur_pages) + continue; + + *rblock = end; + max_block = begin; + } + return max_block; +} + static struct drm_buddy_block * get_maxblock(struct list_head *head) { @@ -637,7 +718,7 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm, struct list_head *blocks, unsigned long flags) { - struct drm_buddy_block *block = NULL; + struct drm_buddy_block *block = NULL, *rblock = NULL; unsigned int min_order, order; unsigned long pages; LIST_HEAD(allocated); @@ -689,17 +770,30 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm, break; if (order-- == min_order) { + if (!(flags & DRM_BUDDY_RANGE_ALLOCATION) && + min_order != 0 && pages == BIT(order + 1)) { + block = find_continuous_blocks(mm, + order, + flags, + &rblock); + if (block) + break; + } err = -ENOSPC; goto err_free; } } while (1); - mark_allocated(block); - mm->avail -= drm_buddy_block_size(mm, block); - kmemleak_update_trace(block); - list_add_tail(&block->link, &allocated); - - pages -= BIT(order); + do { + mark_allocated(block); + mm->avail -= drm_buddy_block_size(mm, block); + kmemleak_update_trace(block); + list_add_tail(&block->link, &allocated); + pages -= BIT(drm_buddy_block_order(block)); + if (block == rblock || !rblock) + break; + block = list_next_entry(block, leaf_link); + } while (true); if (!pages) break; diff --git a/include/drm/drm_buddy.h b/include/drm/drm_buddy.h index 572077ff8ae7..c5437bd4f4f3 100644 --- a/include/drm/drm_buddy.h +++ b/include/drm/drm_buddy.h @@ -50,6 +50,7 @@ struct drm_buddy_block { */ struct list_head link; struct list_head tmp_link; + struct list_head leaf_link; }; /* Order-zero must be at least PAGE_SIZE */ -- 2.34.1