Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp3611799imm; Fri, 25 May 2018 08:34:44 -0700 (PDT) X-Google-Smtp-Source: AB8JxZolZz2jebsdoQ43UqLSnGnLRJiuJ2uA1P4Jp7cGNfir8d79lCuv1UsPCIutoUKf1Eb19opd X-Received: by 2002:a17:902:b409:: with SMTP id x9-v6mr3175748plr.180.1527262484321; Fri, 25 May 2018 08:34:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527262484; cv=none; d=google.com; s=arc-20160816; b=CyJ3v5asOgzrvjpSuKDA9Vv7DCy4DqWBB5qpEkEqodLucz6oUHramCbuxr9lh4Z2Im ih50VtCmmz77A2WoL62CwTFMklcTmcsJ/eJuJkn7+p/BoqzI0eiiMmXPOyWReL6p5zIH fe6QT75qhZh/SsFO3STwGVJsjBBWlAdRmRdBS7W4kS2+ys/0BX8AyDfrpDF279FX1YyX pooQRWMDng/y136ZeCtYxRyytT+jDvLbdVijc8Kb8U0/CYGmkyPKz5a/hgjB+Hsq08s7 5VklbeKTO5V8V32LTOWOf3nNd1amHifui1yTp6OFRnRVlkDH+BBMWRa91SrWaBUfVpZz z56w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=zu1wXTcqgkwxKjBZAlg35D5YKIX1NZrzeHkABNrGbdk=; b=ylNjUoIYSnbptgUvjN8lzG01QAtIT46dSn6bGGHHRUr1WPeygHr30rqAr29EYjgF+F 9okDnFaLYeH14oZglRzg/ro+Ll/amMUDeDEC5s26meFQlj/c+5F6MhidSxp37II3okTS g6bysdSYdafcyN9DjRYaFCIiPYjAUNspjPrUGI4y2le+hfBAwEHJVE0RMeeWUtPZ4g97 Oc5ycI+ErL3acTTSSsgmwy/xb2khjIxLo9SifMfJVRXAgYMq08KZ0XxY7ekTX73HdteU MrcOL/GBEP2h7Ine2pJZuRlJ5c+wNI3AC6o4wAv5V73wZ/3RQTbRcZVpxoJBcl0ShEn/ ASGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Tonum2Yi; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 127-v6si24055869pfe.49.2018.05.25.08.34.28; Fri, 25 May 2018 08:34:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Tonum2Yi; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966796AbeEYPeB (ORCPT + 99 others); Fri, 25 May 2018 11:34:01 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:51102 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S966743AbeEYPd6 (ORCPT ); Fri, 25 May 2018 11:33:58 -0400 Received: by mail-wm0-f68.google.com with SMTP id t11-v6so15545546wmt.0; Fri, 25 May 2018 08:33:57 -0700 (PDT) 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=zu1wXTcqgkwxKjBZAlg35D5YKIX1NZrzeHkABNrGbdk=; b=Tonum2YiLP4xrOGKH1Qv0dwJqtvUFj9Ido//EgoBuP81TYoUwFOcBfLYcYr2loMRhu VJ5wsoU/JLy+3Irb3RK7wsoI+W04L9ZodQW6zBxCHGEDRSuIbxM9viMlaY/a/zQ2s57R iJOC27s8PwQc/QUuWk0AAo1QQLN6ZZuojR7fkyZg6t35pFBqDiZmfT2Zz4hYInEVE3gV rnbG9yRPLg7r2pyPzXWxzHJ4UMpV0xBg0QqHk/lLFibf7HqAIPvL/oPHQ3rlcKjwLVhC JDGjdTJnBzhaem2SBZfD/zkJ2ow0GjW1aAXFJQULqkVlQodF8Wvy+0myqRa6rsMysgON J5tw== 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=zu1wXTcqgkwxKjBZAlg35D5YKIX1NZrzeHkABNrGbdk=; b=t+PL/afLbGhWMsAB+lsU26w+M8JIrPNZ7fYkjvVRxMRGtmVKH7DIdqtbPlfEvkGGMb iQGNd0K03QxDZsW/xPyJce00pWsxcPrMZcXtqFQzLI2UnuU+hiWJsQJ7hXIoMNgPQqUp O/WGW1uyRmEb30tMUT3Sx/8QPt8KXPyrkIbKoF/i6g1rOTaPfPp2gFQLGV9RLbDeFOW5 a3KI+pGFE0tRBKTE5mfhXaQjV4Dhyr52IhNvfPuIEL5N7KJkOn1ebsM6k1hr1MYUI1bA 1tf1LuIhB5G5nat0g9uHTA2e6GmlQrJpES7QiYAkPIQLf0MEgaGxrmAce5CQYw/Uf90T hFiw== X-Gm-Message-State: ALKqPwdPIaeUZB0OaxT3aDube8N7ZZBVyGnDMr6U+sjW5JucT5W+6+63 Bmsif7vRlntRmRFPLpnuHv4= X-Received: by 2002:a2e:9ac3:: with SMTP id p3-v6mr2072072ljj.60.1527262436768; Fri, 25 May 2018 08:33:56 -0700 (PDT) Received: from a2k-HP-ProDesk-600-G2-SFF.kyiv.epam.com (ll-54.209.223.85.sovam.net.ua. [85.223.209.54]) by smtp.gmail.com with ESMTPSA id l7-v6sm314231ljh.53.2018.05.25.08.33.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 25 May 2018 08:33:55 -0700 (PDT) From: Oleksandr Andrushchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, jgross@suse.com, boris.ostrovsky@oracle.com, konrad.wilk@oracle.com Cc: daniel.vetter@intel.com, andr2000@gmail.com, dongwon.kim@intel.com, matthew.d.roper@intel.com, Oleksandr Andrushchenko Subject: [PATCH 8/8] xen/gntdev: Expose gntdev's dma-buf API for in-kernel use Date: Fri, 25 May 2018 18:33:31 +0300 Message-Id: <20180525153331.31188-9-andr2000@gmail.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180525153331.31188-1-andr2000@gmail.com> References: <20180525153331.31188-1-andr2000@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Oleksandr Andrushchenko Allow creating grant device context for use by kernel modules which require functionality, provided by gntdev. Export symbols for dma-buf API provided by the module. Signed-off-by: Oleksandr Andrushchenko --- drivers/xen/gntdev.c | 116 ++++++++++++++++++++++++++-------------- include/xen/grant_dev.h | 37 +++++++++++++ 2 files changed, 113 insertions(+), 40 deletions(-) create mode 100644 include/xen/grant_dev.h diff --git a/drivers/xen/gntdev.c b/drivers/xen/gntdev.c index d8b6168f2cd9..912056f3e909 100644 --- a/drivers/xen/gntdev.c +++ b/drivers/xen/gntdev.c @@ -684,14 +684,33 @@ static const struct mmu_notifier_ops gntdev_mmu_ops = { /* ------------------------------------------------------------------ */ -static int gntdev_open(struct inode *inode, struct file *flip) +void gntdev_free_context(struct gntdev_priv *priv) +{ + struct grant_map *map; + + pr_debug("priv %p\n", priv); + + mutex_lock(&priv->lock); + while (!list_empty(&priv->maps)) { + map = list_entry(priv->maps.next, struct grant_map, next); + list_del(&map->next); + gntdev_put_map(NULL /* already removed */, map); + } + WARN_ON(!list_empty(&priv->freeable_maps)); + + mutex_unlock(&priv->lock); + + kfree(priv); +} +EXPORT_SYMBOL(gntdev_free_context); + +struct gntdev_priv *gntdev_alloc_context(struct device *dev) { struct gntdev_priv *priv; - int ret = 0; priv = kzalloc(sizeof(*priv), GFP_KERNEL); if (!priv) - return -ENOMEM; + return ERR_PTR(-ENOMEM); INIT_LIST_HEAD(&priv->maps); INIT_LIST_HEAD(&priv->freeable_maps); @@ -704,6 +723,32 @@ static int gntdev_open(struct inode *inode, struct file *flip) INIT_LIST_HEAD(&priv->dmabuf_imp_list); #endif +#ifdef CONFIG_XEN_GRANT_DMA_ALLOC + priv->dma_dev = dev; + + /* + * The device is not spawn from a device tree, so arch_setup_dma_ops + * is not called, thus leaving the device with dummy DMA ops. + * Fix this call of_dma_configure() with a NULL node to set + * default DMA ops. + */ + of_dma_configure(priv->dma_dev, NULL); +#endif + pr_debug("priv %p\n", priv); + + return priv; +} +EXPORT_SYMBOL(gntdev_alloc_context); + +static int gntdev_open(struct inode *inode, struct file *flip) +{ + struct gntdev_priv *priv; + int ret = 0; + + priv = gntdev_alloc_context(gntdev_miscdev.this_device); + if (IS_ERR(priv)) + return PTR_ERR(priv); + if (use_ptemod) { priv->mm = get_task_mm(current); if (!priv->mm) { @@ -716,23 +761,11 @@ static int gntdev_open(struct inode *inode, struct file *flip) } if (ret) { - kfree(priv); + gntdev_free_context(priv); return ret; } flip->private_data = priv; -#ifdef CONFIG_XEN_GRANT_DMA_ALLOC - priv->dma_dev = gntdev_miscdev.this_device; - - /* - * The device is not spawn from a device tree, so arch_setup_dma_ops - * is not called, thus leaving the device with dummy DMA ops. - * Fix this call of_dma_configure() with a NULL node to set - * default DMA ops. - */ - of_dma_configure(priv->dma_dev, NULL); -#endif - pr_debug("priv %p\n", priv); return 0; } @@ -740,22 +773,11 @@ static int gntdev_open(struct inode *inode, struct file *flip) static int gntdev_release(struct inode *inode, struct file *flip) { struct gntdev_priv *priv = flip->private_data; - struct grant_map *map; - - pr_debug("priv %p\n", priv); - - mutex_lock(&priv->lock); - while (!list_empty(&priv->maps)) { - map = list_entry(priv->maps.next, struct grant_map, next); - list_del(&map->next); - gntdev_put_map(NULL /* already removed */, map); - } - WARN_ON(!list_empty(&priv->freeable_maps)); - mutex_unlock(&priv->lock); if (use_ptemod) mmu_notifier_unregister(&priv->mn, priv->mm); - kfree(priv); + + gntdev_free_context(priv); return 0; } @@ -1210,7 +1232,7 @@ dmabuf_exp_wait_obj_get_by_fd(struct gntdev_priv *priv, int fd) return ret; } -static int dmabuf_exp_wait_released(struct gntdev_priv *priv, int fd, +int gntdev_dmabuf_exp_wait_released(struct gntdev_priv *priv, int fd, int wait_to_ms) { struct xen_dmabuf *xen_dmabuf; @@ -1242,6 +1264,7 @@ static int dmabuf_exp_wait_released(struct gntdev_priv *priv, int fd, dmabuf_exp_wait_obj_free(priv, obj); return ret; } +EXPORT_SYMBOL(gntdev_dmabuf_exp_wait_released); /* ------------------------------------------------------------------ */ /* DMA buffer export support. */ @@ -1511,7 +1534,7 @@ dmabuf_exp_alloc_backing_storage(struct gntdev_priv *priv, int dmabuf_flags, return map; } -static int dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags, +int gntdev_dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags, int count, u32 domid, u32 *refs, u32 *fd) { struct grant_map *map; @@ -1557,6 +1580,7 @@ static int dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags, gntdev_remove_map(priv, map); return ret; } +EXPORT_SYMBOL(gntdev_dmabuf_exp_from_refs); /* ------------------------------------------------------------------ */ /* DMA buffer import support. */ @@ -1646,8 +1670,9 @@ static struct xen_dmabuf *dmabuf_imp_alloc_storage(int count) return ERR_PTR(-ENOMEM); } -static struct xen_dmabuf * -dmabuf_imp_to_refs(struct gntdev_priv *priv, int fd, int count, int domid) +struct xen_dmabuf * +gntdev_dmabuf_imp_to_refs(struct gntdev_priv *priv, int fd, + int count, int domid) { struct xen_dmabuf *xen_dmabuf, *ret; struct dma_buf *dma_buf; @@ -1736,6 +1761,16 @@ dmabuf_imp_to_refs(struct gntdev_priv *priv, int fd, int count, int domid) dma_buf_put(dma_buf); return ret; } +EXPORT_SYMBOL(gntdev_dmabuf_imp_to_refs); + +u32 *gntdev_dmabuf_imp_get_refs(struct xen_dmabuf *xen_dmabuf) +{ + if (xen_dmabuf) + return xen_dmabuf->u.imp.refs; + + return NULL; +} +EXPORT_SYMBOL(gntdev_dmabuf_imp_get_refs); /* * Find the hyper dma-buf by its file descriptor and remove @@ -1759,7 +1794,7 @@ dmabuf_imp_find_unlink(struct gntdev_priv *priv, int fd) return ret; } -static int dmabuf_imp_release(struct gntdev_priv *priv, u32 fd) +int gntdev_dmabuf_imp_release(struct gntdev_priv *priv, u32 fd) { struct xen_dmabuf *xen_dmabuf; struct dma_buf_attachment *attach; @@ -1785,6 +1820,7 @@ static int dmabuf_imp_release(struct gntdev_priv *priv, u32 fd) dmabuf_imp_free_storage(xen_dmabuf); return 0; } +EXPORT_SYMBOL(gntdev_dmabuf_imp_release); /* ------------------------------------------------------------------ */ /* DMA buffer IOCTL support. */ @@ -1810,8 +1846,8 @@ gntdev_ioctl_dmabuf_exp_from_refs(struct gntdev_priv *priv, goto out; } - ret = dmabuf_exp_from_refs(priv, op.flags, op.count, - op.domid, refs, &op.fd); + ret = gntdev_dmabuf_exp_from_refs(priv, op.flags, op.count, + op.domid, refs, &op.fd); if (ret) goto out; @@ -1832,7 +1868,7 @@ gntdev_ioctl_dmabuf_exp_wait_released(struct gntdev_priv *priv, if (copy_from_user(&op, u, sizeof(op)) != 0) return -EFAULT; - return dmabuf_exp_wait_released(priv, op.fd, op.wait_to_ms); + return gntdev_dmabuf_exp_wait_released(priv, op.fd, op.wait_to_ms); } static long @@ -1846,7 +1882,7 @@ gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv *priv, if (copy_from_user(&op, u, sizeof(op)) != 0) return -EFAULT; - xen_dmabuf = dmabuf_imp_to_refs(priv, op.fd, op.count, op.domid); + xen_dmabuf = gntdev_dmabuf_imp_to_refs(priv, op.fd, op.count, op.domid); if (IS_ERR(xen_dmabuf)) return PTR_ERR(xen_dmabuf); @@ -1858,7 +1894,7 @@ gntdev_ioctl_dmabuf_imp_to_refs(struct gntdev_priv *priv, return 0; out_release: - dmabuf_imp_release(priv, op.fd); + gntdev_dmabuf_imp_release(priv, op.fd); return ret; } @@ -1871,7 +1907,7 @@ gntdev_ioctl_dmabuf_imp_release(struct gntdev_priv *priv, if (copy_from_user(&op, u, sizeof(op)) != 0) return -EFAULT; - return dmabuf_imp_release(priv, op.fd); + return gntdev_dmabuf_imp_release(priv, op.fd); } #endif diff --git a/include/xen/grant_dev.h b/include/xen/grant_dev.h new file mode 100644 index 000000000000..faccc9170174 --- /dev/null +++ b/include/xen/grant_dev.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ + +/* + * Grant device kernel API + * + * Copyright (C) 2018 EPAM Systems Inc. + * + * Author: Oleksandr Andrushchenko + */ + +#ifndef _GRANT_DEV_H +#define _GRANT_DEV_H + +#include + +struct device; +struct gntdev_priv; +#ifdef CONFIG_XEN_GNTDEV_DMABUF +struct xen_dmabuf; +#endif + +struct gntdev_priv *gntdev_alloc_context(struct device *dev); +void gntdev_free_context(struct gntdev_priv *priv); + +#ifdef CONFIG_XEN_GNTDEV_DMABUF +int gntdev_dmabuf_exp_from_refs(struct gntdev_priv *priv, int flags, + int count, u32 domid, u32 *refs, u32 *fd); +int gntdev_dmabuf_exp_wait_released(struct gntdev_priv *priv, int fd, + int wait_to_ms); + +struct xen_dmabuf *gntdev_dmabuf_imp_to_refs(struct gntdev_priv *priv, + int fd, int count, int domid); +u32 *gntdev_dmabuf_imp_get_refs(struct xen_dmabuf *xen_dmabuf); +int gntdev_dmabuf_imp_release(struct gntdev_priv *priv, u32 fd); +#endif + +#endif -- 2.17.0