From patchwork Wed Feb 15 19:10:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Nicolai_H=C3=A4hnle?= X-Patchwork-Id: 9574831 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9090960493 for ; Wed, 15 Feb 2017 19:11:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 78464284CE for ; Wed, 15 Feb 2017 19:11:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6C727284DA; Wed, 15 Feb 2017 19:11:14 +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=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID 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 AC0A6284CE for ; Wed, 15 Feb 2017 19:11:13 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 34F806E9D0; Wed, 15 Feb 2017 19:11:11 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr0-x243.google.com (mail-wr0-x243.google.com [IPv6:2a00:1450:400c:c0c::243]) by gabe.freedesktop.org (Postfix) with ESMTPS id 729816E9D0; Wed, 15 Feb 2017 19:11:09 +0000 (UTC) Received: by mail-wr0-x243.google.com with SMTP id q39so1692408wrb.2; Wed, 15 Feb 2017 11:11:09 -0800 (PST) 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=a53+OB9S2UcSdAzarHXt/GPmUnL+0bnyVE4eiR933q0=; b=diT9g97GC9hAnUb4t8gWTwG7JoL/Q4KLo27JTVOxcT40SNxdjqT9nfKACCAtrhhlSe wmt6hLL1OQuNcwWpFuIB4KSGnqfgH9onupgGekg82dFlYp6TX1PKMvzkf7t+ZwVwIOJR U1XpFEoCSJ6n/tSiFrTJ4mAXDDc0J/j1ByAOkXDmUHhgxvn4CAfX8v5rHfddYDwxVuPT 98WXwhT+mIhIjweISDRHcUeexK3UqJD7bzfPX2u/ich2BumsP3GYd+0TPV15ZCO5NRch QPPkX7WSVSHYDBnxPfAQrUq0547owIIcXZ2g8QtQYZuV1MluaAec+fSHw+spssnlxYPL Wotw== 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=a53+OB9S2UcSdAzarHXt/GPmUnL+0bnyVE4eiR933q0=; b=Lo7pDJo3Pmm6qMcdUwFI6hcGYteIlVfrlp7RdHJNzieINuQcBk99kZ0aTyn4tFLXS3 xh+wKbbnVzu+io0pdFQaXesjYRSiIdaD86B4cJKurIIHXxwiEnD+HvJJQam4gSE22cSv sT9CImQvuO3yU+TRJ9MSJ2zTxka8chaqaoJkd0W2GQgTM/jA4n2p7i4oCaOkg2I8b4ML ktZQhNMWvmOfd67J0UhgfOFpCMgz4cuORCDwOrtQqcbT1EVPowtyesBDdkUXAry0We+r pNZHybeF9FiTu+dCjEg1NsAOFAYV2f80cQXYAD47hk3zkvlPMWfL5i59jJBNJlP8PSpQ zUnw== X-Gm-Message-State: AMke39lzuWaCmReLMCQtqgQ0YNokUKBznkjtPy57RpwGARn7g9SXUDukDNrU5KFAaaZjNw== X-Received: by 10.223.151.53 with SMTP id r50mr29598508wrb.129.1487185867729; Wed, 15 Feb 2017 11:11:07 -0800 (PST) Received: from capella.localdomain (x55b54b64.dyn.telefonica.de. [85.181.75.100]) by smtp.gmail.com with ESMTPSA id w204sm440510wmd.17.2017.02.15.11.11.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Feb 2017 11:11:07 -0800 (PST) From: =?UTF-8?q?Nicolai=20H=C3=A4hnle?= To: amd-gfx@lists.freedesktop.org Subject: [PATCH v2 1/3] drm/ttm: split BO structure initialization into a separate function Date: Wed, 15 Feb 2017 20:10:54 +0100 Message-Id: <20170215191056.17718-1-nhaehnle@gmail.com> X-Mailer: git-send-email 2.9.3 MIME-Version: 1.0 Cc: =?UTF-8?q?Nicolai=20H=C3=A4hnle?= , dri-devel@lists.freedesktop.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Nicolai Hähnle Allow callers to opt out of calling ttm_bo_validate immediately. This allows more flexibility in how locking of the reservation object is done, which is needed to fix a locking bug (destroy locked mutex) in amdgpu. v2: callers of ttm_bo_init_top should use ttm_bo_unref for error handling Signed-off-by: Nicolai Hähnle --- drivers/gpu/drm/ttm/ttm_bo.c | 89 ++++++++++++++++++++++++++++---------------- include/drm/ttm/ttm_bo_api.h | 44 ++++++++++++++++++++++ 2 files changed, 100 insertions(+), 33 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c index c438b04..390b763 100644 --- a/drivers/gpu/drm/ttm/ttm_bo.c +++ b/drivers/gpu/drm/ttm/ttm_bo.c @@ -1119,44 +1119,23 @@ int ttm_bo_validate(struct ttm_buffer_object *bo, } EXPORT_SYMBOL(ttm_bo_validate); -int ttm_bo_init(struct ttm_bo_device *bdev, - struct ttm_buffer_object *bo, - unsigned long size, - enum ttm_bo_type type, - struct ttm_placement *placement, - uint32_t page_alignment, - bool interruptible, - struct file *persistent_swap_storage, - size_t acc_size, - struct sg_table *sg, - struct reservation_object *resv, - void (*destroy) (struct ttm_buffer_object *)) +int ttm_bo_init_top(struct ttm_bo_device *bdev, + struct ttm_buffer_object *bo, + unsigned long size, + enum ttm_bo_type type, + uint32_t page_alignment, + struct file *persistent_swap_storage, + size_t acc_size, + struct sg_table *sg, + struct reservation_object *resv, + void (*destroy) (struct ttm_buffer_object *)) { int ret = 0; unsigned long num_pages; struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; - bool locked; - - ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); - if (ret) { - pr_err("Out of kernel memory\n"); - if (destroy) - (*destroy)(bo); - else - kfree(bo); - return -ENOMEM; - } num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; - if (num_pages == 0) { - pr_err("Illegal buffer object size\n"); - if (destroy) - (*destroy)(bo); - else - kfree(bo); - ttm_mem_global_free(mem_glob, acc_size); - return -EINVAL; - } + bo->destroy = destroy; kref_init(&bo->kref); @@ -1181,7 +1160,7 @@ int ttm_bo_init(struct ttm_bo_device *bdev, bo->moving = NULL; bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); bo->persistent_swap_storage = persistent_swap_storage; - bo->acc_size = acc_size; + bo->acc_size = 0; bo->sg = sg; if (resv) { bo->resv = resv; @@ -1195,6 +1174,22 @@ int ttm_bo_init(struct ttm_bo_device *bdev, bo->priority = 0; /* + * Error checking comes after initialization, so that the regular + * buffer object cleanup code can be used safely by the caller. + */ + if (num_pages == 0) { + pr_err("Illegal buffer object size\n"); + return -EINVAL; + } + + ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); + if (ret) { + pr_err("Out of kernel memory\n"); + return -ENOMEM; + } + bo->acc_size = acc_size; + + /* * For ttm_bo_type_device buffers, allocate * address space from the device. */ @@ -1203,6 +1198,34 @@ int ttm_bo_init(struct ttm_bo_device *bdev, ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node, bo->mem.num_pages); + return ret; +} +EXPORT_SYMBOL(ttm_bo_init_top); + +int ttm_bo_init(struct ttm_bo_device *bdev, + struct ttm_buffer_object *bo, + unsigned long size, + enum ttm_bo_type type, + struct ttm_placement *placement, + uint32_t page_alignment, + bool interruptible, + struct file *persistent_swap_storage, + size_t acc_size, + struct sg_table *sg, + struct reservation_object *resv, + void (*destroy) (struct ttm_buffer_object *)) +{ + bool locked; + int ret; + + ret = ttm_bo_init_top(bdev, bo, size, type, page_alignment, + persistent_swap_storage, acc_size, sg, resv, + destroy); + if (ret) { + ttm_bo_unref(&bo); + return ret; + } + /* passed reservation objects should already be locked, * since otherwise lockdep will be angered in radeon. */ diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h index f195899..6e8eaee 100644 --- a/include/drm/ttm/ttm_bo_api.h +++ b/include/drm/ttm/ttm_bo_api.h @@ -453,6 +453,50 @@ size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, unsigned struct_size); /** + * ttm_bo_init_top + * + * @bdev: Pointer to a ttm_bo_device struct. + * @bo: Pointer to a ttm_buffer_object to be initialized. + * @size: Requested size of buffer object. + * @type: Requested type of buffer object. + * @flags: Initial placement flags. + * @page_alignment: Data alignment in pages. + * @persistent_swap_storage: Usually the swap storage is deleted for buffers + * pinned in physical memory. If this behaviour is not desired, this member + * holds a pointer to a persistent shmem object. Typically, this would + * point to the shmem object backing a GEM object if TTM is used to back a + * GEM user interface. + * @acc_size: Accounted size for this object. + * @resv: Pointer to a reservation_object, or NULL to let ttm allocate one. + * @destroy: Destroy function. Use NULL for kfree(). + * + * This function initializes a pre-allocated struct ttm_buffer_object. + * As this object may be part of a larger structure, this function, + * together with the @destroy function, + * enables driver-specific objects derived from a ttm_buffer_object. + * + * Unlike ttm_bo_init, @bo is not validated, and when an error is returned, + * the caller is responsible for freeing @bo (using ttm_bo_unref). + * + * On successful return, the object kref and list_kref are set to 1. + * + * Returns + * -ENOMEM: Out of memory. + * -EINVAL: Invalid buffer size. + */ + +extern int ttm_bo_init_top(struct ttm_bo_device *bdev, + struct ttm_buffer_object *bo, + unsigned long size, + enum ttm_bo_type type, + uint32_t page_alignment, + struct file *persistent_swap_storage, + size_t acc_size, + struct sg_table *sg, + struct reservation_object *resv, + void (*destroy) (struct ttm_buffer_object *)); + +/** * ttm_bo_init * * @bdev: Pointer to a ttm_bo_device struct.