From patchwork Fri Sep 27 13:46:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qiang Yu X-Patchwork-Id: 11164537 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0339C924 for ; Fri, 27 Sep 2019 13:47:09 +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 DF2C8217D7 for ; Fri, 27 Sep 2019 13:47:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DF2C8217D7 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.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 396166E155; Fri, 27 Sep 2019 13:47:08 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by gabe.freedesktop.org (Postfix) with ESMTPS id 87EC26E155; Fri, 27 Sep 2019 13:47:06 +0000 (UTC) Received: by mail-pf1-x442.google.com with SMTP id b128so1646410pfa.1; Fri, 27 Sep 2019 06:47:06 -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; bh=x5RJMtRFIUCEvOtz+LaV+1tuclWJatKNIS20t2HAKQk=; b=KJ8aKaz2od5zPYCAZ3Gw/sZwFwDSRTKPM/q+Pu7YDihk/fHEHP9x4MUEfSgp24SsMB LlgcwNT+W26wX9o23ceWAKX92yzUUjMjsyjHAIToN8aBmZ1lD9jwmgWVc4PummuTiyoo DG5EKv+yvhC3hDsEBXcDBeI2mYnrSX/eVrr7wkqT5EINm+9dtq/87v7AH1fA+h+CNmjt +9NSiUo61XCJaY+lVryBJU1nFnE71m7iOTSIU3C0ivdG93vwiuy32CoVNtX33VSgDhE+ BX+/ewJwU+/nsrBwGIrq5YGuJQhhYiJIr9GT0mZQyM6vhyGrJP0le//Ga0TOkW3DKFN8 YboA== X-Gm-Message-State: APjAAAWWM7jHJ61qSQJmlfecvUWDdeTJ/xPZH6u3AgolWDtXMEgUvqHi HXwmCgnhMwfup7iWOCmQoyhPMF+EwIY= X-Google-Smtp-Source: APXvYqy5wIJpd0Gg72LXlOJ5tPTyMlpC2R4k8GTroBI+a+p/AS9SKRitgMCGh8X7N8rXb2qMFLDtUw== X-Received: by 2002:a63:7b58:: with SMTP id k24mr9399070pgn.138.1569592025861; Fri, 27 Sep 2019 06:47:05 -0700 (PDT) Received: from localhost.localdomain (ah.ptr230.ptrcloud.net. [153.122.161.8]) by smtp.gmail.com with ESMTPSA id q71sm5608860pjb.26.2019.09.27.06.47.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2019 06:47:05 -0700 (PDT) From: Qiang Yu To: dri-devel@lists.freedesktop.org Subject: [PATCH v2 1/6] drm/gem: refine drm_gem_objects_lookup Date: Fri, 27 Sep 2019 21:46:11 +0800 Message-Id: <20190927134616.21899-2-yuq825@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190927134616.21899-1-yuq825@gmail.com> References: <20190927134616.21899-1-yuq825@gmail.com> 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; bh=x5RJMtRFIUCEvOtz+LaV+1tuclWJatKNIS20t2HAKQk=; b=P90tyCHucly03dEwBTI7gkAae/nqkG0hLqhn0m0yLtiDACqLUi0olbZv7fSUidA/VZ 8M1keeJqgl6bsKZJmD2dkhiH86l4MBQRpyYohpdfgRG9bSzCmSQS2nIYgXKoxOA4HWTt qn0Q8m8LjTHI/pM4hayMWxpIpP/majH8qM87h13rENDmmc4XCDvuk4yQ/xd+PxKVbPED m1LPgkEYcJPOtmUggkgK8uRpONDklW5q7TzVdzUIuYcma0u2/cp3OM1tCYPCZ/n5FxbF BkomTx74QucEjLFFtdhl8Qojyg4KjvUzL6WPdzCXwZjI3jTsMwdU+E6lt7+p4tJJPQVg Nc4A== 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: Tomeu Vizoso , lima@lists.freedesktop.org, Maxime Ripard , Steven Price , David Airlie , Qiang Yu , Sean Paul , Alyssa Rosenzweig MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" drm_gem_objects_lookup does not use user space bo handles directly and left the user to kernel copy work to a new interface drm_gem_objects_lookup_user. This is for driver like lima which does not pass gem bo handles continously in an array in ioctl argument. v2: 1. add drm_gem_objects_lookup_user 2. remove none-zero check as all caller will check before calling this function Cc: Rob Herring Cc: Tomeu Vizoso Cc: Steven Price Cc: Alyssa Rosenzweig Suggested-by: Rob Herring Signed-off-by: Qiang Yu Reviewed-by: Steven Price --- drivers/gpu/drm/drm_gem.c | 57 +++++++++++++++++++------ drivers/gpu/drm/panfrost/panfrost_drv.c | 6 +-- include/drm/drm_gem.h | 4 +- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c index 6854f5867d51..a5e88c3e6d25 100644 --- a/drivers/gpu/drm/drm_gem.c +++ b/drivers/gpu/drm/drm_gem.c @@ -679,11 +679,11 @@ static int objects_lookup(struct drm_file *filp, u32 *handle, int count, /** * 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 + * @bo_handles: array of GEM object handles * @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 + * Takes an array of GEM object handles and returns a newly allocated array of * GEM objects. * * For a single handle lookup, use drm_gem_object_lookup(). @@ -695,26 +695,56 @@ static int objects_lookup(struct drm_file *filp, u32 *handle, int count, * failure. 0 is returned on success. * */ -int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, +int drm_gem_objects_lookup(struct drm_file *filp, u32 *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; + ret = objects_lookup(filp, bo_handles, count, objs); + if (ret) + kvfree(objs); + else + *objs_out = objs; + + return ret; + +} +EXPORT_SYMBOL(drm_gem_objects_lookup); + +/** + * drm_gem_objects_lookup_user - 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_user(struct drm_file *filp, void __user *bo_handles, + int count, struct drm_gem_object ***objs_out) +{ + int ret; + u32 *handles; + handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL); - if (!handles) { - ret = -ENOMEM; - goto out; - } + if (!handles) + return -ENOMEM; if (copy_from_user(handles, bo_handles, count * sizeof(u32))) { ret = -EFAULT; @@ -722,15 +752,14 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, goto out; } - ret = objects_lookup(filp, handles, count, objs); - *objs_out = objs; + ret = drm_gem_objects_lookup(filp, handles, count, objs_out); out: kvfree(handles); return ret; } -EXPORT_SYMBOL(drm_gem_objects_lookup); +EXPORT_SYMBOL(drm_gem_objects_lookup_user); /** * drm_gem_object_lookup - look up a GEM object from its handle diff --git a/drivers/gpu/drm/panfrost/panfrost_drv.c b/drivers/gpu/drm/panfrost/panfrost_drv.c index d74442d71048..486ca51d5662 100644 --- a/drivers/gpu/drm/panfrost/panfrost_drv.c +++ b/drivers/gpu/drm/panfrost/panfrost_drv.c @@ -130,9 +130,9 @@ panfrost_lookup_bos(struct drm_device *dev, if (!job->implicit_fences) return -ENOMEM; - return drm_gem_objects_lookup(file_priv, - (void __user *)(uintptr_t)args->bo_handles, - job->bo_count, &job->bos); + return drm_gem_objects_lookup_user(file_priv, + (void __user *)(uintptr_t)args->bo_handles, + job->bo_count, &job->bos); } /** diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index 6aaba14f5972..354fc8d240e4 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -387,8 +387,10 @@ 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 drm_gem_objects_lookup(struct drm_file *filp, u32 *bo_handles, int count, struct drm_gem_object ***objs_out); +int drm_gem_objects_lookup_user(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_dma_resv_wait(struct drm_file *filep, u32 handle, bool wait_all, unsigned long timeout);