From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207199; cv=none; d=zohomail.com; s=zohoarc; b=mpuCTdBQ0FgoU5/0jX+XNyuAGpGcwk87xDoO+Qn9uYeP4WzQyi7Eu+/Ga5lReh2GNXRbLQ9Gw0nZ5OWWx2wNxB/r9s2KVS1kyBwussHajlr3TgWHKtJrkJX1rgmcMQhxJcsvmmjTe8h1pm8+TXZ3JM73Z59eOAZBWp7pC3OA9bI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207199; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5oHGm6LLXasahcp4Q1X4QKjS+y/mvnMSYWOq5ZFnisM=; b=K2joHviFRC9hhKx69Mw9KZQf2ZHewVo4OleGSGkBTDvCvIEQ9J8kR4JVMIIA7Gx3pr8ulnJG+npA5HTIBBuBpDdZqhfZJfQfkuglR/zgXiTNQFgQuWubXcqJbMMlSMQpxAkIIpV+jI7uDi2KXChuN0BJlfyhbrz8jGF0emNNLqg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661207199408630.7716033244599; Mon, 22 Aug 2022 15:26:39 -0700 (PDT) Received: from localhost ([::1]:57858 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQFsR-0004DT-MT for importer@patchew.org; Mon, 22 Aug 2022 18:26:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50688) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqE-0001E9-Ui for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:21023) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqA-0004YV-B5 for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:17 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-36-ms67eq5GO3aiVvX9JasZHA-1; Mon, 22 Aug 2022 18:24:10 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8246680390C; Mon, 22 Aug 2022 22:24:09 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5F3BA40CFD0A; Mon, 22 Aug 2022 22:24:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207053; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5oHGm6LLXasahcp4Q1X4QKjS+y/mvnMSYWOq5ZFnisM=; b=ZXKxhK4/TMQ1fcD2ktxBM6VBxrPhI3+qDX4A3VQrgnYvy77qiJS9F1vYijB0rfBdrOC0fr BCO2vKMDcABPs51wtnTMXPAqSsSueh6wqr9GwfVyXSKiuiYYN6+isx2QQktkW8Wf+cHxAL SxwTpU/C8zKKsvMAd4Xw1ALmeKYCp0s= X-MC-Unique: ms67eq5GO3aiVvX9JasZHA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 01/11] blkio: add libblkio block driver Date: Mon, 22 Aug 2022 18:23:52 -0400 Message-Id: <20220822222402.176088-2-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207202101100001 Content-Type: text/plain; charset="utf-8" libblkio (https://gitlab.com/libblkio/libblkio/) is a library for high-performance disk I/O. It currently supports io_uring, virtio-blk-vhost-user, and virtio-blk-vhost-vdpa with additional drivers under development. One of the reasons for developing libblkio is that other applications besides QEMU can use it. This will be particularly useful for virtio-blk-vhost-user which applications may wish to use for connecting to qemu-storage-daemon. libblkio also gives us an opportunity to develop in Rust behind a C API that is easy to consume from QEMU. This commit adds io_uring, virtio-blk-vhost-user, and virtio-blk-vhost-vdpa BlockDrivers to QEMU using libblkio. It will be easy to add other libblkio drivers since they will share the majority of code. For now I/O buffers are copied through bounce buffers if the libblkio driver requires it. Later commits add an optimization for pre-registering guest RAM to avoid bounce buffers. The syntax is: --blockdev io_uring,node-name=3Ddrive0,filename=3Dtest.img,readonly=3Don|= off,cache.direct=3Don|off and: --blockdev virtio-blk-vhost-vdpa,node-name=3Ddrive0,path=3D/dev/vdpa...,r= eadonly=3Don|off Signed-off-by: Stefan Hajnoczi Acked-by: Markus Armbruster --- MAINTAINERS | 6 + meson_options.txt | 2 + qapi/block-core.json | 53 ++- meson.build | 9 + block/blkio.c | 725 ++++++++++++++++++++++++++++++++++ tests/qtest/modules-test.c | 3 + block/meson.build | 1 + scripts/meson-buildoptions.sh | 3 + 8 files changed, 800 insertions(+), 2 deletions(-) create mode 100644 block/blkio.c diff --git a/MAINTAINERS b/MAINTAINERS index 5ce4227ff6..f8ccd5954c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3397,6 +3397,12 @@ L: qemu-block@nongnu.org S: Maintained F: block/vdi.c =20 +blkio +M: Stefan Hajnoczi +L: qemu-block@nongnu.org +S: Maintained +F: block/blkio.c + iSCSI M: Ronnie Sahlberg M: Paolo Bonzini diff --git a/meson_options.txt b/meson_options.txt index e58e158396..67d841a8d2 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -117,6 +117,8 @@ option('bzip2', type : 'feature', value : 'auto', description: 'bzip2 support for DMG images') option('cap_ng', type : 'feature', value : 'auto', description: 'cap_ng support') +option('blkio', type : 'feature', value : 'auto', + description: 'libblkio block device driver') option('bpf', type : 'feature', value : 'auto', description: 'eBPF support') option('cocoa', type : 'feature', value : 'auto', diff --git a/qapi/block-core.json b/qapi/block-core.json index 2173e7734a..c8d217b50c 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -2951,11 +2951,16 @@ 'file', 'snapshot-access', 'ftp', 'ftps', 'gluster', {'name': 'host_cdrom', 'if': 'HAVE_HOST_BLOCK_DEVICE' }, {'name': 'host_device', 'if': 'HAVE_HOST_BLOCK_DEVICE' }, - 'http', 'https', 'iscsi', + 'http', 'https', + { 'name': 'io_uring', 'if': 'CONFIG_BLKIO' }, + 'iscsi', 'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallel= s', 'preallocate', 'qcow', 'qcow2', 'qed', 'quorum', 'raw', 'rbd', { 'name': 'replication', 'if': 'CONFIG_REPLICATION' }, - 'ssh', 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat' ] } + 'ssh', 'throttle', 'vdi', 'vhdx', + { 'name': 'virtio-blk-vhost-user', 'if': 'CONFIG_BLKIO' }, + { 'name': 'virtio-blk-vhost-vdpa', 'if': 'CONFIG_BLKIO' }, + 'vmdk', 'vpc', 'vvfat' ] } =20 ## # @BlockdevOptionsFile: @@ -3678,6 +3683,42 @@ '*debug': 'int', '*logfile': 'str' } } =20 +## +# @BlockdevOptionsIoUring: +# +# Driver specific block device options for the io_uring backend. +# +# @filename: path to the image file +# +# Since: 7.2 +## +{ 'struct': 'BlockdevOptionsIoUring', + 'data': { 'filename': 'str' } } + +## +# @BlockdevOptionsVirtioBlkVhostUser: +# +# Driver specific block device options for the virtio-blk-vhost-user backe= nd. +# +# @path: path to the vhost-user UNIX domain socket. +# +# Since: 7.2 +## +{ 'struct': 'BlockdevOptionsVirtioBlkVhostUser', + 'data': { 'path': 'str' } } + +## +# @BlockdevOptionsVirtioBlkVhostVdpa: +# +# Driver specific block device options for the virtio-blk-vhost-vdpa backe= nd. +# +# @path: path to the vhost-vdpa character device. +# +# Since: 7.2 +## +{ 'struct': 'BlockdevOptionsVirtioBlkVhostVdpa', + 'data': { 'path': 'str' } } + ## # @IscsiTransport: # @@ -4305,6 +4346,8 @@ 'if': 'HAVE_HOST_BLOCK_DEVICE' }, 'http': 'BlockdevOptionsCurlHttp', 'https': 'BlockdevOptionsCurlHttps', + 'io_uring': { 'type': 'BlockdevOptionsIoUring', + 'if': 'CONFIG_BLKIO' }, 'iscsi': 'BlockdevOptionsIscsi', 'luks': 'BlockdevOptionsLUKS', 'nbd': 'BlockdevOptionsNbd', @@ -4327,6 +4370,12 @@ 'throttle': 'BlockdevOptionsThrottle', 'vdi': 'BlockdevOptionsGenericFormat', 'vhdx': 'BlockdevOptionsGenericFormat', + 'virtio-blk-vhost-user': + { 'type': 'BlockdevOptionsVirtioBlkVhostUser', + 'if': 'CONFIG_BLKIO' }, + 'virtio-blk-vhost-vdpa': + { 'type': 'BlockdevOptionsVirtioBlkVhostVdpa', + 'if': 'CONFIG_BLKIO' }, 'vmdk': 'BlockdevOptionsGenericCOWFormat', 'vpc': 'BlockdevOptionsGenericFormat', 'vvfat': 'BlockdevOptionsVVFAT' diff --git a/meson.build b/meson.build index 20fddbd707..c0280c076d 100644 --- a/meson.build +++ b/meson.build @@ -719,6 +719,13 @@ if not get_option('virglrenderer').auto() or have_syst= em or have_vhost_user_gpu required: get_option('virglrenderer'), kwargs: static_kwargs) endif +blkio =3D not_found +if not get_option('blkio').auto() or have_block + blkio =3D dependency('blkio', + method: 'pkg-config', + required: get_option('blkio'), + kwargs: static_kwargs) +endif curl =3D not_found if not get_option('curl').auto() or have_block curl =3D dependency('libcurl', version: '>=3D7.29.0', @@ -1769,6 +1776,7 @@ config_host_data.set('CONFIG_LIBUDEV', libudev.found(= )) config_host_data.set('CONFIG_LZO', lzo.found()) config_host_data.set('CONFIG_MPATH', mpathpersist.found()) config_host_data.set('CONFIG_MPATH_NEW_API', mpathpersist_new_api) +config_host_data.set('CONFIG_BLKIO', blkio.found()) config_host_data.set('CONFIG_CURL', curl.found()) config_host_data.set('CONFIG_CURSES', curses.found()) config_host_data.set('CONFIG_GBM', gbm.found()) @@ -3917,6 +3925,7 @@ summary_info +=3D {'PAM': pam} summary_info +=3D {'iconv support': iconv} summary_info +=3D {'curses support': curses} summary_info +=3D {'virgl support': virgl} +summary_info +=3D {'blkio support': blkio} summary_info +=3D {'curl support': curl} summary_info +=3D {'Multipath support': mpathpersist} summary_info +=3D {'PNG support': png} diff --git a/block/blkio.c b/block/blkio.c new file mode 100644 index 0000000000..cdcba1b63e --- /dev/null +++ b/block/blkio.c @@ -0,0 +1,725 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * libblkio BlockDriver + * + * Copyright Red Hat, Inc. + * + * Author: + * Stefan Hajnoczi + */ + +#include "qemu/osdep.h" +#include +#include "block/block_int.h" +#include "qapi/error.h" +#include "qapi/qmp/qdict.h" +#include "qemu/module.h" + +/* + * Keep the QEMU BlockDriver names identical to the libblkio driver names. + * Using macros instead of typing out the string literals avoids typos. + */ +#define DRIVER_IO_URING "io_uring" +#define DRIVER_VIRTIO_BLK_VHOST_USER "virtio-blk-vhost-user" +#define DRIVER_VIRTIO_BLK_VHOST_VDPA "virtio-blk-vhost-vdpa" + +typedef struct BlkAIOCB { + BlockAIOCB common; + struct blkio_mem_region mem_region; + QEMUIOVector qiov; + struct iovec bounce_iov; +} BlkioAIOCB; + +typedef struct { + /* Protects ->blkio and request submission on ->blkioq */ + QemuMutex lock; + + struct blkio *blkio; + struct blkioq *blkioq; /* this could be multi-queue in the future */ + int completion_fd; + + /* Polling fetches the next completion into this field */ + struct blkio_completion poll_completion; + + /* The value of the "mem-region-alignment" property */ + size_t mem_region_alignment; + + /* Can we skip adding/deleting blkio_mem_regions? */ + bool needs_mem_regions; +} BDRVBlkioState; + +static void blkio_aiocb_complete(BlkioAIOCB *acb, int ret) +{ + /* Copy bounce buffer back to qiov */ + if (acb->qiov.niov > 0) { + qemu_iovec_from_buf(&acb->qiov, 0, + acb->bounce_iov.iov_base, + acb->bounce_iov.iov_len); + qemu_iovec_destroy(&acb->qiov); + } + + acb->common.cb(acb->common.opaque, ret); + + if (acb->mem_region.len > 0) { + BDRVBlkioState *s =3D acb->common.bs->opaque; + + WITH_QEMU_LOCK_GUARD(&s->lock) { + blkio_unmap_mem_region(s->blkio, &acb->mem_region); + blkio_free_mem_region(s->blkio, &acb->mem_region); + } + } + + qemu_aio_unref(&acb->common); +} + +/* + * Only the thread that calls aio_poll() invokes fd and poll handlers. + * Therefore locks are not necessary except when accessing s->blkio. + * + * No locking is performed around blkioq_get_completions() although other + * threads may submit I/O requests on s->blkioq. We're assuming there is no + * inteference between blkioq_get_completions() and other s->blkioq APIs. + */ + +static void blkio_completion_fd_read(void *opaque) +{ + BlockDriverState *bs =3D opaque; + BDRVBlkioState *s =3D bs->opaque; + struct blkio_completion completion; + uint64_t val; + int ret; + + /* Polling may have already fetched a completion */ + if (s->poll_completion.user_data !=3D NULL) { + completion =3D s->poll_completion; + + /* Clear it in case blkio_aiocb_complete() has a nested event loop= */ + s->poll_completion.user_data =3D NULL; + + blkio_aiocb_complete(completion.user_data, completion.ret); + } + + /* Reset completion fd status */ + ret =3D read(s->completion_fd, &val, sizeof(val)); + + /* Ignore errors, there's nothing we can do */ + (void)ret; + + /* + * Reading one completion at a time makes nested event loop re-entrancy + * simple. Change this loop to get multiple completions in one go if it + * becomes a performance bottleneck. + */ + while (blkioq_do_io(s->blkioq, &completion, 0, 1, NULL) =3D=3D 1) { + blkio_aiocb_complete(completion.user_data, completion.ret); + } +} + +static bool blkio_completion_fd_poll(void *opaque) +{ + BlockDriverState *bs =3D opaque; + BDRVBlkioState *s =3D bs->opaque; + + /* Just in case we already fetched a completion */ + if (s->poll_completion.user_data !=3D NULL) { + return true; + } + + return blkioq_do_io(s->blkioq, &s->poll_completion, 0, 1, NULL) =3D=3D= 1; +} + +static void blkio_completion_fd_poll_ready(void *opaque) +{ + blkio_completion_fd_read(opaque); +} + +static void blkio_attach_aio_context(BlockDriverState *bs, + AioContext *new_context) +{ + BDRVBlkioState *s =3D bs->opaque; + + aio_set_fd_handler(new_context, + s->completion_fd, + false, + blkio_completion_fd_read, + NULL, + blkio_completion_fd_poll, + blkio_completion_fd_poll_ready, + bs); +} + +static void blkio_detach_aio_context(BlockDriverState *bs) +{ + BDRVBlkioState *s =3D bs->opaque; + + aio_set_fd_handler(bdrv_get_aio_context(bs), + s->completion_fd, + false, NULL, NULL, NULL, NULL, NULL); +} + +static const AIOCBInfo blkio_aiocb_info =3D { + .aiocb_size =3D sizeof(BlkioAIOCB), +}; + +/* Create a BlkioAIOCB */ +static BlkioAIOCB *blkio_aiocb_get(BlockDriverState *bs, + BlockCompletionFunc *cb, + void *opaque) +{ + BlkioAIOCB *acb =3D qemu_aio_get(&blkio_aiocb_info, bs, cb, opaque); + + /* A few fields need to be initialized, leave the rest... */ + acb->qiov.niov =3D 0; + acb->mem_region.len =3D 0; + return acb; +} + +/* s->lock must be held */ +static int blkio_aiocb_init_mem_region_locked(BlkioAIOCB *acb, size_t len) +{ + BDRVBlkioState *s =3D acb->common.bs->opaque; + size_t mem_region_len =3D QEMU_ALIGN_UP(len, s->mem_region_alignment); + int ret; + + ret =3D blkio_alloc_mem_region(s->blkio, &acb->mem_region, mem_region_= len); + if (ret < 0) { + return ret; + } + + ret =3D blkio_map_mem_region(s->blkio, &acb->mem_region); + if (ret < 0) { + blkio_free_mem_region(s->blkio, &acb->mem_region); + return ret; + } + + acb->bounce_iov.iov_base =3D acb->mem_region.addr; + acb->bounce_iov.iov_len =3D len; + return 0; +} + +/* Call this to submit I/O after enqueuing a new request */ +static void blkio_submit_io(BlockDriverState *bs) +{ + if (qatomic_read(&bs->io_plugged) =3D=3D 0) { + BDRVBlkioState *s =3D bs->opaque; + + blkioq_do_io(s->blkioq, NULL, 0, 0, NULL); + } +} + +static BlockAIOCB *blkio_aio_pdiscard(BlockDriverState *bs, int64_t offset, + int bytes, BlockCompletionFunc *cb, void *opaque) +{ + BDRVBlkioState *s =3D bs->opaque; + BlkioAIOCB *acb; + + QEMU_LOCK_GUARD(&s->lock); + + acb =3D blkio_aiocb_get(bs, cb, opaque); + blkioq_discard(s->blkioq, offset, bytes, acb, 0); + blkio_submit_io(bs); + return &acb->common; +} + +static BlockAIOCB *blkio_aio_preadv(BlockDriverState *bs, int64_t offset, + int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags, + BlockCompletionFunc *cb, void *opaque) +{ + BDRVBlkioState *s =3D bs->opaque; + struct iovec *iov =3D qiov->iov; + int iovcnt =3D qiov->niov; + BlkioAIOCB *acb; + + QEMU_LOCK_GUARD(&s->lock); + + acb =3D blkio_aiocb_get(bs, cb, opaque); + + if (s->needs_mem_regions) { + if (blkio_aiocb_init_mem_region_locked(acb, bytes) < 0) { + qemu_aio_unref(&acb->common); + return NULL; + } + + /* Copy qiov because we'll call qemu_iovec_from_buf() on completio= n */ + qemu_iovec_init_slice(&acb->qiov, qiov, 0, qiov->size); + + iov =3D &acb->bounce_iov; + iovcnt =3D 1; + } + + blkioq_readv(s->blkioq, offset, iov, iovcnt, acb, 0); + blkio_submit_io(bs); + return &acb->common; +} + +static BlockAIOCB *blkio_aio_pwritev(BlockDriverState *bs, int64_t offset, + int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags, + BlockCompletionFunc *cb, void *opaque) +{ + uint32_t blkio_flags =3D (flags & BDRV_REQ_FUA) ? BLKIO_REQ_FUA : 0; + BDRVBlkioState *s =3D bs->opaque; + struct iovec *iov =3D qiov->iov; + int iovcnt =3D qiov->niov; + BlkioAIOCB *acb; + + QEMU_LOCK_GUARD(&s->lock); + + acb =3D blkio_aiocb_get(bs, cb, opaque); + + if (s->needs_mem_regions) { + if (blkio_aiocb_init_mem_region_locked(acb, bytes) < 0) { + qemu_aio_unref(&acb->common); + return NULL; + } + + qemu_iovec_to_buf(qiov, 0, acb->bounce_iov.iov_base, bytes); + + iov =3D &acb->bounce_iov; + iovcnt =3D 1; + } + + blkioq_writev(s->blkioq, offset, iov, iovcnt, acb, blkio_flags); + blkio_submit_io(bs); + return &acb->common; +} + +static BlockAIOCB *blkio_aio_flush(BlockDriverState *bs, + BlockCompletionFunc *cb, + void *opaque) +{ + BDRVBlkioState *s =3D bs->opaque; + BlkioAIOCB *acb; + + QEMU_LOCK_GUARD(&s->lock); + + acb =3D blkio_aiocb_get(bs, cb, opaque); + + blkioq_flush(s->blkioq, acb, 0); + blkio_submit_io(bs); + return &acb->common; +} + +/* For async to .bdrv_co_*() conversion */ +typedef struct { + Coroutine *coroutine; + int ret; +} BlkioCoData; + +static void blkio_co_pwrite_zeroes_complete(void *opaque, int ret) +{ + BlkioCoData *data =3D opaque; + + data->ret =3D ret; + aio_co_wake(data->coroutine); +} + +static int coroutine_fn blkio_co_pwrite_zeroes(BlockDriverState *bs, + int64_t offset, int64_t bytes, BdrvRequestFlags flags) +{ + BDRVBlkioState *s =3D bs->opaque; + BlkioCoData data =3D { + .coroutine =3D qemu_coroutine_self(), + }; + uint32_t blkio_flags =3D 0; + + if (flags & BDRV_REQ_FUA) { + blkio_flags |=3D BLKIO_REQ_FUA; + } + if (!(flags & BDRV_REQ_MAY_UNMAP)) { + blkio_flags |=3D BLKIO_REQ_NO_UNMAP; + } + if (flags & BDRV_REQ_NO_FALLBACK) { + blkio_flags |=3D BLKIO_REQ_NO_FALLBACK; + } + + WITH_QEMU_LOCK_GUARD(&s->lock) { + BlkioAIOCB *acb =3D + blkio_aiocb_get(bs, blkio_co_pwrite_zeroes_complete, &data); + blkioq_write_zeroes(s->blkioq, offset, bytes, acb, blkio_flags); + blkio_submit_io(bs); + } + + qemu_coroutine_yield(); + return data.ret; +} + +static void blkio_io_unplug(BlockDriverState *bs) +{ + BDRVBlkioState *s =3D bs->opaque; + + WITH_QEMU_LOCK_GUARD(&s->lock) { + blkio_submit_io(bs); + } +} + +static int blkio_io_uring_open(BlockDriverState *bs, QDict *options, int f= lags, + Error **errp) +{ + const char *filename =3D qdict_get_str(options, "filename"); + BDRVBlkioState *s =3D bs->opaque; + int ret; + + ret =3D blkio_set_str(s->blkio, "path", filename); + qdict_del(options, "filename"); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to set path: %s", + blkio_get_error_msg()); + return ret; + } + + if (flags & BDRV_O_NOCACHE) { + ret =3D blkio_set_bool(s->blkio, "direct", true); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to set direct: %s", + blkio_get_error_msg()); + return ret; + } + } + + return 0; +} + +static int blkio_virtio_blk_vhost_user_open(BlockDriverState *bs, + QDict *options, int flags, Error **errp) +{ + const char *path =3D qdict_get_try_str(options, "path"); + BDRVBlkioState *s =3D bs->opaque; + int ret; + + if (!path) { + error_setg(errp, "missing 'path' option"); + return -EINVAL; + } + + ret =3D blkio_set_str(s->blkio, "path", path); + qdict_del(options, "path"); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to set path: %s", + blkio_get_error_msg()); + return ret; + } + + if (!(flags & BDRV_O_NOCACHE)) { + error_setg(errp, "cache.direct=3Doff is not supported"); + return -EINVAL; + } + return 0; +} + +static int blkio_virtio_blk_vhost_vdpa_open(BlockDriverState *bs, + QDict *options, int flags, Error **errp) +{ + const char *path =3D qdict_get_try_str(options, "path"); + BDRVBlkioState *s =3D bs->opaque; + int ret; + + if (!path) { + error_setg(errp, "missing 'path' option"); + return -EINVAL; + } + + ret =3D blkio_set_str(s->blkio, "path", path); + qdict_del(options, "path"); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to set path: %s", + blkio_get_error_msg()); + return ret; + } + + if (!(flags & BDRV_O_NOCACHE)) { + error_setg(errp, "cache.direct=3Doff is not supported"); + return -EINVAL; + } + return 0; +} + +static int blkio_file_open(BlockDriverState *bs, QDict *options, int flags, + Error **errp) +{ + const char *blkio_driver =3D bs->drv->protocol_name; + BDRVBlkioState *s =3D bs->opaque; + int ret; + + ret =3D blkio_create(blkio_driver, &s->blkio); + if (ret < 0) { + error_setg_errno(errp, -ret, "blkio_create failed: %s", + blkio_get_error_msg()); + return ret; + } + + if (strcmp(blkio_driver, DRIVER_IO_URING) =3D=3D 0) { + ret =3D blkio_io_uring_open(bs, options, flags, errp); + } else if (strcmp(blkio_driver, DRIVER_VIRTIO_BLK_VHOST_USER) =3D=3D 0= ) { + ret =3D blkio_virtio_blk_vhost_user_open(bs, options, flags, errp); + } else if (strcmp(blkio_driver, DRIVER_VIRTIO_BLK_VHOST_VDPA) =3D=3D 0= ) { + ret =3D blkio_virtio_blk_vhost_vdpa_open(bs, options, flags, errp); + } else { + g_assert_not_reached(); + } + if (ret < 0) { + blkio_destroy(&s->blkio); + return ret; + } + + if (!(flags & BDRV_O_RDWR)) { + ret =3D blkio_set_bool(s->blkio, "read-only", true); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to set read-only: %s", + blkio_get_error_msg()); + blkio_destroy(&s->blkio); + return ret; + } + } + + ret =3D blkio_connect(s->blkio); + if (ret < 0) { + error_setg_errno(errp, -ret, "blkio_connect failed: %s", + blkio_get_error_msg()); + blkio_destroy(&s->blkio); + return ret; + } + + ret =3D blkio_get_bool(s->blkio, + "needs-mem-regions", + &s->needs_mem_regions); + if (ret < 0) { + error_setg_errno(errp, -ret, + "failed to get needs-mem-regions: %s", + blkio_get_error_msg()); + blkio_destroy(&s->blkio); + return ret; + } + + ret =3D blkio_get_uint64(s->blkio, + "mem-region-alignment", + &s->mem_region_alignment); + if (ret < 0) { + error_setg_errno(errp, -ret, + "failed to get mem-region-alignment: %s", + blkio_get_error_msg()); + blkio_destroy(&s->blkio); + return ret; + } + + ret =3D blkio_start(s->blkio); + if (ret < 0) { + error_setg_errno(errp, -ret, "blkio_start failed: %s", + blkio_get_error_msg()); + blkio_destroy(&s->blkio); + return ret; + } + + bs->supported_write_flags =3D BDRV_REQ_FUA; + bs->supported_zero_flags =3D BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | + BDRV_REQ_NO_FALLBACK; + + qemu_mutex_init(&s->lock); + s->blkioq =3D blkio_get_queue(s->blkio, 0); + s->completion_fd =3D blkioq_get_completion_fd(s->blkioq); + + blkio_attach_aio_context(bs, bdrv_get_aio_context(bs)); + return 0; +} + +static void blkio_close(BlockDriverState *bs) +{ + BDRVBlkioState *s =3D bs->opaque; + + qemu_mutex_destroy(&s->lock); + blkio_detach_aio_context(bs); + blkio_destroy(&s->blkio); +} + +static int64_t blkio_getlength(BlockDriverState *bs) +{ + BDRVBlkioState *s =3D bs->opaque; + uint64_t capacity; + int ret; + + WITH_QEMU_LOCK_GUARD(&s->lock) { + ret =3D blkio_get_uint64(s->blkio, "capacity", &capacity); + } + if (ret < 0) { + return -ret; + } + + return capacity; +} + +static int blkio_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) +{ + return 0; +} + +static void blkio_refresh_limits(BlockDriverState *bs, Error **errp) +{ + BDRVBlkioState *s =3D bs->opaque; + int value; + int ret; + + ret =3D blkio_get_int(s->blkio, "request-alignment", &value); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to get \"request-alignment\":= %s", + blkio_get_error_msg()); + return; + } + bs->bl.request_alignment =3D value; + if (bs->bl.request_alignment < 1 || + bs->bl.request_alignment >=3D INT_MAX || + !is_power_of_2(bs->bl.request_alignment)) { + error_setg(errp, "invalid \"request-alignment\" value %" PRIu32 ",= " + "must be a power of 2 less than INT_MAX", + bs->bl.request_alignment); + return; + } + + ret =3D blkio_get_int(s->blkio, "optimal-io-size", &value); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to get \"optimal-io-size\": %= s", + blkio_get_error_msg()); + return; + } + bs->bl.opt_transfer =3D value; + if (bs->bl.opt_transfer > INT_MAX || + (bs->bl.opt_transfer % bs->bl.request_alignment)) { + error_setg(errp, "invalid \"optimal-io-size\" value %" PRIu32 ", m= ust " + "be a multiple of %" PRIu32, bs->bl.opt_transfer, + bs->bl.request_alignment); + return; + } + + ret =3D blkio_get_int(s->blkio, "max-transfer", &value); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to get \"max-transfer\": %s", + blkio_get_error_msg()); + return; + } + bs->bl.max_transfer =3D value; + if ((bs->bl.max_transfer % bs->bl.request_alignment) || + (bs->bl.opt_transfer && (bs->bl.max_transfer % bs->bl.opt_transfer= ))) { + error_setg(errp, "invalid \"max-transfer\" value %" PRIu32 ", must= be " + "a multiple of %" PRIu32 " and %" PRIu32 " (if non-zero= )", + bs->bl.max_transfer, bs->bl.request_alignment, + bs->bl.opt_transfer); + return; + } + + ret =3D blkio_get_int(s->blkio, "buf-alignment", &value); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to get \"buf-alignment\": %s", + blkio_get_error_msg()); + return; + } + if (value < 1) { + error_setg(errp, "invalid \"buf-alignment\" value %d, must be " + "positive", value); + return; + } + bs->bl.min_mem_alignment =3D value; + + ret =3D blkio_get_int(s->blkio, "optimal-buf-alignment", &value); + if (ret < 0) { + error_setg_errno(errp, -ret, + "failed to get \"optimal-buf-alignment\": %s", + blkio_get_error_msg()); + return; + } + if (value < 1) { + error_setg(errp, "invalid \"optimal-buf-alignment\" value %d, " + "must be positive", value); + return; + } + bs->bl.opt_mem_alignment =3D value; + + ret =3D blkio_get_int(s->blkio, "max-segments", &bs->bl.max_iov); + if (ret < 0) { + error_setg_errno(errp, -ret, "failed to get \"max-segments\": %s", + blkio_get_error_msg()); + return; + } + if (value < 1) { + error_setg(errp, "invalid \"max-segments\" value %d, must be posit= ive", + bs->bl.max_iov); + return; + } +} + +/* + * TODO + * Missing libblkio APIs: + * - block_status + * - co_invalidate_cache + * + * Out of scope? + * - create + * - truncate + */ + +static BlockDriver bdrv_io_uring =3D { + .format_name =3D DRIVER_IO_URING, + .protocol_name =3D DRIVER_IO_URING, + .instance_size =3D sizeof(BDRVBlkioState), + .bdrv_needs_filename =3D true, + .bdrv_file_open =3D blkio_file_open, + .bdrv_close =3D blkio_close, + .bdrv_getlength =3D blkio_getlength, + .bdrv_get_info =3D blkio_get_info, + .bdrv_attach_aio_context =3D blkio_attach_aio_context, + .bdrv_detach_aio_context =3D blkio_detach_aio_context, + .bdrv_aio_pdiscard =3D blkio_aio_pdiscard, + .bdrv_aio_preadv =3D blkio_aio_preadv, + .bdrv_aio_pwritev =3D blkio_aio_pwritev, + .bdrv_aio_flush =3D blkio_aio_flush, + .bdrv_co_pwrite_zeroes =3D blkio_co_pwrite_zeroes, + .bdrv_io_unplug =3D blkio_io_unplug, + .bdrv_refresh_limits =3D blkio_refresh_limits, +}; + +static BlockDriver bdrv_virtio_blk_vhost_user =3D { + .format_name =3D DRIVER_VIRTIO_BLK_VHOST_USER, + .protocol_name =3D DRIVER_VIRTIO_BLK_VHOST_USER, + .instance_size =3D sizeof(BDRVBlkioState), + .bdrv_file_open =3D blkio_file_open, + .bdrv_close =3D blkio_close, + .bdrv_getlength =3D blkio_getlength, + .bdrv_get_info =3D blkio_get_info, + .bdrv_attach_aio_context =3D blkio_attach_aio_context, + .bdrv_detach_aio_context =3D blkio_detach_aio_context, + .bdrv_aio_pdiscard =3D blkio_aio_pdiscard, + .bdrv_aio_preadv =3D blkio_aio_preadv, + .bdrv_aio_pwritev =3D blkio_aio_pwritev, + .bdrv_aio_flush =3D blkio_aio_flush, + .bdrv_co_pwrite_zeroes =3D blkio_co_pwrite_zeroes, + .bdrv_io_unplug =3D blkio_io_unplug, + .bdrv_refresh_limits =3D blkio_refresh_limits, + .bdrv_register_buf =3D blkio_register_buf, + .bdrv_unregister_buf =3D blkio_unregister_buf, +}; + +static BlockDriver bdrv_virtio_blk_vhost_vdpa =3D { + .format_name =3D DRIVER_VIRTIO_BLK_VHOST_VDPA, + .protocol_name =3D DRIVER_VIRTIO_BLK_VHOST_VDPA, + .instance_size =3D sizeof(BDRVBlkioState), + .bdrv_file_open =3D blkio_file_open, + .bdrv_close =3D blkio_close, + .bdrv_getlength =3D blkio_getlength, + .bdrv_get_info =3D blkio_get_info, + .bdrv_attach_aio_context =3D blkio_attach_aio_context, + .bdrv_detach_aio_context =3D blkio_detach_aio_context, + .bdrv_aio_pdiscard =3D blkio_aio_pdiscard, + .bdrv_aio_preadv =3D blkio_aio_preadv, + .bdrv_aio_pwritev =3D blkio_aio_pwritev, + .bdrv_aio_flush =3D blkio_aio_flush, + .bdrv_co_pwrite_zeroes =3D blkio_co_pwrite_zeroes, + .bdrv_io_unplug =3D blkio_io_unplug, + .bdrv_refresh_limits =3D blkio_refresh_limits, +}; + +static void bdrv_blkio_init(void) +{ + bdrv_register(&bdrv_io_uring); + bdrv_register(&bdrv_virtio_blk_vhost_user); + bdrv_register(&bdrv_virtio_blk_vhost_vdpa); +} + +block_init(bdrv_blkio_init); diff --git a/tests/qtest/modules-test.c b/tests/qtest/modules-test.c index 88217686e1..be2575ae6d 100644 --- a/tests/qtest/modules-test.c +++ b/tests/qtest/modules-test.c @@ -16,6 +16,9 @@ static void test_modules_load(const void *data) int main(int argc, char *argv[]) { const char *modules[] =3D { +#ifdef CONFIG_BLKIO + "block-", "blkio", +#endif #ifdef CONFIG_CURL "block-", "curl", #endif diff --git a/block/meson.build b/block/meson.build index 60bc305597..500878f082 100644 --- a/block/meson.build +++ b/block/meson.build @@ -92,6 +92,7 @@ block_modules =3D {} =20 modsrc =3D [] foreach m : [ + [blkio, 'blkio', files('blkio.c')], [curl, 'curl', files('curl.c')], [glusterfs, 'gluster', files('gluster.c')], [libiscsi, 'iscsi', [files('iscsi.c'), libm]], diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index 359b04e0e6..965a4e0c16 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -69,6 +69,7 @@ meson_options_help() { printf "%s\n" ' auth-pam PAM access control' printf "%s\n" ' avx2 AVX2 optimizations' printf "%s\n" ' avx512f AVX512F optimizations' + printf "%s\n" ' blkio libblkio block device driver' printf "%s\n" ' bochs bochs image format support' printf "%s\n" ' bpf eBPF support' printf "%s\n" ' brlapi brlapi character device driver' @@ -198,6 +199,8 @@ _meson_option_parse() { --disable-gcov) printf "%s" -Db_coverage=3Dfalse ;; --enable-lto) printf "%s" -Db_lto=3Dtrue ;; --disable-lto) printf "%s" -Db_lto=3Dfalse ;; + --enable-blkio) printf "%s" -Dblkio=3Denabled ;; + --disable-blkio) printf "%s" -Dblkio=3Ddisabled ;; --block-drv-ro-whitelist=3D*) quote_sh "-Dblock_drv_ro_whitelist=3D$2"= ;; --block-drv-rw-whitelist=3D*) quote_sh "-Dblock_drv_rw_whitelist=3D$2"= ;; --enable-block-drv-whitelist-in-tools) printf "%s" -Dblock_drv_whiteli= st_in_tools=3Dtrue ;; --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207377; cv=none; d=zohomail.com; s=zohoarc; b=NYClx54+6+86ZRK10FxFFSStMor+0U6GGXPc2fgIMTRib2vV7ahfcDiiW7Uc3ygbymtLJXIgwzMGbjMJTCxblsHKNXPkw/Nq/K1iNqTqGo84Bp13nsVn2XX4MUYE6hRMZlUuBMBeb2A6CEtiVYZYRBEhL6YV3L5HCNZLB6vfteY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207377; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=zfSC9MdZbDebgbL/3/yNh02cGi/xZxeiSS70x+yAUC4=; b=lkTtSwc8+D/V9nPC4Xr6spz/O7K/D1ik+Z23WiiGA86uFgEB3rzdJ/wYs93xjLLqetX8Ljk+/3yfD8MFog5fMHEWmMdVyyxvRIX1HqwVPeoXvvW53YFJvLNZdYRUCSonNdiKVJbZdeoisxlmDahaITBnvqAMLGH1keTNjiqrAyI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661207377417126.36163286043313; Mon, 22 Aug 2022 15:29:37 -0700 (PDT) Received: from localhost ([::1]:39766 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQFvL-0001pM-2i for importer@patchew.org; Mon, 22 Aug 2022 18:29:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50690) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqG-0001Fr-Tq for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:52264) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqD-0004Yw-14 for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:20 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-518-kCG2P8lFOmKskHhhDomUfg-1; Mon, 22 Aug 2022 18:24:13 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 304423C0D842; Mon, 22 Aug 2022 22:24:12 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6696D141511A; Mon, 22 Aug 2022 22:24:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207056; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zfSC9MdZbDebgbL/3/yNh02cGi/xZxeiSS70x+yAUC4=; b=Ll85fYRlJcEavixgxDpeg7K2WyFLhm6HyK1F7BsbuOlWD78TFrf1DI4XwNZwZQ6w4fxiBo 0cGW9F3BQFwuAj28DQ9vjHPANVlE0JKzYOMsd+Cs8chCpk19XXwtuG3YPPpudrCFkRMkwC q2YUDRf6m+xcu/pGGp7dBy2b3YFf1qc= X-MC-Unique: kCG2P8lFOmKskHhhDomUfg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 02/11] numa: call ->ram_block_removed() in ram_block_notifer_remove() Date: Mon, 22 Aug 2022 18:23:53 -0400 Message-Id: <20220822222402.176088-3-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207378241100001 Content-Type: text/plain; charset="utf-8" When a RAMBlockNotifier is added, ->ram_block_added() is called with all existing RAMBlocks. There is no equivalent ->ram_block_removed() call when a RAMBlockNotifier is removed. The util/vfio-helpers.c code (the sole user of RAMBlockNotifier) is fine with this asymmetry because it does not rely on RAMBlockNotifier for cleanup. It walks its internal list of DMA mappings and unmaps them by itself. Future users of RAMBlockNotifier may not have an internal data structure that records added RAMBlocks so they will need ->ram_block_removed() callbacks. This patch makes ram_block_notifier_remove() symmetric with respect to callbacks. Now util/vfio-helpers.c needs to unmap remaining DMA mappings after ram_block_notifier_remove() has been called. This is necessary since users like block/nvme.c may create additional DMA mappings that do not originate from the RAMBlockNotifier. Reviewed-by: David Hildenbrand Signed-off-by: Stefan Hajnoczi --- hw/core/numa.c | 17 +++++++++++++++++ util/vfio-helpers.c | 5 ++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/hw/core/numa.c b/hw/core/numa.c index 26d8e5f616..31e6fe1caa 100644 --- a/hw/core/numa.c +++ b/hw/core/numa.c @@ -822,6 +822,19 @@ static int ram_block_notify_add_single(RAMBlock *rb, v= oid *opaque) return 0; } =20 +static int ram_block_notify_remove_single(RAMBlock *rb, void *opaque) +{ + const ram_addr_t max_size =3D qemu_ram_get_max_length(rb); + const ram_addr_t size =3D qemu_ram_get_used_length(rb); + void *host =3D qemu_ram_get_host_addr(rb); + RAMBlockNotifier *notifier =3D opaque; + + if (host) { + notifier->ram_block_removed(notifier, host, size, max_size); + } + return 0; +} + void ram_block_notifier_add(RAMBlockNotifier *n) { QLIST_INSERT_HEAD(&ram_list.ramblock_notifiers, n, next); @@ -835,6 +848,10 @@ void ram_block_notifier_add(RAMBlockNotifier *n) void ram_block_notifier_remove(RAMBlockNotifier *n) { QLIST_REMOVE(n, next); + + if (n->ram_block_removed) { + qemu_ram_foreach_block(ram_block_notify_remove_single, n); + } } =20 void ram_block_notify_add(void *host, size_t size, size_t max_size) diff --git a/util/vfio-helpers.c b/util/vfio-helpers.c index 5ba01177bf..0d1520caac 100644 --- a/util/vfio-helpers.c +++ b/util/vfio-helpers.c @@ -847,10 +847,13 @@ void qemu_vfio_close(QEMUVFIOState *s) if (!s) { return; } + + ram_block_notifier_remove(&s->ram_notifier); + for (i =3D 0; i < s->nr_mappings; ++i) { qemu_vfio_undo_mapping(s, &s->mappings[i], NULL); } - ram_block_notifier_remove(&s->ram_notifier); + g_free(s->usable_iova_ranges); s->nb_iova_ranges =3D 0; qemu_vfio_reset(s); --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207384; cv=none; d=zohomail.com; s=zohoarc; b=XGcfNg9F1jPbzuBKvy7WLK4679y4iGridrkwbQfhXAr/XzwnfTd9jOE0DMTcMLIUcLbAtnCfiD0PW+qUfiKwFOfozV+73RqbLd8NFaP8DDOWnqgz4YkTFqfc3RMPIBC1tXU3gERF9Hiko5ZZfYJpmBMg3CVgRmthBu5phrLJATs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207384; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=o5VG/ttpirsoROMkhajAhBK+WvpGTEsN9HDC3Gzi+Eg=; b=d26tsk6DdvRBm2c/N/9rxUcHKJrOt8DUYRisJuVP9wdHCTh4AyMzlX8muwCR8+RsujyZbw2PkRaU+7XeeZBmMEOz0VarwA/AQW89LXvt925XtJA+Y4qdyti6lO/0UH2CFDRjWyHUFRpJEWeOF5uoZT4QZu7meR0p/oFq5X3CCyg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 166120738453650.45898979788535; Mon, 22 Aug 2022 15:29:44 -0700 (PDT) Received: from localhost ([::1]:53198 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQFvT-0002Dy-Fe for importer@patchew.org; Mon, 22 Aug 2022 18:29:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50698) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqK-0001MG-Dn for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:46538) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqF-0004ZL-UP for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:22 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-584-w0_Sj2oSN7OpkgaxCbJbHg-1; Mon, 22 Aug 2022 18:24:15 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 89ED529ABA3E; Mon, 22 Aug 2022 22:24:14 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 06ECC40CFD0A; Mon, 22 Aug 2022 22:24:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207058; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=o5VG/ttpirsoROMkhajAhBK+WvpGTEsN9HDC3Gzi+Eg=; b=e+KeijKnLYraJX1OH/tQVcvH2y+5VR5xu2r//Q55xuEwccqEdi8qNMCN5Bp1XVlTa3WPO+ B1HRtnV8o8rh1Zc8MYMnhsrrlpeTJhOv56gJPkZOuLsvPKzjLcJY0mgRLpQeXqJTgJZAue v20f1vncXGF/jdE7V8+zUzngwYiaywM= X-MC-Unique: w0_Sj2oSN7OpkgaxCbJbHg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 03/11] block: pass size to bdrv_unregister_buf() Date: Mon, 22 Aug 2022 18:23:54 -0400 Message-Id: <20220822222402.176088-4-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207386272100001 Content-Type: text/plain; charset="utf-8" The only implementor of bdrv_register_buf() is block/nvme.c, where the size is not needed when unregistering a buffer. This is because util/vfio-helpers.c can look up mappings by address. Future block drivers that implement bdrv_register_buf() may not be able to do their job given only the buffer address. Add a size argument to bdrv_unregister_buf(). Also document the assumptions about bdrv_register_buf()/bdrv_unregister_buf() calls. The same values that were given to bdrv_register_buf() must be given to bdrv_unregister_buf(). gcc 11.2.1 emits a spurious warning that img_bench()'s buf_size local variable might be uninitialized, so it's necessary to silence the compiler. Signed-off-by: Stefan Hajnoczi --- include/block/block-global-state.h | 5 ++++- include/block/block_int-common.h | 2 +- include/sysemu/block-backend-global-state.h | 2 +- block/block-backend.c | 4 ++-- block/io.c | 6 +++--- block/nvme.c | 2 +- qemu-img.c | 4 ++-- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index 21265e3966..7901f35863 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -243,9 +243,12 @@ void bdrv_del_child(BlockDriverState *parent, BdrvChil= d *child, Error **errp); * Register/unregister a buffer for I/O. For example, VFIO drivers are * interested to know the memory areas that would later be used for I/O, so * that they can prepare IOMMU mapping etc., to get better performance. + * + * Buffers must not overlap and they must be unregistered with the same values that they were registered with. */ void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size); -void bdrv_unregister_buf(BlockDriverState *bs, void *host); +void bdrv_unregister_buf(BlockDriverState *bs, void *host, size_t size); =20 void bdrv_cancel_in_flight(BlockDriverState *bs); =20 diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 8947abab76..b7a7cbd3a5 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -435,7 +435,7 @@ struct BlockDriver { * DMA mapping for hot buffers. */ void (*bdrv_register_buf)(BlockDriverState *bs, void *host, size_t siz= e); - void (*bdrv_unregister_buf)(BlockDriverState *bs, void *host); + void (*bdrv_unregister_buf)(BlockDriverState *bs, void *host, size_t s= ize); =20 /* * This field is modified only under the BQL, and is part of diff --git a/include/sysemu/block-backend-global-state.h b/include/sysemu/b= lock-backend-global-state.h index 415f0c91d7..97f7dad2c3 100644 --- a/include/sysemu/block-backend-global-state.h +++ b/include/sysemu/block-backend-global-state.h @@ -107,7 +107,7 @@ void blk_io_limits_update_group(BlockBackend *blk, cons= t char *group); void blk_set_force_allow_inactivate(BlockBackend *blk); =20 void blk_register_buf(BlockBackend *blk, void *host, size_t size); -void blk_unregister_buf(BlockBackend *blk, void *host); +void blk_unregister_buf(BlockBackend *blk, void *host, size_t size); =20 const BdrvChild *blk_root(BlockBackend *blk); =20 diff --git a/block/block-backend.c b/block/block-backend.c index d4a5df2ac2..99141f8f06 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -2551,10 +2551,10 @@ void blk_register_buf(BlockBackend *blk, void *host= , size_t size) bdrv_register_buf(blk_bs(blk), host, size); } =20 -void blk_unregister_buf(BlockBackend *blk, void *host) +void blk_unregister_buf(BlockBackend *blk, void *host, size_t size) { GLOBAL_STATE_CODE(); - bdrv_unregister_buf(blk_bs(blk), host); + bdrv_unregister_buf(blk_bs(blk), host, size); } =20 int coroutine_fn blk_co_copy_range(BlockBackend *blk_in, int64_t off_in, diff --git a/block/io.c b/block/io.c index 0a8cbefe86..af85fa4bcc 100644 --- a/block/io.c +++ b/block/io.c @@ -3305,16 +3305,16 @@ void bdrv_register_buf(BlockDriverState *bs, void *= host, size_t size) } } =20 -void bdrv_unregister_buf(BlockDriverState *bs, void *host) +void bdrv_unregister_buf(BlockDriverState *bs, void *host, size_t size) { BdrvChild *child; =20 GLOBAL_STATE_CODE(); if (bs->drv && bs->drv->bdrv_unregister_buf) { - bs->drv->bdrv_unregister_buf(bs, host); + bs->drv->bdrv_unregister_buf(bs, host, size); } QLIST_FOREACH(child, &bs->children, next) { - bdrv_unregister_buf(child->bs, host); + bdrv_unregister_buf(child->bs, host, size); } } =20 diff --git a/block/nvme.c b/block/nvme.c index 01fb28aa63..696502acea 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -1592,7 +1592,7 @@ static void nvme_register_buf(BlockDriverState *bs, v= oid *host, size_t size) } } =20 -static void nvme_unregister_buf(BlockDriverState *bs, void *host) +static void nvme_unregister_buf(BlockDriverState *bs, void *host, size_t s= ize) { BDRVNVMeState *s =3D bs->opaque; =20 diff --git a/qemu-img.c b/qemu-img.c index 7d4b33b3da..b8a8818ddb 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -4368,7 +4368,7 @@ static int img_bench(int argc, char **argv) struct timeval t1, t2; int i; bool force_share =3D false; - size_t buf_size; + size_t buf_size =3D 0; =20 for (;;) { static const struct option long_options[] =3D { @@ -4590,7 +4590,7 @@ static int img_bench(int argc, char **argv) =20 out: if (data.buf) { - blk_unregister_buf(blk, data.buf); + blk_unregister_buf(blk, data.buf, buf_size); } qemu_vfree(data.buf); blk_unref(blk); --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207672; cv=none; d=zohomail.com; s=zohoarc; b=MjNGWeTdJMD1EU4lJqGzYPrj5ZH6L8vjGWRoJ3qehAkYMYkjuxpOLiGG8po6QsdqfuYls2ByZTGkflsmcJZlCTz5OKPl6qyjwM3spF53uAmEmakYF61dowicNduDjw3MerNAoBe48Ik3GWz6Xsafrgamw6zS2NpUJ9/Q98jT7zA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207672; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=BEeT8KAOfR/sU2kpmvClQenVLTJvWkevNHG3atAuP3I=; b=erhSDa5IkDvs78y/3Gk0iI3WnIQhtdQESpxxTsSx5y6XUGDc7ByUdj8CUCqxF6nXNKmtwf6hgVfFyaP1+uFAQITs7pR+dx1te2dZJMI+4/683w00kn66xGQ62PygB/LIjIYnVAVNwfdzaEu4FwMdx5X0Q9zGkiL14qv5HM1dPkM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661207672141124.95446791890174; Mon, 22 Aug 2022 15:34:32 -0700 (PDT) Received: from localhost ([::1]:45680 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQG07-0007ma-3K for importer@patchew.org; Mon, 22 Aug 2022 18:34:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50694) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqK-0001JA-8m for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:33184) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqG-0004ZN-Hb for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:21 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-160-uO8rlSj3Mjesf6fadJEuSg-1; Mon, 22 Aug 2022 18:24:17 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0251F85A581; Mon, 22 Aug 2022 22:24:17 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 460971121319; Mon, 22 Aug 2022 22:24:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207058; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BEeT8KAOfR/sU2kpmvClQenVLTJvWkevNHG3atAuP3I=; b=AY5rb0tn/zOW8jyXerAk/cAAsTBXz3dYKPSd+dOFoDZ32JiQynTh8VJJaOQJAaBNfKnztE VoNhDKGQO59XDb9tOXHcijJVDPB1vNCzgV4CBMv5gkVXAuH1TPWxG2SYIRqgmAv3qshuNJ lBEcyIVaZxmqfPAS8GND1dNK8DY7vss= X-MC-Unique: uO8rlSj3Mjesf6fadJEuSg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 04/11] block: use BdrvRequestFlags type for supported flag fields Date: Mon, 22 Aug 2022 18:23:55 -0400 Message-Id: <20220822222402.176088-5-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207673786100001 Content-Type: text/plain; charset="utf-8" Use the enum type so GDB displays the enum members instead of printing a numeric constant. Signed-off-by: Stefan Hajnoczi --- include/block/block_int-common.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index b7a7cbd3a5..19798d0e77 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -1051,7 +1051,7 @@ struct BlockDriverState { /* * Flags honored during pread */ - unsigned int supported_read_flags; + BdrvRequestFlags supported_read_flags; /* * Flags honored during pwrite (so far: BDRV_REQ_FUA, * BDRV_REQ_WRITE_UNCHANGED). @@ -1069,12 +1069,12 @@ struct BlockDriverState { * flag), or they have to explicitly take the WRITE permission for * their children. */ - unsigned int supported_write_flags; + BdrvRequestFlags supported_write_flags; /* * Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA, * BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED) */ - unsigned int supported_zero_flags; + BdrvRequestFlags supported_zero_flags; /* * Flags honoured during truncate (so far: BDRV_REQ_ZERO_WRITE). * @@ -1082,7 +1082,7 @@ struct BlockDriverState { * that any added space reads as all zeros. If this can't be guarantee= d, * the operation must fail. */ - unsigned int supported_truncate_flags; + BdrvRequestFlags supported_truncate_flags; =20 /* the following member gives a name to every node on the bs graph. */ char node_name[32]; --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207692; cv=none; d=zohomail.com; s=zohoarc; b=bNWJOMBMUhaoQKe2EnFHzMJf4wZr3kKTO2+DEihCUCR82viVlOnj62xFG8iXH0uwKK0XoLFCav4CdmXWWUpDWwM79efAZmemLOlyy02oOKVHi8vV3Ca4Cb4GGy+SqUmwUIIZGqouoS7Coq+1P2aK9bRYtrEFTajQnpCUG9fxJ3o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207692; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=AHHPGhYY0TEVbdi/i+5jGp1YHoaE7af1vrTn/9j+lxc=; b=NLmgc3EbWYXCjIqC70NXKy4QUYbBJ/ul1gXUFh1qVfsutw/p/vg7djWrZ2kHufierNYn2xFTe2oHGJy70Vjo38E9+UDuQ1KSjsGfva1fpBSWwWhrBIx/ImvZbuP0+4FJRjFLfn3bYRwn87fRYjPGOgKELsmeevT8VSf1bi6gGvE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661207692912654.9407424499938; Mon, 22 Aug 2022 15:34:52 -0700 (PDT) Received: from localhost ([::1]:54024 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQG0Q-0008IV-9Y for importer@patchew.org; Mon, 22 Aug 2022 18:34:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55706) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqR-0001cP-6y for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:24974) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqN-0004aT-Ds for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:30 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-161-_HQc3JN2PyaLXkKNWTToew-1; Mon, 22 Aug 2022 18:24:21 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BAD5E8037AF; Mon, 22 Aug 2022 22:24:19 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0E9901121314; Mon, 22 Aug 2022 22:24:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207066; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AHHPGhYY0TEVbdi/i+5jGp1YHoaE7af1vrTn/9j+lxc=; b=SvxxM0P3SRsRDmemlrgy4JXfadklMr++PM9BooEV2gYKalKuZEjrsMieI2z8ZPMDqEbNfF XsTqZxi0wzY64N0NRH0++tQ9tYhmVM/pv8H8J9e4XRlu2SZnzO+Na9ENt/R4rX97Geo26H ttYsgf71MBSCA+NIgKd/95PVagITuso= X-MC-Unique: _HQc3JN2PyaLXkKNWTToew-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 05/11] block: add BDRV_REQ_REGISTERED_BUF request flag Date: Mon, 22 Aug 2022 18:23:56 -0400 Message-Id: <20220822222402.176088-6-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207693804100001 Content-Type: text/plain; charset="utf-8" Block drivers may optimize I/O requests accessing buffers previously registered with bdrv_register_buf(). Checking whether all elements of a request's QEMUIOVector are within previously registered buffers is expensive, so we need a hint from the user to avoid costly checks. Add a BDRV_REQ_REGISTERED_BUF request flag to indicate that all QEMUIOVector elements in an I/O request are known to be within previously registered buffers. Always pass the flag through to driver read/write functions. There is little harm in passing the flag to a driver that does not use it. Passing the flag to drivers avoids changes across many block drivers. Filter drivers would need to explicitly support the flag and pass through to their children when the children support it. That's a lot of code changes and it's hard to remember to do that everywhere, leading to silent reduced performance when the flag is accidentally dropped. The only problematic scenario with the approach in this patch is when a driver passes the flag through to internal I/O requests that don't use the same I/O buffer. In that case the hint may be set when it should actually be clear. This is a rare case though so the risk is low. Some drivers have assert(!flags), which no longer works when BDRV_REQ_REGISTERED_BUF is passed in. These assertions aren't very useful anyway since the functions are called almost exclusively by bdrv_driver_preadv/pwritev() so if we get flags handling right there then the assertion is not needed. Signed-off-by: Stefan Hajnoczi --- include/block/block-common.h | 9 ++++++ block.c | 14 +++++++++ block/blkverify.c | 4 +-- block/crypto.c | 4 +-- block/file-posix.c | 1 - block/gluster.c | 1 - block/io.c | 61 ++++++++++++++++++++++-------------- block/mirror.c | 2 ++ block/nbd.c | 1 - block/parallels.c | 1 - block/qcow.c | 2 -- block/qed.c | 1 - block/raw-format.c | 2 ++ block/replication.c | 1 - block/ssh.c | 1 - block/vhdx.c | 1 - 16 files changed, 69 insertions(+), 37 deletions(-) diff --git a/include/block/block-common.h b/include/block/block-common.h index fdb7306e78..061606e867 100644 --- a/include/block/block-common.h +++ b/include/block/block-common.h @@ -80,6 +80,15 @@ typedef enum { */ BDRV_REQ_MAY_UNMAP =3D 0x4, =20 + /* + * An optimization hint when all QEMUIOVector elements are within + * previously registered bdrv_register_buf() memory ranges. + * + * Code that replaces the user's QEMUIOVector elements with bounce buf= fers + * must take care to clear this flag. + */ + BDRV_REQ_REGISTERED_BUF =3D 0x8, + BDRV_REQ_FUA =3D 0x10, BDRV_REQ_WRITE_COMPRESSED =3D 0x20, =20 diff --git a/block.c b/block.c index bc85f46eed..70abbf774e 100644 --- a/block.c +++ b/block.c @@ -1640,6 +1640,20 @@ static int bdrv_open_driver(BlockDriverState *bs, Bl= ockDriver *drv, goto open_failed; } =20 + assert(!(bs->supported_read_flags & ~BDRV_REQ_MASK)); + assert(!(bs->supported_write_flags & ~BDRV_REQ_MASK)); + + /* + * Always allow the BDRV_REQ_REGISTERED_BUF optimization hint. This sa= ves + * drivers that pass read/write requests through to a child the troubl= e of + * declaring support explicitly. + * + * Drivers must not propagate this flag accidentally when they initiat= e I/O + * to a bounce buffer. That case should be rare though. + */ + bs->supported_read_flags |=3D BDRV_REQ_REGISTERED_BUF; + bs->supported_write_flags |=3D BDRV_REQ_REGISTERED_BUF; + ret =3D refresh_total_sectors(bs, bs->total_sectors); if (ret < 0) { error_setg_errno(errp, -ret, "Could not refresh total sector count= "); diff --git a/block/blkverify.c b/block/blkverify.c index e4a37af3b2..d624f4fd05 100644 --- a/block/blkverify.c +++ b/block/blkverify.c @@ -235,8 +235,8 @@ blkverify_co_preadv(BlockDriverState *bs, int64_t offse= t, int64_t bytes, qemu_iovec_init(&raw_qiov, qiov->niov); qemu_iovec_clone(&raw_qiov, qiov, buf); =20 - ret =3D blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, flag= s, - false); + ret =3D blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, + flags & ~BDRV_REQ_REGISTERED_BUF, false); =20 cmp_offset =3D qemu_iovec_compare(qiov, &raw_qiov); if (cmp_offset !=3D -1) { diff --git a/block/crypto.c b/block/crypto.c index 7a57774b76..c7365598a7 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -410,7 +410,6 @@ block_crypto_co_preadv(BlockDriverState *bs, int64_t of= fset, int64_t bytes, uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); uint64_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->b= lock); =20 - assert(!flags); assert(payload_offset < INT64_MAX); assert(QEMU_IS_ALIGNED(offset, sector_size)); assert(QEMU_IS_ALIGNED(bytes, sector_size)); @@ -473,7 +472,8 @@ block_crypto_co_pwritev(BlockDriverState *bs, int64_t o= ffset, int64_t bytes, uint64_t sector_size =3D qcrypto_block_get_sector_size(crypto->block); uint64_t payload_offset =3D qcrypto_block_get_payload_offset(crypto->b= lock); =20 - assert(!(flags & ~BDRV_REQ_FUA)); + flags &=3D ~BDRV_REQ_REGISTERED_BUF; + assert(payload_offset < INT64_MAX); assert(QEMU_IS_ALIGNED(offset, sector_size)); assert(QEMU_IS_ALIGNED(bytes, sector_size)); diff --git a/block/file-posix.c b/block/file-posix.c index 48cd096624..51fd57137d 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2120,7 +2120,6 @@ static int coroutine_fn raw_co_pwritev(BlockDriverSta= te *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags) { - assert(flags =3D=3D 0); return raw_co_prw(bs, offset, bytes, qiov, QEMU_AIO_WRITE); } =20 diff --git a/block/gluster.c b/block/gluster.c index b60213ab80..3894422e23 100644 --- a/block/gluster.c +++ b/block/gluster.c @@ -1236,7 +1236,6 @@ static coroutine_fn int qemu_gluster_co_writev(BlockD= riverState *bs, QEMUIOVector *qiov, int flags) { - assert(!flags); return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 1); } =20 diff --git a/block/io.c b/block/io.c index af85fa4bcc..de04594299 100644 --- a/block/io.c +++ b/block/io.c @@ -1139,8 +1139,7 @@ static int coroutine_fn bdrv_driver_preadv(BlockDrive= rState *bs, int ret; =20 bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort= ); - assert(!(flags & ~BDRV_REQ_MASK)); - assert(!(flags & BDRV_REQ_NO_FALLBACK)); + assert(!(flags & ~bs->supported_read_flags)); =20 if (!drv) { return -ENOMEDIUM; @@ -1204,23 +1203,29 @@ static int coroutine_fn bdrv_driver_pwritev(BlockDr= iverState *bs, BdrvRequestFlags flags) { BlockDriver *drv =3D bs->drv; + bool emulate_fua =3D false; int64_t sector_num; unsigned int nb_sectors; QEMUIOVector local_qiov; int ret; =20 bdrv_check_qiov_request(offset, bytes, qiov, qiov_offset, &error_abort= ); - assert(!(flags & ~BDRV_REQ_MASK)); - assert(!(flags & BDRV_REQ_NO_FALLBACK)); =20 if (!drv) { return -ENOMEDIUM; } =20 + if ((flags & BDRV_REQ_FUA) && + (~bs->supported_write_flags & BDRV_REQ_FUA)) { + flags &=3D ~BDRV_REQ_FUA; + emulate_fua =3D true; + } + + flags &=3D bs->supported_write_flags; + if (drv->bdrv_co_pwritev_part) { ret =3D drv->bdrv_co_pwritev_part(bs, offset, bytes, qiov, qiov_of= fset, - flags & bs->supported_write_flags); - flags &=3D ~bs->supported_write_flags; + flags); goto emulate_flags; } =20 @@ -1230,9 +1235,7 @@ static int coroutine_fn bdrv_driver_pwritev(BlockDriv= erState *bs, } =20 if (drv->bdrv_co_pwritev) { - ret =3D drv->bdrv_co_pwritev(bs, offset, bytes, qiov, - flags & bs->supported_write_flags); - flags &=3D ~bs->supported_write_flags; + ret =3D drv->bdrv_co_pwritev(bs, offset, bytes, qiov, flags); goto emulate_flags; } =20 @@ -1242,10 +1245,8 @@ static int coroutine_fn bdrv_driver_pwritev(BlockDri= verState *bs, .coroutine =3D qemu_coroutine_self(), }; =20 - acb =3D drv->bdrv_aio_pwritev(bs, offset, bytes, qiov, - flags & bs->supported_write_flags, + acb =3D drv->bdrv_aio_pwritev(bs, offset, bytes, qiov, flags, bdrv_co_io_em_complete, &co); - flags &=3D ~bs->supported_write_flags; if (acb =3D=3D NULL) { ret =3D -EIO; } else { @@ -1263,12 +1264,10 @@ static int coroutine_fn bdrv_driver_pwritev(BlockDr= iverState *bs, assert(bytes <=3D BDRV_REQUEST_MAX_BYTES); =20 assert(drv->bdrv_co_writev); - ret =3D drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov, - flags & bs->supported_write_flags); - flags &=3D ~bs->supported_write_flags; + ret =3D drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov, flags); =20 emulate_flags: - if (ret =3D=3D 0 && (flags & BDRV_REQ_FUA)) { + if (ret =3D=3D 0 && emulate_fua) { ret =3D bdrv_co_flush(bs); } =20 @@ -1496,11 +1495,14 @@ static int coroutine_fn bdrv_aligned_preadv(BdrvChi= ld *child, max_transfer =3D QEMU_ALIGN_DOWN(MIN_NON_ZERO(bs->bl.max_transfer, INT= _MAX), align); =20 - /* TODO: We would need a per-BDS .supported_read_flags and + /* + * TODO: We would need a per-BDS .supported_read_flags and * potential fallback support, if we ever implement any read flags * to pass through to drivers. For now, there aren't any - * passthrough flags. */ - assert(!(flags & ~(BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH))); + * passthrough flags except the BDRV_REQ_REGISTERED_BUF optimization h= int. + */ + assert(!(flags & ~(BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH | + BDRV_REQ_REGISTERED_BUF))); =20 /* Handle Copy on Read and associated serialisation */ if (flags & BDRV_REQ_COPY_ON_READ) { @@ -1541,7 +1543,7 @@ static int coroutine_fn bdrv_aligned_preadv(BdrvChild= *child, goto out; } =20 - assert(!(flags & ~bs->supported_read_flags)); + assert(!(flags & ~(bs->supported_read_flags | BDRV_REQ_REGISTERED_BUF)= )); =20 max_bytes =3D ROUND_UP(MAX(0, total_bytes - offset), align); if (bytes <=3D max_bytes && bytes <=3D max_transfer) { @@ -1730,7 +1732,8 @@ static void bdrv_padding_destroy(BdrvRequestPadding *= pad) static int bdrv_pad_request(BlockDriverState *bs, QEMUIOVector **qiov, size_t *qiov_offset, int64_t *offset, int64_t *bytes, - BdrvRequestPadding *pad, bool *padded) + BdrvRequestPadding *pad, bool *padded, + BdrvRequestFlags *flags) { int ret; =20 @@ -1758,6 +1761,10 @@ static int bdrv_pad_request(BlockDriverState *bs, if (padded) { *padded =3D true; } + if (flags) { + /* Can't use optimization hint with bounce buffer */ + *flags &=3D ~BDRV_REQ_REGISTERED_BUF; + } =20 return 0; } @@ -1812,7 +1819,7 @@ int coroutine_fn bdrv_co_preadv_part(BdrvChild *child, } =20 ret =3D bdrv_pad_request(bs, &qiov, &qiov_offset, &offset, &bytes, &pa= d, - NULL); + NULL, &flags); if (ret < 0) { goto fail; } @@ -1857,6 +1864,11 @@ static int coroutine_fn bdrv_co_do_pwrite_zeroes(Blo= ckDriverState *bs, return -ENOTSUP; } =20 + /* By definition there is no user buffer so this flag doesn't make sen= se */ + if (flags & BDRV_REQ_REGISTERED_BUF) { + return -EINVAL; + } + /* Invalidate the cached block-status data range if this write overlap= s */ bdrv_bsc_invalidate_range(bs, offset, bytes); =20 @@ -2142,6 +2154,9 @@ static int coroutine_fn bdrv_co_do_zero_pwritev(BdrvC= hild *child, bool padding; BdrvRequestPadding pad; =20 + /* This flag doesn't make sense for padding or zero writes */ + flags &=3D ~BDRV_REQ_REGISTERED_BUF; + padding =3D bdrv_init_padding(bs, offset, bytes, &pad); if (padding) { assert(!(flags & BDRV_REQ_NO_WAIT)); @@ -2259,7 +2274,7 @@ int coroutine_fn bdrv_co_pwritev_part(BdrvChild *chil= d, * alignment only if there is no ZERO flag. */ ret =3D bdrv_pad_request(bs, &qiov, &qiov_offset, &offset, &bytes,= &pad, - &padded); + &padded, &flags); if (ret < 0) { return ret; } diff --git a/block/mirror.c b/block/mirror.c index 3c4ab1159d..8d3fc3f19b 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -1477,6 +1477,8 @@ static int coroutine_fn bdrv_mirror_top_pwritev(Block= DriverState *bs, qemu_iovec_init(&bounce_qiov, 1); qemu_iovec_add(&bounce_qiov, bounce_buf, bytes); qiov =3D &bounce_qiov; + + flags &=3D ~BDRV_REQ_REGISTERED_BUF; } =20 ret =3D bdrv_mirror_top_do_write(bs, MIRROR_METHOD_COPY, offset, bytes= , qiov, diff --git a/block/nbd.c b/block/nbd.c index 97683cce27..ce2956e083 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -1221,7 +1221,6 @@ static int coroutine_fn nbd_client_co_preadv(BlockDri= verState *bs, int64_t offse }; =20 assert(bytes <=3D NBD_MAX_BUFFER_SIZE); - assert(!flags); =20 if (!bytes) { return 0; diff --git a/block/parallels.c b/block/parallels.c index a229c06f25..7c04c6a135 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -328,7 +328,6 @@ static coroutine_fn int parallels_co_writev(BlockDriver= State *bs, QEMUIOVector hd_qiov; int ret =3D 0; =20 - assert(!flags); qemu_iovec_init(&hd_qiov, qiov->niov); =20 while (nb_sectors > 0) { diff --git a/block/qcow.c b/block/qcow.c index 311aaa8705..e9180c7b61 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -628,7 +628,6 @@ static coroutine_fn int qcow_co_preadv(BlockDriverState= *bs, int64_t offset, uint8_t *buf; void *orig_buf; =20 - assert(!flags); if (qiov->niov > 1) { buf =3D orig_buf =3D qemu_try_blockalign(bs, qiov->size); if (buf =3D=3D NULL) { @@ -725,7 +724,6 @@ static coroutine_fn int qcow_co_pwritev(BlockDriverStat= e *bs, int64_t offset, uint8_t *buf; void *orig_buf; =20 - assert(!flags); s->cluster_cache_offset =3D -1; /* disable compressed cache */ =20 /* We must always copy the iov when encrypting, so we diff --git a/block/qed.c b/block/qed.c index 40943e679b..32429682a4 100644 --- a/block/qed.c +++ b/block/qed.c @@ -1388,7 +1388,6 @@ static int coroutine_fn bdrv_qed_co_writev(BlockDrive= rState *bs, int64_t sector_num, int nb_sect= ors, QEMUIOVector *qiov, int flags) { - assert(!flags); return qed_co_request(bs, sector_num, qiov, nb_sectors, QED_AIOCB_WRIT= E); } =20 diff --git a/block/raw-format.c b/block/raw-format.c index 69fd650eaf..9bae3dd7f2 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -258,6 +258,8 @@ static int coroutine_fn raw_co_pwritev(BlockDriverState= *bs, int64_t offset, qemu_iovec_add(&local_qiov, buf, 512); qemu_iovec_concat(&local_qiov, qiov, 512, qiov->size - 512); qiov =3D &local_qiov; + + flags &=3D ~BDRV_REQ_REGISTERED_BUF; } =20 ret =3D raw_adjust_offset(bs, &offset, bytes, true); diff --git a/block/replication.c b/block/replication.c index 55c8f894aa..942f88b205 100644 --- a/block/replication.c +++ b/block/replication.c @@ -260,7 +260,6 @@ static coroutine_fn int replication_co_writev(BlockDriv= erState *bs, int ret; int64_t n; =20 - assert(!flags); ret =3D replication_get_io_status(s); if (ret < 0) { goto out; diff --git a/block/ssh.c b/block/ssh.c index a2dc646536..a3cddc392c 100644 --- a/block/ssh.c +++ b/block/ssh.c @@ -1196,7 +1196,6 @@ static coroutine_fn int ssh_co_writev(BlockDriverStat= e *bs, BDRVSSHState *s =3D bs->opaque; int ret; =20 - assert(!flags); qemu_co_mutex_lock(&s->lock); ret =3D ssh_write(s, bs, sector_num * BDRV_SECTOR_SIZE, nb_sectors * BDRV_SECTOR_SIZE, qiov); diff --git a/block/vhdx.c b/block/vhdx.c index e10e78ebfd..e2344ee0b7 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -1342,7 +1342,6 @@ static coroutine_fn int vhdx_co_writev(BlockDriverSta= te *bs, int64_t sector_num, uint64_t bat_prior_offset =3D 0; bool bat_update =3D false; =20 - assert(!flags); qemu_iovec_init(&hd_qiov, qiov->niov); =20 qemu_co_mutex_lock(&s->lock); --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207231; cv=none; d=zohomail.com; s=zohoarc; b=HaoLmGSx204aTpLO7WyZGYeRlisWhICF6DonTqXyHmoo3RtlMrIIztp2ynF6nPymru7F1anT1qoCGItrcvMwY/LlyYRBwy4+TXhmB5pDP8KADOiH4QGvSzVyNT+LOJUJqBwJVjAtDpLX4kTXxuv3YIzM9Nem/sjhPlJQzs4nwZo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207231; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=35+5La3uFg1fCLNHdu+nIiQ9CxD1kv3EB5WgePqet3A=; b=OZGItVfrFlTNmCTByjXZAOCbFV0vYb1pk/YLzZoJuX9aHWLmXC8KoKednOMtRA0VfpiPJQYkp8TMTup/6eyXiJC0Z87b1elroT6pFYZn+IRhnqMzuN6jhIGnNHnB1zfOQAxqQYxV6npW3NRYzhesvkSDf25HULDF1g27f9gp+SI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661207231028578.3883139762385; Mon, 22 Aug 2022 15:27:11 -0700 (PDT) Received: from localhost ([::1]:52604 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQFt0-0005KD-0v for importer@patchew.org; Mon, 22 Aug 2022 18:27:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55710) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqR-0001dB-DP for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:50675) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqP-0004ai-CU for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:31 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-263-wlae_vcpMWmp2cRGmRFDXw-1; Mon, 22 Aug 2022 18:24:22 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CB45D3C0D842; Mon, 22 Aug 2022 22:24:21 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 47967141511A; Mon, 22 Aug 2022 22:24:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207068; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=35+5La3uFg1fCLNHdu+nIiQ9CxD1kv3EB5WgePqet3A=; b=MRSmm0ajzaIIvmQUVzjIv9pr0hJvbZOmVW+q+q9dTe1AFczI7+wbd5dwzoeFm9WawvDYzs a7TgGd2pJqcbp0Y1smFWc/Oqc4qnODWLkg/BkoXVait6mx04aZDkrsGO3d7kwm2WOBTDYd kxh8kmZH2ubrlsShLll3sM5oTDu3NIo= X-MC-Unique: wlae_vcpMWmp2cRGmRFDXw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 06/11] block: return errors from bdrv_register_buf() Date: Mon, 22 Aug 2022 18:23:57 -0400 Message-Id: <20220822222402.176088-7-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207231800100001 Content-Type: text/plain; charset="utf-8" Registering an I/O buffer is only a performance optimization hint but it is still necessary to return errors when it fails. Later patches will need to detect errors when registering buffers but an immediate advantage is that error_report() calls are no longer needed in block driver .bdrv_register_buf() functions. Signed-off-by: Stefan Hajnoczi --- include/block/block-global-state.h | 5 ++- include/block/block_int-common.h | 5 ++- include/sysemu/block-backend-global-state.h | 2 +- block/block-backend.c | 4 +-- block/io.c | 34 +++++++++++++++++++-- block/nvme.c | 18 +++++------ qemu-img.c | 2 +- 7 files changed, 52 insertions(+), 18 deletions(-) diff --git a/include/block/block-global-state.h b/include/block/block-globa= l-state.h index 7901f35863..eba4ed23b4 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -246,8 +246,11 @@ void bdrv_del_child(BlockDriverState *parent, BdrvChil= d *child, Error **errp); * * Buffers must not overlap and they must be unregistered with the same values that they were registered with. + * + * Returns: true on success, false on failure */ -void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size); +bool bdrv_register_buf(BlockDriverState *bs, void *host, size_t size, + Error **errp); void bdrv_unregister_buf(BlockDriverState *bs, void *host, size_t size); =20 void bdrv_cancel_in_flight(BlockDriverState *bs); diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index 19798d0e77..9c569be162 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -433,8 +433,11 @@ struct BlockDriver { * that it can do IOMMU mapping with VFIO etc., in order to get better * performance. In the case of VFIO drivers, this callback is used to = do * DMA mapping for hot buffers. + * + * Returns: true on success, false on failure */ - void (*bdrv_register_buf)(BlockDriverState *bs, void *host, size_t siz= e); + bool (*bdrv_register_buf)(BlockDriverState *bs, void *host, size_t siz= e, + Error **errp); void (*bdrv_unregister_buf)(BlockDriverState *bs, void *host, size_t s= ize); =20 /* diff --git a/include/sysemu/block-backend-global-state.h b/include/sysemu/b= lock-backend-global-state.h index 97f7dad2c3..6858e39cb6 100644 --- a/include/sysemu/block-backend-global-state.h +++ b/include/sysemu/block-backend-global-state.h @@ -106,7 +106,7 @@ void blk_io_limits_enable(BlockBackend *blk, const char= *group); void blk_io_limits_update_group(BlockBackend *blk, const char *group); void blk_set_force_allow_inactivate(BlockBackend *blk); =20 -void blk_register_buf(BlockBackend *blk, void *host, size_t size); +bool blk_register_buf(BlockBackend *blk, void *host, size_t size, Error **= errp); void blk_unregister_buf(BlockBackend *blk, void *host, size_t size); =20 const BdrvChild *blk_root(BlockBackend *blk); diff --git a/block/block-backend.c b/block/block-backend.c index 99141f8f06..34399d3b7b 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -2545,10 +2545,10 @@ static void blk_root_drained_end(BdrvChild *child, = int *drained_end_counter) } } =20 -void blk_register_buf(BlockBackend *blk, void *host, size_t size) +bool blk_register_buf(BlockBackend *blk, void *host, size_t size, Error **= errp) { GLOBAL_STATE_CODE(); - bdrv_register_buf(blk_bs(blk), host, size); + return bdrv_register_buf(blk_bs(blk), host, size, errp); } =20 void blk_unregister_buf(BlockBackend *blk, void *host, size_t size) diff --git a/block/io.c b/block/io.c index de04594299..1c3392c7f6 100644 --- a/block/io.c +++ b/block/io.c @@ -3307,17 +3307,45 @@ void bdrv_io_unplug(BlockDriverState *bs) } } =20 -void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size) +/* Helper that undoes bdrv_register_buf() when it fails partway through */ +static void bdrv_register_buf_rollback(BlockDriverState *bs, + void *host, + size_t size, + BdrvChild *final_child) +{ + BdrvChild *child; + + QLIST_FOREACH(child, &bs->children, next) { + if (child =3D=3D final_child) { + break; + } + + bdrv_unregister_buf(child->bs, host, size); + } + + if (bs->drv && bs->drv->bdrv_unregister_buf) { + bs->drv->bdrv_unregister_buf(bs, host, size); + } +} + +bool bdrv_register_buf(BlockDriverState *bs, void *host, size_t size, + Error **errp) { BdrvChild *child; =20 GLOBAL_STATE_CODE(); if (bs->drv && bs->drv->bdrv_register_buf) { - bs->drv->bdrv_register_buf(bs, host, size); + if (!bs->drv->bdrv_register_buf(bs, host, size, errp)) { + return false; + } } QLIST_FOREACH(child, &bs->children, next) { - bdrv_register_buf(child->bs, host, size); + if (!bdrv_register_buf(child->bs, host, size, errp)) { + bdrv_register_buf_rollback(bs, host, size, child); + return false; + } } + return true; } =20 void bdrv_unregister_buf(BlockDriverState *bs, void *host, size_t size) diff --git a/block/nvme.c b/block/nvme.c index 696502acea..1e76fe21d3 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -1577,19 +1577,19 @@ static void nvme_aio_unplug(BlockDriverState *bs) } } =20 -static void nvme_register_buf(BlockDriverState *bs, void *host, size_t siz= e) +static bool nvme_register_buf(BlockDriverState *bs, void *host, size_t siz= e, + Error **errp) { int ret; - Error *local_err =3D NULL; BDRVNVMeState *s =3D bs->opaque; =20 - ret =3D qemu_vfio_dma_map(s->vfio, host, size, false, NULL, &local_err= ); - if (ret) { - /* FIXME: we may run out of IOVA addresses after repeated - * bdrv_register_buf/bdrv_unregister_buf, because nvme_vfio_dma_un= map - * doesn't reclaim addresses for fixed mappings. */ - error_reportf_err(local_err, "nvme_register_buf failed: "); - } + /* + * FIXME: we may run out of IOVA addresses after repeated + * bdrv_register_buf/bdrv_unregister_buf, because nvme_vfio_dma_unmap + * doesn't reclaim addresses for fixed mappings. + */ + ret =3D qemu_vfio_dma_map(s->vfio, host, size, false, NULL, errp); + return ret =3D=3D 0; } =20 static void nvme_unregister_buf(BlockDriverState *bs, void *host, size_t s= ize) diff --git a/qemu-img.c b/qemu-img.c index b8a8818ddb..3f2927f5b4 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -4567,7 +4567,7 @@ static int img_bench(int argc, char **argv) data.buf =3D blk_blockalign(blk, buf_size); memset(data.buf, pattern, data.nrreq * data.bufsize); =20 - blk_register_buf(blk, data.buf, buf_size); + blk_register_buf(blk, data.buf, buf_size, &error_fatal); =20 data.qiov =3D g_new(QEMUIOVector, data.nrreq); for (i =3D 0; i < data.nrreq; i++) { --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207857; cv=none; d=zohomail.com; s=zohoarc; b=DcPBH6e55Ea3p9+IwGqqDadd7+MdPZYHqp6bUlCQZIC4S8cxFJhZRzENIlcMG4xt0Jdnpb4FhbCsg9ZbBiYg76t7iV60B1hTSPWsNRdC7B/eHUkBeekWLbW5RHkmbqKWaY9TZ16bDUlnGdp5OrZzVz09U5Sx89UI/E7JokECm7E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207857; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iIPckBZmpoTVp+wpt7nwKoBLlGmerjHyTkZtDj8xxYg=; b=mhE0cZRtlZKEYBx3WF+lhr4UDjtaIN89ZFQhE9PkVEND/BBNFrBGe74pFEzlgck68Y2l7UzV7+ukMyYIpjYvoHYOkqae6e+PEwvGOoD87JlVadkLHXvbnsCvgHgX38+741zJvyRSPvPLk/VV8H6Y1R2VrwVmGGsipVEALDA9KNI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661207857921971.4241643587325; Mon, 22 Aug 2022 15:37:37 -0700 (PDT) Received: from localhost ([::1]:41434 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQG35-0002UN-67 for importer@patchew.org; Mon, 22 Aug 2022 18:37:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55708) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqR-0001cS-7z for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:31641) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqP-0004ak-CJ for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:30 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-255-WfKUbgyjO9SbhBY2zkQISQ-1; Mon, 22 Aug 2022 18:24:25 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5E221811E84; Mon, 22 Aug 2022 22:24:24 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 855C1492C3B; Mon, 22 Aug 2022 22:24:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207068; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iIPckBZmpoTVp+wpt7nwKoBLlGmerjHyTkZtDj8xxYg=; b=fHee9gQYBRpgfRkevBJFZ+/5q7UIUS76xvX85wXDtxVQzZJcRFcPCB2ETn2aVkkY6tAwu2 o4FeFXyugmK+gm9Rn9rp964ZwlnEgxz1pgE2WXooKYk9dnmk0YPGssDdROJM7jCaQfwx17 hpfNOb9u9PXzs8DCrkX2EntNmR4Yejg= X-MC-Unique: WfKUbgyjO9SbhBY2zkQISQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 07/11] block: add BlockRAMRegistrar Date: Mon, 22 Aug 2022 18:23:58 -0400 Message-Id: <20220822222402.176088-8-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207858623100001 Content-Type: text/plain; charset="utf-8" Emulated devices and other BlockBackend users wishing to take advantage of blk_register_buf() all have the same repetitive job: register RAMBlocks with the BlockBackend using RAMBlockNotifier. Add a BlockRAMRegistrar API to do this. A later commit will use this from hw/block/virtio-blk.c. Signed-off-by: Stefan Hajnoczi --- MAINTAINERS | 1 + include/sysemu/block-ram-registrar.h | 37 +++++++++++++++++++ block/block-ram-registrar.c | 54 ++++++++++++++++++++++++++++ block/meson.build | 1 + 4 files changed, 93 insertions(+) create mode 100644 include/sysemu/block-ram-registrar.h create mode 100644 block/block-ram-registrar.c diff --git a/MAINTAINERS b/MAINTAINERS index f8ccd5954c..a3f74ab42a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2491,6 +2491,7 @@ F: block* F: block/ F: hw/block/ F: include/block/ +F: include/sysemu/block-*.h F: qemu-img* F: docs/tools/qemu-img.rst F: qemu-io* diff --git a/include/sysemu/block-ram-registrar.h b/include/sysemu/block-ra= m-registrar.h new file mode 100644 index 0000000000..d8b2f7942b --- /dev/null +++ b/include/sysemu/block-ram-registrar.h @@ -0,0 +1,37 @@ +/* + * BlockBackend RAM Registrar + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef BLOCK_RAM_REGISTRAR_H +#define BLOCK_RAM_REGISTRAR_H + +#include "exec/ramlist.h" + +/** + * struct BlockRAMRegistrar: + * + * Keeps RAMBlock memory registered with a BlockBackend using + * blk_register_buf() including hotplugged memory. + * + * Emulated devices or other BlockBackend users initialize a BlockRAMRegis= trar + * with blk_ram_registrar_init() before submitting I/O requests with the + * BDRV_REQ_REGISTERED_BUF flag set. + */ +typedef struct { + BlockBackend *blk; + RAMBlockNotifier notifier; + bool ok; +} BlockRAMRegistrar; + +void blk_ram_registrar_init(BlockRAMRegistrar *r, BlockBackend *blk); +void blk_ram_registrar_destroy(BlockRAMRegistrar *r); + +/* Have all RAMBlocks been registered successfully? */ +static inline bool blk_ram_registrar_ok(BlockRAMRegistrar *r) +{ + return r->ok; +} + +#endif /* BLOCK_RAM_REGISTRAR_H */ diff --git a/block/block-ram-registrar.c b/block/block-ram-registrar.c new file mode 100644 index 0000000000..32935006c1 --- /dev/null +++ b/block/block-ram-registrar.c @@ -0,0 +1,54 @@ +/* + * BlockBackend RAM Registrar + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "sysemu/block-backend.h" +#include "sysemu/block-ram-registrar.h" +#include "qapi/error.h" + +static void ram_block_added(RAMBlockNotifier *n, void *host, size_t size, + size_t max_size) +{ + BlockRAMRegistrar *r =3D container_of(n, BlockRAMRegistrar, notifier); + Error *err =3D NULL; + + if (!blk_register_buf(r->blk, host, max_size, &err)) { + error_report_err(err); + ram_block_notifier_remove(&r->notifier); + r->ok =3D false; + } +} + +static void ram_block_removed(RAMBlockNotifier *n, void *host, size_t size, + size_t max_size) +{ + BlockRAMRegistrar *r =3D container_of(n, BlockRAMRegistrar, notifier); + blk_unregister_buf(r->blk, host, max_size); +} + +void blk_ram_registrar_init(BlockRAMRegistrar *r, BlockBackend *blk) +{ + r->blk =3D blk; + r->notifier =3D (RAMBlockNotifier){ + .ram_block_added =3D ram_block_added, + .ram_block_removed =3D ram_block_removed, + + /* + * .ram_block_resized() is not necessary because we use the max_si= ze + * value that does not change across resize. + */ + }; + r->ok =3D true; + + ram_block_notifier_add(&r->notifier); +} + +void blk_ram_registrar_destroy(BlockRAMRegistrar *r) +{ + if (r->ok) { + ram_block_notifier_remove(&r->notifier); + } +} diff --git a/block/meson.build b/block/meson.build index 500878f082..b7c68b83a3 100644 --- a/block/meson.build +++ b/block/meson.build @@ -46,6 +46,7 @@ block_ss.add(files( ), zstd, zlib, gnutls) =20 softmmu_ss.add(when: 'CONFIG_TCG', if_true: files('blkreplay.c')) +softmmu_ss.add(files('block-ram-registrar.c')) =20 if get_option('qcow1').allowed() block_ss.add(files('qcow.c')) --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207298; cv=none; d=zohomail.com; s=zohoarc; b=d5napeIO72OoH9yZOsmhJIR6KK8Z9zZd0Wx+qEWb93hnCAwoDVIlmLJRyXCGBuGhV7/Jp/S4Yzz+RiemagiBrMIFTzGG/oIiUP2rgX1GuxGvV81qxaJBtvW9QlPzceC5ZSynQyxqQ0Xpu2NK4Nk92JtZGvKbb9ZZlmVfPbmxTfo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207298; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Fu/a45rJHMIUEyQrGQeb6Alviz1AoFhUo7br1qxbQeo=; b=cbW2TsI7KCAQvAY2997iWbh700gF1Uie9mEhsQjD3n0RUN891gu4Og5O25nO/t+JJpOyY9UKekmEQwl+MHMZ3x8Evp6q49fDPHBZYf9VAfJQ1nVgQ7zAZEiA42df2Tz+Pe750EWqTZ7rPf3fiT2KJUJIZDN2FKgCPjFGk3cjQuY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 166120729832218.92720885952383; Mon, 22 Aug 2022 15:28:18 -0700 (PDT) Received: from localhost ([::1]:57582 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQFu3-00072X-Qd for importer@patchew.org; Mon, 22 Aug 2022 18:28:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55714) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqU-0001i6-OD for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:52860) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqT-0004bc-3x for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:34 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-154-J10edu_iP2i70YJRS1iNRQ-1; Mon, 22 Aug 2022 18:24:27 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C39DC3C0D843; Mon, 22 Aug 2022 22:24:26 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B9D3141511A; Mon, 22 Aug 2022 22:24:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207072; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Fu/a45rJHMIUEyQrGQeb6Alviz1AoFhUo7br1qxbQeo=; b=PbLvbY0IU2jmN8fVEWsdYLXe+WStj3jQBuH66n6E446rmV99zLEPW7YnF1ihI9cIpzjwYh Zc2fJo5L9nHJ77FadrFeZl31I8kpIOgILZpBgPweCDC6LqNrJK9DgIeIXm7IoVPrBO5JrN /WPghbLb+SHzIEWj+/kZRw9G7tm1ruw= X-MC-Unique: J10edu_iP2i70YJRS1iNRQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 08/11] exec/cpu-common: add qemu_ram_get_fd() Date: Mon, 22 Aug 2022 18:23:59 -0400 Message-Id: <20220822222402.176088-9-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207299940100001 Content-Type: text/plain; charset="utf-8" Add a function to get the file descriptor for a RAMBlock. Device emulation code typically uses the MemoryRegion APIs but vhost-style code may use RAMBlock directly for sharing guest memory with another process. This new API will be used by the libblkio block driver so it can share guest memory via .bdrv_register_buf(). Signed-off-by: Stefan Hajnoczi --- include/exec/cpu-common.h | 1 + softmmu/physmem.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index 2281be4e10..ccef44b3d3 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -90,6 +90,7 @@ void qemu_ram_set_uf_zeroable(RAMBlock *rb); bool qemu_ram_is_migratable(RAMBlock *rb); void qemu_ram_set_migratable(RAMBlock *rb); void qemu_ram_unset_migratable(RAMBlock *rb); +int qemu_ram_get_fd(RAMBlock *rb); =20 size_t qemu_ram_pagesize(RAMBlock *block); size_t qemu_ram_pagesize_largest(void); diff --git a/softmmu/physmem.c b/softmmu/physmem.c index dc3c3e5f2e..f2bdad2583 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1765,6 +1765,11 @@ void qemu_ram_unset_migratable(RAMBlock *rb) rb->flags &=3D ~RAM_MIGRATABLE; } =20 +int qemu_ram_get_fd(RAMBlock *rb) +{ + return rb->fd; +} + /* Called with iothread lock held. */ void qemu_ram_set_idstr(RAMBlock *new_block, const char *name, DeviceState= *dev) { --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207455; cv=none; d=zohomail.com; s=zohoarc; b=iFFawudbB6QqGonTUSD//9hzCB8zz2Bit8wQlts97gsNGSE941Nds3eN+k3msciGXl+IZJJ4owk9AnQsphlQSCyTbScdJ0qyTBW2pqOEh+2tiDb0kyZvmyNnk8O07vbK5p8T6Y0b0ap378OBk+sHC/ofKJw5WfzPm97iTA//Rvw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207455; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=SGlMrQq7a+bcb/HnaNokUZUt1wwP6gWHLtMtPIbkMrk=; b=BHlaJ/FHwRVmTe8GI/7nNXJ0s1n9wzU4Cy1xRy33dvnxHpy2vFUcsei1v8Hf3mse0smN3e6+u3WWoWBdz+u5sk5imhZjEzWoUv8cIxE8zaqY2S+mhlkWIre81Za136zfGhLOggMdadidwFQ43ak7AjZCdgJd1c47fv+z9eVvjzY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 166120745555242.87731436767842; Mon, 22 Aug 2022 15:30:55 -0700 (PDT) Received: from localhost ([::1]:57000 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQFwc-0004KQ-Iw for importer@patchew.org; Mon, 22 Aug 2022 18:30:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34456) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqY-0001ml-Tz for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:20616) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqV-0004c0-UE for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:37 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-45-owRqVrrHM1yr5VG1hbsX2g-1; Mon, 22 Aug 2022 18:24:30 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1C90F299E776; Mon, 22 Aug 2022 22:24:29 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8D50414152E0; Mon, 22 Aug 2022 22:24:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207075; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SGlMrQq7a+bcb/HnaNokUZUt1wwP6gWHLtMtPIbkMrk=; b=Gg9Wpy+sMbjWzVFH4UubDQ60nTHb/DAspAsHHsMKtwTqqTtA8fYp8wsZlUxNCapNzC76bc 5QIb6X99sDP1fTsNbFoHQkZA537WNV++H0X4y7w8web/Rljc/zVbQcL3uS75sJyvzhXJy9 rvmDxrgMUgSSWK6clpwuOUeAatItdOQ= X-MC-Unique: owRqVrrHM1yr5VG1hbsX2g-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 09/11] stubs: add qemu_ram_block_from_host() and qemu_ram_get_fd() Date: Mon, 22 Aug 2022 18:24:00 -0400 Message-Id: <20220822222402.176088-10-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207456377100001 Content-Type: text/plain; charset="utf-8" The blkio block driver will need to look up the file descriptor for a given pointer. This is possible in softmmu builds where the RAMBlock API is available for querying guest RAM. Add stubs so tools like qemu-img that link the block layer still build successfully. In this case there is no guest RAM but that is fine. Bounce buffers and their file descriptors will be allocated with libblkio's blkio_alloc_mem_region() so we won't rely on QEMU's qemu_ram_get_fd() in that case. Signed-off-by: Stefan Hajnoczi --- stubs/physmem.c | 13 +++++++++++++ stubs/meson.build | 1 + 2 files changed, 14 insertions(+) create mode 100644 stubs/physmem.c diff --git a/stubs/physmem.c b/stubs/physmem.c new file mode 100644 index 0000000000..1fc5f2df29 --- /dev/null +++ b/stubs/physmem.c @@ -0,0 +1,13 @@ +#include "qemu/osdep.h" +#include "exec/cpu-common.h" + +RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset, + ram_addr_t *offset) +{ + return NULL; +} + +int qemu_ram_get_fd(RAMBlock *rb) +{ + return -1; +} diff --git a/stubs/meson.build b/stubs/meson.build index d8f3fd5c44..4314161f5f 100644 --- a/stubs/meson.build +++ b/stubs/meson.build @@ -29,6 +29,7 @@ stub_ss.add(files('migr-blocker.c')) stub_ss.add(files('module-opts.c')) stub_ss.add(files('monitor.c')) stub_ss.add(files('monitor-core.c')) +stub_ss.add(files('physmem.c')) stub_ss.add(files('qemu-timer-notify-cb.c')) stub_ss.add(files('qmp_memory_device.c')) stub_ss.add(files('qmp-command-available.c')) --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207904; cv=none; d=zohomail.com; s=zohoarc; b=B5ZM23VTbF4nGCfyrAkFzJ5tKhBP3KK+HOy24IYdPPe0buFPO1xFEsY8izZSiKI0dDecSV1QNZk2baGICDwDJFpHiqDxEqCNBGR8Y6uoPdyVQa09wp1LxntknWYpcodGkj664SLh9g/k3K9nt4ZujJfzSSg5oXaUX8vRF7Son/U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207904; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=N2R0iRNXrjBHd100CoIGDUAallZK+wQOgIgZJ8ae35U=; b=kcY340D91T93/luoPfncbhSQnH7l50m9tgKFhvPSk9Y0V/KB13J5InVjrtD4utO+ykdjZkw1I+cHFFMNWxVUou9diYNZmufp6/55mOwVoN8S047WgrfpN25JggYZU2rxgpY0bT7nc5q6AOTzzvWX2ja8zIXp9Mi2njKBG9YM1Dc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661207904780296.1495098626665; Mon, 22 Aug 2022 15:38:24 -0700 (PDT) Received: from localhost ([::1]:33218 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQG3r-0003GQ-HO for importer@patchew.org; Mon, 22 Aug 2022 18:38:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48180) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqf-0002BA-Qu for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:24290) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqX-0004cH-Vw for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:45 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-191-fNpGBn2ePkekiDikc6vxsg-1; Mon, 22 Aug 2022 18:24:32 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 58E9D299E776; Mon, 22 Aug 2022 22:24:31 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id C6B01492C3B; Mon, 22 Aug 2022 22:24:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207077; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N2R0iRNXrjBHd100CoIGDUAallZK+wQOgIgZJ8ae35U=; b=P6TBSSVSQmPAlFtor0i16khvWInehcRrm3LWwR7UbQPWl+/bvcZUfg6SiULeSteJgcN6z4 9a6ygMS7oKoutUT5qDSaAEhz54BTlPZXfC3qkKlYSkGSoj9j2zve4049c3mZ/R7qoRGRNK Yi8NmGkogkSPOgGlNixyK+5eBWaclfw= X-MC-Unique: fNpGBn2ePkekiDikc6vxsg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 10/11] blkio: implement BDRV_REQ_REGISTERED_BUF optimization Date: Mon, 22 Aug 2022 18:24:01 -0400 Message-Id: <20220822222402.176088-11-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207906700100001 Content-Type: text/plain; charset="utf-8" Avoid bounce buffers when QEMUIOVector elements are within previously registered bdrv_register_buf() buffers. The idea is that emulated storage controllers will register guest RAM using bdrv_register_buf() and set the BDRV_REQ_REGISTERED_BUF on I/O requests. Therefore no blkio_map_mem_region() calls are necessary in the performance-critical I/O code path. This optimization doesn't apply if the I/O buffer is internally allocated by QEMU (e.g. qcow2 metadata). There we still take the slow path because BDRV_REQ_REGISTERED_BUF is not set. Signed-off-by: Stefan Hajnoczi --- block/blkio.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 139 insertions(+), 3 deletions(-) diff --git a/block/blkio.c b/block/blkio.c index cdcba1b63e..976d37705c 100644 --- a/block/blkio.c +++ b/block/blkio.c @@ -11,7 +11,10 @@ #include "qemu/osdep.h" #include #include "block/block_int.h" +#include "exec/memory.h" +#include "exec/cpu-common.h" /* for qemu_ram_get_fd() */ #include "qapi/error.h" +#include "qemu/error-report.h" #include "qapi/qmp/qdict.h" #include "qemu/module.h" =20 @@ -46,6 +49,9 @@ typedef struct { =20 /* Can we skip adding/deleting blkio_mem_regions? */ bool needs_mem_regions; + + /* Are file descriptors necessary for blkio_mem_regions? */ + bool needs_mem_region_fd; } BDRVBlkioState; =20 static void blkio_aiocb_complete(BlkioAIOCB *acb, int ret) @@ -226,6 +232,8 @@ static BlockAIOCB *blkio_aio_preadv(BlockDriverState *b= s, int64_t offset, BlockCompletionFunc *cb, void *opaque) { BDRVBlkioState *s =3D bs->opaque; + bool needs_mem_regions =3D + s->needs_mem_regions && !(flags & BDRV_REQ_REGISTERED_BUF); struct iovec *iov =3D qiov->iov; int iovcnt =3D qiov->niov; BlkioAIOCB *acb; @@ -234,7 +242,7 @@ static BlockAIOCB *blkio_aio_preadv(BlockDriverState *b= s, int64_t offset, =20 acb =3D blkio_aiocb_get(bs, cb, opaque); =20 - if (s->needs_mem_regions) { + if (needs_mem_regions) { if (blkio_aiocb_init_mem_region_locked(acb, bytes) < 0) { qemu_aio_unref(&acb->common); return NULL; @@ -258,6 +266,8 @@ static BlockAIOCB *blkio_aio_pwritev(BlockDriverState *= bs, int64_t offset, { uint32_t blkio_flags =3D (flags & BDRV_REQ_FUA) ? BLKIO_REQ_FUA : 0; BDRVBlkioState *s =3D bs->opaque; + bool needs_mem_regions =3D + s->needs_mem_regions && !(flags & BDRV_REQ_REGISTERED_BUF); struct iovec *iov =3D qiov->iov; int iovcnt =3D qiov->niov; BlkioAIOCB *acb; @@ -266,7 +276,7 @@ static BlockAIOCB *blkio_aio_pwritev(BlockDriverState *= bs, int64_t offset, =20 acb =3D blkio_aiocb_get(bs, cb, opaque); =20 - if (s->needs_mem_regions) { + if (needs_mem_regions) { if (blkio_aiocb_init_mem_region_locked(acb, bytes) < 0) { qemu_aio_unref(&acb->common); return NULL; @@ -352,6 +362,117 @@ static void blkio_io_unplug(BlockDriverState *bs) } } =20 +typedef enum { + BMRR_OK, + BMRR_SKIP, + BMRR_FAIL, +} BlkioMemRegionResult; + +/* + * Produce a struct blkio_mem_region for a given address and size. + * + * This function produces identical results when called multiple times wit= h the + * same arguments. This property is necessary because blkio_unmap_mem_regi= on() + * must receive the same struct blkio_mem_region field values that were pa= ssed + * to blkio_map_mem_region(). + */ +static BlkioMemRegionResult +blkio_mem_region_from_host(BlockDriverState *bs, + void *host, size_t size, + struct blkio_mem_region *region, + Error **errp) +{ + BDRVBlkioState *s =3D bs->opaque; + int fd =3D -1; + ram_addr_t fd_offset =3D 0; + + if (((uintptr_t)host | size) % s->mem_region_alignment) { + error_setg(errp, "unaligned buf %p with size %zu", host, size); + return BMRR_FAIL; + } + + /* Attempt to find the fd for the underlying memory */ + if (s->needs_mem_region_fd) { + RAMBlock *ram_block; + RAMBlock *end_block; + ram_addr_t offset; + + /* + * bdrv_register_buf() is called with the BQL held so mr lives at = least + * until this function returns. + */ + ram_block =3D qemu_ram_block_from_host(host, false, &fd_offset); + if (ram_block) { + fd =3D qemu_ram_get_fd(ram_block); + } + if (fd =3D=3D -1) { + /* + * Ideally every RAMBlock would have an fd. pc-bios and other + * things don't. Luckily they are usually not I/O buffers and = we + * can just ignore them. + */ + return BMRR_SKIP; + } + + /* Make sure the fd covers the entire range */ + end_block =3D qemu_ram_block_from_host(host + size - 1, false, &of= fset); + if (ram_block !=3D end_block) { + error_setg(errp, "registered buffer at %p with size %zu extend= s " + "beyond RAMBlock", host, size); + return BMRR_FAIL; + } + } + + *region =3D (struct blkio_mem_region){ + .addr =3D host, + .len =3D size, + .fd =3D fd, + .fd_offset =3D fd_offset, + }; + return BMRR_OK; +} + +static bool blkio_register_buf(BlockDriverState *bs, void *host, size_t si= ze, + Error **errp) +{ + BDRVBlkioState *s =3D bs->opaque; + struct blkio_mem_region region; + BlkioMemRegionResult region_result; + int ret; + + region_result =3D blkio_mem_region_from_host(bs, host, size, ®ion, = errp); + if (region_result =3D=3D BMRR_SKIP) { + return true; + } else if (region_result !=3D BMRR_OK) { + return false; + } + + WITH_QEMU_LOCK_GUARD(&s->lock) { + ret =3D blkio_map_mem_region(s->blkio, ®ion); + } + + if (ret < 0) { + error_setg(errp, "Failed to add blkio mem region %p with size %zu:= %s", + host, size, blkio_get_error_msg()); + return false; + } + return true; +} + +static void blkio_unregister_buf(BlockDriverState *bs, void *host, size_t = size) +{ + BDRVBlkioState *s =3D bs->opaque; + struct blkio_mem_region region; + + if (blkio_mem_region_from_host(bs, host, size, ®ion, NULL) !=3D BMR= R_OK) { + return; + } + + WITH_QEMU_LOCK_GUARD(&s->lock) { + blkio_unmap_mem_region(s->blkio, ®ion); + } +} + static int blkio_io_uring_open(BlockDriverState *bs, QDict *options, int f= lags, Error **errp) { @@ -490,6 +611,17 @@ static int blkio_file_open(BlockDriverState *bs, QDict= *options, int flags, return ret; } =20 + ret =3D blkio_get_bool(s->blkio, + "needs-mem-region-fd", + &s->needs_mem_region_fd); + if (ret < 0) { + error_setg_errno(errp, -ret, + "failed to get needs-mem-region-fd: %s", + blkio_get_error_msg()); + blkio_destroy(&s->blkio); + return ret; + } + ret =3D blkio_get_uint64(s->blkio, "mem-region-alignment", &s->mem_region_alignment); @@ -509,7 +641,7 @@ static int blkio_file_open(BlockDriverState *bs, QDict = *options, int flags, return ret; } =20 - bs->supported_write_flags =3D BDRV_REQ_FUA; + bs->supported_write_flags =3D BDRV_REQ_FUA | BDRV_REQ_REGISTERED_BUF; bs->supported_zero_flags =3D BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK; =20 @@ -673,6 +805,8 @@ static BlockDriver bdrv_io_uring =3D { .bdrv_co_pwrite_zeroes =3D blkio_co_pwrite_zeroes, .bdrv_io_unplug =3D blkio_io_unplug, .bdrv_refresh_limits =3D blkio_refresh_limits, + .bdrv_register_buf =3D blkio_register_buf, + .bdrv_unregister_buf =3D blkio_unregister_buf, }; =20 static BlockDriver bdrv_virtio_blk_vhost_user =3D { @@ -713,6 +847,8 @@ static BlockDriver bdrv_virtio_blk_vhost_vdpa =3D { .bdrv_co_pwrite_zeroes =3D blkio_co_pwrite_zeroes, .bdrv_io_unplug =3D blkio_io_unplug, .bdrv_refresh_limits =3D blkio_refresh_limits, + .bdrv_register_buf =3D blkio_register_buf, + .bdrv_unregister_buf =3D blkio_unregister_buf, }; =20 static void bdrv_blkio_init(void) --=20 2.37.2 From nobody Mon May 13 07:56:42 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661207401; cv=none; d=zohomail.com; s=zohoarc; b=PWsizS2P/KqYDqyqSNbFAuwuV02K3DgZ3S8//Vm3guK4Qs0FzBvN1N+q77HQZGBr8uIYmgUGdbPkH8RdjwJ5b+R4UW5FjynUk7HR4ubFqKNvBEo7oi9U4XvZAGml4ZpUwSlIZsuk9TBI9mD96prv11CReun4GjlDhT0cEeU8XYQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661207401; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=qUr6b3nJkUrWElMFR9+l2HmdOnxVRTT75yw1plr5//I=; b=CScl9anEcMfL1xoKZgf5bbDqaEZDe3c/YkRWIooYHiNlRNs9sCxMpl5f52sJOzNon988nsCx+QhQLkq0Rhj/jG32g3m1VVVnfZI1ee0V1oXPCeBh5kIyDNPh87KQUjXzcxeZ0DvOf4uCffNaxVl43gTpsGO/nVS54CPjfaNAe4c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661207401321528.7892868412962; Mon, 22 Aug 2022 15:30:01 -0700 (PDT) Received: from localhost ([::1]:50406 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oQFvk-00039Z-97 for importer@patchew.org; Mon, 22 Aug 2022 18:30:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34460) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqd-00022H-A4 for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:43 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:59046) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oQFqa-0004cW-7f for qemu-devel@nongnu.org; Mon, 22 Aug 2022 18:24:42 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-421-ZS6eQJnmM4uLTarlS9Dp2w-1; Mon, 22 Aug 2022 18:24:34 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AD6093802AC5; Mon, 22 Aug 2022 22:24:33 +0000 (UTC) Received: from localhost (unknown [10.39.192.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2A0D814152E0; Mon, 22 Aug 2022 22:24:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661207078; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qUr6b3nJkUrWElMFR9+l2HmdOnxVRTT75yw1plr5//I=; b=fEZLZv+SaUjXPcq4Rhg1kYDkzmucTHOotGmumPEu8uaz+4VicZMH5NaQtNq1srpmEwFDof MyZj8teqMJjL5PgHt7lGtcJnGD9L3R1DVOTZEc9y1VIk+bnhM0FJ5e8Tkkl8zx/vA/vncD bA0s5HgH2C3FUzkIcSAtjrK9RTaSLQ8= X-MC-Unique: ZS6eQJnmM4uLTarlS9Dp2w-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Denis V. Lunev" , Peter Xu , Yanan Wang , Marcel Apfelbaum , "Richard W.M. Jones" , qemu-block@nongnu.org, John Snow , integration@gluster.org, Vladimir Sementsov-Ogievskiy , Vladimir Sementsov-Ogievskiy , David Hildenbrand , Laurent Vivier , Markus Armbruster , Raphael Norwitz , Richard Henderson , Fam Zheng , sgarzare@redhat.com, Alberto Faria , Kevin Wolf , Stefan Hajnoczi , Wen Congyang , Eric Blake , Hanna Reitz , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , "Michael S. Tsirkin" , Thomas Huth , Jeff Cody , Xie Changlong Subject: [RFC v4 11/11] virtio-blk: use BDRV_REQ_REGISTERED_BUF optimization hint Date: Mon, 22 Aug 2022 18:24:02 -0400 Message-Id: <20220822222402.176088-12-stefanha@redhat.com> In-Reply-To: <20220822222402.176088-1-stefanha@redhat.com> References: <20220822222402.176088-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661207401995100001 Content-Type: text/plain; charset="utf-8" Register guest RAM using BlockRAMRegistrar and set the BDRV_REQ_REGISTERED_BUF flag so block drivers can optimize memory accesses in I/O requests. This is for vdpa-blk, vhost-user-blk, and other I/O interfaces that rely on DMA mapping/unmapping. Signed-off-by: Stefan Hajnoczi --- include/hw/virtio/virtio-blk.h | 2 ++ hw/block/virtio-blk.c | 39 ++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/include/hw/virtio/virtio-blk.h b/include/hw/virtio/virtio-blk.h index d311c57cca..7f589b4146 100644 --- a/include/hw/virtio/virtio-blk.h +++ b/include/hw/virtio/virtio-blk.h @@ -19,6 +19,7 @@ #include "hw/block/block.h" #include "sysemu/iothread.h" #include "sysemu/block-backend.h" +#include "sysemu/block-ram-registrar.h" #include "qom/object.h" =20 #define TYPE_VIRTIO_BLK "virtio-blk-device" @@ -64,6 +65,7 @@ struct VirtIOBlock { struct VirtIOBlockDataPlane *dataplane; uint64_t host_features; size_t config_size; + BlockRAMRegistrar blk_ram_registrar; }; =20 typedef struct VirtIOBlockReq { diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index e9ba752f6b..907f012c45 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -21,6 +21,7 @@ #include "hw/block/block.h" #include "hw/qdev-properties.h" #include "sysemu/blockdev.h" +#include "sysemu/block-ram-registrar.h" #include "sysemu/sysemu.h" #include "sysemu/runstate.h" #include "hw/virtio/virtio-blk.h" @@ -384,12 +385,14 @@ static void virtio_blk_handle_scsi(VirtIOBlockReq *re= q) } } =20 -static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb, +static inline void submit_requests(VirtIOBlock *s, MultiReqBuffer *mrb, int start, int num_reqs, int niov) { + BlockBackend *blk =3D s->blk; QEMUIOVector *qiov =3D &mrb->reqs[start]->qiov; int64_t sector_num =3D mrb->reqs[start]->sector_num; bool is_write =3D mrb->is_write; + BdrvRequestFlags flags =3D 0; =20 if (num_reqs > 1) { int i; @@ -420,12 +423,18 @@ static inline void submit_requests(BlockBackend *blk,= MultiReqBuffer *mrb, num_reqs - 1); } =20 + if (blk_ram_registrar_ok(&s->blk_ram_registrar)) { + flags |=3D BDRV_REQ_REGISTERED_BUF; + } + if (is_write) { - blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0, - virtio_blk_rw_complete, mrb->reqs[start]); + blk_aio_pwritev(blk, sector_num << BDRV_SECTOR_BITS, qiov, + flags, virtio_blk_rw_complete, + mrb->reqs[start]); } else { - blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov, 0, - virtio_blk_rw_complete, mrb->reqs[start]); + blk_aio_preadv(blk, sector_num << BDRV_SECTOR_BITS, qiov, + flags, virtio_blk_rw_complete, + mrb->reqs[start]); } } =20 @@ -447,14 +456,14 @@ static int multireq_compare(const void *a, const void= *b) } } =20 -static void virtio_blk_submit_multireq(BlockBackend *blk, MultiReqBuffer *= mrb) +static void virtio_blk_submit_multireq(VirtIOBlock *s, MultiReqBuffer *mrb) { int i =3D 0, start =3D 0, num_reqs =3D 0, niov =3D 0, nb_sectors =3D 0; uint32_t max_transfer; int64_t sector_num =3D 0; =20 if (mrb->num_reqs =3D=3D 1) { - submit_requests(blk, mrb, 0, 1, -1); + submit_requests(s, mrb, 0, 1, -1); mrb->num_reqs =3D 0; return; } @@ -474,11 +483,11 @@ static void virtio_blk_submit_multireq(BlockBackend *= blk, MultiReqBuffer *mrb) * 3. merge would exceed maximum transfer length of backend de= vice */ if (sector_num + nb_sectors !=3D req->sector_num || - niov > blk_get_max_iov(blk) - req->qiov.niov || + niov > blk_get_max_iov(s->blk) - req->qiov.niov || req->qiov.size > max_transfer || nb_sectors > (max_transfer - req->qiov.size) / BDRV_SECTOR_SIZE) { - submit_requests(blk, mrb, start, num_reqs, niov); + submit_requests(s, mrb, start, num_reqs, niov); num_reqs =3D 0; } } @@ -494,7 +503,7 @@ static void virtio_blk_submit_multireq(BlockBackend *bl= k, MultiReqBuffer *mrb) num_reqs++; } =20 - submit_requests(blk, mrb, start, num_reqs, niov); + submit_requests(s, mrb, start, num_reqs, niov); mrb->num_reqs =3D 0; } =20 @@ -509,7 +518,7 @@ static void virtio_blk_handle_flush(VirtIOBlockReq *req= , MultiReqBuffer *mrb) * Make sure all outstanding writes are posted to the backing device. */ if (mrb->is_write && mrb->num_reqs > 0) { - virtio_blk_submit_multireq(s->blk, mrb); + virtio_blk_submit_multireq(s, mrb); } blk_aio_flush(s->blk, virtio_blk_flush_complete, req); } @@ -689,7 +698,7 @@ static int virtio_blk_handle_request(VirtIOBlockReq *re= q, MultiReqBuffer *mrb) if (mrb->num_reqs > 0 && (mrb->num_reqs =3D=3D VIRTIO_BLK_MAX_MERG= E_REQS || is_write !=3D mrb->is_write || !s->conf.request_merging)) { - virtio_blk_submit_multireq(s->blk, mrb); + virtio_blk_submit_multireq(s, mrb); } =20 assert(mrb->num_reqs < VIRTIO_BLK_MAX_MERGE_REQS); @@ -796,7 +805,7 @@ void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq) } while (!virtio_queue_empty(vq)); =20 if (mrb.num_reqs) { - virtio_blk_submit_multireq(s->blk, &mrb); + virtio_blk_submit_multireq(s, &mrb); } =20 blk_io_unplug(s->blk); @@ -845,7 +854,7 @@ void virtio_blk_process_queued_requests(VirtIOBlock *s,= bool is_bh) } =20 if (mrb.num_reqs) { - virtio_blk_submit_multireq(s->blk, &mrb); + virtio_blk_submit_multireq(s, &mrb); } if (is_bh) { blk_dec_in_flight(s->conf.conf.blk); @@ -1227,6 +1236,7 @@ static void virtio_blk_device_realize(DeviceState *de= v, Error **errp) } =20 s->change =3D qemu_add_vm_change_state_handler(virtio_blk_dma_restart_= cb, s); + blk_ram_registrar_init(&s->blk_ram_registrar, s->blk); blk_set_dev_ops(s->blk, &virtio_block_ops, s); =20 blk_iostatus_enable(s->blk); @@ -1252,6 +1262,7 @@ static void virtio_blk_device_unrealize(DeviceState *= dev) virtio_del_queue(vdev, i); } qemu_coroutine_dec_pool_size(conf->num_queues * conf->queue_size / 2); + blk_ram_registrar_destroy(&s->blk_ram_registrar); qemu_del_vm_change_state_handler(s->change); blockdev_mark_auto_del(s->blk); virtio_cleanup(vdev); --=20 2.37.2