From patchwork Tue Apr 9 20:54:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rob Herring \(Arm\)" X-Patchwork-Id: 161990 Delivered-To: patch@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp5402467jan; Tue, 9 Apr 2019 13:54:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqw1Uwfd5V4wHsoSflp6f7pY6fxU7dpFh097AEs0IK0Ew9jVXJnP1kt4kaqawQu+tHAxTBIh X-Received: by 2002:a17:902:266:: with SMTP id 93mr1428515plc.201.1554843278009; Tue, 09 Apr 2019 13:54:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554843278; cv=none; d=google.com; s=arc-20160816; b=aLNtDx4lz1WSwNcd3CmcPwLyhTQ/J1VvEYslUfp6cZvkiUo3u1KizPBh0youNhTxZL 99HaC87UJwUUtby6vtstW9HAziB3MQWUM9AyQ/hmBq1CNddpLSc6rqWOmsKH8Tglwu4a OHkGl/VLBAkbYV5676LuYN30KVPPQpfmcI5ICTxqizLpKjjkZf5VhI71byqC+iERwwvD nzXpBm7aVBZUs91tvJdOK49y8qB3Pg4KSTynfosriC7ldYNI2dkL4knOCYECEkEnkEcq XQQkilzK0IGVV5kcw4XkUL9UaOocWWG1jA7KRJeqYp/MKdbFbB6mUTrYKv1mHl8+KRO2 axwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:mime-version:references:in-reply-to:message-id:date :subject:to:from:delivered-to; bh=xuTQA369vyLuH4nVmNR62eg+IsIOCqMmHLrhB1eqSec=; b=anCMPGj8pxG+clNGxB4yd30le4CCOKu/pd/siP5P27lSrb7UZ9qq8TZ6YwL3+kxKXz DKIZSMq2sYd67T3bjgyBXvo6mrIc18oUQW6Ctod6Vdc6qJ4JPTx4vm8h1QgD9GgUeClk f8YHxzAoaUub2UxI8t/5vy2bjFzCTHGo5Knl/JKdftnV+NIx9sOsA9Eg526newX8Sm93 T0ZoN1Lz45d2gzKx5pPs0Gq9rZe51otEsVc6F0qUOLnw/0vnBdh7fKqM7WHgB+HzyUIA f3Tyjwh5OwRtGJ2Emg1xNZIzKdsUukiiFfw98jnKBTJ4E3+/Yxx3aaozpaGHMS3SvA74 Jh7g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 2610:10:20:722:a800:ff:fe36:1795 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from gabe.freedesktop.org (gabe.freedesktop.org. [2610:10:20:722:a800:ff:fe36:1795]) by mx.google.com with ESMTPS id w3si19933587plp.260.2019.04.09.13.54.37 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 09 Apr 2019 13:54:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 2610:10:20:722:a800:ff:fe36:1795 as permitted sender) client-ip=2610:10:20:722:a800:ff:fe36:1795; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of dri-devel-bounces@lists.freedesktop.org designates 2610:10:20:722:a800:ff:fe36:1795 as permitted sender) smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E705C8914D; Tue, 9 Apr 2019 20:54:34 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-oi1-f194.google.com (mail-oi1-f194.google.com [209.85.167.194]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4BAE789129 for ; Tue, 9 Apr 2019 20:54:32 +0000 (UTC) Received: by mail-oi1-f194.google.com with SMTP id a6so14815036oie.5 for ; Tue, 09 Apr 2019 13:54:32 -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=ZmRuFlM08g2DvXWoccIjmnRAB0mfVQU1sbM2/zfQB84=; b=PKsuOaAccO2SFOFW0vDV7DYlx911PKZzV/37FwFXP+zH6cC93iT3eJeLjWJ7nEc8C3 AwXiF0PLQnuDTdjLz5EBcOAgW/RhH4zmC4YQsC54d0FJcNe+q6XWxXCOHxuCHRwPeun2 DpHdSWev7ghspplyteOhr2DjgPI7/hdpETwfkkvcovqUbfin8SjWDr57qFvsHodftg94 htj4+FvtGY3vlVdZgDx2jD43BzWrNqrbAGcrEly3t/z35jaAFlrG0UV0fk/nCoAfhkfC VnTtZwODlNyesUaYXvFDWkTGOykiRCDSD4fSUpU7mp0fGHPgXu13NQy+c5NjpZhSbLjG +BVg== X-Gm-Message-State: APjAAAWdfPK57qHFq0PlU0G+foArmfel2gcWkUzwfhJ5TPxmC8oIC1Rb d6QGxoFu5zcKuk5N8DIsyvW36Lo= X-Received: by 2002:aca:3746:: with SMTP id e67mr220414oia.37.1554843271211; Tue, 09 Apr 2019 13:54:31 -0700 (PDT) Received: from xps15.herring.priv (24-155-109-49.dyn.grandenetworks.net. [24.155.109.49]) by smtp.googlemail.com with ESMTPSA id o66sm6472175oig.11.2019.04.09.13.54.30 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 09 Apr 2019 13:54:30 -0700 (PDT) From: Rob Herring To: dri-devel@lists.freedesktop.org Subject: [PATCH v3 2/3] drm: Add a drm_gem_objects_lookup helper Date: Tue, 9 Apr 2019 15:54:26 -0500 Message-Id: <20190409205427.6943-3-robh@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190409205427.6943-1-robh@kernel.org> References: <20190409205427.6943-1-robh@kernel.org> 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: Maxime Ripard , linux-kernel@vger.kernel.org, David Airlie , Sean Paul , Alyssa Rosenzweig Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Similar to the single handle drm_gem_object_lookup(), drm_gem_objects_lookup() takes an array of handles and returns an array of GEM objects. v2: - Take the userspace pointer directly and allocate the array. - Expand the function documentation. Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Sean Paul Cc: David Airlie Cc: Daniel Vetter Acked-by: Alyssa Rosenzweig Signed-off-by: Rob Herring Acked-by: Tomeu Vizoso --- drivers/gpu/drm/drm_gem.c | 93 ++++++++++++++++++++++++++++++++++----- include/drm/drm_gem.h | 2 + 2 files changed, 85 insertions(+), 10 deletions(-) -- 2.19.1 diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c index 388b3742e562..faa2718e85e8 100644 --- a/drivers/gpu/drm/drm_gem.c +++ b/drivers/gpu/drm/drm_gem.c @@ -663,6 +663,85 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, } EXPORT_SYMBOL(drm_gem_put_pages); +static int objects_lookup(struct drm_file *filp, u32 *handle, int count, + struct drm_gem_object **objs) +{ + int i, ret = 0; + struct drm_gem_object *obj; + + spin_lock(&filp->table_lock); + + for (i = 0; i < count; i++) { + /* Check if we currently have a reference on the object */ + obj = idr_find(&filp->object_idr, handle[i]); + if (!obj) { + ret = -ENOENT; + break; + } + drm_gem_object_get(obj); + objs[i] = obj; + } + spin_unlock(&filp->table_lock); + + return ret; +} + +/** + * drm_gem_objects_lookup - look up GEM objects from an array of handles + * @filp: DRM file private date + * @bo_handles: user pointer to array of userspace handle + * @count: size of handle array + * @objs_out: returned pointer to array of drm_gem_object pointers + * + * Takes an array of userspace handles and returns a newly allocated array of + * GEM objects. + * + * For a single handle lookup, use drm_gem_object_lookup(). + * + * Returns: + * + * @objs filled in with GEM object pointers. Returned GEM objects need to be + * released with drm_gem_object_put(). -ENOENT is returned on a lookup + * failure. 0 is returned on success. + * + */ +int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, + int count, struct drm_gem_object ***objs_out) +{ + int ret; + u32 *handles; + struct drm_gem_object **objs; + + if (!count) + return 0; + + objs = kvmalloc_array(count, sizeof(struct drm_gem_object *), + GFP_KERNEL | __GFP_ZERO); + if (!objs) + return -ENOMEM; + + handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL); + if (!handles) { + ret = -ENOMEM; + goto out; + } + + if (copy_from_user(handles, bo_handles, count * sizeof(u32))) { + ret = -EFAULT; + DRM_DEBUG("Failed to copy in GEM handles\n"); + goto out; + } + + ret = objects_lookup(filp, handles, count, objs); + *objs_out = objs; + +out: + kvfree(handles); + return ret; + +} +EXPORT_SYMBOL(drm_gem_objects_lookup); + /** * drm_gem_object_lookup - look up a GEM object from its handle * @filp: DRM file private date @@ -672,21 +751,15 @@ EXPORT_SYMBOL(drm_gem_put_pages); * * A reference to the object named by the handle if such exists on @filp, NULL * otherwise. + * + * If looking up an array of handles, use drm_gem_objects_lookup(). */ struct drm_gem_object * drm_gem_object_lookup(struct drm_file *filp, u32 handle) { - struct drm_gem_object *obj; - - spin_lock(&filp->table_lock); - - /* Check if we currently have a reference on the object */ - obj = idr_find(&filp->object_idr, handle); - if (obj) - drm_gem_object_get(obj); - - spin_unlock(&filp->table_lock); + struct drm_gem_object *obj = NULL; + objects_lookup(filp, &handle, 1, &obj); return obj; } EXPORT_SYMBOL(drm_gem_object_lookup); diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index 2955aaab3dca..5ee85c9eaa9d 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -381,6 +381,8 @@ struct page **drm_gem_get_pages(struct drm_gem_object *obj); void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, bool dirty, bool accessed); +int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, + int count, struct drm_gem_object ***objs_out); struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle); long drm_gem_reservation_object_wait(struct drm_file *filep, u32 handle, bool wait_all, unsigned long timeout);