From patchwork Tue Dec 19 19:29:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kim, Dongwon" X-Patchwork-Id: 10124083 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 0F6CA6019C for ; Tue, 19 Dec 2017 19:37:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F37402924D for ; Tue, 19 Dec 2017 19:37:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E5C3F29551; Tue, 19 Dec 2017 19:37:53 +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.2 required=2.0 tests=BAYES_00, 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 362B32924D for ; Tue, 19 Dec 2017 19:37:53 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EE61C6E33D; Tue, 19 Dec 2017 19:36:43 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTPS id 516386E345 for ; Tue, 19 Dec 2017 19:36:36 +0000 (UTC) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Dec 2017 11:36:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,428,1508828400"; d="scan'208";a="4018526" Received: from downor-z87x-ud5h.fm.intel.com ([10.1.122.11]) by orsmga007.jf.intel.com with ESMTP; 19 Dec 2017 11:36:35 -0800 From: Dongwon Kim To: linux-kernel@vger.kernel.org Subject: [RFC PATCH 15/60] hyper_dmabuf: reusing previously released hyper_dmabuf_id Date: Tue, 19 Dec 2017 11:29:31 -0800 Message-Id: <1513711816-2618-15-git-send-email-dongwon.kim@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1513711816-2618-1-git-send-email-dongwon.kim@intel.com> References: <1513711816-2618-1-git-send-email-dongwon.kim@intel.com> MIME-Version: 1.0 Cc: xen-devel@lists.xenproject.org, mateuszx.potrola@intel.com, dri-devel@lists.freedesktop.org, dongwon.kim@intel.com 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 Now, released hyper_dmabuf_ids are stored in a stack - (hyper_dmabuf_private.id_queue) for reuse. This is to prevent overflow of ids for buffers. We also limit maximum number for the id to 1000 for the stability and optimal performance. Signed-off-by: Dongwon Kim --- drivers/xen/hyper_dmabuf/Makefile | 1 + drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.c | 5 ++ drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.h | 6 ++ drivers/xen/hyper_dmabuf/hyper_dmabuf_id.c | 76 ++++++++++++++++++++++ drivers/xen/hyper_dmabuf/hyper_dmabuf_id.h | 24 +++++++ drivers/xen/hyper_dmabuf/hyper_dmabuf_imp.c | 1 + drivers/xen/hyper_dmabuf/hyper_dmabuf_ioctl.c | 15 ++--- .../xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c | 3 + drivers/xen/hyper_dmabuf/hyper_dmabuf_struct.h | 9 --- 9 files changed, 120 insertions(+), 20 deletions(-) create mode 100644 drivers/xen/hyper_dmabuf/hyper_dmabuf_id.c create mode 100644 drivers/xen/hyper_dmabuf/hyper_dmabuf_id.h diff --git a/drivers/xen/hyper_dmabuf/Makefile b/drivers/xen/hyper_dmabuf/Makefile index 3459382..c9b8b7f 100644 --- a/drivers/xen/hyper_dmabuf/Makefile +++ b/drivers/xen/hyper_dmabuf/Makefile @@ -7,6 +7,7 @@ ifneq ($(KERNELRELEASE),) hyper_dmabuf_list.o \ hyper_dmabuf_imp.o \ hyper_dmabuf_msg.o \ + hyper_dmabuf_id.o \ hyper_dmabuf_remote_sync.o \ xen/hyper_dmabuf_xen_comm.o \ xen/hyper_dmabuf_xen_comm_list.o diff --git a/drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.c b/drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.c index 5a7cfa5..66d6cb9 100644 --- a/drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.c +++ b/drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.c @@ -5,6 +5,7 @@ #include "hyper_dmabuf_drv.h" #include "hyper_dmabuf_conf.h" #include "hyper_dmabuf_list.h" +#include "hyper_dmabuf_id.h" #include "xen/hyper_dmabuf_xen_comm_list.h" #include "xen/hyper_dmabuf_xen_comm.h" @@ -67,6 +68,10 @@ static void hyper_dmabuf_drv_exit(void) if (hyper_dmabuf_private.work_queue) destroy_workqueue(hyper_dmabuf_private.work_queue); + /* destroy id_queue */ + if (hyper_dmabuf_private.id_queue) + destroy_reusable_list(); + hyper_dmabuf_destroy_data_dir(); printk( KERN_NOTICE "dma_buf-src_sink model: Exiting" ); unregister_device(); diff --git a/drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.h b/drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.h index ff883e1..37b0cc1 100644 --- a/drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.h +++ b/drivers/xen/hyper_dmabuf/hyper_dmabuf_drv.h @@ -1,10 +1,16 @@ #ifndef __LINUX_PUBLIC_HYPER_DMABUF_DRV_H__ #define __LINUX_PUBLIC_HYPER_DMABUF_DRV_H__ +struct list_reusable_id { + int id; + struct list_head list; +}; + struct hyper_dmabuf_private { struct device *device; int domid; struct workqueue_struct *work_queue; + struct list_reusable_id *id_queue; }; typedef int (*hyper_dmabuf_ioctl_t)(void *data); diff --git a/drivers/xen/hyper_dmabuf/hyper_dmabuf_id.c b/drivers/xen/hyper_dmabuf/hyper_dmabuf_id.c new file mode 100644 index 0000000..7bbb179 --- /dev/null +++ b/drivers/xen/hyper_dmabuf/hyper_dmabuf_id.c @@ -0,0 +1,76 @@ +#include +#include +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_id.h" + +extern struct hyper_dmabuf_private hyper_dmabuf_private; + +void store_reusable_id(int id) +{ + struct list_reusable_id *reusable_head = hyper_dmabuf_private.id_queue; + struct list_reusable_id *new_reusable; + + new_reusable = kmalloc(sizeof(*new_reusable), GFP_KERNEL); + new_reusable->id = id; + + list_add(&new_reusable->list, &reusable_head->list); +} + +static int retrieve_reusable_id(void) +{ + struct list_reusable_id *reusable_head = hyper_dmabuf_private.id_queue; + + /* check there is reusable id */ + if (!list_empty(&reusable_head->list)) { + reusable_head = list_first_entry(&reusable_head->list, + struct list_reusable_id, + list); + + list_del(&reusable_head->list); + return reusable_head->id; + } + + return -1; +} + +void destroy_reusable_list(void) +{ + struct list_reusable_id *reusable_head = hyper_dmabuf_private.id_queue; + struct list_reusable_id *temp_head; + + if (reusable_head) { + /* freeing mem space all reusable ids in the stack */ + while (!list_empty(&reusable_head->list)) { + temp_head = list_first_entry(&reusable_head->list, + struct list_reusable_id, + list); + list_del(&temp_head->list); + kfree(temp_head); + } + + /* freeing head */ + kfree(reusable_head); + } +} + +int hyper_dmabuf_get_id(void) +{ + static int id = 0; + struct list_reusable_id *reusable_head; + int ret; + + /* first cla to hyper_dmabuf_get_id */ + if (id == 0) { + reusable_head = kmalloc(sizeof(*reusable_head), GFP_KERNEL); + reusable_head->id = -1; /* list head have invalid id */ + INIT_LIST_HEAD(&reusable_head->list); + hyper_dmabuf_private.id_queue = reusable_head; + } + + ret = retrieve_reusable_id(); + + if (ret < 0 && id < HYPER_DMABUF_ID_MAX) + return HYPER_DMABUF_ID_CREATE(hyper_dmabuf_private.domid, id++); + + return ret; +} diff --git a/drivers/xen/hyper_dmabuf/hyper_dmabuf_id.h b/drivers/xen/hyper_dmabuf/hyper_dmabuf_id.h new file mode 100644 index 0000000..2c8daf3 --- /dev/null +++ b/drivers/xen/hyper_dmabuf/hyper_dmabuf_id.h @@ -0,0 +1,24 @@ +#ifndef __HYPER_DMABUF_ID_H__ +#define __HYPER_DMABUF_ID_H__ + +/* Importer combine source domain id with given hyper_dmabuf_id + * to make it unique in case there are multiple exporters */ + +#define HYPER_DMABUF_ID_CREATE(domid, id) \ + ((((domid) & 0xFF) << 24) | ((id) & 0xFFFFFF)) + +#define HYPER_DMABUF_DOM_ID(id) \ + (((id) >> 24) & 0xFF) + +/* currently maximum number of buffers shared + * at any given moment is limited to 1000 + */ +#define HYPER_DMABUF_ID_MAX 1000 + +void store_reusable_id(int id); + +void destroy_reusable_list(void); + +int hyper_dmabuf_get_id(void); + +#endif /*__HYPER_DMABUF_ID_H*/ diff --git a/drivers/xen/hyper_dmabuf/hyper_dmabuf_imp.c b/drivers/xen/hyper_dmabuf/hyper_dmabuf_imp.c index fa445e5..b109138 100644 --- a/drivers/xen/hyper_dmabuf/hyper_dmabuf_imp.c +++ b/drivers/xen/hyper_dmabuf/hyper_dmabuf_imp.c @@ -7,6 +7,7 @@ #include #include "hyper_dmabuf_struct.h" #include "hyper_dmabuf_imp.h" +#include "hyper_dmabuf_id.h" #include "xen/hyper_dmabuf_xen_comm.h" #include "hyper_dmabuf_msg.h" #include "hyper_dmabuf_list.h" diff --git a/drivers/xen/hyper_dmabuf/hyper_dmabuf_ioctl.c b/drivers/xen/hyper_dmabuf/hyper_dmabuf_ioctl.c index e334b77..5c6d9c8 100644 --- a/drivers/xen/hyper_dmabuf/hyper_dmabuf_ioctl.c +++ b/drivers/xen/hyper_dmabuf/hyper_dmabuf_ioctl.c @@ -11,6 +11,7 @@ #include "hyper_dmabuf_imp.h" #include "hyper_dmabuf_list.h" #include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_id.h" #include "hyper_dmabuf_query.h" #include "xen/hyper_dmabuf_xen_comm.h" #include "xen/hyper_dmabuf_xen_comm_list.h" @@ -18,16 +19,6 @@ extern struct hyper_dmabuf_private hyper_dmabuf_private; -static uint32_t hyper_dmabuf_id_gen(void) { - /* TODO: add proper implementation */ - static uint32_t id = 1000; - static int32_t domid = -1; - if (domid == -1) { - domid = hyper_dmabuf_get_domid(); - } - return HYPER_DMABUF_ID_IMPORTER(domid, id++); -} - static int hyper_dmabuf_exporter_ring_setup(void *data) { struct ioctl_hyper_dmabuf_exporter_ring_setup *ring_attr; @@ -133,7 +124,7 @@ static int hyper_dmabuf_export_remote(void *data) sgt_info = kmalloc(sizeof(*sgt_info), GFP_KERNEL); - sgt_info->hyper_dmabuf_id = hyper_dmabuf_id_gen(); + sgt_info->hyper_dmabuf_id = hyper_dmabuf_get_id(); /* TODO: We might need to consider using port number on event channel? */ sgt_info->hyper_dmabuf_rdomain = export_remote_attr->remote_domain; sgt_info->dma_buf = dma_buf; @@ -334,6 +325,8 @@ static int hyper_dmabuf_unexport(void *data) hyper_dmabuf_cleanup_sgt_info(sgt_info, false); hyper_dmabuf_remove_exported(unexport_attr->hyper_dmabuf_id); kfree(sgt_info); + /* register hyper_dmabuf_id to the list for reuse */ + store_reusable_id(unexport_attr->hyper_dmabuf_id); } return ret; diff --git a/drivers/xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c b/drivers/xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c index 5017b17..c5950e0 100644 --- a/drivers/xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c +++ b/drivers/xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c @@ -6,6 +6,7 @@ #include "hyper_dmabuf_struct.h" #include "hyper_dmabuf_list.h" #include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_id.h" #include "xen/hyper_dmabuf_xen_comm.h" #include "hyper_dmabuf_msg.h" #include "hyper_dmabuf_imp.h" @@ -124,6 +125,8 @@ int hyper_dmabuf_remote_sync(int id, int ops) hyper_dmabuf_cleanup_sgt_info(sgt_info, false); hyper_dmabuf_remove_exported(id); kfree(sgt_info); + /* store hyper_dmabuf_id in the list for reuse */ + store_reusable_id(id); } break; diff --git a/drivers/xen/hyper_dmabuf/hyper_dmabuf_struct.h b/drivers/xen/hyper_dmabuf/hyper_dmabuf_struct.h index 92e06ff..b52f958 100644 --- a/drivers/xen/hyper_dmabuf/hyper_dmabuf_struct.h +++ b/drivers/xen/hyper_dmabuf/hyper_dmabuf_struct.h @@ -3,15 +3,6 @@ #include -/* Importer combine source domain id with given hyper_dmabuf_id - * to make it unique in case there are multiple exporters */ - -#define HYPER_DMABUF_ID_IMPORTER(domid, id) \ - ((((domid) & 0xFF) << 24) | ((id) & 0xFFFFFF)) - -#define HYPER_DMABUF_DOM_ID(id) \ - (((id) >> 24) & 0xFF) - /* each grant_ref_t is 4 bytes, so total 4096 grant_ref_t can be * in this block meaning we can share 4KB*4096 = 16MB of buffer * (needs to be increased for large buffer use-cases such as 4K