From patchwork Mon Jun 24 16:54:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Michel_D=C3=A4nzer?= X-Patchwork-Id: 11013803 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 301A713AF for ; Mon, 24 Jun 2019 16:54:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2081228BD4 for ; Mon, 24 Jun 2019 16:54:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 14E4528AA7; Mon, 24 Jun 2019 16:54:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B0E1228ABC for ; Mon, 24 Jun 2019 16:54:19 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0429989CD9; Mon, 24 Jun 2019 16:54:11 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from netline-mail3.netline.ch (mail.netline.ch [148.251.143.178]) by gabe.freedesktop.org (Postfix) with ESMTP id 046D189C6B; Mon, 24 Jun 2019 16:54:09 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by netline-mail3.netline.ch (Postfix) with ESMTP id 280462AA040; Mon, 24 Jun 2019 18:54:08 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at netline-mail3.netline.ch Received: from netline-mail3.netline.ch ([127.0.0.1]) by localhost (netline-mail3.netline.ch [127.0.0.1]) (amavisd-new, port 10024) with LMTP id GyQyUo12ae-o; Mon, 24 Jun 2019 18:54:07 +0200 (CEST) Received: from thor (116.245.63.188.dynamic.wline.res.cust.swisscom.ch [188.63.245.116]) by netline-mail3.netline.ch (Postfix) with ESMTPSA id 49DBF2A604A; Mon, 24 Jun 2019 18:54:07 +0200 (CEST) Received: from daenzer by thor with local (Exim 4.92) (envelope-from ) id 1hfSEI-0003Zo-QS; Mon, 24 Jun 2019 18:54:06 +0200 From: =?utf-8?q?Michel_D=C3=A4nzer?= To: amd-gfx@lists.freedesktop.org Subject: [PATCH libdrm 8/9] amdgpu: Drop refcount member from struct amdgpu_core_bo/device Date: Mon, 24 Jun 2019 18:54:05 +0200 Message-Id: <20190624165406.13682-9-michel@daenzer.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190624165406.13682-1-michel@daenzer.net> References: <20190624165406.13682-1-michel@daenzer.net> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Emil Velikov , dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Michel Dänzer We can just walk the lists of struct amdgpu_bo/device to find out if there are any references left. Signed-off-by: Michel Dänzer --- amdgpu/amdgpu_bo.c | 14 +++++++++++--- amdgpu/amdgpu_device.c | 30 ++++++++++++++++++++++-------- amdgpu/amdgpu_internal.h | 2 -- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/amdgpu/amdgpu_bo.c b/amdgpu/amdgpu_bo.c index f40df484..332ce4be 100644 --- a/amdgpu/amdgpu_bo.c +++ b/amdgpu/amdgpu_bo.c @@ -65,7 +65,6 @@ static int amdgpu_core_bo_create(struct amdgpu_core_device *dev, return r; } - bo->refcount = 1; bo->alloc_size = size; bo->handle = handle; pthread_mutex_init(&bo->cpu_access_mutex, NULL); @@ -95,7 +94,6 @@ static int amdgpu_bo_create(amdgpu_device_handle user_dev, goto out; } } - bo->refcount++; } else { r = amdgpu_core_bo_create(dev, size, handle, &bo); if (r) @@ -466,8 +464,18 @@ static void amdgpu_core_bo_free(struct amdgpu_bo *user_bo) { struct amdgpu_core_device *dev = user_bo->dev->core; struct amdgpu_core_bo *bo = user_bo->core; + struct amdgpu_bo *iter_bo; - if (--bo->refcount == 0) { + /* Are there any other user BOs left referencing the same core BO? */ + for (iter_bo = bo->user_bos; iter_bo; iter_bo = iter_bo->next) { + if (iter_bo == user_bo) + continue; + + if (iter_bo->core == bo) + break; + } + + if (!iter_bo) { /* Remove the buffer from the hash tables. */ handle_table_remove(&dev->bo_handles, bo->handle); diff --git a/amdgpu/amdgpu_device.c b/amdgpu/amdgpu_device.c index 8a99b8bb..1709099e 100644 --- a/amdgpu/amdgpu_device.c +++ b/amdgpu/amdgpu_device.c @@ -72,9 +72,6 @@ static int fd_compare(int fd1, int fd2) static void amdgpu_device_free(struct amdgpu_core_device *dev) { - if (--dev->refcount > 0) - return; - close(dev->fd); amdgpu_vamgr_deinit(&dev->vamgr_32); @@ -102,6 +99,26 @@ static bool same_file_description(int fd1, int fd2) return fd1 == fd2; } +static void amdgpu_device_unreference(amdgpu_device_handle user_dev) +{ + struct amdgpu_device *dev; + + if (!user_dev->core) + return; + + /* Are there any other user devices left referencing the core device? */ + for (dev = dev_list; dev; dev = dev->next) { + if (dev == user_dev) + continue; + + if (dev->core == user_dev->core) + break; + } + + if (!dev) + amdgpu_device_free(user_dev->core); +} + static int amdgpu_device_init(amdgpu_device_handle user_dev) { struct amdgpu_device *dev_iter; @@ -115,7 +132,6 @@ static int amdgpu_device_init(amdgpu_device_handle user_dev) break; if (dev_iter) { - dev_iter->core->refcount++; user_dev->core = dev_iter->core; return 0; } @@ -126,7 +142,6 @@ static int amdgpu_device_init(amdgpu_device_handle user_dev) return -ENOMEM; } - dev->refcount = 1; pthread_mutex_init(&dev->bo_table_mutex, NULL); dev->fd = user_dev->user_fd; @@ -251,8 +266,7 @@ out: cleanup: if (!user_dev->core || user_dev->user_fd != user_dev->core->fd) close(user_dev->user_fd); - if (user_dev->core) - amdgpu_device_free(user_dev->core); + amdgpu_device_unreference(user_dev); free(user_dev); pthread_mutex_unlock(&dev_mutex); return r; @@ -274,7 +288,7 @@ drm_public int amdgpu_device_deinitialize(amdgpu_device_handle user_dev) if (user_dev->user_fd != dev->fd) close(user_dev->user_fd); - amdgpu_device_free(dev); + amdgpu_device_unreference(user_dev); free(user_dev); } diff --git a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h index 70566f98..7110e30c 100644 --- a/amdgpu/amdgpu_internal.h +++ b/amdgpu/amdgpu_internal.h @@ -65,7 +65,6 @@ struct amdgpu_va { }; struct amdgpu_core_device { - int refcount; int fd; unsigned major_version; unsigned minor_version; @@ -97,7 +96,6 @@ struct amdgpu_device { }; struct amdgpu_core_bo { - int refcount; amdgpu_bo_handle user_bos; uint64_t alloc_size;