From nobody Tue Nov 26 18:19:47 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=1705688189; cv=none; d=zohomail.com; s=zohoarc; b=LVwDQQdZ91M3IlMtQu/ypbEfN5yielUy6owDac4SJGo1qvvzzNNUbG2QsQ5Cie8g9CB1iX3ujo6bdH7Ch4lPorTenPq51yGxskZtRAFb+Z5z83EhaB3eVACmDafKYJwk/T9ryliFMUGPk2LTQrAZ4NQ+SuTMBfRQMlqFU0SfT+k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705688189; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Sj3ULLvzpdZSSoBFU8DUbG4v/mhg3sH2J0N7KsejgkQ=; b=Lt6ii6/YcCn5jmgxiwiXg+KL4qEB4sRap1OSUo36sI2pmf7LZ9oOlTsxWBpRHMqIppn1dotJ7PUJCcrPUnpeXvBe5iYw+kp0xs3gu0QIhsqt/JfdTMhHwbj4fc0YQbiNeEvmZ7pX6unvR/dI5y3bQMQsOkFZW6ApUS/SNQ5G6Lw= 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 1705688189129492.0940419356482; Fri, 19 Jan 2024 10:16:29 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rQtNM-0001jf-Tl; Fri, 19 Jan 2024 13:13:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rQtNL-0001iB-6L for qemu-devel@nongnu.org; Fri, 19 Jan 2024 13:13:55 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rQtNF-0007gO-3F for qemu-devel@nongnu.org; Fri, 19 Jan 2024 13:13:54 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-5-hig_P3L5Ooiw46CJ8Su1bg-1; Fri, 19 Jan 2024 13:13:45 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C8A3410665A2; Fri, 19 Jan 2024 18:13:44 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.194.86]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A9FC1B96; Fri, 19 Jan 2024 18:13:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705688028; 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=Sj3ULLvzpdZSSoBFU8DUbG4v/mhg3sH2J0N7KsejgkQ=; b=LG5k+D4WhjYyUi1bc413hPNJOWil9Eeu7n00YNAzlR7UIN8xTyzMceQvhnv2NaRA/Oi64U 2+sOFbdW+7MbDbDANuku+oUrokwlEVTb3xCPIPn/jEyjv1VJGeDkypk8K5M53g1TIv4Zcc ybYAdrikYn5Q9kK539T7UxWaU7D6ly0= X-MC-Unique: hig_P3L5Ooiw46CJ8Su1bg-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 14/14] block/blklogwrites: Protect mutable driver state with a mutex. Date: Fri, 19 Jan 2024 19:13:27 +0100 Message-ID: <20240119181327.236745-15-kwolf@redhat.com> In-Reply-To: <20240119181327.236745-1-kwolf@redhat.com> References: <20240119181327.236745-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 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.129.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -45 X-Spam_score: -4.6 X-Spam_bar: ---- X-Spam_report: (-4.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.519, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, 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-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1705688190054100003 Content-Type: text/plain; charset="utf-8" From: Ari Sundholm During the review of a fix for a concurrency issue in blklogwrites, it was found that the driver needs an additional fix when enabling multiqueue, which is a new feature introduced in QEMU 9.0, as the driver state may be read and written by multiple threads at the same time, which was not the case when the driver was originally written. Fix the multi-threaded scenario by introducing a mutex to protect the mutable fields in the driver state, and always having the mutex locked by the current thread when accessing them. Also use the mutex and a CoQueue to ensure that the super block is not being written to by multiple threads concurrently and updates are properly serialized. Additionally, add the const qualifier to a few BDRVBlkLogWritesState pointer targets in contexts where the driver state is not written to. Signed-off-by: Ari Sundholm Message-ID: <20240119162913.2620245-1-ari@tuxera.com> Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf --- block/blklogwrites.c | 85 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 8 deletions(-) diff --git a/block/blklogwrites.c b/block/blklogwrites.c index ba717dab4d..399819a2a1 100644 --- a/block/blklogwrites.c +++ b/block/blklogwrites.c @@ -3,7 +3,7 @@ * * Copyright (c) 2017 Tuomas Tynkkynen * Copyright (c) 2018 Aapo Vienamo - * Copyright (c) 2018 Ari Sundholm + * Copyright (c) 2018-2024 Ari Sundholm * * This work is licensed under the terms of the GNU GPL, version 2 or late= r. * See the COPYING file in the top-level directory. @@ -55,9 +55,34 @@ typedef struct { BdrvChild *log_file; uint32_t sectorsize; uint32_t sectorbits; + uint64_t update_interval; + + /* + * The mutable state of the driver, consisting of the current log sect= or + * and the number of log entries. + * + * May be read and/or written from multiple threads, and the mutex mus= t be + * held when accessing these fields. + */ uint64_t cur_log_sector; uint64_t nr_entries; - uint64_t update_interval; + QemuMutex mutex; + + /* + * The super block sequence number. Non-zero if a super block update i= s in + * progress. + * + * The mutex must be held when accessing this field. + */ + uint64_t super_update_seq; + + /* + * A coroutine-aware queue to serialize super block updates. + * + * Used with the mutex to ensure that only one thread be updating the = super + * block at a time. + */ + CoQueue super_update_queue; } BDRVBlkLogWritesState; =20 static QemuOptsList runtime_opts =3D { @@ -169,6 +194,9 @@ static int blk_log_writes_open(BlockDriverState *bs, QD= ict *options, int flags, goto fail; } =20 + qemu_mutex_init(&s->mutex); + qemu_co_queue_init(&s->super_update_queue); + log_append =3D qemu_opt_get_bool(opts, "log-append", false); =20 if (log_append) { @@ -231,6 +259,8 @@ static int blk_log_writes_open(BlockDriverState *bs, QD= ict *options, int flags, s->nr_entries =3D 0; } =20 + s->super_update_seq =3D 0; + if (!blk_log_writes_sector_size_valid(log_sector_size)) { ret =3D -EINVAL; error_setg(errp, "Invalid log sector size %"PRIu64, log_sector_siz= e); @@ -255,6 +285,7 @@ fail_log: bdrv_unref_child(bs, s->log_file); bdrv_graph_wrunlock(); s->log_file =3D NULL; + qemu_mutex_destroy(&s->mutex); } fail: qemu_opts_del(opts); @@ -269,6 +300,7 @@ static void blk_log_writes_close(BlockDriverState *bs) bdrv_unref_child(bs, s->log_file); s->log_file =3D NULL; bdrv_graph_wrunlock(); + qemu_mutex_destroy(&s->mutex); } =20 static int64_t coroutine_fn GRAPH_RDLOCK @@ -295,7 +327,7 @@ static void blk_log_writes_child_perm(BlockDriverState = *bs, BdrvChild *c, =20 static void blk_log_writes_refresh_limits(BlockDriverState *bs, Error **er= rp) { - BDRVBlkLogWritesState *s =3D bs->opaque; + const BDRVBlkLogWritesState *s =3D bs->opaque; bs->bl.request_alignment =3D s->sectorsize; } =20 @@ -338,15 +370,18 @@ blk_log_writes_co_do_log(BlkLogWritesLogReq *lr) * driver may be modified by other driver operations while waiting for= the * I/O to complete. */ + qemu_mutex_lock(&s->mutex); const uint64_t entry_start_sector =3D s->cur_log_sector; const uint64_t entry_offset =3D entry_start_sector << s->sectorbits; const uint64_t qiov_aligned_size =3D ROUND_UP(lr->qiov->size, s->secto= rsize); const uint64_t entry_aligned_size =3D qiov_aligned_size + ROUND_UP(lr->zero_size, s->sectorsize); const uint64_t entry_nr_sectors =3D entry_aligned_size >> s->sectorbit= s; + const uint64_t entry_seq =3D s->nr_entries + 1; =20 - s->nr_entries++; + s->nr_entries =3D entry_seq; s->cur_log_sector +=3D entry_nr_sectors; + qemu_mutex_unlock(&s->mutex); =20 /* * Write the log entry. Note that if this is a "write zeroes" operatio= n, @@ -366,17 +401,44 @@ blk_log_writes_co_do_log(BlkLogWritesLogReq *lr) =20 /* Update super block on flush or every update interval */ if (lr->log_ret =3D=3D 0 && ((lr->entry.flags & LOG_FLUSH_FLAG) - || (s->nr_entries % s->update_interval =3D=3D 0))) + || (entry_seq % s->update_interval =3D=3D 0))) { struct log_write_super super =3D { .magic =3D cpu_to_le64(WRITE_LOG_MAGIC), .version =3D cpu_to_le64(WRITE_LOG_VERSION), - .nr_entries =3D cpu_to_le64(s->nr_entries), + .nr_entries =3D const_le64(0), .sectorsize =3D cpu_to_le32(s->sectorsize), }; - void *zeroes =3D g_malloc0(s->sectorsize - sizeof(super)); + void *zeroes; QEMUIOVector qiov; =20 + /* + * Wait if a super block update is already in progress. + * Bail out if a newer update got its turn before us. + */ + WITH_QEMU_LOCK_GUARD(&s->mutex) { + CoQueueWaitFlags wait_flags =3D 0; + while (s->super_update_seq) { + if (entry_seq < s->super_update_seq) { + return; + } + qemu_co_queue_wait_flags(&s->super_update_queue, + &s->mutex, wait_flags); + + /* + * In case the wait condition remains true after wakeup, + * to avoid starvation, make sure that this request is + * scheduled to rerun next by pushing it to the front of t= he + * queue. + */ + wait_flags =3D CO_QUEUE_WAIT_FRONT; + } + s->super_update_seq =3D entry_seq; + super.nr_entries =3D cpu_to_le64(s->nr_entries); + } + + zeroes =3D g_malloc0(s->sectorsize - sizeof(super)); + qemu_iovec_init(&qiov, 2); qemu_iovec_add(&qiov, &super, sizeof(super)); qemu_iovec_add(&qiov, zeroes, s->sectorsize - sizeof(super)); @@ -386,6 +448,13 @@ blk_log_writes_co_do_log(BlkLogWritesLogReq *lr) if (lr->log_ret =3D=3D 0) { lr->log_ret =3D bdrv_co_flush(s->log_file->bs); } + + /* The super block has been updated. Let another request have a go= . */ + qemu_mutex_lock(&s->mutex); + s->super_update_seq =3D 0; + (void) qemu_co_queue_next(&s->super_update_queue); + qemu_mutex_unlock(&s->mutex); + qemu_iovec_destroy(&qiov); g_free(zeroes); } @@ -405,7 +474,7 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t of= fset, uint64_t bytes, { QEMUIOVector log_qiov; size_t niov =3D qiov ? qiov->niov : 0; - BDRVBlkLogWritesState *s =3D bs->opaque; + const BDRVBlkLogWritesState *s =3D bs->opaque; BlkLogWritesFileReq fr =3D { .bs =3D bs, .offset =3D offset, --=20 2.43.0