From nobody Mon Sep 16 19:31:10 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=1669646098; cv=none; d=zohomail.com; s=zohoarc; b=Id8LzPm1m49VRtaZa8cdlQpCH9T7kJUBxvBSNgidVTxZlBKVPoVYTqekHJEx11CvtkZbD9cojpqZ7jaTcq7kZFXWCQY13xjYjj6MvYpsO73ZY/2tlzk0vuGs9D6qyluhUzrczSh0vVVkAOLXs0v67j+/EL95E9czouyp/D42e68= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669646098; 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=TltU+xSwgGuUjCH0YEXqTkmoHpALsUTB43j7TaCvHVY=; b=TVkXj6Uf1pCVTl+f5jA+CZCbaImvdgBseh0psDb9mhhCko/Gre+p+KUGg9ByjcqMUgVtCG+SQulanCT4NQ2dtnXL4+q/xeBNM85K1Zh6UP001DfzJBYuDpPWPQVkBzh9L4HN65hlxH0vFn7onk7UTS4g/wb9y/0IKQq5pcI7W8A= 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 166964609881325.791519084245692; Mon, 28 Nov 2022 06:34:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ozf4t-0005u8-3S; Mon, 28 Nov 2022 09:25:47 -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 1ozf37-0004lK-Sv for qemu-devel@nongnu.org; Mon, 28 Nov 2022 09:24:00 -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 1ozf2z-0006ub-AZ for qemu-devel@nongnu.org; Mon, 28 Nov 2022 09:23:55 -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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-515-Pbq26JGFO2aiIYxJepVgIA-1; Mon, 28 Nov 2022 09:23:45 -0500 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 CD0A9185A7AA; Mon, 28 Nov 2022 14:23:44 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6B1951402BDF; Mon, 28 Nov 2022 14:23:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669645428; 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=TltU+xSwgGuUjCH0YEXqTkmoHpALsUTB43j7TaCvHVY=; b=hgNzR5f4Gh0LOKrwFUICxZ6Cq4qZcat7B0HdjjWqdulJbNVzyNHhp8Vi1a7SviP5uVjlUu pcf3tq/t7spZIGVdSthL9m77YX3NnPcleqciQFP3u5+F1lHnjxxbpwv2b/3I1nqtI25oof /uERLs3R88J83KQEjqZZuRQe7pejnlQ= X-MC-Unique: Pbq26JGFO2aiIYxJepVgIA-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Cc: Kevin Wolf , Paolo Bonzini , Hanna Reitz , John Snow , Vladimir Sementsov-Ogievskiy , Eric Blake , Fam Zheng , Stefan Hajnoczi , "Denis V. Lunev" , Stefan Weil , Jeff Cody , Cleber Rosa , qemu-devel@nongnu.org, Emanuele Giuseppe Esposito Subject: [PATCH v7 10/14] block-coroutine-wrapper.py: introduce co_wrapper Date: Mon, 28 Nov 2022 09:23:33 -0500 Message-Id: <20221128142337.657646-11-eesposit@redhat.com> In-Reply-To: <20221128142337.657646-1-eesposit@redhat.com> References: <20221128142337.657646-1-eesposit@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 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=eesposit@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, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: 1669646099873100003 Content-Type: text/plain; charset="utf-8" This new annotation starts just a function wrapper that creates a new coroutine. It assumes the caller is not a coroutine. It will be the default annotation to be used in the future. This is much better as c_w_mixed, because it is clear if the caller is a coroutine or not, and provides the advantage of automating the code creation. In the future all c_w_mixed functions will be substituted by co_wrapper. Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy --- docs/devel/block-coroutine-wrapper.rst | 6 +- include/block/block-common.h | 8 +- scripts/block-coroutine-wrapper.py | 110 +++++++++++++++++-------- 3 files changed, 86 insertions(+), 38 deletions(-) diff --git a/docs/devel/block-coroutine-wrapper.rst b/docs/devel/block-coro= utine-wrapper.rst index 64acc8d65d..6dd2cdcab3 100644 --- a/docs/devel/block-coroutine-wrapper.rst +++ b/docs/devel/block-coroutine-wrapper.rst @@ -26,12 +26,12 @@ called ``bdrv_foo()``. In this case the scri= pt can help. To trigger the generation: =20 1. You need ``bdrv_foo`` declaration somewhere (for example, in - ``block/coroutines.h``) with the ``co_wrapper_mixed`` mark, + ``block/coroutines.h``) with the ``co_wrapper`` mark, like this: =20 .. code-block:: c =20 - int co_wrapper_mixed bdrv_foo(); + int co_wrapper bdrv_foo(); =20 2. You need to feed this declaration to block-coroutine-wrapper script. For this, add the .h (or .c) file with the declaration to the @@ -46,7 +46,7 @@ Links =20 1. The script location is ``scripts/block-coroutine-wrapper.py``. =20 -2. Generic place for private ``co_wrapper_mixed`` declarations is +2. Generic place for private ``co_wrapper`` declarations is ``block/coroutines.h``, for public declarations: ``include/block/block.h`` =20 diff --git a/include/block/block-common.h b/include/block/block-common.h index ec2309055b..847e4d4626 100644 --- a/include/block/block-common.h +++ b/include/block/block-common.h @@ -42,9 +42,13 @@ * * Usage: read docs/devel/block-coroutine-wrapper.rst * - * co_wrapper_mixed functions can be called by both coroutine and - * non-coroutine context. + * There are 2 kind of specifiers: + * - co_wrapper functions can be called by only non-coroutine context, bec= ause + * they always generate a new coroutine. + * - co_wrapper_mixed functions can be called by both coroutine and + * non-coroutine context. */ +#define co_wrapper #define co_wrapper_mixed =20 /* block.c */ diff --git a/scripts/block-coroutine-wrapper.py b/scripts/block-coroutine-w= rapper.py index 56e6425356..2090c3bf73 100644 --- a/scripts/block-coroutine-wrapper.py +++ b/scripts/block-coroutine-wrapper.py @@ -2,7 +2,7 @@ """Generate coroutine wrappers for block subsystem. =20 The program parses one or several concatenated c files from stdin, -searches for functions with the 'co_wrapper_mixed' specifier +searches for functions with the 'co_wrapper' specifier and generates corresponding wrappers on stdout. =20 Usage: block-coroutine-wrapper.py generated-file.c FILE.[ch]... @@ -62,10 +62,25 @@ def __init__(self, param_decl: str) -> None: =20 =20 class FuncDecl: - def __init__(self, return_type: str, name: str, args: str) -> None: + def __init__(self, return_type: str, name: str, args: str, + variant: str) -> None: self.return_type =3D return_type.strip() self.name =3D name.strip() + self.struct_name =3D snake_to_camel(self.name) self.args =3D [ParamDecl(arg.strip()) for arg in args.split(',')] + self.create_only_co =3D 'mixed' not in variant + + subsystem, subname =3D self.name.split('_', 1) + self.co_name =3D f'{subsystem}_co_{subname}' + + t =3D self.args[0].type + if t =3D=3D 'BlockDriverState *': + bs =3D 'bs' + elif t =3D=3D 'BdrvChild *': + bs =3D 'child->bs' + else: + bs =3D 'blk_bs(blk)' + self.bs =3D bs =20 def gen_list(self, format: str) -> str: return ', '.join(format.format_map(arg.__dict__) for arg in self.a= rgs) @@ -74,8 +89,9 @@ def gen_block(self, format: str) -> str: return '\n'.join(format.format_map(arg.__dict__) for arg in self.a= rgs) =20 =20 -# Match wrappers declared with a co_wrapper_mixed mark -func_decl_re =3D re.compile(r'^int\s*co_wrapper_mixed\s*' +# Match wrappers declared with a co_wrapper mark +func_decl_re =3D re.compile(r'^int\s*co_wrapper' + r'(?P(_[a-z][a-z0-9_]*)?)\s*' r'(?P[a-z][a-z0-9_]*)' r'\((?P[^)]*)\);$', re.MULTILINE) =20 @@ -84,7 +100,8 @@ def func_decl_iter(text: str) -> Iterator: for m in func_decl_re.finditer(text): yield FuncDecl(return_type=3D'int', name=3Dm.group('wrapper_name'), - args=3Dm.group('args')) + args=3Dm.group('args'), + variant=3Dm.group('variant')) =20 =20 def snake_to_camel(func_name: str) -> str: @@ -97,24 +114,67 @@ def snake_to_camel(func_name: str) -> str: return ''.join(words) =20 =20 +def create_mixed_wrapper(func: FuncDecl) -> str: + """ + Checks if we are already in coroutine + """ + name =3D func.co_name + struct_name =3D func.struct_name + return f"""\ +int {func.name}({ func.gen_list('{decl}') }) +{{ + if (qemu_in_coroutine()) {{ + return {name}({ func.gen_list('{name}') }); + }} else {{ + {struct_name} s =3D {{ + .poll_state.bs =3D {func.bs}, + .poll_state.in_progress =3D true, + +{ func.gen_block(' .{name} =3D {name},') } + }}; + + s.poll_state.co =3D qemu_coroutine_create({name}_entry, &s); + + return bdrv_poll_co(&s.poll_state); + }} +}}""" + + +def create_co_wrapper(func: FuncDecl) -> str: + """ + Assumes we are not in coroutine, and creates one + """ + name =3D func.co_name + struct_name =3D func.struct_name + return f"""\ +int {func.name}({ func.gen_list('{decl}') }) +{{ + {struct_name} s =3D {{ + .poll_state.bs =3D {func.bs}, + .poll_state.in_progress =3D true, + +{ func.gen_block(' .{name} =3D {name},') } + }}; + assert(!qemu_in_coroutine()); + + s.poll_state.co =3D qemu_coroutine_create({name}_entry, &s); + + return bdrv_poll_co(&s.poll_state); +}}""" + + def gen_wrapper(func: FuncDecl) -> str: assert not '_co_' in func.name assert func.return_type =3D=3D 'int' assert func.args[0].type in ['BlockDriverState *', 'BdrvChild *', 'BlockBackend *'] =20 - subsystem, subname =3D func.name.split('_', 1) - - name =3D f'{subsystem}_co_{subname}' + name =3D func.co_name + struct_name =3D func.struct_name =20 - t =3D func.args[0].type - if t =3D=3D 'BlockDriverState *': - bs =3D 'bs' - elif t =3D=3D 'BdrvChild *': - bs =3D 'child->bs' - else: - bs =3D 'blk_bs(blk)' - struct_name =3D snake_to_camel(name) + creation_function =3D create_mixed_wrapper + if func.create_only_co: + creation_function =3D create_co_wrapper =20 return f"""\ /* @@ -136,23 +196,7 @@ def gen_wrapper(func: FuncDecl) -> str: aio_wait_kick(); }} =20 -int {func.name}({ func.gen_list('{decl}') }) -{{ - if (qemu_in_coroutine()) {{ - return {name}({ func.gen_list('{name}') }); - }} else {{ - {struct_name} s =3D {{ - .poll_state.bs =3D {bs}, - .poll_state.in_progress =3D true, - -{ func.gen_block(' .{name} =3D {name},') } - }}; - - s.poll_state.co =3D qemu_coroutine_create({name}_entry, &s); - - return bdrv_poll_co(&s.poll_state); - }} -}}""" +{creation_function(func)}""" =20 =20 def gen_wrappers(input_code: str) -> str: --=20 2.31.1