From nobody Wed Dec 17 05:35:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1563543910; cv=none; d=zoho.com; s=zohoarc; b=VPXpFCe7R0jGgwkbasBJu0vEBflGHkxWUMuyXAWK/DPDVi+km/cbPP1q3q6UTAXxkFKLIMqf3S+MKVlOp8hhJpEPdj99DEXt0E0TG9IOtz0sTA2YnoEKO1ufqj3FPu/sHsk3y66qfwnkTv8Ihi/Wv3ssPyHN2klAIHpEdcT4dDc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563543910; 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:ARC-Authentication-Results; bh=l35U9knKL++fxn530+2i97Hy7xhSJGwnimEzky6FisA=; b=iUd+oqOeaRdh/btiIrBEH69bq5NSpgCRZaD3o8JypLiVsdejGdVc+3Nr/chipOxOfFOw8kHzIlcw0JItMvp81NMUdAhLFnEqsqvvkW7vCLzzGa5vTfBgSJjuVaF5cCeXZrApuNtZN6FCgtIoWkx8K6njAZFxTM3c0u5d3tfjJpI= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 156354391069877.30956784119712; Fri, 19 Jul 2019 06:45:10 -0700 (PDT) Received: from localhost ([::1]:45622 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hoTC9-0007MW-5N for importer@patchew.org; Fri, 19 Jul 2019 09:45:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40293) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hoTB6-0003Er-AW for qemu-devel@nongnu.org; Fri, 19 Jul 2019 09:44:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hoTB5-0003U7-0t for qemu-devel@nongnu.org; Fri, 19 Jul 2019 09:44:04 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42502) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hoTB2-0003Rq-5L; Fri, 19 Jul 2019 09:44:00 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7A62A3091754; Fri, 19 Jul 2019 13:43:59 +0000 (UTC) Received: from dhcp-200-226.str.redhat.com (dhcp-200-226.str.redhat.com [10.33.200.226]) by smtp.corp.redhat.com (Postfix) with ESMTP id 91987608A4; Fri, 19 Jul 2019 13:43:58 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 19 Jul 2019 15:43:35 +0200 Message-Id: <20190719134345.23526-4-kwolf@redhat.com> In-Reply-To: <20190719134345.23526-1-kwolf@redhat.com> References: <20190719134345.23526-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Fri, 19 Jul 2019 13:43:59 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 03/13] tests: Add job commit by drained_end test X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Max Reitz Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- tests/test-bdrv-drain.c | 119 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c index 12e2ecf517..3503ce3b69 100644 --- a/tests/test-bdrv-drain.c +++ b/tests/test-bdrv-drain.c @@ -1527,6 +1527,122 @@ static void test_set_aio_context(void) iothread_join(b); } =20 + +typedef struct TestDropBackingBlockJob { + BlockJob common; + bool should_complete; + bool *did_complete; +} TestDropBackingBlockJob; + +static int coroutine_fn test_drop_backing_job_run(Job *job, Error **errp) +{ + TestDropBackingBlockJob *s =3D + container_of(job, TestDropBackingBlockJob, common.job); + + while (!s->should_complete) { + job_sleep_ns(job, 0); + } + + return 0; +} + +static void test_drop_backing_job_commit(Job *job) +{ + TestDropBackingBlockJob *s =3D + container_of(job, TestDropBackingBlockJob, common.job); + + bdrv_set_backing_hd(blk_bs(s->common.blk), NULL, &error_abort); + + *s->did_complete =3D true; +} + +static const BlockJobDriver test_drop_backing_job_driver =3D { + .job_driver =3D { + .instance_size =3D sizeof(TestDropBackingBlockJob), + .free =3D block_job_free, + .user_resume =3D block_job_user_resume, + .drain =3D block_job_drain, + .run =3D test_drop_backing_job_run, + .commit =3D test_drop_backing_job_commit, + } +}; + +/** + * Creates a child node with three parent nodes on it, and then runs a + * block job on the final one, parent-node-2. + * + * (TODO: parent-node-0 currently serves no purpose, but will as of a + * follow-up patch.) + * + * The job is then asked to complete before a section where the child + * is drained. + * + * Ending this section will undrain the child's parents, first + * parent-node-2, then parent-node-1, then parent-node-0 -- the parent + * list is in reverse order of how they were added. Ending the drain + * on parent-node-2 will resume the job, thus completing it and + * scheduling job_exit(). + * + * Ending the drain on parent-node-1 will poll the AioContext, which + * lets job_exit() and thus test_drop_backing_job_commit() run. That + * function removes the child as parent-node-2's backing file. + * + * In old (and buggy) implementations, there are two problems with + * that: + * (A) bdrv_drain_invoke() polls for every node that leaves the + * drained section. This means that job_exit() is scheduled + * before the child has left the drained section. Its + * quiesce_counter is therefore still 1 when it is removed from + * parent-node-2. + * + * (B) bdrv_replace_child_noperm() calls drained_end() on the old + * child's parents as many times as the child is quiesced. This + * means it will call drained_end() on parent-node-2 once. + * Because parent-node-2 is no longer quiesced at this point, this + * will fail. + * + * bdrv_replace_child_noperm() therefore must call drained_end() on + * the parent only if it really is still drained because the child is + * drained. + */ +static void test_blockjob_commit_by_drained_end(void) +{ + BlockDriverState *bs_child, *bs_parents[3]; + TestDropBackingBlockJob *job; + bool job_has_completed =3D false; + int i; + + bs_child =3D bdrv_new_open_driver(&bdrv_test, "child-node", BDRV_O_RDW= R, + &error_abort); + + for (i =3D 0; i < 3; i++) { + char name[32]; + snprintf(name, sizeof(name), "parent-node-%i", i); + bs_parents[i] =3D bdrv_new_open_driver(&bdrv_test, name, BDRV_O_RD= WR, + &error_abort); + bdrv_set_backing_hd(bs_parents[i], bs_child, &error_abort); + } + + job =3D block_job_create("job", &test_drop_backing_job_driver, NULL, + bs_parents[2], 0, BLK_PERM_ALL, 0, 0, NULL, NUL= L, + &error_abort); + + job->did_complete =3D &job_has_completed; + + job_start(&job->common.job); + + job->should_complete =3D true; + bdrv_drained_begin(bs_child); + g_assert(!job_has_completed); + bdrv_drained_end(bs_child); + g_assert(job_has_completed); + + bdrv_unref(bs_parents[0]); + bdrv_unref(bs_parents[1]); + bdrv_unref(bs_parents[2]); + bdrv_unref(bs_child); +} + int main(int argc, char **argv) { int ret; @@ -1610,6 +1726,9 @@ int main(int argc, char **argv) =20 g_test_add_func("/bdrv-drain/set_aio_context", test_set_aio_context); =20 + g_test_add_func("/bdrv-drain/blockjob/commit_by_drained_end", + test_blockjob_commit_by_drained_end); + ret =3D g_test_run(); qemu_event_destroy(&done_event); return ret; --=20 2.20.1