From mboxrd@z Thu Jan 1 00:00:00 1970 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 75764C433FE for ; Sun, 9 Jan 2022 14:20:38 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2FD0710EA05; Sun, 9 Jan 2022 14:20:32 +0000 (UTC) Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2043.outbound.protection.outlook.com [40.107.93.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id 759AE10E784; Sun, 9 Jan 2022 14:20:29 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nCB4UqwKTcnhpG3mzC8nqpiET//0oKuPsWUL8bA29MdvQeriSYgkpIk0xxknUOZhJyMQbWNACYue0BtHhMJNWQQOiSZYsKsQYVSxz/Fd/IT5v+uQTaYYFV09iAqr9LK0p7UpRyIlgUVE0KKIdvA+O1JLgNLCQ1EhEe3LQbWEXSKJ/4hBI/YJay7BfdnsBr80lh5u4+AQk1P+8rK6uwxqOpU7NoVuubsoN+d1roJ4yNgmt6p/n+TqpPvKRK3OtDAf+awFYuOzmFQ4wMGCE9bKOKFLthc2CAh9zg+zrdS6j3MOst+JLHUZys/qKuO8L6C8KDTfmTdc3nvmHzxy0FSMQg== 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=qD/CEqV5AcVc0Tn5E0LcCXd7GJiBZk2kFIlU/QeH3Jo=; b=oOlhlzJsgfOos+86VRpmFZ3nugz0Ittp4Ltyb/VUYmccDpnCCfLimEgTSSYXZsMIFkTX7+m/WVrrJtgJ5Vt+F+LTXCxTGadrR852Asa8/ifdlbq5jsdcR+C4EixuIFZsm/jXAht//Vi2pPEM5J/2kugk7zjDWl0Az/r9VbrtVLqG9da/HLWmGbJ1Yqjuf5bakMfZd2XlaRb1dZg96NYLopMI4bRifPu1ZTuQCW96bLqxP9+08ZqtquM6Ix0o9hiZCS9K32US9CdRnyBhPXkLqRdQQf/GZErYnEkdK6lo9s3LTIfeN0bPX3XhqG5umur3rCjx1kFiwTS/fzu1HYk0Aw== 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=qD/CEqV5AcVc0Tn5E0LcCXd7GJiBZk2kFIlU/QeH3Jo=; b=Uf6ejmuYeGWj9AruqzGdAR+9aF32ZgKA67uOj0kf+M4kE3bpVh+1mrmcplA+y4VsLSz5tNE/Vq/6wwf3Al4g99Mf+TwHH3mgA9++rK2Kx0OWHKLnf2K91L+D2WLief0M+4Ciij5pih3Ru9SvjflchmPWJpwW5xYJ6bWgZWZQxfY= Received: from BN1PR10CA0010.namprd10.prod.outlook.com (2603:10b6:408:e0::15) by DM4PR12MB5151.namprd12.prod.outlook.com (2603:10b6:5:392::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.9; Sun, 9 Jan 2022 14:20:25 +0000 Received: from BN8NAM11FT044.eop-nam11.prod.protection.outlook.com (2603:10b6:408:e0:cafe::31) by BN1PR10CA0010.outlook.office365.com (2603:10b6:408:e0::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.7 via Frontend Transport; Sun, 9 Jan 2022 14:20:25 +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; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT044.mail.protection.outlook.com (10.13.177.219) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4867.9 via Frontend Transport; Sun, 9 Jan 2022 14:20:25 +0000 Received: from rtg-amd.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.17; Sun, 9 Jan 2022 08:20:18 -0600 From: Arunpravin To: , , Subject: [PATCH v7 4/6] drm: implement a method to free unused pages Date: Sun, 9 Jan 2022 19:49:49 +0530 Message-ID: <20220109141951.134432-4-Arunpravin.PaneerSelvam@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220109141951.134432-1-Arunpravin.PaneerSelvam@amd.com> References: <20220109141951.134432-1-Arunpravin.PaneerSelvam@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d6add08f-d0d4-47a4-9591-08d9d37b2e2d X-MS-TrafficTypeDiagnostic: DM4PR12MB5151:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9W9G2f8BpkxutCYKXHH4llNgtOUQkADHThnr2XE9iiE7B0KiH6zP2B53LmYOlF5JKzl0K+Ras+FvdBLW0lT6eRuepWT69k0dfTH2OcHZf++KFYrSB/jkbHDQ2JV0vXbpWVQ0yM1J8FXOKQ4m3tb9mae5EMPxwWdSXXn1bMPvIIHFNkjuUKwa8ji+J+YzPjXHq0u4DF1Jkbnh6WJogtnN7b+tVe/ad/oh2R/6PmcEto6QSToeMZUXJGaCXjEqC+KH/EHOEtl4S/cxPRRbhLf6ZDuhfWu3iNlJmZqpSTNzAlgJ02JmV9hR8H503MR7Fo1aeUvLUV98ETlrjom5R9XUSqMYCpLdCP0DFWKm1DkCLaYBHIKF6OyMHggso8lVZm+ykAxRcexeB9s82spplFWnjZKCPDWB7WYZd6lQL5MGjXZiqJZcrw0byLPZNOOQtEAGbAZjJYunK/HNfeDecxHRuFUP46hClzYcZG2RGC2Tu0x/H4OTsSJHo5Hc+kQURfLCHs0rakn2MhWQ44065r7dymQbz7WRSHcnS2IHMZNNma20si2AxJzFILj+QenCn/puEsejRqoHqWPfwXQUWcJAwHZTaAAVgy99MN1G/EakHZ06ZNemI5mJiiM36kTRji7XYsHgy3O2VbJ3WbKPGyODxY9D9zzgFQ3cDIMHtXsFh6G3lHDfDuHIAVrwwVgl6mgavbi1nJWbLWKyqg4kYEAhN+tPhS+Tc9vtFJ7AuZYk0Iv//NXuudMvf6s0NjtygaUkTY7aS/zUggSeA3M997UYJO09AcNA4YHl3c1FCq3QTPc= 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:(4636009)(36840700001)(40470700002)(46966006)(8936002)(508600001)(82310400004)(47076005)(26005)(70206006)(70586007)(8676002)(16526019)(5660300002)(54906003)(186003)(4326008)(426003)(336012)(86362001)(2906002)(356005)(316002)(40460700001)(7696005)(81166007)(1076003)(83380400001)(6666004)(36860700001)(110136005)(36756003)(2616005)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jan 2022 14:20:25.8344 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d6add08f-d0d4-47a4-9591-08d9d37b2e2d 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: BN8NAM11FT044.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5151 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: Arunpravin , matthew.auld@intel.com, tzimmermann@suse.de, alexander.deucher@amd.com, christian.koenig@amd.com Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" On contiguous allocation, we round up the size to the *next* power of 2, implement a function to free the unused pages after the newly allocate block. v2(Matthew Auld): - replace function name 'drm_buddy_free_unused_pages' with drm_buddy_block_trim - replace input argument name 'actual_size' with 'new_size' - add more validation checks for input arguments - add overlaps check to avoid needless searching and splitting - merged the below patch to see the feature in action - add free unused pages support to i915 driver - lock drm_buddy_block_trim() function as it calls mark_free/mark_split are all globally visible v3(Matthew Auld): - remove trim method error handling as we address the failure case at drm_buddy_block_trim() function v4: - in case of trim, at __alloc_range() split_block failure path marks the block as free and removes it from the original list, potentially also freeing it, to overcome this problem, we turn the drm_buddy_block_trim() input node into a temporary node to prevent recursively freeing itself, but still retain the un-splitting/freeing of the other nodes(Matthew Auld) - modify the drm_buddy_block_trim() function return type v5(Matthew Auld): - revert drm_buddy_block_trim() function return type changes in v4 - modify drm_buddy_block_trim() passing argument n_pages to original_size as n_pages has already been rounded up to the next power-of-two and passing n_pages results noop Signed-off-by: Arunpravin --- drivers/gpu/drm/drm_buddy.c | 65 +++++++++++++++++++ drivers/gpu/drm/i915/i915_ttm_buddy_manager.c | 10 +++ include/drm/drm_buddy.h | 4 ++ 3 files changed, 79 insertions(+) diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c index 11356c2bb7aa..d97b0ba8aa1f 100644 --- a/drivers/gpu/drm/drm_buddy.c +++ b/drivers/gpu/drm/drm_buddy.c @@ -540,6 +540,71 @@ static int __drm_buddy_alloc_range(struct drm_buddy *mm, return __alloc_range(mm, &dfs, start, size, blocks); } +/** + * drm_buddy_block_trim - free unused pages + * + * @mm: DRM buddy manager + * @new_size: original size requested + * @blocks: output list head to add allocated blocks + * + * For contiguous allocation, we round up the size to the nearest + * power of two value, drivers consume *actual* size, so remaining + * portions are unused and it can be freed. + * + * Returns: + * 0 on success, error code on failure. + */ +int drm_buddy_block_trim(struct drm_buddy *mm, + u64 new_size, + struct list_head *blocks) +{ + struct drm_buddy_block *parent; + struct drm_buddy_block *block; + LIST_HEAD(dfs); + u64 new_start; + int err; + + if (!list_is_singular(blocks)) + return -EINVAL; + + block = list_first_entry(blocks, + struct drm_buddy_block, + link); + + if (!drm_buddy_block_is_allocated(block)) + return -EINVAL; + + if (new_size > drm_buddy_block_size(mm, block)) + return -EINVAL; + + if (!new_size && !IS_ALIGNED(new_size, mm->chunk_size)) + return -EINVAL; + + if (new_size == drm_buddy_block_size(mm, block)) + return 0; + + list_del(&block->link); + mark_free(mm, block); + mm->avail += drm_buddy_block_size(mm, block); + + /* Prevent recursively freeing this node */ + parent = block->parent; + block->parent = NULL; + + new_start = drm_buddy_block_offset(block); + list_add(&block->tmp_link, &dfs); + err = __alloc_range(mm, &dfs, new_start, new_size, blocks); + if (err) { + mark_allocated(block); + mm->avail -= drm_buddy_block_size(mm, block); + list_add(&block->link, blocks); + } + + block->parent = parent; + return err; +} +EXPORT_SYMBOL(drm_buddy_block_trim); + /** * drm_buddy_alloc_blocks - allocate power-of-two blocks * diff --git a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c index ae9201246bb5..626108fb9725 100644 --- a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c +++ b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c @@ -97,6 +97,16 @@ static int i915_ttm_buddy_man_alloc(struct ttm_resource_manager *man, if (unlikely(err)) goto err_free_blocks; + if (place->flags & TTM_PL_FLAG_CONTIGUOUS) { + u64 original_size = (u64)bman_res->base.num_pages << PAGE_SHIFT; + + mutex_lock(&bman->lock); + drm_buddy_block_trim(mm, + original_size, + &bman_res->blocks); + mutex_unlock(&bman->lock); + } + *res = &bman_res->base; return 0; diff --git a/include/drm/drm_buddy.h b/include/drm/drm_buddy.h index 424fc443115e..17ca928fce8e 100644 --- a/include/drm/drm_buddy.h +++ b/include/drm/drm_buddy.h @@ -145,6 +145,10 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm, struct list_head *blocks, unsigned long flags); +int drm_buddy_block_trim(struct drm_buddy *mm, + u64 new_size, + struct list_head *blocks); + void drm_buddy_free_block(struct drm_buddy *mm, struct drm_buddy_block *block); void drm_buddy_free_list(struct drm_buddy *mm, struct list_head *objects); -- 2.25.1 From mboxrd@z Thu Jan 1 00:00:00 1970 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 7EBB6C433F5 for ; Sun, 9 Jan 2022 14:20:37 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D219F10EE32; Sun, 9 Jan 2022 14:20:31 +0000 (UTC) Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2043.outbound.protection.outlook.com [40.107.93.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id 759AE10E784; Sun, 9 Jan 2022 14:20:29 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nCB4UqwKTcnhpG3mzC8nqpiET//0oKuPsWUL8bA29MdvQeriSYgkpIk0xxknUOZhJyMQbWNACYue0BtHhMJNWQQOiSZYsKsQYVSxz/Fd/IT5v+uQTaYYFV09iAqr9LK0p7UpRyIlgUVE0KKIdvA+O1JLgNLCQ1EhEe3LQbWEXSKJ/4hBI/YJay7BfdnsBr80lh5u4+AQk1P+8rK6uwxqOpU7NoVuubsoN+d1roJ4yNgmt6p/n+TqpPvKRK3OtDAf+awFYuOzmFQ4wMGCE9bKOKFLthc2CAh9zg+zrdS6j3MOst+JLHUZys/qKuO8L6C8KDTfmTdc3nvmHzxy0FSMQg== 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=qD/CEqV5AcVc0Tn5E0LcCXd7GJiBZk2kFIlU/QeH3Jo=; b=oOlhlzJsgfOos+86VRpmFZ3nugz0Ittp4Ltyb/VUYmccDpnCCfLimEgTSSYXZsMIFkTX7+m/WVrrJtgJ5Vt+F+LTXCxTGadrR852Asa8/ifdlbq5jsdcR+C4EixuIFZsm/jXAht//Vi2pPEM5J/2kugk7zjDWl0Az/r9VbrtVLqG9da/HLWmGbJ1Yqjuf5bakMfZd2XlaRb1dZg96NYLopMI4bRifPu1ZTuQCW96bLqxP9+08ZqtquM6Ix0o9hiZCS9K32US9CdRnyBhPXkLqRdQQf/GZErYnEkdK6lo9s3LTIfeN0bPX3XhqG5umur3rCjx1kFiwTS/fzu1HYk0Aw== 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=qD/CEqV5AcVc0Tn5E0LcCXd7GJiBZk2kFIlU/QeH3Jo=; b=Uf6ejmuYeGWj9AruqzGdAR+9aF32ZgKA67uOj0kf+M4kE3bpVh+1mrmcplA+y4VsLSz5tNE/Vq/6wwf3Al4g99Mf+TwHH3mgA9++rK2Kx0OWHKLnf2K91L+D2WLief0M+4Ciij5pih3Ru9SvjflchmPWJpwW5xYJ6bWgZWZQxfY= Received: from BN1PR10CA0010.namprd10.prod.outlook.com (2603:10b6:408:e0::15) by DM4PR12MB5151.namprd12.prod.outlook.com (2603:10b6:5:392::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.9; Sun, 9 Jan 2022 14:20:25 +0000 Received: from BN8NAM11FT044.eop-nam11.prod.protection.outlook.com (2603:10b6:408:e0:cafe::31) by BN1PR10CA0010.outlook.office365.com (2603:10b6:408:e0::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.7 via Frontend Transport; Sun, 9 Jan 2022 14:20:25 +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; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT044.mail.protection.outlook.com (10.13.177.219) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4867.9 via Frontend Transport; Sun, 9 Jan 2022 14:20:25 +0000 Received: from rtg-amd.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.17; Sun, 9 Jan 2022 08:20:18 -0600 From: Arunpravin To: , , Date: Sun, 9 Jan 2022 19:49:49 +0530 Message-ID: <20220109141951.134432-4-Arunpravin.PaneerSelvam@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220109141951.134432-1-Arunpravin.PaneerSelvam@amd.com> References: <20220109141951.134432-1-Arunpravin.PaneerSelvam@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d6add08f-d0d4-47a4-9591-08d9d37b2e2d X-MS-TrafficTypeDiagnostic: DM4PR12MB5151:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9W9G2f8BpkxutCYKXHH4llNgtOUQkADHThnr2XE9iiE7B0KiH6zP2B53LmYOlF5JKzl0K+Ras+FvdBLW0lT6eRuepWT69k0dfTH2OcHZf++KFYrSB/jkbHDQ2JV0vXbpWVQ0yM1J8FXOKQ4m3tb9mae5EMPxwWdSXXn1bMPvIIHFNkjuUKwa8ji+J+YzPjXHq0u4DF1Jkbnh6WJogtnN7b+tVe/ad/oh2R/6PmcEto6QSToeMZUXJGaCXjEqC+KH/EHOEtl4S/cxPRRbhLf6ZDuhfWu3iNlJmZqpSTNzAlgJ02JmV9hR8H503MR7Fo1aeUvLUV98ETlrjom5R9XUSqMYCpLdCP0DFWKm1DkCLaYBHIKF6OyMHggso8lVZm+ykAxRcexeB9s82spplFWnjZKCPDWB7WYZd6lQL5MGjXZiqJZcrw0byLPZNOOQtEAGbAZjJYunK/HNfeDecxHRuFUP46hClzYcZG2RGC2Tu0x/H4OTsSJHo5Hc+kQURfLCHs0rakn2MhWQ44065r7dymQbz7WRSHcnS2IHMZNNma20si2AxJzFILj+QenCn/puEsejRqoHqWPfwXQUWcJAwHZTaAAVgy99MN1G/EakHZ06ZNemI5mJiiM36kTRji7XYsHgy3O2VbJ3WbKPGyODxY9D9zzgFQ3cDIMHtXsFh6G3lHDfDuHIAVrwwVgl6mgavbi1nJWbLWKyqg4kYEAhN+tPhS+Tc9vtFJ7AuZYk0Iv//NXuudMvf6s0NjtygaUkTY7aS/zUggSeA3M997UYJO09AcNA4YHl3c1FCq3QTPc= 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:(4636009)(36840700001)(40470700002)(46966006)(8936002)(508600001)(82310400004)(47076005)(26005)(70206006)(70586007)(8676002)(16526019)(5660300002)(54906003)(186003)(4326008)(426003)(336012)(86362001)(2906002)(356005)(316002)(40460700001)(7696005)(81166007)(1076003)(83380400001)(6666004)(36860700001)(110136005)(36756003)(2616005)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jan 2022 14:20:25.8344 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d6add08f-d0d4-47a4-9591-08d9d37b2e2d 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: BN8NAM11FT044.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5151 Subject: [Intel-gfx] [PATCH v7 4/6] drm: implement a method to free unused pages X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Arunpravin , matthew.auld@intel.com, tzimmermann@suse.de, alexander.deucher@amd.com, christian.koenig@amd.com Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" On contiguous allocation, we round up the size to the *next* power of 2, implement a function to free the unused pages after the newly allocate block. v2(Matthew Auld): - replace function name 'drm_buddy_free_unused_pages' with drm_buddy_block_trim - replace input argument name 'actual_size' with 'new_size' - add more validation checks for input arguments - add overlaps check to avoid needless searching and splitting - merged the below patch to see the feature in action - add free unused pages support to i915 driver - lock drm_buddy_block_trim() function as it calls mark_free/mark_split are all globally visible v3(Matthew Auld): - remove trim method error handling as we address the failure case at drm_buddy_block_trim() function v4: - in case of trim, at __alloc_range() split_block failure path marks the block as free and removes it from the original list, potentially also freeing it, to overcome this problem, we turn the drm_buddy_block_trim() input node into a temporary node to prevent recursively freeing itself, but still retain the un-splitting/freeing of the other nodes(Matthew Auld) - modify the drm_buddy_block_trim() function return type v5(Matthew Auld): - revert drm_buddy_block_trim() function return type changes in v4 - modify drm_buddy_block_trim() passing argument n_pages to original_size as n_pages has already been rounded up to the next power-of-two and passing n_pages results noop Signed-off-by: Arunpravin --- drivers/gpu/drm/drm_buddy.c | 65 +++++++++++++++++++ drivers/gpu/drm/i915/i915_ttm_buddy_manager.c | 10 +++ include/drm/drm_buddy.h | 4 ++ 3 files changed, 79 insertions(+) diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c index 11356c2bb7aa..d97b0ba8aa1f 100644 --- a/drivers/gpu/drm/drm_buddy.c +++ b/drivers/gpu/drm/drm_buddy.c @@ -540,6 +540,71 @@ static int __drm_buddy_alloc_range(struct drm_buddy *mm, return __alloc_range(mm, &dfs, start, size, blocks); } +/** + * drm_buddy_block_trim - free unused pages + * + * @mm: DRM buddy manager + * @new_size: original size requested + * @blocks: output list head to add allocated blocks + * + * For contiguous allocation, we round up the size to the nearest + * power of two value, drivers consume *actual* size, so remaining + * portions are unused and it can be freed. + * + * Returns: + * 0 on success, error code on failure. + */ +int drm_buddy_block_trim(struct drm_buddy *mm, + u64 new_size, + struct list_head *blocks) +{ + struct drm_buddy_block *parent; + struct drm_buddy_block *block; + LIST_HEAD(dfs); + u64 new_start; + int err; + + if (!list_is_singular(blocks)) + return -EINVAL; + + block = list_first_entry(blocks, + struct drm_buddy_block, + link); + + if (!drm_buddy_block_is_allocated(block)) + return -EINVAL; + + if (new_size > drm_buddy_block_size(mm, block)) + return -EINVAL; + + if (!new_size && !IS_ALIGNED(new_size, mm->chunk_size)) + return -EINVAL; + + if (new_size == drm_buddy_block_size(mm, block)) + return 0; + + list_del(&block->link); + mark_free(mm, block); + mm->avail += drm_buddy_block_size(mm, block); + + /* Prevent recursively freeing this node */ + parent = block->parent; + block->parent = NULL; + + new_start = drm_buddy_block_offset(block); + list_add(&block->tmp_link, &dfs); + err = __alloc_range(mm, &dfs, new_start, new_size, blocks); + if (err) { + mark_allocated(block); + mm->avail -= drm_buddy_block_size(mm, block); + list_add(&block->link, blocks); + } + + block->parent = parent; + return err; +} +EXPORT_SYMBOL(drm_buddy_block_trim); + /** * drm_buddy_alloc_blocks - allocate power-of-two blocks * diff --git a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c index ae9201246bb5..626108fb9725 100644 --- a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c +++ b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c @@ -97,6 +97,16 @@ static int i915_ttm_buddy_man_alloc(struct ttm_resource_manager *man, if (unlikely(err)) goto err_free_blocks; + if (place->flags & TTM_PL_FLAG_CONTIGUOUS) { + u64 original_size = (u64)bman_res->base.num_pages << PAGE_SHIFT; + + mutex_lock(&bman->lock); + drm_buddy_block_trim(mm, + original_size, + &bman_res->blocks); + mutex_unlock(&bman->lock); + } + *res = &bman_res->base; return 0; diff --git a/include/drm/drm_buddy.h b/include/drm/drm_buddy.h index 424fc443115e..17ca928fce8e 100644 --- a/include/drm/drm_buddy.h +++ b/include/drm/drm_buddy.h @@ -145,6 +145,10 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm, struct list_head *blocks, unsigned long flags); +int drm_buddy_block_trim(struct drm_buddy *mm, + u64 new_size, + struct list_head *blocks); + void drm_buddy_free_block(struct drm_buddy *mm, struct drm_buddy_block *block); void drm_buddy_free_list(struct drm_buddy *mm, struct list_head *objects); -- 2.25.1 From mboxrd@z Thu Jan 1 00:00:00 1970 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 8DDADC433FE for ; Sun, 9 Jan 2022 14:20:35 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4913510EC6E; Sun, 9 Jan 2022 14:20:31 +0000 (UTC) Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2043.outbound.protection.outlook.com [40.107.93.43]) by gabe.freedesktop.org (Postfix) with ESMTPS id 759AE10E784; Sun, 9 Jan 2022 14:20:29 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nCB4UqwKTcnhpG3mzC8nqpiET//0oKuPsWUL8bA29MdvQeriSYgkpIk0xxknUOZhJyMQbWNACYue0BtHhMJNWQQOiSZYsKsQYVSxz/Fd/IT5v+uQTaYYFV09iAqr9LK0p7UpRyIlgUVE0KKIdvA+O1JLgNLCQ1EhEe3LQbWEXSKJ/4hBI/YJay7BfdnsBr80lh5u4+AQk1P+8rK6uwxqOpU7NoVuubsoN+d1roJ4yNgmt6p/n+TqpPvKRK3OtDAf+awFYuOzmFQ4wMGCE9bKOKFLthc2CAh9zg+zrdS6j3MOst+JLHUZys/qKuO8L6C8KDTfmTdc3nvmHzxy0FSMQg== 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=qD/CEqV5AcVc0Tn5E0LcCXd7GJiBZk2kFIlU/QeH3Jo=; b=oOlhlzJsgfOos+86VRpmFZ3nugz0Ittp4Ltyb/VUYmccDpnCCfLimEgTSSYXZsMIFkTX7+m/WVrrJtgJ5Vt+F+LTXCxTGadrR852Asa8/ifdlbq5jsdcR+C4EixuIFZsm/jXAht//Vi2pPEM5J/2kugk7zjDWl0Az/r9VbrtVLqG9da/HLWmGbJ1Yqjuf5bakMfZd2XlaRb1dZg96NYLopMI4bRifPu1ZTuQCW96bLqxP9+08ZqtquM6Ix0o9hiZCS9K32US9CdRnyBhPXkLqRdQQf/GZErYnEkdK6lo9s3LTIfeN0bPX3XhqG5umur3rCjx1kFiwTS/fzu1HYk0Aw== 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=qD/CEqV5AcVc0Tn5E0LcCXd7GJiBZk2kFIlU/QeH3Jo=; b=Uf6ejmuYeGWj9AruqzGdAR+9aF32ZgKA67uOj0kf+M4kE3bpVh+1mrmcplA+y4VsLSz5tNE/Vq/6wwf3Al4g99Mf+TwHH3mgA9++rK2Kx0OWHKLnf2K91L+D2WLief0M+4Ciij5pih3Ru9SvjflchmPWJpwW5xYJ6bWgZWZQxfY= Received: from BN1PR10CA0010.namprd10.prod.outlook.com (2603:10b6:408:e0::15) by DM4PR12MB5151.namprd12.prod.outlook.com (2603:10b6:5:392::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.9; Sun, 9 Jan 2022 14:20:25 +0000 Received: from BN8NAM11FT044.eop-nam11.prod.protection.outlook.com (2603:10b6:408:e0:cafe::31) by BN1PR10CA0010.outlook.office365.com (2603:10b6:408:e0::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.7 via Frontend Transport; Sun, 9 Jan 2022 14:20:25 +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; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT044.mail.protection.outlook.com (10.13.177.219) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.4867.9 via Frontend Transport; Sun, 9 Jan 2022 14:20:25 +0000 Received: from rtg-amd.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.17; Sun, 9 Jan 2022 08:20:18 -0600 From: Arunpravin To: , , Subject: [PATCH v7 4/6] drm: implement a method to free unused pages Date: Sun, 9 Jan 2022 19:49:49 +0530 Message-ID: <20220109141951.134432-4-Arunpravin.PaneerSelvam@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220109141951.134432-1-Arunpravin.PaneerSelvam@amd.com> References: <20220109141951.134432-1-Arunpravin.PaneerSelvam@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d6add08f-d0d4-47a4-9591-08d9d37b2e2d X-MS-TrafficTypeDiagnostic: DM4PR12MB5151:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9W9G2f8BpkxutCYKXHH4llNgtOUQkADHThnr2XE9iiE7B0KiH6zP2B53LmYOlF5JKzl0K+Ras+FvdBLW0lT6eRuepWT69k0dfTH2OcHZf++KFYrSB/jkbHDQ2JV0vXbpWVQ0yM1J8FXOKQ4m3tb9mae5EMPxwWdSXXn1bMPvIIHFNkjuUKwa8ji+J+YzPjXHq0u4DF1Jkbnh6WJogtnN7b+tVe/ad/oh2R/6PmcEto6QSToeMZUXJGaCXjEqC+KH/EHOEtl4S/cxPRRbhLf6ZDuhfWu3iNlJmZqpSTNzAlgJ02JmV9hR8H503MR7Fo1aeUvLUV98ETlrjom5R9XUSqMYCpLdCP0DFWKm1DkCLaYBHIKF6OyMHggso8lVZm+ykAxRcexeB9s82spplFWnjZKCPDWB7WYZd6lQL5MGjXZiqJZcrw0byLPZNOOQtEAGbAZjJYunK/HNfeDecxHRuFUP46hClzYcZG2RGC2Tu0x/H4OTsSJHo5Hc+kQURfLCHs0rakn2MhWQ44065r7dymQbz7WRSHcnS2IHMZNNma20si2AxJzFILj+QenCn/puEsejRqoHqWPfwXQUWcJAwHZTaAAVgy99MN1G/EakHZ06ZNemI5mJiiM36kTRji7XYsHgy3O2VbJ3WbKPGyODxY9D9zzgFQ3cDIMHtXsFh6G3lHDfDuHIAVrwwVgl6mgavbi1nJWbLWKyqg4kYEAhN+tPhS+Tc9vtFJ7AuZYk0Iv//NXuudMvf6s0NjtygaUkTY7aS/zUggSeA3M997UYJO09AcNA4YHl3c1FCq3QTPc= 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:(4636009)(36840700001)(40470700002)(46966006)(8936002)(508600001)(82310400004)(47076005)(26005)(70206006)(70586007)(8676002)(16526019)(5660300002)(54906003)(186003)(4326008)(426003)(336012)(86362001)(2906002)(356005)(316002)(40460700001)(7696005)(81166007)(1076003)(83380400001)(6666004)(36860700001)(110136005)(36756003)(2616005)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jan 2022 14:20:25.8344 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d6add08f-d0d4-47a4-9591-08d9d37b2e2d 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: BN8NAM11FT044.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5151 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: daniel@ffwll.ch, Arunpravin , jani.nikula@linux.intel.com, matthew.auld@intel.com, tzimmermann@suse.de, alexander.deucher@amd.com, christian.koenig@amd.com Errors-To: amd-gfx-bounces@lists.freedesktop.org Sender: "amd-gfx" On contiguous allocation, we round up the size to the *next* power of 2, implement a function to free the unused pages after the newly allocate block. v2(Matthew Auld): - replace function name 'drm_buddy_free_unused_pages' with drm_buddy_block_trim - replace input argument name 'actual_size' with 'new_size' - add more validation checks for input arguments - add overlaps check to avoid needless searching and splitting - merged the below patch to see the feature in action - add free unused pages support to i915 driver - lock drm_buddy_block_trim() function as it calls mark_free/mark_split are all globally visible v3(Matthew Auld): - remove trim method error handling as we address the failure case at drm_buddy_block_trim() function v4: - in case of trim, at __alloc_range() split_block failure path marks the block as free and removes it from the original list, potentially also freeing it, to overcome this problem, we turn the drm_buddy_block_trim() input node into a temporary node to prevent recursively freeing itself, but still retain the un-splitting/freeing of the other nodes(Matthew Auld) - modify the drm_buddy_block_trim() function return type v5(Matthew Auld): - revert drm_buddy_block_trim() function return type changes in v4 - modify drm_buddy_block_trim() passing argument n_pages to original_size as n_pages has already been rounded up to the next power-of-two and passing n_pages results noop Signed-off-by: Arunpravin --- drivers/gpu/drm/drm_buddy.c | 65 +++++++++++++++++++ drivers/gpu/drm/i915/i915_ttm_buddy_manager.c | 10 +++ include/drm/drm_buddy.h | 4 ++ 3 files changed, 79 insertions(+) diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c index 11356c2bb7aa..d97b0ba8aa1f 100644 --- a/drivers/gpu/drm/drm_buddy.c +++ b/drivers/gpu/drm/drm_buddy.c @@ -540,6 +540,71 @@ static int __drm_buddy_alloc_range(struct drm_buddy *mm, return __alloc_range(mm, &dfs, start, size, blocks); } +/** + * drm_buddy_block_trim - free unused pages + * + * @mm: DRM buddy manager + * @new_size: original size requested + * @blocks: output list head to add allocated blocks + * + * For contiguous allocation, we round up the size to the nearest + * power of two value, drivers consume *actual* size, so remaining + * portions are unused and it can be freed. + * + * Returns: + * 0 on success, error code on failure. + */ +int drm_buddy_block_trim(struct drm_buddy *mm, + u64 new_size, + struct list_head *blocks) +{ + struct drm_buddy_block *parent; + struct drm_buddy_block *block; + LIST_HEAD(dfs); + u64 new_start; + int err; + + if (!list_is_singular(blocks)) + return -EINVAL; + + block = list_first_entry(blocks, + struct drm_buddy_block, + link); + + if (!drm_buddy_block_is_allocated(block)) + return -EINVAL; + + if (new_size > drm_buddy_block_size(mm, block)) + return -EINVAL; + + if (!new_size && !IS_ALIGNED(new_size, mm->chunk_size)) + return -EINVAL; + + if (new_size == drm_buddy_block_size(mm, block)) + return 0; + + list_del(&block->link); + mark_free(mm, block); + mm->avail += drm_buddy_block_size(mm, block); + + /* Prevent recursively freeing this node */ + parent = block->parent; + block->parent = NULL; + + new_start = drm_buddy_block_offset(block); + list_add(&block->tmp_link, &dfs); + err = __alloc_range(mm, &dfs, new_start, new_size, blocks); + if (err) { + mark_allocated(block); + mm->avail -= drm_buddy_block_size(mm, block); + list_add(&block->link, blocks); + } + + block->parent = parent; + return err; +} +EXPORT_SYMBOL(drm_buddy_block_trim); + /** * drm_buddy_alloc_blocks - allocate power-of-two blocks * diff --git a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c index ae9201246bb5..626108fb9725 100644 --- a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c +++ b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c @@ -97,6 +97,16 @@ static int i915_ttm_buddy_man_alloc(struct ttm_resource_manager *man, if (unlikely(err)) goto err_free_blocks; + if (place->flags & TTM_PL_FLAG_CONTIGUOUS) { + u64 original_size = (u64)bman_res->base.num_pages << PAGE_SHIFT; + + mutex_lock(&bman->lock); + drm_buddy_block_trim(mm, + original_size, + &bman_res->blocks); + mutex_unlock(&bman->lock); + } + *res = &bman_res->base; return 0; diff --git a/include/drm/drm_buddy.h b/include/drm/drm_buddy.h index 424fc443115e..17ca928fce8e 100644 --- a/include/drm/drm_buddy.h +++ b/include/drm/drm_buddy.h @@ -145,6 +145,10 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm, struct list_head *blocks, unsigned long flags); +int drm_buddy_block_trim(struct drm_buddy *mm, + u64 new_size, + struct list_head *blocks); + void drm_buddy_free_block(struct drm_buddy *mm, struct drm_buddy_block *block); void drm_buddy_free_list(struct drm_buddy *mm, struct list_head *objects); -- 2.25.1