From nobody Wed May 1 12:19:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1617184399; cv=none; d=zohomail.com; s=zohoarc; b=UVJRaSsfeNSclrc80n1h5ZzaxWphoe/L/XdDCDRQ0h+yfVg2nJU9w3Nze0VM5XcLcmuhd1yWd4lQrwgqkcRn1ojvuCBZ9YxPCv8Ys9UuGalGrxy/1Ull37gOWqWZio14Wqdhsee7kDQu/PF22JTOkex4A6UHZOniKCIHaIL64Dk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617184399; h=Content-Type: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=1CdueM3gEogQ5G3KRAQE7L4SV5arKAMcS6BZEoBjje4=; b=JvYx/TwpZQaBgRdxXRKEf1GSl9Jr70Vkg2ccF6EmOjb9oOk3qHf38q4TVGbMIIgV4ezZHNIFDjnFEMeIpuIMuBuBMTuCM6Stmgg5WLPrRPAhG75dB3Q4yQjc9EPbSDR9TmJAJrCvNTHxAG5kWzE1rZwRmVu5uFOBcWK15FKZ/Ls= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=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 1617184399455556.0156085785502; Wed, 31 Mar 2021 02:53:19 -0700 (PDT) Received: from localhost ([::1]:56382 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRXXK-00010s-AX for importer@patchew.org; Wed, 31 Mar 2021 05:53:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51338) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVS-000882-IJ for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:55268) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVR-00013u-2g for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:22 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-210-KjL5E--eNpSukC1G0t822w-1; Wed, 31 Mar 2021 05:51:18 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DA09C801FD8; Wed, 31 Mar 2021 09:51:16 +0000 (UTC) Received: from localhost (ovpn-115-85.ams2.redhat.com [10.36.115.85]) by smtp.corp.redhat.com (Postfix) with ESMTP id B103D19C44; Wed, 31 Mar 2021 09:51:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617184280; 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=1CdueM3gEogQ5G3KRAQE7L4SV5arKAMcS6BZEoBjje4=; b=gc+Z1Nv4UUb2kRmhoZ/MSwpCkK/mRkz+mqh1SKpKI0lOKddnm4OAbp5EbWwMHJqaPam9KO 29xRvOu9ts/mWfhSWQf+C44RN9bhPJVvDDmtfKYqCdM+9FaRJmecHpd53j78a1r0YlLRzP mOxeE1IqsuIZ+yu7+KWTfcKcmccvqfo= X-MC-Unique: KjL5E--eNpSukC1G0t822w-1 From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL for-6.0 1/6] block/vdi: When writing new bmap entry fails, don't leak the buffer Date: Wed, 31 Mar 2021 10:50:54 +0100 Message-Id: <20210331095059.303996-2-stefanha@redhat.com> In-Reply-To: <20210331095059.303996-1-stefanha@redhat.com> References: <20210331095059.303996-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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: -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, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org, Juan Quintela , David Edmondson , Stefan Weil , "Michael S. Tsirkin" , "Dr. David Alan Gilbert" , Max Reitz , John Snow , Stefan Hajnoczi , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: David Edmondson If a new bitmap entry is allocated, requiring the entire block to be written, avoiding leaking the buffer allocated for the block should the write fail. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: David Edmondson Signed-off-by: Paolo Bonzini Acked-by: Max Reitz Message-id: 20210325112941.365238-2-pbonzini@redhat.com Message-Id: <20210309144015.557477-2-david.edmondson@oracle.com> Acked-by: Max Reitz Signed-off-by: Paolo Bonzini Signed-off-by: Stefan Hajnoczi --- block/vdi.c | 1 + 1 file changed, 1 insertion(+) diff --git a/block/vdi.c b/block/vdi.c index 5627e7d764..2a6dc26124 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -690,6 +690,7 @@ nonallocating_write: =20 logout("finished data write\n"); if (ret < 0) { + g_free(block); return ret; } =20 --=20 2.30.2 From nobody Wed May 1 12:19:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1617184584; cv=none; d=zohomail.com; s=zohoarc; b=eDy2zrQwkV1l5Q7f03RPiNe7o60ILkv7femuBcoROpl+5rnWSf+z08aaD7lbElUweNma6oN9kmijqLsErB4DGVVx0cDQ57oxTK20U0XvOt2qTvr3tJjcWruT9WiippYPhxmE6KPWUqlsTvAoxJW3SC3cxwdaWL3HCPzEzVTl+Ag= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617184584; h=Content-Type: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=YoVaqoPs4A6987YuUxRT/Rw8lPPxe8ACl9sFBJLCayU=; b=aqyk44yik1egpMFX5/FjGAgwpM8VygUlZqW7ZHfb94pfbbIPVMSm1z7udbNLA38D30WvDwUfvIRjI6KTkcLYTQLZYp37vQuNKmhimfywquJvSZQJvhcTTsOzdfX/oMbdobUtyFXB5V/F+GOCR+svzxAXw7qPmFmVlRkTVAK1VpE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=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 161718458440521.987779153939755; Wed, 31 Mar 2021 02:56:24 -0700 (PDT) Received: from localhost ([::1]:36208 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRXaJ-0004Mz-6r for importer@patchew.org; Wed, 31 Mar 2021 05:56:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51378) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVV-0008C8-QK for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:32034) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVS-000164-VI for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:25 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-219-kQlDqS5UMkykWMGuA7opRQ-1; Wed, 31 Mar 2021 05:51:20 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 93E428030BB; Wed, 31 Mar 2021 09:51:18 +0000 (UTC) Received: from localhost (ovpn-115-85.ams2.redhat.com [10.36.115.85]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F2CE19C44; Wed, 31 Mar 2021 09:51:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617184282; 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=YoVaqoPs4A6987YuUxRT/Rw8lPPxe8ACl9sFBJLCayU=; b=CWtaLdayOGWlhY4V1ilK0aOUEl0BxwSZDwC+PMjERgUm09RWFtT/Z0E3/EMVXmEpjXio6v UzCkkFUM7MTa6XCKiTnBWjBNm30hFsQ3BK+VHxHOWHSmsd8unePsVEXIgbZG4O+nkNDKva QhErs5++0tCzqegyEgBYdmQhwo+og5g= X-MC-Unique: kQlDqS5UMkykWMGuA7opRQ-1 From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL for-6.0 2/6] block/vdi: Don't assume that blocks are larger than VdiHeader Date: Wed, 31 Mar 2021 10:50:55 +0100 Message-Id: <20210331095059.303996-3-stefanha@redhat.com> In-Reply-To: <20210331095059.303996-1-stefanha@redhat.com> References: <20210331095059.303996-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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=216.205.24.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.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, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org, Juan Quintela , David Edmondson , Stefan Weil , "Michael S. Tsirkin" , "Dr. David Alan Gilbert" , Max Reitz , John Snow , Stefan Hajnoczi , Paolo Bonzini , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: David Edmondson Given that the block size is read from the header of the VDI file, a wide variety of sizes might be seen. Rather than re-using a block sized memory region when writing the VDI header, allocate an appropriately sized buffer. Signed-off-by: David Edmondson Signed-off-by: Paolo Bonzini Acked-by: Max Reitz Message-id: 20210325112941.365238-3-pbonzini@redhat.com Message-Id: <20210309144015.557477-3-david.edmondson@oracle.com> Acked-by: Max Reitz Signed-off-by: Paolo Bonzini Signed-off-by: Stefan Hajnoczi --- block/vdi.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/block/vdi.c b/block/vdi.c index 2a6dc26124..548f8a057b 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -696,18 +696,20 @@ nonallocating_write: =20 if (block) { /* One or more new blocks were allocated. */ - VdiHeader *header =3D (VdiHeader *) block; + VdiHeader *header; uint8_t *base; uint64_t offset; uint32_t n_sectors; =20 + g_free(block); + header =3D g_malloc(sizeof(*header)); + logout("now writing modified header\n"); assert(VDI_IS_ALLOCATED(bmap_first)); *header =3D s->header; vdi_header_to_le(header); - ret =3D bdrv_pwrite(bs->file, 0, block, sizeof(VdiHeader)); - g_free(block); - block =3D NULL; + ret =3D bdrv_pwrite(bs->file, 0, header, sizeof(*header)); + g_free(header); =20 if (ret < 0) { return ret; --=20 2.30.2 From nobody Wed May 1 12:19:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1617184755; cv=none; d=zohomail.com; s=zohoarc; b=b8kpeWafRxuESV/QYm60tr62e4Cov0wfYvJp+Q5BAgW6/Z02+2lcZDi79+XZcABc55fewX0xOAoeVJvl44c/ilcyx8hjf+PsmM5KMP+5oz2hby7k2w6R3GH/UlRhY44vwqIWPuf+KfPXw5fjcf8Sc9nAhJLLkeZQjTtauHjVyB4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617184755; h=Content-Type: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=+T0Y4suzH9bAuGQlsJCr4UlfeAbPVFDegeuHhhrGHPA=; b=TukKDalt4OUL6Ebx/sk+tzpmzeg+Vfhu9CPHJ/f6bO25EgFXACMEFVJq4pPqa8e2ZBNljxecs9stDwOxFv6eJqQHbRHOMFHp/FUgcefFWIEa1GF7WwXHROuoKGRkwkihx2lt8yGLKdzjkWcWM6mDqVkbMlX5JoXIFlRtqJIYAqA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=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 1617184755154745.481208256653; Wed, 31 Mar 2021 02:59:15 -0700 (PDT) Received: from localhost ([::1]:45042 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRXd4-000815-4b for importer@patchew.org; Wed, 31 Mar 2021 05:59:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51580) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVk-0008PP-Sp for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:40 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:33418) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVd-0001F5-Qu for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:40 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-456-IZuWw5bpO7a-eDar2Son3Q-1; Wed, 31 Mar 2021 05:51:28 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 029BC180FCAA; Wed, 31 Mar 2021 09:51:27 +0000 (UTC) Received: from localhost (ovpn-115-85.ams2.redhat.com [10.36.115.85]) by smtp.corp.redhat.com (Postfix) with ESMTP id B4E625F705; Wed, 31 Mar 2021 09:51:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617184292; 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=+T0Y4suzH9bAuGQlsJCr4UlfeAbPVFDegeuHhhrGHPA=; b=FtUlNGzvtMjvU/298latdfN5k16zgfWGuhp7ODLURX1ITooGaYgdPxJEnYTQuX4LFXVecL qs90wevIcxYxR5A8OGQVx322oHAb/sCE70cpMEf7VdyZKKQdU6HoW0nwyIHUyoPFkG3RH6 /3JBWO0RV6/uHhlo7Zoy2cZdEmm9YzI= X-MC-Unique: IZuWw5bpO7a-eDar2Son3Q-1 From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL for-6.0 3/6] coroutine-lock: Store the coroutine in the CoWaitRecord only once Date: Wed, 31 Mar 2021 10:50:56 +0100 Message-Id: <20210331095059.303996-4-stefanha@redhat.com> In-Reply-To: <20210331095059.303996-1-stefanha@redhat.com> References: <20210331095059.303996-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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=63.128.21.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, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org, Juan Quintela , David Edmondson , Stefan Weil , "Michael S. Tsirkin" , "Dr. David Alan Gilbert" , Max Reitz , John Snow , Stefan Hajnoczi , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: David Edmondson When taking the slow path for mutex acquisition, set the coroutine value in the CoWaitRecord in push_waiter(), rather than both there and in the caller. Reviewed-by: Paolo Bonzini Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: David Edmondson Signed-off-by: Paolo Bonzini Message-id: 20210325112941.365238-4-pbonzini@redhat.com Message-Id: <20210309144015.557477-4-david.edmondson@oracle.com> Signed-off-by: Paolo Bonzini Signed-off-by: Stefan Hajnoczi --- util/qemu-coroutine-lock.c | 1 - 1 file changed, 1 deletion(-) diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index 5816bf8900..eb73cf11dc 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -204,7 +204,6 @@ static void coroutine_fn qemu_co_mutex_lock_slowpath(Ai= oContext *ctx, unsigned old_handoff; =20 trace_qemu_co_mutex_lock_entry(mutex, self); - w.co =3D self; push_waiter(mutex, &w); =20 /* This is the "Responsibility Hand-Off" protocol; a lock() picks from --=20 2.30.2 From nobody Wed May 1 12:19:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1617184408; cv=none; d=zohomail.com; s=zohoarc; b=XBFxvM9TT1C2U/hmKo4i0cJnzfJ242fijq8MP8PlM8x1tQI7ERPbTgeGmLGrkUVtunZgr5R8KANJvCImfYnDIqk/RaiZe5Et4vv4Fy5Mm3Db/XoDk7SRl8akHDKZFb7dPSz0tnzhL9cGbeFZDRr8SWdyJNWlIHGMgCwGRxU7YNs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617184408; h=Content-Type: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=zwtDVEdWFIqQ1efWKGsC2baQhZmklAripYMZ3Jpc9Zg=; b=iOAc5VWd4Iqi14i6mmhVwUfPx144g8obcAynQGQSe+kWvKtRjb7Usev1d+yeYtULLVkBfh7+vWOnqrEPV1VWqhiyOYKB5woFRKc13fFyzdhoLqsBBgplGp2a6eJ6vci3pluvSdruZAOTYO1DNequODhE74E13ngpXEQWaqJFgOE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=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 1617184408335752.6836591755176; Wed, 31 Mar 2021 02:53:28 -0700 (PDT) Received: from localhost ([::1]:57112 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRXXT-0001Iw-9Q for importer@patchew.org; Wed, 31 Mar 2021 05:53:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51592) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVl-0008QN-Ii for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:41 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:60325) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVd-0001E6-5v for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:41 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-530-nguSOEALPz6B2k9Dl9fyog-1; Wed, 31 Mar 2021 05:51:30 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ECE248189C6; Wed, 31 Mar 2021 09:51:28 +0000 (UTC) Received: from localhost (ovpn-115-85.ams2.redhat.com [10.36.115.85]) by smtp.corp.redhat.com (Postfix) with ESMTP id 620555F705; Wed, 31 Mar 2021 09:51:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617184292; 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=zwtDVEdWFIqQ1efWKGsC2baQhZmklAripYMZ3Jpc9Zg=; b=CVy0JOjve7zCRE37dJuNGm0vovia49hedC9Goj3DwtNKUmWohPtdS/EBPdV8yzLHDRoKKg EMntoin47POxBuNVogpsSAvDEO/iUj0GAH2ZPbruHJfGy/VM0n1W4RWBZZDpS//AZPqE9d WDpUb7VrXTBh027JVzpt3Iyg0vN+guc= X-MC-Unique: nguSOEALPz6B2k9Dl9fyog-1 From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL for-6.0 4/6] coroutine-lock: Reimplement CoRwlock to fix downgrade bug Date: Wed, 31 Mar 2021 10:50:57 +0100 Message-Id: <20210331095059.303996-5-stefanha@redhat.com> In-Reply-To: <20210331095059.303996-1-stefanha@redhat.com> References: <20210331095059.303996-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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=63.128.21.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.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, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org, Juan Quintela , David Edmondson , Stefan Weil , "Michael S. Tsirkin" , "Dr. David Alan Gilbert" , Max Reitz , John Snow , Stefan Hajnoczi , Paolo Bonzini , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini An invariant of the current rwlock is that if multiple coroutines hold a reader lock, all must be runnable. The unlock implementation relies on this, choosing to wake a single coroutine when the final read lock holder exits the critical section, assuming that it will wake a coroutine attempting to acquire a write lock. The downgrade implementation violates this assumption by creating a read lock owning coroutine that is exclusively runnable - any other coroutines that are waiting to acquire a read lock are *not* made runnable when the write lock holder converts its ownership to read only. More in general, the old implementation had lots of other fairness bugs. The root cause of the bugs was that CoQueue would wake up readers even if there were pending writers, and would wake up writers even if there were readers. In that case, the coroutine would go back to sleep *at the end* of the CoQueue, losing its place at the head of the line. To fix this, keep the queue of waiters explicitly in the CoRwlock instead of using CoQueue, and store for each whether it is a potential reader or a writer. This way, downgrade can look at the first queued coroutines and wake it only if it is a reader, causing all other readers in line to be released in turn. Reported-by: David Edmondson Reviewed-by: David Edmondson Signed-off-by: Paolo Bonzini Message-id: 20210325112941.365238-5-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi --- include/qemu/coroutine.h | 17 ++-- util/qemu-coroutine-lock.c | 164 +++++++++++++++++++++++-------------- 2 files changed, 114 insertions(+), 67 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 84eab6e3bf..ce5b9c6851 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -237,11 +237,15 @@ bool qemu_co_enter_next_impl(CoQueue *queue, QemuLock= able *lock); bool qemu_co_queue_empty(CoQueue *queue); =20 =20 +typedef struct CoRwTicket CoRwTicket; typedef struct CoRwlock { - int pending_writer; - int reader; CoMutex mutex; - CoQueue queue; + + /* Number of readers, or -1 if owned for writing. */ + int owners; + + /* Waiting coroutines. */ + QSIMPLEQ_HEAD(, CoRwTicket) tickets; } CoRwlock; =20 /** @@ -260,10 +264,9 @@ void qemu_co_rwlock_rdlock(CoRwlock *lock); /** * Write Locks the CoRwlock from a reader. This is a bit more efficient t= han * @qemu_co_rwlock_unlock followed by a separate @qemu_co_rwlock_wrlock. - * However, if the lock cannot be upgraded immediately, control is transfe= rred - * to the caller of the current coroutine. Also, @qemu_co_rwlock_upgrade - * only overrides CoRwlock fairness if there are no concurrent readers, so - * another writer might run while @qemu_co_rwlock_upgrade blocks. + * Note that if the lock cannot be upgraded immediately, control is transf= erred + * to the caller of the current coroutine; another writer might run while + * @qemu_co_rwlock_upgrade blocks. */ void qemu_co_rwlock_upgrade(CoRwlock *lock); =20 diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index eb73cf11dc..2669403839 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -327,11 +327,51 @@ void coroutine_fn qemu_co_mutex_unlock(CoMutex *mutex) trace_qemu_co_mutex_unlock_return(mutex, self); } =20 +struct CoRwTicket { + bool read; + Coroutine *co; + QSIMPLEQ_ENTRY(CoRwTicket) next; +}; + void qemu_co_rwlock_init(CoRwlock *lock) { - memset(lock, 0, sizeof(*lock)); - qemu_co_queue_init(&lock->queue); qemu_co_mutex_init(&lock->mutex); + lock->owners =3D 0; + QSIMPLEQ_INIT(&lock->tickets); +} + +/* Releases the internal CoMutex. */ +static void qemu_co_rwlock_maybe_wake_one(CoRwlock *lock) +{ + CoRwTicket *tkt =3D QSIMPLEQ_FIRST(&lock->tickets); + Coroutine *co =3D NULL; + + /* + * Setting lock->owners here prevents rdlock and wrlock from + * sneaking in between unlock and wake. + */ + + if (tkt) { + if (tkt->read) { + if (lock->owners >=3D 0) { + lock->owners++; + co =3D tkt->co; + } + } else { + if (lock->owners =3D=3D 0) { + lock->owners =3D -1; + co =3D tkt->co; + } + } + } + + if (co) { + QSIMPLEQ_REMOVE_HEAD(&lock->tickets, next); + qemu_co_mutex_unlock(&lock->mutex); + aio_co_wake(co); + } else { + qemu_co_mutex_unlock(&lock->mutex); + } } =20 void qemu_co_rwlock_rdlock(CoRwlock *lock) @@ -340,84 +380,88 @@ void qemu_co_rwlock_rdlock(CoRwlock *lock) =20 qemu_co_mutex_lock(&lock->mutex); /* For fairness, wait if a writer is in line. */ - while (lock->pending_writer) { - qemu_co_queue_wait(&lock->queue, &lock->mutex); - } - lock->reader++; - qemu_co_mutex_unlock(&lock->mutex); - - /* The rest of the read-side critical section is run without the mutex= . */ - self->locks_held++; -} - -void qemu_co_rwlock_unlock(CoRwlock *lock) -{ - Coroutine *self =3D qemu_coroutine_self(); - - assert(qemu_in_coroutine()); - if (!lock->reader) { - /* The critical section started in qemu_co_rwlock_wrlock. */ - qemu_co_queue_restart_all(&lock->queue); + if (lock->owners =3D=3D 0 || (lock->owners > 0 && QSIMPLEQ_EMPTY(&lock= ->tickets))) { + lock->owners++; + qemu_co_mutex_unlock(&lock->mutex); } else { - self->locks_held--; + CoRwTicket my_ticket =3D { true, self }; =20 + QSIMPLEQ_INSERT_TAIL(&lock->tickets, &my_ticket, next); + qemu_co_mutex_unlock(&lock->mutex); + qemu_coroutine_yield(); + assert(lock->owners >=3D 1); + + /* Possibly wake another reader, which will wake the next in line.= */ qemu_co_mutex_lock(&lock->mutex); - lock->reader--; - assert(lock->reader >=3D 0); - /* Wakeup only one waiting writer */ - if (!lock->reader) { - qemu_co_queue_next(&lock->queue); - } + qemu_co_rwlock_maybe_wake_one(lock); } - qemu_co_mutex_unlock(&lock->mutex); + + self->locks_held++; +} + +void qemu_co_rwlock_unlock(CoRwlock *lock) +{ + Coroutine *self =3D qemu_coroutine_self(); + + assert(qemu_in_coroutine()); + self->locks_held--; + + qemu_co_mutex_lock(&lock->mutex); + if (lock->owners > 0) { + lock->owners--; + } else { + assert(lock->owners =3D=3D -1); + lock->owners =3D 0; + } + + qemu_co_rwlock_maybe_wake_one(lock); } =20 void qemu_co_rwlock_downgrade(CoRwlock *lock) { - Coroutine *self =3D qemu_coroutine_self(); + qemu_co_mutex_lock(&lock->mutex); + assert(lock->owners =3D=3D -1); + lock->owners =3D 1; =20 - /* lock->mutex critical section started in qemu_co_rwlock_wrlock or - * qemu_co_rwlock_upgrade. - */ - assert(lock->reader =3D=3D 0); - lock->reader++; - qemu_co_mutex_unlock(&lock->mutex); - - /* The rest of the read-side critical section is run without the mutex= . */ - self->locks_held++; + /* Possibly wake another reader, which will wake the next in line. */ + qemu_co_rwlock_maybe_wake_one(lock); } =20 void qemu_co_rwlock_wrlock(CoRwlock *lock) { + Coroutine *self =3D qemu_coroutine_self(); + qemu_co_mutex_lock(&lock->mutex); - lock->pending_writer++; - while (lock->reader) { - qemu_co_queue_wait(&lock->queue, &lock->mutex); + if (lock->owners =3D=3D 0) { + lock->owners =3D -1; + qemu_co_mutex_unlock(&lock->mutex); + } else { + CoRwTicket my_ticket =3D { false, qemu_coroutine_self() }; + + QSIMPLEQ_INSERT_TAIL(&lock->tickets, &my_ticket, next); + qemu_co_mutex_unlock(&lock->mutex); + qemu_coroutine_yield(); + assert(lock->owners =3D=3D -1); } - lock->pending_writer--; =20 - /* The rest of the write-side critical section is run with - * the mutex taken, so that lock->reader remains zero. - * There is no need to update self->locks_held. - */ + self->locks_held++; } =20 void qemu_co_rwlock_upgrade(CoRwlock *lock) { - Coroutine *self =3D qemu_coroutine_self(); - qemu_co_mutex_lock(&lock->mutex); - assert(lock->reader > 0); - lock->reader--; - lock->pending_writer++; - while (lock->reader) { - qemu_co_queue_wait(&lock->queue, &lock->mutex); + assert(lock->owners > 0); + /* For fairness, wait if a writer is in line. */ + if (lock->owners =3D=3D 1 && QSIMPLEQ_EMPTY(&lock->tickets)) { + lock->owners =3D -1; + qemu_co_mutex_unlock(&lock->mutex); + } else { + CoRwTicket my_ticket =3D { false, qemu_coroutine_self() }; + + lock->owners--; + QSIMPLEQ_INSERT_TAIL(&lock->tickets, &my_ticket, next); + qemu_co_rwlock_maybe_wake_one(lock); + qemu_coroutine_yield(); + assert(lock->owners =3D=3D -1); } - lock->pending_writer--; - - /* The rest of the write-side critical section is run with - * the mutex taken, similar to qemu_co_rwlock_wrlock. Do - * not account for the lock twice in self->locks_held. - */ - self->locks_held--; } --=20 2.30.2 From nobody Wed May 1 12:19:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1617184597; cv=none; d=zohomail.com; s=zohoarc; b=et8b/K/YRRSXf/j75tDrq4/ciVAehFTNliE472brIigml52YbIvR0p/5m8of/r7J9mg1APefx4GjFNTAbkm344uqvZhAdP3UilBAMUrO0EZ14WTHSSCPStRB3kox6BAHDZTZaQ3iHPOdSEDe50H7kGx8B/gw0a0Q63UR2zz+E8s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617184597; h=Content-Type: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=LJZOOwzx6xVn2ZqNoqqfvd5WL9r43uckU8MubkJTNy8=; b=VZvvQORj1d5tr0+jnPcE6avZ84w36R+IOMZSfRHf3htABBOj7ykgDwSi9Vo08USeR+ZfzP0Pr4ZJ/Ck+kIOvfv6Nc5Z8+sWMosPCnv4Y1FWny/zGS+UOzoRaItihdyAdq+9r/PevPDGM93iQ4stw8eRVe1wSjrZCBQIJWB9ZwKU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=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 1617184597228110.13048248792381; Wed, 31 Mar 2021 02:56:37 -0700 (PDT) Received: from localhost ([::1]:36612 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRXaV-0004X2-Ry for importer@patchew.org; Wed, 31 Mar 2021 05:56:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51606) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVo-0008Qd-3R for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:32853) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVl-0001Jf-QF for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:43 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-161-ZhYSU6USOx2cdo1lA5BVow-1; Wed, 31 Mar 2021 05:51:38 -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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 59D4B8030B5; Wed, 31 Mar 2021 09:51:37 +0000 (UTC) Received: from localhost (ovpn-115-85.ams2.redhat.com [10.36.115.85]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1750C6F991; Wed, 31 Mar 2021 09:51:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617184301; 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=LJZOOwzx6xVn2ZqNoqqfvd5WL9r43uckU8MubkJTNy8=; b=T5Ycye9O4vnBwryu+v50nR86Me/M3hx18QfheITC79PFhN81UB4aJAYjPwcTKm4EtkaMx5 epelRKFT2C3ozNyjPcUcZ2YoOt/Hip8FnH3HwoiSZIjVlny9ZlpiLy9HHpcQ9GGAhvJ9eH KdbnE4AG3xWFQL9C1JFyszwY/uDyEvw= X-MC-Unique: ZhYSU6USOx2cdo1lA5BVow-1 From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL for-6.0 5/6] test-coroutine: Add rwlock upgrade test Date: Wed, 31 Mar 2021 10:50:58 +0100 Message-Id: <20210331095059.303996-6-stefanha@redhat.com> In-Reply-To: <20210331095059.303996-1-stefanha@redhat.com> References: <20210331095059.303996-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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=216.205.24.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.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, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org, Juan Quintela , Stefan Weil , "Michael S. Tsirkin" , "Dr. David Alan Gilbert" , Max Reitz , John Snow , Stefan Hajnoczi , Paolo Bonzini , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini Test that rwlock upgrade is fair, and that readers go back to sleep if a writer is in line. Signed-off-by: Paolo Bonzini Message-id: 20210325112941.365238-6-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi --- tests/unit/test-coroutine.c | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/unit/test-coroutine.c b/tests/unit/test-coroutine.c index e946d93a65..6e6f51d480 100644 --- a/tests/unit/test-coroutine.c +++ b/tests/unit/test-coroutine.c @@ -264,6 +264,67 @@ static void test_co_mutex_lockable(void) g_assert(QEMU_MAKE_LOCKABLE(null_pointer) =3D=3D NULL); } =20 +static CoRwlock rwlock; + +/* Test that readers are properly sent back to the queue when upgrading, + * even if they are the sole readers. The test scenario is as follows: + * + * + * | c1 | c2 | + * |--------------+------------+ + * | rdlock | | + * | yield | | + * | | wrlock | + * | | | + * | upgrade | | + * | | | + * | | unlock | + * | | | + * | unlock | | + */ + +static void coroutine_fn rwlock_yield_upgrade(void *opaque) +{ + qemu_co_rwlock_rdlock(&rwlock); + qemu_coroutine_yield(); + + qemu_co_rwlock_upgrade(&rwlock); + qemu_co_rwlock_unlock(&rwlock); + + *(bool *)opaque =3D true; +} + +static void coroutine_fn rwlock_wrlock_yield(void *opaque) +{ + qemu_co_rwlock_wrlock(&rwlock); + qemu_coroutine_yield(); + + qemu_co_rwlock_unlock(&rwlock); + *(bool *)opaque =3D true; +} + +static void test_co_rwlock_upgrade(void) +{ + bool c1_done =3D false; + bool c2_done =3D false; + Coroutine *c1, *c2; + + qemu_co_rwlock_init(&rwlock); + c1 =3D qemu_coroutine_create(rwlock_yield_upgrade, &c1_done); + c2 =3D qemu_coroutine_create(rwlock_wrlock_yield, &c2_done); + + qemu_coroutine_enter(c1); + qemu_coroutine_enter(c2); + + /* c1 now should go to sleep. */ + qemu_coroutine_enter(c1); + g_assert(!c1_done); + + qemu_coroutine_enter(c2); + g_assert(c1_done); + g_assert(c2_done); +} + /* * Check that creation, enter, and return work */ @@ -501,6 +562,7 @@ int main(int argc, char **argv) g_test_add_func("/basic/order", test_order); g_test_add_func("/locking/co-mutex", test_co_mutex); g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable); + g_test_add_func("/locking/co-rwlock/upgrade", test_co_rwlock_upgrade); if (g_test_perf()) { g_test_add_func("/perf/lifecycle", perf_lifecycle); g_test_add_func("/perf/nesting", perf_nesting); --=20 2.30.2 From nobody Wed May 1 12:19:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1617184761; cv=none; d=zohomail.com; s=zohoarc; b=fz0ux/RK2gKRR1bPVQIQuDNWZyaA8BjX4vzXiazXzW2s9BL0Kqu+nw/miQdN4/7USp5ouXQdL+32Tk+t6/4ON0dnlXytFlgP55ESQCRqnCoDBj2cB+KKHr2TA20vvUSP9ZItnlr3NeVZQdlm1/IC9/gCaBgnYTTCNPDeDvZkQZw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617184761; h=Content-Type: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=wckQYKVlGLePyac3Ud9XANvFrgZCkQVjzf9UPTUYLHk=; b=BCzKHWoJU5ElwZilQzSP6rZWHzREUFD5SLqo+NOOFxWOTQxXqnAcJGX4H+rItbWTAn+YgxcmOOMQxjVyxujqucKi52y3CDHX0hFIPLZETfv1BjeDH7eOYmou/TZAyDELUG+XSL8HNvHJv8Yn8MsABQJdpAjFnQJM2LwQ0zD1Bhc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=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 161718476196155.069633637013794; Wed, 31 Mar 2021 02:59:21 -0700 (PDT) Received: from localhost ([::1]:45410 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRXdA-00089l-TJ for importer@patchew.org; Wed, 31 Mar 2021 05:59:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51708) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVw-0008Tg-0f for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:52 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:27588) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRXVu-0001Ot-7f for qemu-devel@nongnu.org; Wed, 31 Mar 2021 05:51:51 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-104-WdlnYraKMISURIwIxb2UXg-1; Wed, 31 Mar 2021 05:51:47 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 07BF98018AC; Wed, 31 Mar 2021 09:51:46 +0000 (UTC) Received: from localhost (ovpn-115-85.ams2.redhat.com [10.36.115.85]) by smtp.corp.redhat.com (Postfix) with ESMTP id A254B4F3C6; Wed, 31 Mar 2021 09:51:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617184309; 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=wckQYKVlGLePyac3Ud9XANvFrgZCkQVjzf9UPTUYLHk=; b=BSyY8CLJQZXhJgcpENkgvtx62EzGMstiqVOnXMT0bNfUrPSPqPAas96qMmXdZd3qDfZzGI 7k2cFADLELJ34fO4KR9qzq4uqvuZ2nUYUIdfEqnupjhE0hoFAZT9N8sFLnPV0xzkmvApmr YXcsUHk1e6MZyiF9NIWmpZXHG6y6Kw4= X-MC-Unique: WdlnYraKMISURIwIxb2UXg-1 From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL for-6.0 6/6] test-coroutine: Add rwlock downgrade test Date: Wed, 31 Mar 2021 10:50:59 +0100 Message-Id: <20210331095059.303996-7-stefanha@redhat.com> In-Reply-To: <20210331095059.303996-1-stefanha@redhat.com> References: <20210331095059.303996-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.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, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org, Juan Quintela , David Edmondson , Stefan Weil , "Michael S. Tsirkin" , "Dr. David Alan Gilbert" , Max Reitz , John Snow , Stefan Hajnoczi , Paolo Bonzini , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: David Edmondson Test that downgrading an rwlock does not result in a failure to schedule coroutines queued on the rwlock. The diagram associated with test_co_rwlock_downgrade() describes the intended behaviour, but what was observed previously corresponds to: | c1 | c2 | c3 | c4 | |--------+------------+------------+----------| | rdlock | | | | | yield | | | | | | wrlock | | | | | | | | | | | rdlock | | | | | | | | | | | wrlock | | | | | | | unlock | | | | | yield | | | | | | | | | | | downgrade | | | | | ... | | | | | unlock | | | | | | | | | | | | | This results in a failure... ERROR:../tests/test-coroutine.c:369:test_co_rwlock_downgrade: assertion fai= led: (c3_done) Bail out! ERROR:../tests/test-coroutine.c:369:test_co_rwlock_downgrade: ass= ertion failed: (c3_done) ...as a result of the c3 coroutine failing to run to completion. Signed-off-by: David Edmondson Signed-off-by: Paolo Bonzini Message-id: 20210325112941.365238-7-pbonzini@redhat.com Message-Id: <20210309144015.557477-5-david.edmondson@oracle.com> Signed-off-by: Paolo Bonzini Signed-off-by: Stefan Hajnoczi --- tests/unit/test-coroutine.c | 99 +++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/tests/unit/test-coroutine.c b/tests/unit/test-coroutine.c index 6e6f51d480..aa77a3bcb3 100644 --- a/tests/unit/test-coroutine.c +++ b/tests/unit/test-coroutine.c @@ -325,6 +325,104 @@ static void test_co_rwlock_upgrade(void) g_assert(c2_done); } =20 +static void coroutine_fn rwlock_rdlock_yield(void *opaque) +{ + qemu_co_rwlock_rdlock(&rwlock); + qemu_coroutine_yield(); + + qemu_co_rwlock_unlock(&rwlock); + qemu_coroutine_yield(); + + *(bool *)opaque =3D true; +} + +static void coroutine_fn rwlock_wrlock_downgrade(void *opaque) +{ + qemu_co_rwlock_wrlock(&rwlock); + + qemu_co_rwlock_downgrade(&rwlock); + qemu_co_rwlock_unlock(&rwlock); + *(bool *)opaque =3D true; +} + +static void coroutine_fn rwlock_rdlock(void *opaque) +{ + qemu_co_rwlock_rdlock(&rwlock); + + qemu_co_rwlock_unlock(&rwlock); + *(bool *)opaque =3D true; +} + +static void coroutine_fn rwlock_wrlock(void *opaque) +{ + qemu_co_rwlock_wrlock(&rwlock); + + qemu_co_rwlock_unlock(&rwlock); + *(bool *)opaque =3D true; +} + +/* + * Check that downgrading a reader-writer lock does not cause a hang. + * + * Four coroutines are used to produce a situation where there are + * both reader and writer hopefuls waiting to acquire an rwlock that + * is held by a reader. + * + * The correct sequence of operations we aim to provoke can be + * represented as: + * + * | c1 | c2 | c3 | c4 | + * |--------+------------+------------+------------| + * | rdlock | | | | + * | yield | | | | + * | | wrlock | | | + * | | | | | + * | | | rdlock | | + * | | | | | + * | | | | wrlock | + * | | | | | + * | unlock | | | | + * | yield | | | | + * | | | | | + * | | downgrade | | | + * | | | | | + * | | | unlock | | + * | | ... | | | + * | | unlock | | | + * | | | | | + * | | | | unlock | + */ +static void test_co_rwlock_downgrade(void) +{ + bool c1_done =3D false; + bool c2_done =3D false; + bool c3_done =3D false; + bool c4_done =3D false; + Coroutine *c1, *c2, *c3, *c4; + + qemu_co_rwlock_init(&rwlock); + + c1 =3D qemu_coroutine_create(rwlock_rdlock_yield, &c1_done); + c2 =3D qemu_coroutine_create(rwlock_wrlock_downgrade, &c2_done); + c3 =3D qemu_coroutine_create(rwlock_rdlock, &c3_done); + c4 =3D qemu_coroutine_create(rwlock_wrlock, &c4_done); + + qemu_coroutine_enter(c1); + qemu_coroutine_enter(c2); + qemu_coroutine_enter(c3); + qemu_coroutine_enter(c4); + + qemu_coroutine_enter(c1); + + g_assert(c2_done); + g_assert(c3_done); + g_assert(c4_done); + + qemu_coroutine_enter(c1); + + g_assert(c1_done); +} + /* * Check that creation, enter, and return work */ @@ -563,6 +661,7 @@ int main(int argc, char **argv) g_test_add_func("/locking/co-mutex", test_co_mutex); g_test_add_func("/locking/co-mutex/lockable", test_co_mutex_lockable); g_test_add_func("/locking/co-rwlock/upgrade", test_co_rwlock_upgrade); + g_test_add_func("/locking/co-rwlock/downgrade", test_co_rwlock_downgra= de); if (g_test_perf()) { g_test_add_func("/perf/lifecycle", perf_lifecycle); g_test_add_func("/perf/nesting", perf_nesting); --=20 2.30.2