From patchwork Tue Dec 19 19:29:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kim, Dongwon" X-Patchwork-Id: 10124037 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 E62746019C for ; Tue, 19 Dec 2017 19:36:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D85B729230 for ; Tue, 19 Dec 2017 19:36:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CD76A29551; Tue, 19 Dec 2017 19:36:42 +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 727C029230 for ; Tue, 19 Dec 2017 19:36:42 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 40BD86E334; Tue, 19 Dec 2017 19:36:34 +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 E46B96E334 for ; Tue, 19 Dec 2017 19:36:32 +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:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,428,1508828400"; d="scan'208";a="4018512" Received: from downor-z87x-ud5h.fm.intel.com ([10.1.122.11]) by orsmga007.jf.intel.com with ESMTP; 19 Dec 2017 11:36:32 -0800 From: Dongwon Kim To: linux-kernel@vger.kernel.org Subject: [RFC PATCH 11/60] hyper_dmabuf: check stack before unmapping/detaching shadow DMA_BUF Date: Tue, 19 Dec 2017 11:29:27 -0800 Message-Id: <1513711816-2618-11-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 Make sure list of mapping/attaching activities on impoter VM is not empty before doing unmapping/detaching shadow DMA BUF for indirect synchronization. Signed-off-by: Dongwon Kim --- .../xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c | 68 +++++++++++++++++----- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/drivers/xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c b/drivers/xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c index 6ba932f..fa2fa11 100644 --- a/drivers/xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c +++ b/drivers/xen/hyper_dmabuf/hyper_dmabuf_remote_sync.c @@ -11,6 +11,21 @@ extern struct hyper_dmabuf_private hyper_dmabuf_private; +/* Whenever importer does dma operations from remote domain, + * a notification is sent to the exporter so that exporter + * issues equivalent dma operation on the original dma buf + * for indirect synchronization via shadow operations. + * + * All ptrs and references (e.g struct sg_table*, + * struct dma_buf_attachment) created via these operations on + * exporter's side are kept in stack (implemented as circular + * linked-lists) separately so that those can be re-referenced + * later when unmapping operations are invoked to free those. + * + * The very first element on the bottom of each stack holds + * are what is created when initial exporting is issued so it + * should not be modified or released by this fuction. + */ int hyper_dmabuf_remote_sync(int id, int ops) { struct hyper_dmabuf_sgt_info *sgt_info; @@ -33,7 +48,7 @@ int hyper_dmabuf_remote_sync(int id, int ops) attachl = kcalloc(1, sizeof(*attachl), GFP_KERNEL); attachl->attach = dma_buf_attach(sgt_info->dma_buf, - hyper_dmabuf_private.device); + hyper_dmabuf_private.device); if (!attachl->attach) { kfree(attachl); @@ -45,22 +60,31 @@ int hyper_dmabuf_remote_sync(int id, int ops) break; case HYPER_DMABUF_OPS_DETACH: - attachl = list_first_entry(&sgt_info->active_attached->list, - struct attachment_list, list); - - if (!attachl) { + if (list_empty(&sgt_info->active_attached->list)) { printk("dmabuf remote sync::error while processing HYPER_DMABUF_OPS_DETACH\n"); + printk("no more dmabuf attachment left to be detached\n"); return -EINVAL; } + + attachl = list_first_entry(&sgt_info->active_attached->list, + struct attachment_list, list); + dma_buf_detach(sgt_info->dma_buf, attachl->attach); list_del(&attachl->list); kfree(attachl); break; case HYPER_DMABUF_OPS_MAP: - sgtl = kcalloc(1, sizeof(*sgtl), GFP_KERNEL); + if (list_empty(&sgt_info->active_attached->list)) { + printk("dmabuf remote sync::error while processing HYPER_DMABUF_OPS_MAP\n"); + printk("no more dmabuf attachment left to be detached\n"); + return -EINVAL; + } + attachl = list_first_entry(&sgt_info->active_attached->list, - struct attachment_list, list); + struct attachment_list, list); + + sgtl = kcalloc(1, sizeof(*sgtl), GFP_KERNEL); sgtl->sgt = dma_buf_map_attachment(attachl->attach, DMA_BIDIRECTIONAL); if (!sgtl->sgt) { kfree(sgtl); @@ -71,17 +95,20 @@ int hyper_dmabuf_remote_sync(int id, int ops) break; case HYPER_DMABUF_OPS_UNMAP: - attachl = list_first_entry(&sgt_info->active_attached->list, - struct attachment_list, list); - sgtl = list_first_entry(&sgt_info->active_sgts->list, - struct sgt_list, list); - if (!attachl || !sgtl) { + if (list_empty(&sgt_info->active_sgts->list) || + list_empty(&sgt_info->active_attached->list)) { printk("dmabuf remote sync::error while processing HYPER_DMABUF_OPS_UNMAP\n"); + printk("no more SGT or attachment left to be freed\n"); return -EINVAL; } + attachl = list_first_entry(&sgt_info->active_attached->list, + struct attachment_list, list); + sgtl = list_first_entry(&sgt_info->active_sgts->list, + struct sgt_list, list); + dma_buf_unmap_attachment(attachl->attach, sgtl->sgt, - DMA_BIDIRECTIONAL); + DMA_BIDIRECTIONAL); list_del(&sgtl->list); kfree(sgtl); break; @@ -129,9 +156,15 @@ int hyper_dmabuf_remote_sync(int id, int ops) case HYPER_DMABUF_OPS_KUNMAP_ATOMIC: case HYPER_DMABUF_OPS_KUNMAP: + if (list_empty(&sgt_info->va_kmapped->list)) { + printk("dmabuf remote sync::error while processing HYPER_DMABUF_OPS_KUNMAP(_ATOMIC)\n"); + printk("no more dmabuf VA to be freed\n"); + return -EINVAL; + } + va_kmapl = list_first_entry(&sgt_info->va_kmapped->list, - struct kmap_vaddr_list, list); - if (!va_kmapl || va_kmapl->vaddr == NULL) { + struct kmap_vaddr_list, list); + if (va_kmapl->vaddr == NULL) { printk("dmabuf remote sync::error while processing HYPER_DMABUF_OPS_KUNMAP(_ATOMIC)\n"); return -EINVAL; } @@ -167,6 +200,11 @@ int hyper_dmabuf_remote_sync(int id, int ops) break; case HYPER_DMABUF_OPS_VUNMAP: + if (list_empty(&sgt_info->va_vmapped->list)) { + printk("dmabuf remote sync::error while processing HYPER_DMABUF_OPS_VUNMAP\n"); + printk("no more dmabuf VA to be freed\n"); + return -EINVAL; + } va_vmapl = list_first_entry(&sgt_info->va_vmapped->list, struct vmap_vaddr_list, list); if (!va_vmapl || va_vmapl->vaddr == NULL) {