From patchwork Thu Apr 15 02:40:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Felix Kuehling X-Patchwork-Id: 12204131 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5224C433ED for ; Thu, 15 Apr 2021 02:40:40 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 64832611B0 for ; Thu, 15 Apr 2021 02:40:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 64832611B0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4959A6E4D0; Thu, 15 Apr 2021 02:40:33 +0000 (UTC) Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2051.outbound.protection.outlook.com [40.107.94.51]) by gabe.freedesktop.org (Postfix) with ESMTPS id 80FF36E0F6; Thu, 15 Apr 2021 02:40:32 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Yrzvej5ZviwMlJGlEj2jIk4h2lGQCOZuF8o/0bWUVGBFj83ZFUBhVZYOYI0J0TZRolkZjFF1yEmJkEqPhhuLM/bZG4xnPlec6jMAVu7lPBdVBSU0+JGFOseIBMAI4u624upaoYuBed3h/9k0OlMMxt5dNS9kuJM/wr4uZ4Tal1vrCn87m0gwznHH05R84GFGQZiTv3Yq2dfxknEDu70T7TuASR2HFmBJNRImx9LlgINY5mjHSDnOp6DZ3ZwjM5oIPIJq17OOqk+KoHopZJ53LRK4K9/Wbt48GzOc5BKxhcgp9sTGnOA/V7MHjp0PsPF7j8KIv2BzFWsYTHXhrepytQ== 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-SenderADCheck; bh=56OpfKRAobATF+ekELY98bzNfHBCbAkFTKX1cvuY3oc=; b=XqZpbRfz7Ktxc2ob3jiM/5NJ6kl+2UP7Mn9+NsC2xrBPPup9Nx5yrRLmk5BIV1sRW5IdbNP7TGGOrpTRPH0kjNYSNCnLr0trs4R3pM8IfHy1CGS3o1o7xtDZwmC3vvF9wANs1J1X3592ETiS8SV6gcwDAerWtJcgFxbtwOUEPah7/V+uULhzRGs2vKQUenZcUCsvykeSzt4qPYLR0JiTnwlFXM2xTbr2SjuVFZV5sW82rK7kL1J7eo6u574kQL9c3GxBcE3bY64S4ymG+X3mq/xdlBvsOG/c2YJgMOcLSfDg9J+R8VvKjvgeKivM3r4GIGJDCGIx1RssBv8tCGWZyw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=56OpfKRAobATF+ekELY98bzNfHBCbAkFTKX1cvuY3oc=; b=kAp6dKTWAnjhZWwHuTpaHP5Y1/pNRpKzAeDXMq9TpSrx6pRlehdnQFzPyxv+MOqVB+BSORkDxoPwJ2jyrA404Hdz7rITZJG9skzp1+4wPwI+BqgrgmgcHP8MtV0kEptH354M74O6FxRmb7xxKVtvsh7erfqpKJy4MVD/UiP16MI= Authentication-Results: lists.freedesktop.org; dkim=none (message not signed) header.d=none; lists.freedesktop.org; dmarc=none action=none header.from=amd.com; Received: from BL0PR12MB4948.namprd12.prod.outlook.com (2603:10b6:208:1cc::20) by MN2PR12MB4206.namprd12.prod.outlook.com (2603:10b6:208:1d5::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4020.21; Thu, 15 Apr 2021 02:40:27 +0000 Received: from BL0PR12MB4948.namprd12.prod.outlook.com ([fe80::70f5:99ed:65a1:c033]) by BL0PR12MB4948.namprd12.prod.outlook.com ([fe80::70f5:99ed:65a1:c033%7]) with mapi id 15.20.3933.040; Thu, 15 Apr 2021 02:40:27 +0000 From: Felix Kuehling To: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [PATCH v2 2/3] drm/amdkfd: Allow access for mmapping KFD BOs Date: Wed, 14 Apr 2021 22:40:12 -0400 Message-Id: <20210415024013.15522-2-Felix.Kuehling@amd.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210415024013.15522-1-Felix.Kuehling@amd.com> References: <20210415024013.15522-1-Felix.Kuehling@amd.com> X-Originating-IP: [165.204.55.251] X-ClientProxiedBy: YTOPR0101CA0045.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b00:14::22) To BL0PR12MB4948.namprd12.prod.outlook.com (2603:10b6:208:1cc::20) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from Harpoon.amd.com (165.204.55.251) by YTOPR0101CA0045.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b00:14::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4020.21 via Frontend Transport; Thu, 15 Apr 2021 02:40:27 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c5f6c1f2-7137-4688-7a20-08d8ffb7d427 X-MS-TrafficTypeDiagnostic: MN2PR12MB4206: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3826; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cEKlqql4aMG0keFd2ubaqdHN3JSgA/L/RDcBDdiR/oV25KyeiMGMUEklVVtg5tgt4g94FqToDZQJAKHFKUTWNwRm+DOD+ZLC/8RvUvkKGSfS1SAbn1P6nLss3ZQ8XUUkRe1D4eqD6ozeGZ+iCm5PrhapC//YC3AqjZuNI3v1uGrU1jcZZS0+WfrdVZiRTGNGM6I5oGea6worsETQvhX1CqzQ0781A1BaRj0uEO6x1o6C62sWMQGkEW90AbIougbgN6kHwfrIAQ69o1yfmzjVg18oOmyf3PI+8ccqrTiC7MWMNxyTrKpusODHfZr3CCMiTOZ0qX+zV/tkGfFcxwsguVY125M2ecJY7F3ept1jLlywx5AXXWK3WzypDFDWkWb7lMVC5H7g2Qnuhv6cDNVOdOen/Z7Xd7wGXA63p7RjFmRqR8dAhNNw2+bkf9WgS+Gm612CnatBcv2RGziwVcRVnt22EUWd1od/zfiBDvK1DoBsdIVZ4t4QRQcjoEJmj7etkFSSG5C8SJP7PWg9pxG0UA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BL0PR12MB4948.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(346002)(136003)(396003)(376002)(366004)(39860400002)(26005)(8676002)(7696005)(2616005)(956004)(16526019)(186003)(66946007)(6486002)(5660300002)(1076003)(316002)(52116002)(38100700002)(6666004)(8936002)(2906002)(38350700002)(66556008)(450100002)(4326008)(66574015)(83380400001)(478600001)(86362001)(66476007)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?utf-8?q?93e4dDkQIq6HbU9+QW3FnfQSTcGdpL?= =?utf-8?q?CcRoUYc4npf4sUuZyJLzFLJeQtjYbqGrUSFEJP+UOcYU7tgNxukmMlnkYjiznYXKf?= =?utf-8?q?WZBxgSvQPxbev7X+MumWkVyQhliiEFfD+Gt2uLvvQNiNUDk3Lk/3bNcqof1Wo0V34?= =?utf-8?q?2cow+qg2yHqt8AylBh+fpYpz59lZ/C3luOgRgXXZ8P9ZlwrvclzPXuoa7WHahuuWj?= =?utf-8?q?pPXMYDdf3eFTM5LvTZ0xobDlH5/BEUxIFL43eknlq6IGYqfWCHRSiEg7wL98A9WUS?= =?utf-8?q?zJU7KJNaUgJyZzqqvKocR3m6PLaLywoTf3kVA8IiMV5EQBh2VgSPtlGQrSEdtLHIZ?= =?utf-8?q?nOdjJ72inZ7zwr/KEQmkoWnHjuAyoeGMNdOqIiurSteOAm+mCGqO1ndqqZrVOYJUC?= =?utf-8?q?SwQ4zb/dPgC7rBX6II3UOmHax3xojS1kiPZ9ANQWsRl3wIk2fTeGYBJtea2ecdaDM?= =?utf-8?q?CTFg92W03vt+Rac7hOmRnjf0pXV1u96EII3NWOhHz1Iv2IdF+l73+QjfHpNyPX8Kq?= =?utf-8?q?09mv9uis1SJTnVyMNvEm37zv+fgd7P2j82//cjTvBU/uIOqJUBgrFyejgP3QklNoE?= =?utf-8?q?zPOBu0MDnnprQYzx97eOeXGrZ6GmXSs/45QP1tj1FuShuiJjyOlsGaRNiDNTsMZhM?= =?utf-8?q?2Oj6wg90FHB6ca6UN3aYeA8y8eabQXBkUwXPbG8lWx/7U4J+3tWluZgB7SVAechLI?= =?utf-8?q?tkp9fTx3tdkYKiht2GF+Lv1Ozc2UVCgxQNBp4CWlaHYokrsFlLFJbi85tEqArWmxq?= =?utf-8?q?vgF/wRBtZNfcZ668Ln2TFALhLHkLMA3CSjNso7IWj14E2phc67X1ODHx+ZPIAKERe?= =?utf-8?q?yk+Wd+nRtwdxwq0/oYSE91lhw4FoRQF30pv8BBOK0WesYSL5/+/hfNBpl3YhFZwZk?= =?utf-8?q?Pvq3Cp7iDt8N3I2jo8M6N8oMLoq0Uj2T+B7EDdNamHA6K/+0z9+rsalw86aHuEFGu?= =?utf-8?q?cXS89ITboZRTIYUGBkkwNG/yY5Chdn/DBuWUtLKFFe8fOsWfkkNN/F0Ds9SNb2YTg?= =?utf-8?q?N7DzcSH59HuRHsTUIkoZPJ42OonugKGO/G1jR/cKO6oriL65V8pUZ6nW5rjn7Uzre?= =?utf-8?q?Z9jPSvXMLAZuFnfl3IdI/KekTHGcvRdHin1qMrRKVNU83qNbhdRpvIcKje+892GC2?= =?utf-8?q?3rORTLltUP8x/wq0MtpbJToqVswPuyusQruhV0aKs3z/izWiNIjti8DLPzR1o7j44?= =?utf-8?q?7Z7hjDCBRvp6dhZ95ic82+ge/hGE+73HtqRj/dQJBsok2lIdodOezvFubMS4SeXBb?= =?utf-8?q?hUVIjsTjtW066P?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c5f6c1f2-7137-4688-7a20-08d8ffb7d427 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB4948.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Apr 2021 02:40:27.7963 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: nzs9vor+6A1LygJCvCaaIpp6h5LF0iGKPaXTmJ2QCe8xFQOyKIPRQA5U9DBW+qxhb0bZmrjZCgPniLwQvmeEXg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4206 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: =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" DRM render node file handles are used for CPU mapping of BOs using mmap by the Thunk. It uses the DRM render node of the GPU where the BO was allocated. DRM allows mmap access automatically when it creates a GEM handle for a BO. KFD BOs don't have GEM handles, so KFD needs to manage access manually. Use drm_vma_node_allow to allow user mode to mmap BOs allocated with kfd_ioctl_alloc_memory_of_gpu through the DRM render node that was used in the kfd_ioctl_acquire_vm call for the same GPU. Signed-off-by: Felix Kuehling Acked-by: Christian König --- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 3 ++- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 18 +++++++++++++++++- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 8 +++++--- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 9 ++++++--- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h index 0d59bebd92af..7c8c5e469707 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h @@ -245,7 +245,8 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( void *drm_priv, struct kgd_mem **mem, uint64_t *offset, uint32_t flags); int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( - struct kgd_dev *kgd, struct kgd_mem *mem, uint64_t *size); + struct kgd_dev *kgd, struct kgd_mem *mem, void *drm_priv, + uint64_t *size); int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( struct kgd_dev *kgd, struct kgd_mem *mem, void *drm_priv); int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu( diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c index dc86faa03b88..f96c331c9b6d 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c @@ -1229,6 +1229,11 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( domain_string(alloc_domain), ret); goto err_bo_create; } + ret = drm_vma_node_allow(&gobj->vma_node, drm_priv); + if (ret) { + pr_debug("Failed to allow vma node access. ret %d\n", ret); + goto err_node_allow; + } bo = gem_to_amdgpu_bo(gobj); if (bo_type == ttm_bo_type_sg) { bo->tbo.sg = sg; @@ -1258,6 +1263,8 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( allocate_init_user_pages_failed: remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info); + drm_vma_node_revoke(&gobj->vma_node, drm_priv); +err_node_allow: amdgpu_bo_unref(&bo); /* Don't unreserve system mem limit twice */ goto err_reserve_limit; @@ -1275,7 +1282,8 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( } int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( - struct kgd_dev *kgd, struct kgd_mem *mem, uint64_t *size) + struct kgd_dev *kgd, struct kgd_mem *mem, void *drm_priv, + uint64_t *size) { struct amdkfd_process_info *process_info = mem->process_info; unsigned long bo_size = mem->bo->tbo.base.size; @@ -1352,6 +1360,7 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu( } /* Free the BO*/ + drm_vma_node_revoke(&mem->bo->tbo.base.vma_node, drm_priv); drm_gem_object_put(&mem->bo->tbo.base); mutex_destroy(&mem->lock); kfree(mem); @@ -1663,6 +1672,7 @@ int amdgpu_amdkfd_gpuvm_import_dmabuf(struct kgd_dev *kgd, struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv); struct drm_gem_object *obj; struct amdgpu_bo *bo; + int ret; if (dma_buf->ops != &amdgpu_dmabuf_ops) /* Can't handle non-graphics buffers */ @@ -1683,6 +1693,12 @@ int amdgpu_amdkfd_gpuvm_import_dmabuf(struct kgd_dev *kgd, if (!*mem) return -ENOMEM; + ret = drm_vma_node_allow(&obj->vma_node, drm_priv); + if (ret) { + kfree(mem); + return ret; + } + if (size) *size = amdgpu_bo_size(bo); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c index 97da1632f504..242e8b28feac 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c @@ -1328,7 +1328,8 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, return 0; err_free: - amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem, NULL); + amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem, + pdd->drm_priv, NULL); err_unlock: mutex_unlock(&p->mutex); return err; @@ -1365,7 +1366,7 @@ static int kfd_ioctl_free_memory_of_gpu(struct file *filep, } ret = amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, - (struct kgd_mem *)mem, &size); + (struct kgd_mem *)mem, pdd->drm_priv, &size); /* If freeing the buffer failed, leave the handle in place for * clean-up during process tear-down. @@ -1721,7 +1722,8 @@ static int kfd_ioctl_import_dmabuf(struct file *filep, return 0; err_free: - amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem, NULL); + amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, (struct kgd_mem *)mem, + pdd->drm_priv, NULL); err_unlock: mutex_unlock(&p->mutex); dma_buf_put(dmabuf); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c index 209e9edd6ddd..0a5379439642 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c @@ -648,7 +648,8 @@ static void kfd_process_free_gpuvm(struct kgd_mem *mem, struct kfd_dev *dev = pdd->dev; amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(dev->kgd, mem, pdd->drm_priv); - amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, mem, NULL); + amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->kgd, mem, pdd->drm_priv, + NULL); } /* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process @@ -712,7 +713,8 @@ static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd, return err; err_map_mem: - amdgpu_amdkfd_gpuvm_free_memory_of_gpu(kdev->kgd, mem, NULL); + amdgpu_amdkfd_gpuvm_free_memory_of_gpu(kdev->kgd, mem, pdd->drm_priv, + NULL); err_alloc_mem: *kptr = NULL; return err; @@ -907,7 +909,8 @@ static void kfd_process_device_free_bos(struct kfd_process_device *pdd) peer_pdd->dev->kgd, mem, peer_pdd->drm_priv); } - amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->kgd, mem, NULL); + amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->kgd, mem, + pdd->drm_priv, NULL); kfd_process_device_remove_obj_handle(pdd, id); } }