From patchwork Tue Nov 21 23:11:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Felix Kuehling X-Patchwork-Id: 13463755 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 73399C61D98 for ; Tue, 21 Nov 2023 23:11:51 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0913810E5A0; Tue, 21 Nov 2023 23:11:45 +0000 (UTC) Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2087.outbound.protection.outlook.com [40.107.237.87]) by gabe.freedesktop.org (Postfix) with ESMTPS id 41CF410E2D5; Tue, 21 Nov 2023 23:11:42 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YeRjvFuQ9FOkFemN8RjlXk9YS6awxXP1NM9vTc4QGnA7HLqscE/UDgQqV1JlgwHUtjFJho4GYsssxjWZqOdsW2nE5oOSCQg73urkPeUsvMl11TxcaaN61Nc9N8hW5NOCXWftcF804LLgCtrbb6403/Y01SUauHruB1iTjwtAC2pFi0kFPPT1yNN4YNPE1PhMZtevnCcYvHjJ21rvcWnwTC7f655zAHwguGQvMc7eTCm292kQOqdQCY4V0YQslMlAK//ZnhyLF3AundKImv4kMVtYhd8hyE+YVlypp7uQb1MnaUV68Av1B8NkSO9ByljwFl7F99OKgrDc90Je/lfX3g== 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=p6RUTn7k26le8slHS9LlgTldfLCBOm/bn3TMrj0N8is=; b=nDm+gp2U38Mz6i3TlW3+WylbMySd04SpusdoilxevQneAnG53RvlMj/Zf9uPHSJ+YR5yKDWoHES61N+idf+vyIpYL9D6ocZ3JJvFTXfTF6HayL3xWTmm8peWNmxTGIomRr/gqxSdJyjcJRrj4itvSGpuWaM1nvN4hN1tHoX0BzyFHEkTYEEeRPTgMt+CDCMz8UnrL6vXnvKqkbY5Rm2OznB3I36ykPsQjvCBr0lCnIAN8QeIYCLPgAv8AnMHDhWmCSxEGAWhEffrmr0FbIRaCd9OWjAxJMZiWPw4VDwGqNjBXU5M103HDhgwxQ+8JX01AMe0XPQl04WOmhMxdYGX2g== 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 (0) 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=p6RUTn7k26le8slHS9LlgTldfLCBOm/bn3TMrj0N8is=; b=ymB8bGJFvQj+03i3JK8IxMZKPUXi2ggKfzlTuys0snBQbNqwxQeb7ITNcITP59pOSAlh/Orm/Dvwhb/ugKkLjnoLzmwfxJRPWuRpla4rI8SpjvnSHl6ihxPvAyIuZ45gMtvGRze0Bq78UObEWU9m1ktdojOvDtMgmVPIAZ3X8z4= Received: from CH0PR03CA0249.namprd03.prod.outlook.com (2603:10b6:610:e5::14) by CY8PR12MB7588.namprd12.prod.outlook.com (2603:10b6:930:9b::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7025.18; Tue, 21 Nov 2023 23:11:38 +0000 Received: from DS3PEPF000099DE.namprd04.prod.outlook.com (2603:10b6:610:e5:cafe::5c) by CH0PR03CA0249.outlook.office365.com (2603:10b6:610:e5::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.28 via Frontend Transport; Tue, 21 Nov 2023 23:11:38 +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 DS3PEPF000099DE.mail.protection.outlook.com (10.167.17.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7025.12 via Frontend Transport; Tue, 21 Nov 2023 23:11:38 +0000 Received: from Harpoon.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.2507.34; Tue, 21 Nov 2023 17:11:37 -0600 From: Felix Kuehling To: , Subject: [PATCH v2 1/4] Revert "drm/prime: Unexport helpers for fd/handle conversion" Date: Tue, 21 Nov 2023 18:11:11 -0500 Message-ID: <20231121231114.703478-1-Felix.Kuehling@amd.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 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: DS3PEPF000099DE:EE_|CY8PR12MB7588:EE_ X-MS-Office365-Filtering-Correlation-Id: b436f8f1-84ad-42b3-a0b0-08dbeae73725 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PpZDHanYcbcb3v1IqmDjEU/jEJzInBtPJVpoJxPiZrxPtfVyC4FAwBVTXFdfpWrtlQanmSyc5Lg2iWh4odgOqD+fzk3150FX8rzDyLLPu7l5pdg/zf6suniaMazBGFAfvb1WtGDhgw4vnTVluovcphAU9hdJeejaa32gSK06iJPlz8QO28bO8eDP8DJit0vlNweCEfKMhqBvhLu8i25AfQgSk3HAc97KMWYe9v7z/iSznmB99oeMXfsyUdlU85kA5eBgvpx1kFygByaMU2WBw8e21sbUmZhcS/O/T4Y9rGODJQ92WI3PAWU8QkP1BAow7EHwvm3vXx/50p+WG0ASLOGcuKktd0N/cfdAeAIpaewTWTRM2t1K0GRsJvE+5zEGEECn/eyveGuFCNA0/wXxQ3Fb2KJn7PZqbsf3r2x+Uqz87Gyfl7quQVlyeVHCxqhSKGRPnyPVLZG/4bn16KwLvFN4RSjbwZdrZ6deOTREsQIA+aZIXUpAoY8+8EZCV1Ckp873gfkk0PFD+/XM2JAYoQ3CrYadzfJwCqUJIK+b3jucaAv3ci6IKomieNByrwv4ogWh0Spb9uxxZQftZUzRrpSc6tHztrXos7ZTjQY1UK1LsTjvhv3h/7bf8VuLbjsycItRlB1x/WminPftkqlXFymXDrNn2h0VxfS+xnOhh40lccFW+wcJF46fBb5znXekkGpf1eEr5v0vYtrXR0+D5FxrFU2Zcx5ES02S/+yFcU71ZQSYKKt0Wi4vXNPeGrDubtB81s+/nNT06tg+HswRhw== 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:(13230031)(4636009)(346002)(39860400002)(396003)(136003)(376002)(230922051799003)(451199024)(1800799012)(64100799003)(82310400011)(186009)(36840700001)(40470700004)(46966006)(40480700001)(83380400001)(82740400003)(66574015)(426003)(336012)(1076003)(2616005)(16526019)(26005)(4326008)(8676002)(8936002)(36860700001)(7696005)(47076005)(356005)(81166007)(40460700003)(6666004)(478600001)(54906003)(70586007)(110136005)(70206006)(316002)(41300700001)(36756003)(2906002)(86362001)(5660300002)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Nov 2023 23:11:38.5833 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b436f8f1-84ad-42b3-a0b0-08dbeae73725 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: DS3PEPF000099DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7588 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: christian.koenig@amd.com, Thomas Zimmermann Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This reverts commit 71a7974ac7019afeec105a54447ae1dc7216cbb3. These helper functions are needed for KFD to export and import DMABufs the right way without duplicating the tracking of DMABufs associated with GEM objects while ensuring that move notifier callbacks are working as intended. CC: Christian König CC: Thomas Zimmermann Signed-off-by: Felix Kuehling --- drivers/gpu/drm/drm_prime.c | 33 ++++++++++++++++++--------------- include/drm/drm_prime.h | 7 +++++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c index 63b709a67471..834a5e28abbe 100644 --- a/drivers/gpu/drm/drm_prime.c +++ b/drivers/gpu/drm/drm_prime.c @@ -278,7 +278,7 @@ void drm_gem_dmabuf_release(struct dma_buf *dma_buf) } EXPORT_SYMBOL(drm_gem_dmabuf_release); -/* +/** * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers * @dev: drm_device to import into * @file_priv: drm file-private structure @@ -292,9 +292,9 @@ EXPORT_SYMBOL(drm_gem_dmabuf_release); * * Returns 0 on success or a negative error code on failure. */ -static int drm_gem_prime_fd_to_handle(struct drm_device *dev, - struct drm_file *file_priv, int prime_fd, - uint32_t *handle) +int drm_gem_prime_fd_to_handle(struct drm_device *dev, + struct drm_file *file_priv, int prime_fd, + uint32_t *handle) { struct dma_buf *dma_buf; struct drm_gem_object *obj; @@ -360,6 +360,7 @@ static int drm_gem_prime_fd_to_handle(struct drm_device *dev, dma_buf_put(dma_buf); return ret; } +EXPORT_SYMBOL(drm_gem_prime_fd_to_handle); int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) @@ -408,7 +409,7 @@ static struct dma_buf *export_and_register_object(struct drm_device *dev, return dmabuf; } -/* +/** * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers * @dev: dev to export the buffer from * @file_priv: drm file-private structure @@ -421,10 +422,10 @@ static struct dma_buf *export_and_register_object(struct drm_device *dev, * The actual exporting from GEM object to a dma-buf is done through the * &drm_gem_object_funcs.export callback. */ -static int drm_gem_prime_handle_to_fd(struct drm_device *dev, - struct drm_file *file_priv, uint32_t handle, - uint32_t flags, - int *prime_fd) +int drm_gem_prime_handle_to_fd(struct drm_device *dev, + struct drm_file *file_priv, uint32_t handle, + uint32_t flags, + int *prime_fd) { struct drm_gem_object *obj; int ret = 0; @@ -506,6 +507,7 @@ static int drm_gem_prime_handle_to_fd(struct drm_device *dev, return ret; } +EXPORT_SYMBOL(drm_gem_prime_handle_to_fd); int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) @@ -864,9 +866,9 @@ EXPORT_SYMBOL(drm_prime_get_contiguous_size); * @obj: GEM object to export * @flags: flags like DRM_CLOEXEC and DRM_RDWR * - * This is the implementation of the &drm_gem_object_funcs.export functions - * for GEM drivers using the PRIME helpers. It is used as the default for - * drivers that do not set their own. + * This is the implementation of the &drm_gem_object_funcs.export functions for GEM drivers + * using the PRIME helpers. It is used as the default in + * drm_gem_prime_handle_to_fd(). */ struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj, int flags) @@ -962,9 +964,10 @@ EXPORT_SYMBOL(drm_gem_prime_import_dev); * @dev: drm_device to import into * @dma_buf: dma-buf object to import * - * This is the implementation of the gem_prime_import functions for GEM - * drivers using the PRIME helpers. It is the default for drivers that do - * not set their own &drm_driver.gem_prime_import. + * This is the implementation of the gem_prime_import functions for GEM drivers + * using the PRIME helpers. Drivers can use this as their + * &drm_driver.gem_prime_import implementation. It is used as the default + * implementation in drm_gem_prime_fd_to_handle(). * * Drivers must arrange to call drm_prime_gem_destroy() from their * &drm_gem_object_funcs.free hook when using this function. diff --git a/include/drm/drm_prime.h b/include/drm/drm_prime.h index a7abf9f3e697..2a1d01e5b56b 100644 --- a/include/drm/drm_prime.h +++ b/include/drm/drm_prime.h @@ -60,12 +60,19 @@ enum dma_data_direction; struct drm_device; struct drm_gem_object; +struct drm_file; /* core prime functions */ struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev, struct dma_buf_export_info *exp_info); void drm_gem_dmabuf_release(struct dma_buf *dma_buf); +int drm_gem_prime_fd_to_handle(struct drm_device *dev, + struct drm_file *file_priv, int prime_fd, uint32_t *handle); +int drm_gem_prime_handle_to_fd(struct drm_device *dev, + struct drm_file *file_priv, uint32_t handle, uint32_t flags, + int *prime_fd); + /* helper functions for exporting */ int drm_gem_map_attach(struct dma_buf *dma_buf, struct dma_buf_attachment *attach); From patchwork Tue Nov 21 23:11:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felix Kuehling X-Patchwork-Id: 13463753 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 3D693C61D97 for ; Tue, 21 Nov 2023 23:11:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4B8A610E594; Tue, 21 Nov 2023 23:11:44 +0000 (UTC) Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2060.outbound.protection.outlook.com [40.107.92.60]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8CBA910E549; Tue, 21 Nov 2023 23:11:42 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HEsHgGwNjc7+Cn77+CcGKKpZj3K1A2lToYThvD5VFvT0/dg6GC59y423aoQs7RmfNo5yWyyfIjnF6j5xiOY+YPF21/vhgLl8fhZ98AAnes8tUDeCO795nj5WC7cUc+HtFFNSLxCLnh6od9Gog54jUJXpnCPTddyfjgNR2ItCAIN2+1uuaVIfpQom5AKWCJk/7CvdgNrNyaE8bXCEPD+n46cfkvMkQmO4nT18zmDz+2hUjkwaI+IkjybqZpjwKHy1Xd7V0RmPjeBaZD8A6Fu19nTbdu8xMIaoURryUFqH47FOzz8B5/IUbLNOd1ymMAEXQyq85/75a6sRqOWbhYF+CQ== 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=jqlTRFKc45BOhZh3JFrIKFUHlXLE0tJhDZFLOAKnCRY=; b=Avw01bUtjm2otd38LZXj81/5SQFA+yyyzmHSkvSit2IHyh318G//nlKfjqdmhThumwVPnem8rRNvCGOpOF+rxkFqmB+jC5DUvI56bEWwxCPJcA4h+r1Bf3d1BnHK9saFjC7VSGOyjE5hQXDBuiuuPqti+ua7IyHowQj90ZCffAYqOryPIr9Zqb0hyzXPF1NymYfjx/ljVc2NSe1s2uH2EvEFjkkX6nRACUhJOPlvgkwGYlDi8WPtmv8RlJkyCzKDAe7TqVkOSW+6fDb9EpczOIzOd60B+7fM8x/Jz7IgOC5/haCznPX0wSaVvYMV5gX+G0aadX2j1ta8Nh5XclET8Q== 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 (0) 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=jqlTRFKc45BOhZh3JFrIKFUHlXLE0tJhDZFLOAKnCRY=; b=g4M4q0tJk/mhea/mlyS+L9fLOvahePcyWXnr2TjcBQ84sY0MRbbenf3xG0Y8Y/UxZJtpMVjyc7aHqhS1f2DcIKsSTDnC84bE8SNpKFCiumltYSO77WefXbPFBILMSrLSAeL5Cvv2dkcg0ywWcKfa3cpnQTk6tCkq6OkZgKC5G4o= Received: from CH0PR03CA0256.namprd03.prod.outlook.com (2603:10b6:610:e5::21) by PH7PR12MB8425.namprd12.prod.outlook.com (2603:10b6:510:240::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.28; Tue, 21 Nov 2023 23:11:39 +0000 Received: from DS3PEPF000099DE.namprd04.prod.outlook.com (2603:10b6:610:e5:cafe::b8) by CH0PR03CA0256.outlook.office365.com (2603:10b6:610:e5::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.27 via Frontend Transport; Tue, 21 Nov 2023 23:11:39 +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 DS3PEPF000099DE.mail.protection.outlook.com (10.167.17.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7025.12 via Frontend Transport; Tue, 21 Nov 2023 23:11:39 +0000 Received: from Harpoon.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.2507.34; Tue, 21 Nov 2023 17:11:37 -0600 From: Felix Kuehling To: , Subject: [PATCH v2 2/4] drm/prime: Helper to export dmabuf without fd Date: Tue, 21 Nov 2023 18:11:12 -0500 Message-ID: <20231121231114.703478-2-Felix.Kuehling@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231121231114.703478-1-Felix.Kuehling@amd.com> References: <20231121231114.703478-1-Felix.Kuehling@amd.com> MIME-Version: 1.0 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: DS3PEPF000099DE:EE_|PH7PR12MB8425:EE_ X-MS-Office365-Filtering-Correlation-Id: 5bb46085-7ff9-41bd-fe8a-08dbeae73784 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Is9YG6aZdbHTQSZdlvFn5dMP7xOPrDrV7irXv/YwJWWYhUSPwVCArPmNTaOwz5YmkSTV3aX9yc9vXIcKTOK1WRyNyabwEpO0HvsVF1GjOJQb7GoRB8naPUUQcgZ1RUK0Q6Ntp9dJ0sgGvKOoBXC29LXFtd9QQvDx1tHyKFyKwncTXq/kvtabIJHf6l4KF3JoPuVvadG62Xnropgfr05G9Nq/e8rO1AwM27Oo1pZrkUuAewmBzkZ0uZkQhr+QVgKjo0kNDPoAXI8NXO6YCLYsxWjAWONXiWydLmkFrR9RU78P0QtLnpthoqhoqAkgmeKEMMfeRXqKU4VxVsYcChxntfr+IFOYTAuf7vggY2TpDJ7vT+PQoB/Mfx8F1heEjIWKHhul+eYuWzxBBZCXPpPLReYy+IIbq5Z+xSyD6FRV6XF2iTCM177OmpL/NgiG/8yaQgoRHGkyoUpYdiYRGQAVR+2RUJCRlsPdki7vG/1ugy9l6EQwbOzaLEbAhEHDuCRssF66qwMFHneYE4BowSUbb3Vb/tpbqHAozZo3D42r9VhwLkpIn420+gWHw/iX7NToJJA2sMfgkIeuL3aqu0m6s6LHsyOI1b8LnQv5t4WnCmPCSD+i29TMK3CwpZwELWw+ILvZD8/kDe0XKFN4JfmbhhMcQQboeASQqxpMDl4A9hMAPrJy8lxMFqhBh8R+vgO9ioSieQnOp8N2PvJhNo8ceLLipCZ7kSL4YGeHd+xqGnXrKRnB50Gur8te5N0lgpxjxN6CGPNic1/cXiz5b2YI1g== 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:(13230031)(4636009)(346002)(136003)(39860400002)(396003)(376002)(230922051799003)(451199024)(186009)(64100799003)(1800799012)(82310400011)(46966006)(36840700001)(40470700004)(40460700003)(426003)(336012)(16526019)(83380400001)(81166007)(356005)(86362001)(36756003)(82740400003)(47076005)(36860700001)(41300700001)(70586007)(110136005)(54906003)(70206006)(8676002)(4326008)(8936002)(316002)(5660300002)(2906002)(26005)(2616005)(1076003)(40480700001)(45080400002)(6666004)(478600001)(7696005)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Nov 2023 23:11:39.2083 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5bb46085-7ff9-41bd-fe8a-08dbeae73784 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: DS3PEPF000099DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB8425 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: christian.koenig@amd.com, Thomas Zimmermann Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Change drm_gem_prime_handle_to_fd to drm_gem_prime_handle_to_dmabuf to export a dmabuf without creating an FD as a user mode handle. This is more useful for users in kernel mode. Suggested-by: Thomas Zimmermann Signed-off-by: Felix Kuehling --- drivers/gpu/drm/drm_prime.c | 63 ++++++++++++++++++------------------- include/drm/drm_prime.h | 6 ++-- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c index 834a5e28abbe..d491b5f73eea 100644 --- a/drivers/gpu/drm/drm_prime.c +++ b/drivers/gpu/drm/drm_prime.c @@ -410,26 +410,25 @@ static struct dma_buf *export_and_register_object(struct drm_device *dev, } /** - * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers + * drm_gem_prime_handle_to_dmabuf - PRIME export function for GEM drivers * @dev: dev to export the buffer from * @file_priv: drm file-private structure * @handle: buffer handle to export * @flags: flags like DRM_CLOEXEC - * @prime_fd: pointer to storage for the fd id of the create dma-buf + * @dma_buf: pointer to storage for the dma-buf reference * * This is the PRIME export function which must be used mandatorily by GEM * drivers to ensure correct lifetime management of the underlying GEM object. * The actual exporting from GEM object to a dma-buf is done through the * &drm_gem_object_funcs.export callback. */ -int drm_gem_prime_handle_to_fd(struct drm_device *dev, - struct drm_file *file_priv, uint32_t handle, - uint32_t flags, - int *prime_fd) +struct dma_buf *drm_gem_prime_handle_to_dmabuf(struct drm_device *dev, + struct drm_file *file_priv, + uint32_t handle, uint32_t flags) { struct drm_gem_object *obj; int ret = 0; - struct dma_buf *dmabuf; + struct dma_buf *dmabuf = NULL; mutex_lock(&file_priv->prime.lock); obj = drm_gem_object_lookup(file_priv, handle); @@ -441,7 +440,7 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev, dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle); if (dmabuf) { get_dma_buf(dmabuf); - goto out_have_handle; + goto out; } mutex_lock(&dev->object_name_lock); @@ -479,40 +478,22 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev, dmabuf, handle); mutex_unlock(&dev->object_name_lock); if (ret) - goto fail_put_dmabuf; - -out_have_handle: - ret = dma_buf_fd(dmabuf, flags); - /* - * We must _not_ remove the buffer from the handle cache since the newly - * created dma buf is already linked in the global obj->dma_buf pointer, - * and that is invariant as long as a userspace gem handle exists. - * Closing the handle will clean out the cache anyway, so we don't leak. - */ - if (ret < 0) { - goto fail_put_dmabuf; - } else { - *prime_fd = ret; - ret = 0; - } - - goto out; - -fail_put_dmabuf: - dma_buf_put(dmabuf); + dma_buf_put(dmabuf); out: drm_gem_object_put(obj); out_unlock: mutex_unlock(&file_priv->prime.lock); - return ret; + return ret ? ERR_PTR(ret) : dmabuf; } -EXPORT_SYMBOL(drm_gem_prime_handle_to_fd); +EXPORT_SYMBOL(drm_gem_prime_handle_to_dmabuf); int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) { struct drm_prime_handle *args = data; + struct dma_buf *dmabuf; + int ret; /* check flags are valid */ if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR)) @@ -523,8 +504,24 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data, args->handle, args->flags, &args->fd); } - return drm_gem_prime_handle_to_fd(dev, file_priv, args->handle, - args->flags, &args->fd); + dmabuf = drm_gem_prime_handle_to_dmabuf(dev, file_priv, args->handle, + args->flags); + if (IS_ERR(dmabuf)) + return PTR_ERR(dmabuf); + ret = dma_buf_fd(dmabuf, args->flags); + /* + * We must _not_ remove the buffer from the handle cache since the newly + * created dma buf is already linked in the global obj->dma_buf pointer, + * and that is invariant as long as a userspace gem handle exists. + * Closing the handle will clean out the cache anyway, so we don't leak. + */ + if (ret < 0) { + dma_buf_put(dmabuf); + } else { + args->fd = ret; + ret = 0; + } + return ret; } /** diff --git a/include/drm/drm_prime.h b/include/drm/drm_prime.h index 2a1d01e5b56b..89e839293d14 100644 --- a/include/drm/drm_prime.h +++ b/include/drm/drm_prime.h @@ -69,9 +69,9 @@ void drm_gem_dmabuf_release(struct dma_buf *dma_buf); int drm_gem_prime_fd_to_handle(struct drm_device *dev, struct drm_file *file_priv, int prime_fd, uint32_t *handle); -int drm_gem_prime_handle_to_fd(struct drm_device *dev, - struct drm_file *file_priv, uint32_t handle, uint32_t flags, - int *prime_fd); +struct dma_buf *drm_gem_prime_handle_to_dmabuf(struct drm_device *dev, + struct drm_file *file_priv, + uint32_t handle, uint32_t flags); /* helper functions for exporting */ int drm_gem_map_attach(struct dma_buf *dma_buf, From patchwork Tue Nov 21 23:11:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felix Kuehling X-Patchwork-Id: 13463754 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 62DCBC61D85 for ; Tue, 21 Nov 2023 23:11:48 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4691210E2D5; Tue, 21 Nov 2023 23:11:44 +0000 (UTC) Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2066.outbound.protection.outlook.com [40.107.212.66]) by gabe.freedesktop.org (Postfix) with ESMTPS id B79F810E2D5; Tue, 21 Nov 2023 23:11:42 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f0cXdnru1lS/0ufslZ9bp4Ob3M9+qD+VE4NwA09YsD+OshieSF72SPaXdpapxMDHPw3NawirzW84GcGH6JAvnjSd6Xm5LFJs7nwgEuDI3eNJVB4L1qjsC5P0xPOt6bbXzGggoUBlYMr1l3ln0eNrpZn/QRr7/dQ1KhgFMQWQxsLNYcJLBZF/uGC1IXpWj8Ed1SJgzfUO+Fhqy/6YpmTvLL8dGGlbZfG5CMT5Ctps4/46Q7rWw1sDztHOKwGv1m6Thd0JoDEKt7fg07RZ9bAJC+8CBrSfcVTn6EBXpflBRBeAZ85VvOlMV/lXHq3bvJKZTaWVdmbgW1ks/FWuudNGCA== 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=+4nwJNQiqUZFUoG2eDAhTR+NFBy5yQXwNMyZVUVIqvM=; b=i4CKBoMAvp2IY6fpVhhfShX2VPhQiUjdbAq95LpCpUVjRrvLKub8aF3sHvfSrrl12AtvnR8K6pXxJl1LHod2DJphEotwFQfCNBPEuIugyoeFIQYajWGiXJ7gvjrCRPVM2mbUlS7xu7D3iD0OLmCYV/kPj1SsjD44q+JJn3n4JdYXILy+8+8elGcWzXh8WTzzRZ7gpuzegFTX2QXgfYNbwaEEm1Y3bKu0G7TUuvyJc83P1LoY7x27863OEn/W/DRw0Tza8d7ouXDxhevF+ARCcNjXansQ1ICENgkA0bCN86knW2KxhRLn8yk0xsgSuVKNxoraIU9fNdJkKje8hO0kNw== 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 (0) 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=+4nwJNQiqUZFUoG2eDAhTR+NFBy5yQXwNMyZVUVIqvM=; b=rFRKioNKKO9KNTk96b2XqIuvbl1q6fiRDFZQ3L0rW+2vzeqt0/iDQU+k7YGgCRFMHHzUVCLKtJ4rk83WmV3zxJQV5IDy3DjiLJEtnFqHd+Tm0buLNYpN5849Y/I0cRJMjXy5zfh26uAh+6UKCMklQxEYhWpY3FVFrX9vuSpABC0= Received: from CH0PR03CA0269.namprd03.prod.outlook.com (2603:10b6:610:e5::34) by BL1PR12MB5189.namprd12.prod.outlook.com (2603:10b6:208:308::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.28; Tue, 21 Nov 2023 23:11:40 +0000 Received: from DS3PEPF000099DE.namprd04.prod.outlook.com (2603:10b6:610:e5:cafe::f4) by CH0PR03CA0269.outlook.office365.com (2603:10b6:610:e5::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7025.18 via Frontend Transport; Tue, 21 Nov 2023 23:11:40 +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 DS3PEPF000099DE.mail.protection.outlook.com (10.167.17.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7025.12 via Frontend Transport; Tue, 21 Nov 2023 23:11:40 +0000 Received: from Harpoon.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.2507.34; Tue, 21 Nov 2023 17:11:38 -0600 From: Felix Kuehling To: , Subject: [PATCH v2 3/4] drm/amdkfd: Export DMABufs from KFD using GEM handles Date: Tue, 21 Nov 2023 18:11:13 -0500 Message-ID: <20231121231114.703478-3-Felix.Kuehling@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231121231114.703478-1-Felix.Kuehling@amd.com> References: <20231121231114.703478-1-Felix.Kuehling@amd.com> MIME-Version: 1.0 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: DS3PEPF000099DE:EE_|BL1PR12MB5189:EE_ X-MS-Office365-Filtering-Correlation-Id: 0267ccfe-3dc6-43bf-94eb-08dbeae7381d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TpVs6nDJrKgRyJA97I4OWJx8ielJQEKnvqGFKRkFFg0dz/k8zOwvkGWOg0GggdNa8LJlOtJU6gOrkgj/bg9+fIybIpQz/76/blNNHFgzrIHAxFPzcGDGpj7GBVJPCBZ81Tt5LQxDKe5Hu19folCS3/CQkjzJcxkesHqNTntWfeYe6uEW1B0Rkxx3MTNXCeB5xorLthGXtwVii+ZPyuuDTe9qmBB5r3bnOBCNYaSTEA+KjvL1YgXGCLm8tXjikuvWdtMI8dINnlNLuT/Wr5+vUaAEEBbsRvzodxeoYL5TqqbxoeISdYiyB9u9rUuQu5Qrty0k4YEpPz3zrHonAOSqxIphpiMgmv91w7QUH+Vk98ElxC6K1seGRsmfopRarjg79BLEt2bndNcoHTvYM0k4SHV4j52XDtZ0bbulcXhtoL87BssPlLPt1xZPy1AP4xj4/B5t3XnAeYVwo9vjOqYWBHNdDgPsKDi8PfHNEznNszvnTDSVxhLSl4RKjG6gsOWN76Fvacy1iqdCHfPgA9pZt+2lrIcN9YE9GqDmJJV0pCJUkjNvy6oZ9eXGNTKjXcAgPD57U3rOLro5uDfNyTMo5HTO43Y8iymcNJiE2n+VTiVuV7QVk08Gmcl69YFtXPfUE1To4IJNL7lqrPvd4navNOZA7ulUbO3AUPmOCZINDwABJCGN9FEG7lmDT4HSer7LrJVFOoIdHTg8Z7Jmw03nTMIUu7kUhJEdHoA3a/kx2yUaDX4lrkd8RKdQtN7AF0zj7AE2YBf3pWSJHEQ/68sebA== 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:(13230031)(4636009)(39860400002)(136003)(396003)(346002)(376002)(230922051799003)(64100799003)(82310400011)(186009)(1800799012)(451199024)(46966006)(36840700001)(40470700004)(2616005)(356005)(86362001)(82740400003)(40480700001)(40460700003)(36756003)(54906003)(110136005)(70206006)(70586007)(6666004)(7696005)(316002)(81166007)(1076003)(26005)(16526019)(47076005)(83380400001)(336012)(426003)(2906002)(450100002)(478600001)(5660300002)(36860700001)(41300700001)(8936002)(4326008)(8676002)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Nov 2023 23:11:40.2083 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0267ccfe-3dc6-43bf-94eb-08dbeae7381d 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: DS3PEPF000099DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5189 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: Ramesh Errabolu , christian.koenig@amd.com Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Create GEM handles for exporting DMABufs using GEM-Prime APIs. The GEM handles are created in a drm_client_dev context to avoid exposing them in user mode contexts through a DMABuf import. Signed-off-by: Felix Kuehling Reviewed-by: Ramesh Errabolu --- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 11 +++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 5 ++++ .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 29 ++++++++++++++----- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c index b8412202a1b0..aa8b24079070 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c @@ -142,6 +142,7 @@ void amdgpu_amdkfd_device_init(struct amdgpu_device *adev) { int i; int last_valid_bit; + int ret; amdgpu_amdkfd_gpuvm_init_mem_limits(); @@ -160,6 +161,12 @@ void amdgpu_amdkfd_device_init(struct amdgpu_device *adev) .enable_mes = adev->enable_mes, }; + ret = drm_client_init(&adev->ddev, &adev->kfd.client, "kfd", NULL); + if (ret) { + dev_err(adev->dev, "Failed to init DRM client: %d\n", ret); + return; + } + /* this is going to have a few of the MSBs set that we need to * clear */ @@ -198,6 +205,10 @@ void amdgpu_amdkfd_device_init(struct amdgpu_device *adev) adev->kfd.init_complete = kgd2kfd_device_init(adev->kfd.dev, &gpu_resources); + if (adev->kfd.init_complete) + drm_client_register(&adev->kfd.client); + else + drm_client_release(&adev->kfd.client); amdgpu_amdkfd_total_mem_size += adev->gmc.real_vram_size; diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h index dac983da961d..c1195eb67057 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h @@ -33,6 +33,7 @@ #include #include #include +#include #include "amdgpu_sync.h" #include "amdgpu_vm.h" #include "amdgpu_xcp.h" @@ -83,6 +84,7 @@ struct kgd_mem { struct amdgpu_sync sync; + uint32_t gem_handle; bool aql_queue; bool is_imported; }; @@ -105,6 +107,9 @@ struct amdgpu_kfd_dev { /* HMM page migration MEMORY_DEVICE_PRIVATE mapping */ struct dev_pagemap pgmap; + + /* Client for KFD BO GEM handle allocations */ + struct drm_client_dev client; }; enum kgd_engine_type { diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c index 41fbc4fd0fac..e96e1595791f 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -806,13 +807,18 @@ kfd_mem_dmaunmap_attachment(struct kgd_mem *mem, static int kfd_mem_export_dmabuf(struct kgd_mem *mem) { if (!mem->dmabuf) { - struct dma_buf *ret = amdgpu_gem_prime_export( - &mem->bo->tbo.base, + struct amdgpu_device *bo_adev; + struct dma_buf *dmabuf; + + bo_adev = amdgpu_ttm_adev(mem->bo->tbo.bdev); + dmabuf = drm_gem_prime_handle_to_dmabuf(&bo_adev->ddev, + bo_adev->kfd.client.file, + mem->gem_handle, mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ? - DRM_RDWR : 0); - if (IS_ERR(ret)) - return PTR_ERR(ret); - mem->dmabuf = ret; + DRM_RDWR : 0); + if (IS_ERR(dmabuf)) + return PTR_ERR(dmabuf); + mem->dmabuf = dmabuf; } return 0; @@ -1779,6 +1785,9 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( pr_debug("Failed to allow vma node access. ret %d\n", ret); goto err_node_allow; } + ret = drm_gem_handle_create(adev->kfd.client.file, gobj, &(*mem)->gem_handle); + if (ret) + goto err_gem_handle_create; bo = gem_to_amdgpu_bo(gobj); if (bo_type == ttm_bo_type_sg) { bo->tbo.sg = sg; @@ -1830,6 +1839,8 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( err_pin_bo: err_validate_bo: remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info); + drm_gem_handle_delete(adev->kfd.client.file, (*mem)->gem_handle); +err_gem_handle_create: drm_vma_node_revoke(&gobj->vma_node, drm_priv); err_node_allow: /* Don't unreserve system mem limit twice */ @@ -1942,8 +1953,12 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( /* Free the BO*/ drm_vma_node_revoke(&mem->bo->tbo.base.vma_node, drm_priv); - if (mem->dmabuf) + if (!mem->is_imported) + drm_gem_handle_delete(adev->kfd.client.file, mem->gem_handle); + if (mem->dmabuf) { dma_buf_put(mem->dmabuf); + mem->dmabuf = NULL; + } mutex_destroy(&mem->lock); /* If this releases the last reference, it will end up calling From patchwork Tue Nov 21 23:11:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Felix Kuehling X-Patchwork-Id: 13463756 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 D8718C61D98 for ; Tue, 21 Nov 2023 23:11:53 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 46BB210E5A4; Tue, 21 Nov 2023 23:11:47 +0000 (UTC) Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2058.outbound.protection.outlook.com [40.107.237.58]) by gabe.freedesktop.org (Postfix) with ESMTPS id CED9D10E2D5; Tue, 21 Nov 2023 23:11:43 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ROkJ6WpcE0/2ambTtcb02Sre/3rAMTgEqToldbXn0h32aHYQhCUQecehH7CXMkmQoDvEcbfSu+VWTPErV3lZd0k9AIl8eFW8s+JBQvZw7eTRnysFpH7vlwL6zxOcD2FH8NowLBcBlBQ1IgsHU0t48M27J4knpwSFbpDrFXzQqNZKFgy7gTTX24UJh7fVLG53r3vAagYC9Yzreq89zeaSV3ycBPuelooiPPqgKUNxVmx/3R0ySh91n3vFb5T0NLTSVZNrjGsJYvClFIo0WJjp9pKaHHWAkE+06wXIdzNT0hy1MDTNiCSFF3Fu6BH2q1A5NBGBOsxnMo0KBR3pU2nJug== 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=hGPQBnepvvu5DK0aDqGUqFm65MI6AqgahnSN+Pqklj0=; b=Zs1Pwta32aaqYCTCoJk7sgGZIdULaDTxbGHFfBZFIkKKfNnFxZ3jynH45O2SO6b1Idod72+fZtb3bXECfeqaIqCmnRnuWIy0/izXBd1SChQez8LlrReK6NWDDn2oFilywf1M3bbX9YTLENK+hjMEWgWKgIdUYDN3wF1HvMB7AAsdlHAnUKLJI6aPZkZagvM6AMf04ouqYtButNAJuvCHi5T/yPM+Iykj7Yqc0BfkEhenwek2QLlT8OQ5HbFcFpWsiw9u36JgyJB/nYy94FX3CuM3CZimyCsXJv/OETqE6jrVGb3CO1IYM6TVUInNLHDblIwDLHTTauJOLKoMyFin1g== 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 (0) 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=hGPQBnepvvu5DK0aDqGUqFm65MI6AqgahnSN+Pqklj0=; b=GZ0eGjx+nu587raUR0xgoI+/vSedtuPOyXKWJypL8ZFoto3bAQTGZDQj9CXRWwNhsnb75FlCsSlPanBkhUAJHqDhM3UGtsHyrv2l4XyH1agRRYLUmtst5JjSV8pR8s35DRJdOuiON8SW47mVnlLL4gWod1Fy0BIFMcAbd6DXid4= Received: from CH0PR03CA0252.namprd03.prod.outlook.com (2603:10b6:610:e5::17) by SJ2PR12MB8135.namprd12.prod.outlook.com (2603:10b6:a03:4f3::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.20; Tue, 21 Nov 2023 23:11:41 +0000 Received: from DS3PEPF000099DE.namprd04.prod.outlook.com (2603:10b6:610:e5:cafe::3) by CH0PR03CA0252.outlook.office365.com (2603:10b6:610:e5::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.28 via Frontend Transport; Tue, 21 Nov 2023 23:11:40 +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 DS3PEPF000099DE.mail.protection.outlook.com (10.167.17.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7025.12 via Frontend Transport; Tue, 21 Nov 2023 23:11:40 +0000 Received: from Harpoon.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.2507.34; Tue, 21 Nov 2023 17:11:38 -0600 From: Felix Kuehling To: , Subject: [PATCH v2 4/4] drm/amdkfd: Import DMABufs for interop through DRM Date: Tue, 21 Nov 2023 18:11:14 -0500 Message-ID: <20231121231114.703478-4-Felix.Kuehling@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231121231114.703478-1-Felix.Kuehling@amd.com> References: <20231121231114.703478-1-Felix.Kuehling@amd.com> MIME-Version: 1.0 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: DS3PEPF000099DE:EE_|SJ2PR12MB8135:EE_ X-MS-Office365-Filtering-Correlation-Id: af3391e6-cf6a-4ba8-7dd6-08dbeae73867 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: vRCkd4sZyeEYEhrcVjkTVzeR0xvjBX5arYPgbDxnjkY8lZzS4SUzN8Mcpi8MfwlncAfTy+M4FH4RDZB8bpAMSz1UT+VG/NQfTxCb/jC8yAzU5srWVV0fL0J948Oy2vomYa8GM+c59Ie9KJ4AX/PfKVhTrHkWAR+p8h32qmN9kiLZA3/LTNJ+leSYAHMOU9YGWtlyBFQ3siCMSiRUQ/47WhCCDlqyRNoLjvRhwTGFrjq8pfP3A/xTUMOffVNNOBa7iakHmmSXYiKfoJ+YL8V0QeZidoV46kuvBxIBP7am9RCLUscIjYPc1QuGjK2gfbin8X2DylLRsthiiJZHfv+6xEXT1GcacpETyWHKy2sf+2o9fSexqpQIqtD+slkdOlc/KEmu16BA8wAlN8gi951SucIOqzqQau30gZMw5/0zETNYnZnjMIcXx/VDLjEpb5pCG78RPpbRJ1YK+aFeVoBOkL/bbS0hCI6W4pA4D2A9l8gdhN/bp12OYeDWzF3FCn25Hm2V/lg8cKbsbc/vGGIVH7foPtGSDondfKF34qo8GDYOK8mNDUAOcwQDffD985M8yyH0hNgkgbtlWFc8VvUT/pKbArarxcLOPzcirpzFEHUSO0EtpGURnbHDfehFAqCzuP9ZvuViUnves3v/16nisS1DrnLq7ac9WfhJG81UI5raTU7baE/UBkhTY4Tpkoe03jymeqZPkUeABnKsa4FIFRVWis52Nz6SMc16YzjhP1zYne8AnQKMEIxTtUYKhcJlAjkeMs8o98U7vPiU1gxbNw== 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:(13230031)(4636009)(396003)(346002)(376002)(39860400002)(136003)(230922051799003)(186009)(451199024)(82310400011)(1800799012)(64100799003)(40470700004)(46966006)(36840700001)(40480700001)(356005)(41300700001)(81166007)(36860700001)(5660300002)(86362001)(83380400001)(47076005)(66574015)(336012)(1076003)(8936002)(8676002)(4326008)(426003)(450100002)(26005)(54906003)(16526019)(316002)(2906002)(2616005)(70206006)(110136005)(70586007)(6666004)(7696005)(40460700003)(478600001)(82740400003)(36756003)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Nov 2023 23:11:40.6771 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: af3391e6-cf6a-4ba8-7dd6-08dbeae73867 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: DS3PEPF000099DE.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8135 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: "Xiaogang . Chen" , Ramesh Errabolu , christian.koenig@amd.com Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Use drm_gem_prime_fd_to_handle to import DMABufs for interop. This ensures that a GEM handle is created on import and that obj->dma_buf will be set and remain set as long as the object is imported into KFD. Signed-off-by: Felix Kuehling Reviewed-by: Ramesh Errabolu Reviewed-by: Xiaogang.Chen Acked-by: Christian König --- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 9 ++- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 64 +++++++++++++------ drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 15 ++--- 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h index c1195eb67057..8da42e0dddcb 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h @@ -319,11 +319,10 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *process_info, struct dma_fence **ef); int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct amdgpu_device *adev, struct kfd_vm_fault_info *info); -int amdgpu_amdkfd_gpuvm_import_dmabuf(struct amdgpu_device *adev, - struct dma_buf *dmabuf, - uint64_t va, void *drm_priv, - struct kgd_mem **mem, uint64_t *size, - uint64_t *mmap_offset); +int amdgpu_amdkfd_gpuvm_import_dmabuf_fd(struct amdgpu_device *adev, int fd, + uint64_t va, void *drm_priv, + struct kgd_mem **mem, uint64_t *size, + uint64_t *mmap_offset); int amdgpu_amdkfd_gpuvm_export_dmabuf(struct kgd_mem *mem, struct dma_buf **dmabuf); void amdgpu_amdkfd_debug_mem_fence(struct amdgpu_device *adev); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c index e96e1595791f..652657c863ff 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c @@ -1953,8 +1953,7 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( /* Free the BO*/ drm_vma_node_revoke(&mem->bo->tbo.base.vma_node, drm_priv); - if (!mem->is_imported) - drm_gem_handle_delete(adev->kfd.client.file, mem->gem_handle); + drm_gem_handle_delete(adev->kfd.client.file, mem->gem_handle); if (mem->dmabuf) { dma_buf_put(mem->dmabuf); mem->dmabuf = NULL; @@ -2310,34 +2309,26 @@ int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct amdgpu_device *adev, return 0; } -int amdgpu_amdkfd_gpuvm_import_dmabuf(struct amdgpu_device *adev, - struct dma_buf *dma_buf, - uint64_t va, void *drm_priv, - struct kgd_mem **mem, uint64_t *size, - uint64_t *mmap_offset) +static int import_obj_create(struct amdgpu_device *adev, + struct dma_buf *dma_buf, + struct drm_gem_object *obj, + uint64_t va, void *drm_priv, + struct kgd_mem **mem, uint64_t *size, + uint64_t *mmap_offset) { struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv); - struct drm_gem_object *obj; struct amdgpu_bo *bo; int ret; - obj = amdgpu_gem_prime_import(adev_to_drm(adev), dma_buf); - if (IS_ERR(obj)) - return PTR_ERR(obj); - bo = gem_to_amdgpu_bo(obj); if (!(bo->preferred_domains & (AMDGPU_GEM_DOMAIN_VRAM | - AMDGPU_GEM_DOMAIN_GTT))) { + AMDGPU_GEM_DOMAIN_GTT))) /* Only VRAM and GTT BOs are supported */ - ret = -EINVAL; - goto err_put_obj; - } + return -EINVAL; *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL); - if (!*mem) { - ret = -ENOMEM; - goto err_put_obj; - } + if (!*mem) + return -ENOMEM; ret = drm_vma_node_allow(&obj->vma_node, drm_priv); if (ret) @@ -2387,8 +2378,41 @@ int amdgpu_amdkfd_gpuvm_import_dmabuf(struct amdgpu_device *adev, drm_vma_node_revoke(&obj->vma_node, drm_priv); err_free_mem: kfree(*mem); + return ret; +} + +int amdgpu_amdkfd_gpuvm_import_dmabuf_fd(struct amdgpu_device *adev, int fd, + uint64_t va, void *drm_priv, + struct kgd_mem **mem, uint64_t *size, + uint64_t *mmap_offset) +{ + struct drm_gem_object *obj; + uint32_t handle; + int ret; + + ret = drm_gem_prime_fd_to_handle(&adev->ddev, adev->kfd.client.file, fd, + &handle); + if (ret) + return ret; + obj = drm_gem_object_lookup(adev->kfd.client.file, handle); + if (!obj) { + ret = -EINVAL; + goto err_release_handle; + } + + ret = import_obj_create(adev, obj->dma_buf, obj, va, drm_priv, mem, size, + mmap_offset); + if (ret) + goto err_put_obj; + + (*mem)->gem_handle = handle; + + return 0; + err_put_obj: drm_gem_object_put(obj); +err_release_handle: + drm_gem_handle_delete(adev->kfd.client.file, handle); return ret; } diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c index f6d4748c1980..247281b1c9d4 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c @@ -1564,16 +1564,11 @@ static int kfd_ioctl_import_dmabuf(struct file *filep, { struct kfd_ioctl_import_dmabuf_args *args = data; struct kfd_process_device *pdd; - struct dma_buf *dmabuf; int idr_handle; uint64_t size; void *mem; int r; - dmabuf = dma_buf_get(args->dmabuf_fd); - if (IS_ERR(dmabuf)) - return PTR_ERR(dmabuf); - mutex_lock(&p->mutex); pdd = kfd_process_device_data_by_id(p, args->gpu_id); if (!pdd) { @@ -1587,10 +1582,10 @@ static int kfd_ioctl_import_dmabuf(struct file *filep, goto err_unlock; } - r = amdgpu_amdkfd_gpuvm_import_dmabuf(pdd->dev->adev, dmabuf, - args->va_addr, pdd->drm_priv, - (struct kgd_mem **)&mem, &size, - NULL); + r = amdgpu_amdkfd_gpuvm_import_dmabuf_fd(pdd->dev->adev, args->dmabuf_fd, + args->va_addr, pdd->drm_priv, + (struct kgd_mem **)&mem, &size, + NULL); if (r) goto err_unlock; @@ -1601,7 +1596,6 @@ static int kfd_ioctl_import_dmabuf(struct file *filep, } mutex_unlock(&p->mutex); - dma_buf_put(dmabuf); args->handle = MAKE_HANDLE(args->gpu_id, idr_handle); @@ -1612,7 +1606,6 @@ static int kfd_ioctl_import_dmabuf(struct file *filep, pdd->drm_priv, NULL); err_unlock: mutex_unlock(&p->mutex); - dma_buf_put(dmabuf); return r; }