From nobody Wed Nov 19 08:19:47 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1615376608; cv=none; d=zohomail.com; s=zohoarc; b=E+8/OhSqMja3Jep9JN3FEEsi6PTdL0bRVi669sQiaoEsL920n2XOOwrYSJaRW+KRzytvzZbSiBn+ocloZv451BSdU3Py35nojUx6TQeHR1oGi8PZu3e5SYGiN6fiKQuIedEsYMLYV56Ygw+BjTU4kRBl4rjgDDkQ4jMvKMOcIzQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615376608; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=esKZYTxlC8yf8oyx0YniIsAHsZo45frlmoVawIo+xGw=; b=Br/HD2efA4ACiWS/37A95Z8oyk9vHcxTlRzHbNElSNWBz3p4LF/LQlRB+XFDxe25v+ONaqckBZ60uVXuT9qmbNLfiaALjRFNCStHeQLDOLl4MvB2N0f+eYK9Vp3EYSquXi2k4YeJWbjkqw3o2p0IqD7E4uOEJKJZsR+CH3Dztcc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1615376608361250.73277746801932; Wed, 10 Mar 2021 03:43:28 -0800 (PST) Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-151-16kP4rHDNJqz5om9PRV0jw-1; Wed, 10 Mar 2021 06:43:23 -0500 Received: by mail-ej1-f69.google.com with SMTP id rl7so7073110ejb.16 for ; Wed, 10 Mar 2021 03:43:23 -0800 (PST) Return-Path: Return-Path: Received: from x1w.redhat.com (68.red-83-57-175.dynamicip.rima-tde.net. [83.57.175.68]) by smtp.gmail.com with ESMTPSA id b6sm9480400ejb.8.2021.03.10.03.43.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Mar 2021 03:43:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615376606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=esKZYTxlC8yf8oyx0YniIsAHsZo45frlmoVawIo+xGw=; b=RtrCJWpNxmz6ivG0f77Qlge+7AByJGY5COa2w+l63S3U/sIyb6E4cYN+H9jjtkASZWNuyD VnEPCoR8MYcfLfl2h1XDReCmyca3zZDdjxbvG7Ti1obTw3VrJyfsHdK8SLvN+UMDZqRQ4x t3QSaFBx+OJMYnKZdeBbvPg1syiEm0o= X-MC-Unique: 16kP4rHDNJqz5om9PRV0jw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=esKZYTxlC8yf8oyx0YniIsAHsZo45frlmoVawIo+xGw=; b=g7ongIjN0y3KVxCypBFqZvfgLk+9fy4dYplYxUE+Zip7b2yfn7w27hwFkdbvt59WX6 SJdiOqHqME3JIPoH/oSiCecxXTStcEO7AQG/BBRU93Unwx48UzpdEZkLw4G9plEvY+x+ 6r9D5uQxlwkTVbIOaSsWS+vscuIVW0n5XwqZYMqyIV2aNLBxaVXuTbL3CQxzYx5M3ZYw wIMGHw/TWdOpdfbTvH4UUtTStly6LMAtd2oFOLb4Tqukgf8Y+T2axgXJRkIJVf/PCb7I yggRG3n5y1T+BRxkuB0HlB2npAQJRCVaHx8cDQzCKPi+a6tvyNJjEgrNJHy+hfSeOTaj /MuQ== X-Gm-Message-State: AOAM532OUAr1hLlPQ2pVk6iO0J/Ytjo5AUb6uONmoh1lXG+NNFJC3Yv/ oIa+UrjTufORtazzFYnHd9QvZU+DCWfhR5Y2Hn6Inibe8yUdNMBXYU0Vqd1yrUP8TRnp86Iedki fLWKJLAUxhuXQlw== X-Received: by 2002:a17:906:73d5:: with SMTP id n21mr3297270ejl.8.1615376602412; Wed, 10 Mar 2021 03:43:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyvw9MsNHBzdwtjT88gKDHurhxCqXRr38gD9zlE6Ci4oY2eSJDvyjgiy7Ikgvgq3NnOPSCxbg== X-Received: by 2002:a17:906:73d5:: with SMTP id n21mr3297253ejl.8.1615376602274; Wed, 10 Mar 2021 03:43:22 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Fam Zheng , Markus Armbruster , qemu-block@nongnu.org, Vladimir Sementsov-Ogievskiy , Kevin Wolf , Max Reitz , Prasad J Pandit , Bandan Das , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH 1/3] block: Introduce the 'zeroes-co' driver Date: Wed, 10 Mar 2021 12:43:12 +0100 Message-Id: <20210310114314.1068957-2-philmd@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210310114314.1068957-1-philmd@redhat.com> References: <20210310114314.1068957-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The 'zeroes-co' block driver is almost a copy of the 'null-co' block driver designed for performance testing, but targets security needs, by always zero-initializing read accesses. Write accesses are discarded. Suggested-by: Fam Zheng Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- block/zeroes.c | 306 ++++++++++++++++++++++++++++++++++++++++++++++ block/meson.build | 1 + 2 files changed, 307 insertions(+) create mode 100644 block/zeroes.c diff --git a/block/zeroes.c b/block/zeroes.c new file mode 100644 index 00000000000..7256b6d02ee --- /dev/null +++ b/block/zeroes.c @@ -0,0 +1,306 @@ +/* + * Zeroes block driver + * + * Based on block/null.c + * + * Copyright (C) 2021 Red Hat, Inc. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qstring.h" +#include "qemu/module.h" +#include "qemu/option.h" +#include "block/block_int.h" +#include "sysemu/replay.h" + +#define NULL_OPT_LATENCY "latency-ns" + +typedef struct { + int64_t length; + int64_t latency_ns; +} BDRVZeroesState; + +static QemuOptsList runtime_opts =3D { + .name =3D "zeroes", + .head =3D QTAILQ_HEAD_INITIALIZER(runtime_opts.head), + .desc =3D { + { + .name =3D BLOCK_OPT_SIZE, + .type =3D QEMU_OPT_SIZE, + .help =3D "size of the zeroes block", + }, + { + .name =3D NULL_OPT_LATENCY, + .type =3D QEMU_OPT_NUMBER, + .help =3D "nanoseconds (approximated) to wait " + "before completing request", + }, + { /* end of list */ } + }, +}; + +static void zeroes_co_parse_filename(const char *filename, QDict *options, + Error **errp) +{ + /* + * This functions only exists so that a zeroes-co:// filename + * is accepted with the zeroes-co driver. + */ + if (strcmp(filename, "zeroes-co://")) { + error_setg(errp, "The only allowed filename for this driver is " + "'zeroes-co://'"); + return; + } +} + +static void zeroes_aio_parse_filename(const char *filename, QDict *options, + Error **errp) +{ + /* + * This functions only exists so that a zeroes-aio:// filename + * is accepted with the zeroes-aio driver. + */ + if (strcmp(filename, "zeroes-aio://")) { + error_setg(errp, "The only allowed filename for this driver is " + "'zeroes-aio://'"); + return; + } +} + +static int zeroes_file_open(BlockDriverState *bs, QDict *options, + int flags, Error **errp) +{ + QemuOpts *opts; + BDRVZeroesState *s =3D bs->opaque; + int ret =3D 0; + + opts =3D qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); + qemu_opts_absorb_qdict(opts, options, &error_abort); + s->length =3D qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0); + if (s->length < 0) { + error_setg(errp, "%s is invalid", BLOCK_OPT_SIZE); + ret =3D -EINVAL; + } + s->latency_ns =3D qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0); + if (s->latency_ns < 0) { + error_setg(errp, "%s is invalid", NULL_OPT_LATENCY); + ret =3D -EINVAL; + } + qemu_opts_del(opts); + bs->supported_write_flags =3D BDRV_REQ_FUA; + return ret; +} + +static int64_t zeroes_getlength(BlockDriverState *bs) +{ + BDRVZeroesState *s =3D bs->opaque; + return s->length; +} + +static coroutine_fn int zeroes_co_common(BlockDriverState *bs) +{ + BDRVZeroesState *s =3D bs->opaque; + + if (s->latency_ns) { + qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, s->latency_ns); + } + return 0; +} + +static coroutine_fn int zeroes_co_preadv(BlockDriverState *bs, + uint64_t offset, uint64_t bytes, + QEMUIOVector *qiov, int flags) +{ + qemu_iovec_memset(qiov, 0, 0, bytes); + + return zeroes_co_common(bs); +} + +static coroutine_fn int zeroes_co_pwritev(BlockDriverState *bs, + uint64_t offset, uint64_t bytes, + QEMUIOVector *qiov, int flags) +{ + return zeroes_co_common(bs); +} + +static coroutine_fn int zeroes_co_flush(BlockDriverState *bs) +{ + return zeroes_co_common(bs); +} + +typedef struct { + BlockAIOCB common; + QEMUTimer timer; +} ZeroesAIOCB; + +static const AIOCBInfo zeroes_aiocb_info =3D { + .aiocb_size =3D sizeof(ZeroesAIOCB), +}; + +static void zeroes_bh_cb(void *opaque) +{ + ZeroesAIOCB *acb =3D opaque; + acb->common.cb(acb->common.opaque, 0); + qemu_aio_unref(acb); +} + +static void zeroes_timer_cb(void *opaque) +{ + ZeroesAIOCB *acb =3D opaque; + acb->common.cb(acb->common.opaque, 0); + timer_deinit(&acb->timer); + qemu_aio_unref(acb); +} + +static inline BlockAIOCB *zeroes_aio_common(BlockDriverState *bs, + BlockCompletionFunc *cb, + void *opaque) +{ + ZeroesAIOCB *acb; + BDRVZeroesState *s =3D bs->opaque; + + acb =3D qemu_aio_get(&zeroes_aiocb_info, bs, cb, opaque); + /* Only emulate latency after vcpu is running. */ + if (s->latency_ns) { + aio_timer_init(bdrv_get_aio_context(bs), &acb->timer, + QEMU_CLOCK_REALTIME, SCALE_NS, + zeroes_timer_cb, acb); + timer_mod_ns(&acb->timer, + qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_n= s); + } else { + replay_bh_schedule_oneshot_event(bdrv_get_aio_context(bs), + zeroes_bh_cb, acb); + } + return &acb->common; +} + +static BlockAIOCB *zeroes_aio_preadv(BlockDriverState *bs, + uint64_t offset, uint64_t bytes, + QEMUIOVector *qiov, int flags, + BlockCompletionFunc *cb, + void *opaque) +{ + qemu_iovec_memset(qiov, 0, 0, bytes); + + return zeroes_aio_common(bs, cb, opaque); +} + +static BlockAIOCB *zeroes_aio_pwritev(BlockDriverState *bs, + uint64_t offset, uint64_t bytes, + QEMUIOVector *qiov, int flags, + BlockCompletionFunc *cb, + void *opaque) +{ + return zeroes_aio_common(bs, cb, opaque); +} + +static BlockAIOCB *zeroes_aio_flush(BlockDriverState *bs, + BlockCompletionFunc *cb, + void *opaque) +{ + return zeroes_aio_common(bs, cb, opaque); +} + +static int zeroes_reopen_prepare(BDRVReopenState *reopen_state, + BlockReopenQueue *queue, Error **errp) +{ + return 0; +} + +static int coroutine_fn zeroes_co_block_status(BlockDriverState *bs, + bool want_zero, int64_t off= set, + int64_t bytes, int64_t *pnu= m, + int64_t *map, + BlockDriverState **file) +{ + *pnum =3D bytes; + *map =3D offset; + *file =3D bs; + + return BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_ZERO; +} + +static void zeroes_refresh_filename(BlockDriverState *bs) +{ + const QDictEntry *e; + + for (e =3D qdict_first(bs->full_open_options); e; + e =3D qdict_next(bs->full_open_options, e)) + { + /* These options can be ignored */ + if (strcmp(qdict_entry_key(e), "filename") && + strcmp(qdict_entry_key(e), "driver") && + strcmp(qdict_entry_key(e), NULL_OPT_LATENCY)) + { + return; + } + } + + snprintf(bs->exact_filename, sizeof(bs->exact_filename), + "%s://", bs->drv->format_name); +} + +static int64_t zeroes_allocated_file_size(BlockDriverState *bs) +{ + return 0; +} + +static const char *const zeroes_strong_runtime_opts[] =3D { + BLOCK_OPT_SIZE, + + NULL +}; + +static BlockDriver bdrv_zeroes_co =3D { + .format_name =3D "zeroes-co", + .protocol_name =3D "zeroes-co", + .instance_size =3D sizeof(BDRVZeroesState), + + .bdrv_file_open =3D zeroes_file_open, + .bdrv_parse_filename =3D zeroes_co_parse_filename, + .bdrv_getlength =3D zeroes_getlength, + .bdrv_get_allocated_file_size =3D zeroes_allocated_file_size, + + .bdrv_co_preadv =3D zeroes_co_preadv, + .bdrv_co_pwritev =3D zeroes_co_pwritev, + .bdrv_co_flush_to_disk =3D zeroes_co_flush, + .bdrv_reopen_prepare =3D zeroes_reopen_prepare, + + .bdrv_co_block_status =3D zeroes_co_block_status, + + .bdrv_refresh_filename =3D zeroes_refresh_filename, + .strong_runtime_opts =3D zeroes_strong_runtime_opts, +}; + +static BlockDriver bdrv_zeroes_aio =3D { + .format_name =3D "zeroes-aio", + .protocol_name =3D "zeroes-aio", + .instance_size =3D sizeof(BDRVZeroesState), + + .bdrv_file_open =3D zeroes_file_open, + .bdrv_parse_filename =3D zeroes_aio_parse_filename, + .bdrv_getlength =3D zeroes_getlength, + .bdrv_get_allocated_file_size =3D zeroes_allocated_file_size, + + .bdrv_aio_preadv =3D zeroes_aio_preadv, + .bdrv_aio_pwritev =3D zeroes_aio_pwritev, + .bdrv_aio_flush =3D zeroes_aio_flush, + .bdrv_reopen_prepare =3D zeroes_reopen_prepare, + + .bdrv_co_block_status =3D zeroes_co_block_status, + + .bdrv_refresh_filename =3D zeroes_refresh_filename, + .strong_runtime_opts =3D zeroes_strong_runtime_opts, +}; + +static void bdrv_zeroes_init(void) +{ + bdrv_register(&bdrv_zeroes_co); + bdrv_register(&bdrv_zeroes_aio); +} + +block_init(bdrv_zeroes_init); diff --git a/block/meson.build b/block/meson.build index d21990ec95a..661d84118fb 100644 --- a/block/meson.build +++ b/block/meson.build @@ -40,6 +40,7 @@ 'vmdk.c', 'vpc.c', 'write-threshold.c', + 'zeroes.c', ), zstd, zlib, gnutls) =20 softmmu_ss.add(when: 'CONFIG_TCG', if_true: files('blkreplay.c')) --=20 2.26.2