Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2891901pxa; Tue, 18 Aug 2020 00:15:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxg6v1lvvK0xUyMEuvQEpNDdYRTX9efNrXMJewHF1nSsUG7ZXZLlVPbV+6vHhYc+onFFG9m X-Received: by 2002:a17:906:2a04:: with SMTP id j4mr19646490eje.440.1597734904815; Tue, 18 Aug 2020 00:15:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597734904; cv=none; d=google.com; s=arc-20160816; b=Oy2e337l3SawG48TeO1dm/CRyxlFWX+UZfStXCVt5CIC58bJmwiJFs040tgGv5g2sw MGiWGY6UkQ4arLuolzll4afmrgOHRu1XPCe5+rsHOpkIxAVIHpIMc2PCoWVBLvX8N+bD 3hxq70REpzTjGggCN+rhInxzhRmPK7jrsDF2h9Gq3aR4D1be0M44Q38Y26m2iIqxRBXD lfUMG+JdA+r4hsoIBPMtAMzsd6RV+pL+yI27+PhH/0zY2Hd+4OSn0tfzeI/lk5Fk/OqO IuuInDqZeOiSl6zrXIVsEYnVOqGlUVVjRHtYv8W9CR1vV+PchgnNtsjGKrJ7/T6qpOk6 nHMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5MYXjVP95LN5hlNIE/pQ1OZM/AE4CZm8fKDojaa4DCU=; b=msWvh/0y6I885Y+ZAAIcEa4PaIQNoYsxFt0AwgZKnood5QkBqNBvKVW6RPpftXdcov ePJTYNh7FeC6efRKdbfHinWEb1M0k9TcgBDuFbEeYqM3hz0TsJ5sNWBJgqp3flbAoAWc XDgWRUZUorPtTwYX/wU8NiBA9ZMacUj3FUzKlBdD0V3GMi8lDaW2+JKcZAD80NHHjrG+ 6a8Mx5jRBxe91XVO5vl7PU5yHHkEWd2k5npbiHj3WBUMlEHrxvQDV0h8QV+ASjT9ommw K0pWmB3C/LTRYPM1tu0QjXBEBuufTt+U5hVxjZbrRaNpcLPC9l7I48s9Ods5Khh9G8hy MUmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Wea53WvQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o24si12689845eji.60.2020.08.18.00.14.40; Tue, 18 Aug 2020 00:15:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Wea53WvQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726203AbgHRHN5 (ORCPT + 99 others); Tue, 18 Aug 2020 03:13:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726357AbgHRHN4 (ORCPT ); Tue, 18 Aug 2020 03:13:56 -0400 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9415CC061389 for ; Tue, 18 Aug 2020 00:13:56 -0700 (PDT) Received: by mail-pl1-x641.google.com with SMTP id g7so7659844plq.1 for ; Tue, 18 Aug 2020 00:13:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5MYXjVP95LN5hlNIE/pQ1OZM/AE4CZm8fKDojaa4DCU=; b=Wea53WvQ/Xmk90IJutfEYenWAtKjdGoiPL3XDCaqijJ1mGlUUbtjQgUOg+Vlz82q99 GvspdgXCMc5SO8bF5RgbuR1hTnn3M7pi6Y/DLGG4koAAt8zeu5xFKMCcOVRAfdIs2COh a+CMZQBg4CXaBaofEdTDl3CCIWoDv/Ri8nXWY= 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:mime-version:content-transfer-encoding; bh=5MYXjVP95LN5hlNIE/pQ1OZM/AE4CZm8fKDojaa4DCU=; b=WAACxLKPQLXKrCiH4BDpJEtL7Q/TLdoT7xd7PjoPAP/L7sKn/PPIDGOBT/oHPnxtYT wK8y2jgHO5/tnRRpmPv+8xzEudVU7cdM90wzxIu2iJ02S0cHabMezRaJzX3lpRwQbDvm /wHP9oxaKgH8IFGfJa1UorCHPwI+khYSfz5Pnekw38SdbA+z4oIiT0PEOl+2LbWxZr7N Qh3+a8QWhkos+DVLh97uEJ1ElKZE2lwHjSBQibE5gFgn5BXMySjoqz6c4L5xQqve7D6M 6/P3PW6hB9loNiOgY/H8e6wCGLciFaKE/832M6P795yICcguZiejCI3bNon6RoRqLqRI i5Dw== X-Gm-Message-State: AOAM531XKInMfnPyTeTyB7vphoojcinTHy3HukZ+c9v+AiC4o+sfm0ZL 0x2yvbNlqWEj1SXooj5l+4RwAw== X-Received: by 2002:a17:90a:630c:: with SMTP id e12mr15099304pjj.17.1597734836088; Tue, 18 Aug 2020 00:13:56 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:f693:9fff:fef4:a930]) by smtp.gmail.com with ESMTPSA id b22sm23209690pfb.213.2020.08.18.00.13.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 18 Aug 2020 00:13:55 -0700 (PDT) From: David Stevens To: Gerd Hoffmann Cc: David Airlie , Daniel Vetter , Sumit Semwal , "Michael S . Tsirkin" , Jason Wang , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Stevens , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, virtualization@lists.linux-foundation.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, virtio-dev@lists.oasis-open.org Subject: [PATCH v7 1/3] virtio: add dma-buf support for exported objects Date: Tue, 18 Aug 2020 16:13:41 +0900 Message-Id: <20200818071343.3461203-2-stevensd@chromium.org> X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog In-Reply-To: <20200818071343.3461203-1-stevensd@chromium.org> References: <20200818071343.3461203-1-stevensd@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This change adds a new flavor of dma-bufs that can be used by virtio drivers to share exported objects. A virtio dma-buf can be queried by virtio drivers to obtain the UUID which identifies the underlying exported object. Signed-off-by: David Stevens --- drivers/virtio/Makefile | 2 +- drivers/virtio/virtio.c | 6 +++ drivers/virtio/virtio_dma_buf.c | 85 +++++++++++++++++++++++++++++++++ include/linux/virtio.h | 1 + include/linux/virtio_dma_buf.h | 37 ++++++++++++++ 5 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 drivers/virtio/virtio_dma_buf.c create mode 100644 include/linux/virtio_dma_buf.h diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 29a1386ecc03..ecdae5b596de 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o +obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o virtio_dma_buf.o obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index a977e32a88f2..5d46f0ded92d 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -357,6 +357,12 @@ int register_virtio_device(struct virtio_device *dev) } EXPORT_SYMBOL_GPL(register_virtio_device); +bool is_virtio_device(struct device *dev) +{ + return dev->bus == &virtio_bus; +} +EXPORT_SYMBOL_GPL(is_virtio_device); + void unregister_virtio_device(struct virtio_device *dev) { int index = dev->index; /* save for after device release */ diff --git a/drivers/virtio/virtio_dma_buf.c b/drivers/virtio/virtio_dma_buf.c new file mode 100644 index 000000000000..45d6e8647dcf --- /dev/null +++ b/drivers/virtio/virtio_dma_buf.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * dma-bufs for virtio exported objects + * + * Copyright (C) 2020 Google, Inc. + */ + +#include + +/** + * virtio_dma_buf_export - Creates a new dma-buf for a virtio exported object + * @exp_info: [in] see dma_buf_export(). ops MUST refer to a dma_buf_ops + * struct embedded in a virtio_dma_buf_ops. + * + * This wraps dma_buf_export() to allow virtio drivers to create a dma-buf + * for an virtio exported object that can be queried by other virtio drivers + * for the object's UUID. + */ +struct dma_buf *virtio_dma_buf_export + (const struct dma_buf_export_info *exp_info) +{ + const struct virtio_dma_buf_ops *virtio_ops = + container_of(exp_info->ops, + const struct virtio_dma_buf_ops, ops); + + if (!exp_info->ops || + exp_info->ops->attach != &virtio_dma_buf_attach || + !virtio_ops->get_uuid) { + return ERR_PTR(-EINVAL); + } + + return dma_buf_export(exp_info); +} +EXPORT_SYMBOL(virtio_dma_buf_export); + +/** + * virtio_dma_buf_attach - mandatory attach callback for virtio dma-bufs + */ +int virtio_dma_buf_attach(struct dma_buf *dma_buf, + struct dma_buf_attachment *attach) +{ + int ret; + const struct virtio_dma_buf_ops *ops = + container_of(dma_buf->ops, + const struct virtio_dma_buf_ops, ops); + + if (ops->device_attach) { + ret = ops->device_attach(dma_buf, attach); + if (ret) + return ret; + } + return 0; +} +EXPORT_SYMBOL(virtio_dma_buf_attach); + +/** + * is_virtio_dma_buf - returns true if the given dma-buf is a virtio dma-buf + * @dma_buf: buffer to query + */ +bool is_virtio_dma_buf(struct dma_buf *dma_buf) +{ + return dma_buf->ops->attach == &virtio_dma_buf_attach; +} +EXPORT_SYMBOL(is_virtio_dma_buf); + +/** + * virtio_dma_buf_get_uuid - gets a virtio dma-buf's exported object's uuid + * @dma_buf: [in] buffer to query + * @uuid: [out] the uuid + * + * Returns: 0 on success, negative on failure. + */ +int virtio_dma_buf_get_uuid(struct dma_buf *dma_buf, + uuid_t *uuid) +{ + const struct virtio_dma_buf_ops *ops = + container_of(dma_buf->ops, + const struct virtio_dma_buf_ops, ops); + + if (!is_virtio_dma_buf(dma_buf)) + return -EINVAL; + + return ops->get_uuid(dma_buf, uuid); +} +EXPORT_SYMBOL(virtio_dma_buf_get_uuid); diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 15f906e4a748..9397e25616c4 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -128,6 +128,7 @@ static inline struct virtio_device *dev_to_virtio(struct device *_dev) void virtio_add_status(struct virtio_device *dev, unsigned int status); int register_virtio_device(struct virtio_device *dev); void unregister_virtio_device(struct virtio_device *dev); +bool is_virtio_device(struct device *dev); void virtio_break_device(struct virtio_device *dev); diff --git a/include/linux/virtio_dma_buf.h b/include/linux/virtio_dma_buf.h new file mode 100644 index 000000000000..a2fdf217ac62 --- /dev/null +++ b/include/linux/virtio_dma_buf.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * dma-bufs for virtio exported objects + * + * Copyright (C) 2020 Google, Inc. + */ + +#ifndef _LINUX_VIRTIO_DMA_BUF_H +#define _LINUX_VIRTIO_DMA_BUF_H + +#include +#include +#include + +/** + * struct virtio_dma_buf_ops - operations possible on exported object dma-buf + * @ops: the base dma_buf_ops. ops.attach MUST be virtio_dma_buf_attach. + * @device_attach: [optional] callback invoked by virtio_dma_buf_attach during + * all attach operations. + * @get_uid: [required] callback to get the uuid of the exported object. + */ +struct virtio_dma_buf_ops { + struct dma_buf_ops ops; + int (*device_attach)(struct dma_buf *dma_buf, + struct dma_buf_attachment *attach); + int (*get_uuid)(struct dma_buf *dma_buf, uuid_t *uuid); +}; + +int virtio_dma_buf_attach(struct dma_buf *dma_buf, + struct dma_buf_attachment *attach); + +struct dma_buf *virtio_dma_buf_export + (const struct dma_buf_export_info *exp_info); +bool is_virtio_dma_buf(struct dma_buf *dma_buf); +int virtio_dma_buf_get_uuid(struct dma_buf *dma_buf, uuid_t *uuid); + +#endif /* _LINUX_VIRTIO_DMA_BUF_H */ -- 2.28.0.220.ged08abb693-goog