From patchwork Thu Apr 25 15:51:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 10917409 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 D1C711575 for ; Thu, 25 Apr 2019 15:51:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C2A1528D0A for ; Thu, 25 Apr 2019 15:51:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B596128D0E; Thu, 25 Apr 2019 15:51:51 +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,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,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 3CCE528BB3 for ; Thu, 25 Apr 2019 15:51:51 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2D01C891CB; Thu, 25 Apr 2019 15:51:50 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5A5A4891CB; Thu, 25 Apr 2019 15:51:49 +0000 (UTC) Received: by mail-wr1-x443.google.com with SMTP id s15so66407wra.12; Thu, 25 Apr 2019 08:51:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=B6SM9aX5iQydmzpc1DfeT9pGUIPYW+uh4vO6EJ3QDVE=; b=hH0ezTtlPwRLxkaE0Vslak5aW2XaPdtLprCodOXS2CzuJI8WJ9qsgkip76umHEq5US AgDhwTvP5ioVTn7mtvdgEafJsMqOw8Ki6SXYnVWnD6i+HI/1+/rUs2bBlO7Fi2m1pkzj vBBzLiTObMatQUw0REa7QXWD3yQ85Qfq/oF81pyq4jZFiLqp6c+jrXmoZ7TyjnB1sT6V GaSpF7SzkOwGICRcv/7l3mUr/SI1N1g1QasJZZ5v/91+OMlCYO4bNY0gMj17DhJ+i3Xp NXWE4HwQKoDWp9zmL6s0jHWYq0PKg/rCNebFTDUYtiTOXxSiRwPL6uL1DJzEDjVMFjgh 809g== X-Gm-Message-State: APjAAAV58/tuBh1NRcQ+x2rndA/4Lhn4O2RGeNfo6Vigkn6wqiRguzWK aOkfb7ft2wzWlT0qNMoQ3VhUA/E6 X-Google-Smtp-Source: APXvYqw/NhGOSVDkEykY37qIOaNeo4gk5ExjvBY6FWnPRNzZnZyQ5AYEOFVxZCfkeSrQOtQ59Oj9Ug== X-Received: by 2002:a5d:620e:: with SMTP id y14mr2149260wru.303.1556207507747; Thu, 25 Apr 2019 08:51:47 -0700 (PDT) Received: from arch-x1c3.cbg.collabora.co.uk ([2a00:5f00:102:0:9665:9cff:feee:aa4d]) by smtp.gmail.com with ESMTPSA id v17sm17268575wmc.30.2019.04.25.08.51.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Apr 2019 08:51:46 -0700 (PDT) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH 1/2] drm/prime: split drm_gem_prime_fd_to_handle() Date: Thu, 25 Apr 2019 16:51:13 +0100 Message-Id: <20190425155114.533-1-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=B6SM9aX5iQydmzpc1DfeT9pGUIPYW+uh4vO6EJ3QDVE=; b=SZcMqaN20bS/MGm+/I/oGz85ywirhjyVPNI0hqrAnuwgREQumRofKdmgkQbCPxrwiN YQrEhDG6QZT/MtSyMQ5k8f36QjqsVWl/i9y3JMGwIah/tbjzYj+1DWUFWg/3L3mt8kU6 kB5veBhjHF1T4NHhTj1BLi0AhvLZRMEB77GGH7dO1AJKD3C2t1uQKw89++FbUj/JrwWS Ot8rDd6NhFp6uyyCQK9qE1pdRhNZn6nahuAZRCtcX9tUsK39Oji4SmDu6AUVixySRmwx 741cB4gOvnQgzTb3OrjutO8sMzMbcWZGnNwLR+Qx3ZVCi65fv84FKfoyWxM/Jwa4NTlt OGgQ== 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: intel-gfx-trybot@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Emil Velikov Currently drm_gem_prime_fd_to_handle() consists of illegible amount of goto labels, for no obvious reason. Split it in two (as below) making the code far simpler and obvious. drm_gem_prime_fd_to_handle() - prime mtx handling - fd/dmabuf refcounting - hash table lookup import_buf_to_handle() - drm_gem_object import and locking - creating a handle for the gem object - adding the handle/fd to the hash table Cc: Daniel Vetter Signed-off-by: Emil Velikov --- drivers/gpu/drm/drm_prime.c | 86 ++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 45 deletions(-) diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c index dc079efb3b0f..0d83b9bbf793 100644 --- a/drivers/gpu/drm/drm_prime.c +++ b/drivers/gpu/drm/drm_prime.c @@ -777,37 +777,14 @@ struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev, } EXPORT_SYMBOL(drm_gem_prime_import); -/** - * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers - * @dev: dev to export the buffer from - * @file_priv: drm file-private structure - * @prime_fd: fd id of the dma-buf which should be imported - * @handle: pointer to storage for the handle of the imported buffer object - * - * This is the PRIME import function which must be used mandatorily by GEM - * drivers to ensure correct lifetime management of the underlying GEM object. - * The actual importing of GEM object from the dma-buf is done through the - * gem_import_export driver callback. - */ -int drm_gem_prime_fd_to_handle(struct drm_device *dev, - struct drm_file *file_priv, int prime_fd, - uint32_t *handle) +static int import_buf_to_handle(struct drm_device *dev, + struct drm_file *file_priv, + struct dma_buf *dma_buf, + uint32_t *handle) { - struct dma_buf *dma_buf; struct drm_gem_object *obj; int ret; - dma_buf = dma_buf_get(prime_fd); - if (IS_ERR(dma_buf)) - return PTR_ERR(dma_buf); - - mutex_lock(&file_priv->prime.lock); - - ret = drm_prime_lookup_buf_handle(&file_priv->prime, - dma_buf, handle); - if (ret == 0) - goto out_put; - /* never seen this one, need to import */ mutex_lock(&dev->object_name_lock); if (dev->driver->gem_prime_import) @@ -816,7 +793,8 @@ int drm_gem_prime_fd_to_handle(struct drm_device *dev, obj = drm_gem_prime_import(dev, dma_buf); if (IS_ERR(obj)) { ret = PTR_ERR(obj); - goto out_unlock; + mutex_unlock(&dev->object_name_lock); + return ret; } if (obj->dma_buf) { @@ -830,29 +808,47 @@ int drm_gem_prime_fd_to_handle(struct drm_device *dev, ret = drm_gem_handle_create_tail(file_priv, obj, handle); drm_gem_object_put_unlocked(obj); if (ret) - goto out_put; + return ret; - ret = drm_prime_add_buf_handle(&file_priv->prime, - dma_buf, *handle); - mutex_unlock(&file_priv->prime.lock); + ret = drm_prime_add_buf_handle(&file_priv->prime, dma_buf, *handle); if (ret) - goto fail; + /* hmm, if driver attached, we are relying on the free-object + * path to detach.. which seems ok.. + */ + drm_gem_handle_delete(file_priv, *handle); - dma_buf_put(dma_buf); + return ret; +} - return 0; +/** + * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers + * @dev: dev to export the buffer from + * @file_priv: drm file-private structure + * @prime_fd: fd id of the dma-buf which should be imported + * @handle: pointer to storage for the handle of the imported buffer object + * + * This is the PRIME import function which must be used mandatorily by GEM + * drivers to ensure correct lifetime management of the underlying GEM object. + * The actual importing of GEM object from the dma-buf is done through the + * gem_import_export driver callback. + */ +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; + int ret; -fail: - /* hmm, if driver attached, we are relying on the free-object path - * to detach.. which seems ok.. - */ - drm_gem_handle_delete(file_priv, *handle); - dma_buf_put(dma_buf); - return ret; + dma_buf = dma_buf_get(prime_fd); + if (IS_ERR(dma_buf)) + return PTR_ERR(dma_buf); + + mutex_lock(&file_priv->prime.lock); + + ret = drm_prime_lookup_buf_handle(&file_priv->prime, dma_buf, handle); + if (ret) + ret = import_buf_to_handle(dev, file_priv, dma_buf, handle); -out_unlock: - mutex_unlock(&dev->object_name_lock); -out_put: mutex_unlock(&file_priv->prime.lock); dma_buf_put(dma_buf); return ret; From patchwork Thu Apr 25 15:51:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 10917411 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 2430E1669 for ; Thu, 25 Apr 2019 15:51:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 16D5328BB3 for ; Thu, 25 Apr 2019 15:51:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0B9E328D14; Thu, 25 Apr 2019 15:51:54 +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,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,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 93C8F28D11 for ; Thu, 25 Apr 2019 15:51:53 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 23CF4891CE; Thu, 25 Apr 2019 15:51:52 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8BF73891CE; Thu, 25 Apr 2019 15:51:50 +0000 (UTC) Received: by mail-wr1-x442.google.com with SMTP id a9so120780wrp.6; Thu, 25 Apr 2019 08:51:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=d6f6rgpYAz4HTqCVViDbhczuzG9DLjERHwn08NIKSsw=; b=elF6OdktYi8Aj8+rElL711EPRRtLWxBWtSUABhzpLX8o3oIfjleFMDGnVjo5daj0Tc 1fRfBKs7CXp1sttE6TObmsS9jfD23TKkdG85M+k6WXfI6JK3yzEwP4vOxyOGT4/5efm6 8X95JaQpkbJSHgjvCXbScLyF7R1e2wdgB+OJ41z7XFDsECr0o4ivIS2MIsP4+xnPB147 +tynoBEmm0M7qkUwLv3mE9H09uN+MjI0Awzp3qfTdG55QmfBpFmkhrOb8YKszYo06T/h xc5mN7vskDS+3mq7P50x2W5KRBgt928sbojh6Tx4cXfNmJrTZIZyh/fAl41ILdwjuOfG eN8A== X-Gm-Message-State: APjAAAUG3W3E/GG/ZgWbaWwY8eZnldkKA+153EUhYwAPp3qlC479TDWY SB3thRQGwIAAfbBro9fELtrVND+g X-Google-Smtp-Source: APXvYqwozNnZfs+B6dkytCo0uEslpS9na3ixZg0JxiEuRkRNWD6loPEYAF1rJ7RZ8GIdsdg9YWMadw== X-Received: by 2002:adf:8bc5:: with SMTP id w5mr9121568wra.226.1556207508842; Thu, 25 Apr 2019 08:51:48 -0700 (PDT) Received: from arch-x1c3.cbg.collabora.co.uk ([2a00:5f00:102:0:9665:9cff:feee:aa4d]) by smtp.gmail.com with ESMTPSA id v17sm17268575wmc.30.2019.04.25.08.51.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Apr 2019 08:51:48 -0700 (PDT) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH 2/2] drm/prime: split drm_gem_prime_handle_to_fd() Date: Thu, 25 Apr 2019 16:51:14 +0100 Message-Id: <20190425155114.533-2-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190425155114.533-1-emil.l.velikov@gmail.com> References: <20190425155114.533-1-emil.l.velikov@gmail.com> MIME-Version: 1.0 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=d6f6rgpYAz4HTqCVViDbhczuzG9DLjERHwn08NIKSsw=; b=aZYB8ge+ye1cu5BiYVM2GN3MaceHWjqxJFB3A2xBE6eBASyk+LS3BGHaJU3OrwEZql z1Bpyd09/eggGzZeauOHYGApA/9lhC760H2x7kmgWeRr4a6Ok1bMsKJqlx8nHEMtSGr4 DmJb5RQFXIRSg5haDXx0i7GowWU6hXgascGf3gVBYwFQi7zYQI37fKTWOuB74gGkYc0X PWhioWvFKEXffmXB6EHDn1lbWnLjDkGZnisJgVnGPmKY5AiqkhNI/YbCRkx5DhZLVB2Y mHKpLAyUX3aPklpruIZbLBHrYYtn+rWm9JSAdlNkOvWLvrd4zTdM1ed1YawY4nD7E04i Et9g== 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: intel-gfx-trybot@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Emil Velikov Currently drm_gem_prime_handle_to_fd() consists of illegible amount of goto labels, for no obvious reason. Split it in two (as below) making the code far simpler and obvious. drm_gem_prime_handle_to_fd() - prime mtx handling - drm_gem_object lookup and refcounting - creating an fd for the dmabuf - hash table lookup export_handle_to_buf() - drm_gem_object export and locking - adding the handle/fd to the hash table Cc: Daniel Vetter Signed-off-by: Emil Velikov --- Currently we dma_buf_put() [in the error path] even for re-export of original imported object and "self-export" - aka obj->import_attach->dmabuf and obj->dmabuf. Have not looked at it too closely, but gut suggests that may be off. --- drivers/gpu/drm/drm_prime.c | 106 +++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 49 deletions(-) diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c index 0d83b9bbf793..2b0b6e7a6a47 100644 --- a/drivers/gpu/drm/drm_prime.c +++ b/drivers/gpu/drm/drm_prime.c @@ -545,11 +545,54 @@ static struct dma_buf *export_and_register_object(struct drm_device *dev, * will clean it up. */ obj->dma_buf = dmabuf; - get_dma_buf(obj->dma_buf); return dmabuf; } +static struct dma_buf *export_handle_to_buf(struct drm_device *dev, + struct drm_file *file_priv, + struct drm_gem_object *obj, + uint32_t handle, + uint32_t flags) +{ + struct dma_buf *dmabuf = NULL; + int ret; + + mutex_lock(&dev->object_name_lock); + /* re-export the original imported object */ + if (obj->import_attach) + dmabuf = obj->import_attach->dmabuf; + + if (!dmabuf) + dmabuf = obj->dma_buf; + + if (!dmabuf) + dmabuf = export_and_register_object(dev, obj, flags); + + if (IS_ERR(dmabuf)) { + /* normally the created dma-buf takes ownership of the ref, + * but if that fails then drop the ref + */ + mutex_unlock(&dev->object_name_lock); + return dmabuf; + } + + get_dma_buf(dmabuf); + + /* + * If we've exported this buffer then cheat and add it to the import list + * so we get the correct handle back. We must do this under the + * protection of dev->object_name_lock to ensure that a racing gem close + * ioctl doesn't miss to remove this buffer handle from the cache. + */ + ret = drm_prime_add_buf_handle(&file_priv->prime, dmabuf, handle); + mutex_unlock(&dev->object_name_lock); + if (ret) { + dma_buf_put(dmabuf); + dmabuf = ERR_PTR(ret); + } + return dmabuf; +} /** * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers * @dev: dev to export the buffer from @@ -569,7 +612,7 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev, int *prime_fd) { struct drm_gem_object *obj; - int ret = 0; + int ret; struct dma_buf *dmabuf; mutex_lock(&file_priv->prime.lock); @@ -580,49 +623,14 @@ 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; - } + if (!dmabuf) + dmabuf = export_handle_to_buf(dev, file_priv, obj, handle, flags); - mutex_lock(&dev->object_name_lock); - /* re-export the original imported object */ - if (obj->import_attach) { - dmabuf = obj->import_attach->dmabuf; - get_dma_buf(dmabuf); - goto out_have_obj; - } - - if (obj->dma_buf) { - get_dma_buf(obj->dma_buf); - dmabuf = obj->dma_buf; - goto out_have_obj; - } - - dmabuf = export_and_register_object(dev, obj, flags); if (IS_ERR(dmabuf)) { - /* normally the created dma-buf takes ownership of the ref, - * but if that fails then drop the ref - */ ret = PTR_ERR(dmabuf); - mutex_unlock(&dev->object_name_lock); - goto out; + goto out_object_put; } -out_have_obj: - /* - * If we've exported this buffer then cheat and add it to the import list - * so we get the correct handle back. We must do this under the - * protection of dev->object_name_lock to ensure that a racing gem close - * ioctl doesn't miss to remove this buffer handle from the cache. - */ - ret = drm_prime_add_buf_handle(&file_priv->prime, - 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 @@ -630,18 +638,18 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev, * 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; - } + if (ret < 0) + goto out_dmabuf_put; - goto out; + *prime_fd = ret; -fail_put_dmabuf: + drm_gem_object_put_unlocked(obj); + mutex_unlock(&file_priv->prime.lock); + return 0; + +out_dmabuf_put: dma_buf_put(dmabuf); -out: +out_object_put: drm_gem_object_put_unlocked(obj); out_unlock: mutex_unlock(&file_priv->prime.lock);