From nobody Wed Nov 19 12:23:31 2025 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=1616004560; cv=none; d=zohomail.com; s=zohoarc; b=C4uNVH2vTUAjOYR9Nlp6XTJix0gPGbunTFE6FV+ansJyn6nAbWeD1TXP7qjzQiW5dPH7OsXNIGwk+7CBtlt/zOT9dXSzzVr1Vjqig+qEri1ndwmyXCIQAn/g0aFvazbWd9jVYs4+zwXU+ALDBLAaIaS8X3UwvAgJk6rLuuV42ng= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616004560; 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=uioFA7kSJG6dSbTCJnInaEvZmY5RBbbDHDyMmMYCNBw=; b=FjgbR27rgzeltaz9WopoqC7WKEk3pK8TtGtDm2VNBprzZdQc3CQQlJrmZkUrmxNZoA18L0U+N0SWaSaFrk+ogZ0PQm1uoBK2NFPhzsCf6Sm8P8/bfy1mRqwKRvJ7Jo4ZwW6pfKjg+skG7DpR3Eaj080T5KDxe+EyyFKX+JcvYZ8= 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 1616004560108665.7880117050063; Wed, 17 Mar 2021 11:09:20 -0700 (PDT) Received: from localhost ([::1]:50064 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMabf-0006u0-10 for importer@patchew.org; Wed, 17 Mar 2021 14:09:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57254) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMaT3-0000lC-Cm; Wed, 17 Mar 2021 14:00:25 -0400 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]:36568) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMaT1-0004wY-Mz; Wed, 17 Mar 2021 14:00:25 -0400 Received: by mail-ed1-x536.google.com with SMTP id o19so3407900edc.3; Wed, 17 Mar 2021 11:00:22 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id b22sm13159303edv.96.2021.03.17.11.00.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 11:00:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uioFA7kSJG6dSbTCJnInaEvZmY5RBbbDHDyMmMYCNBw=; b=hocPyPL1xkkXkBS8N4dCOHss+UlN13f7SqudaGne3iLWJvtP/MaN5C6y/rIQcIaOop 4ZAAtUisxsFAEM7BKQHJQo2sYDryWz4G3bM4upejpq4fF4toWtFy/RjBmInw4x1jzLTN 8iOXB2z/Qk+AJQf4HjolVtDTqY2q9z5fylIdM8hkEG7hnFgXtQPqV4A8oHKO61pVZacl rUU5L1FihTknbRq8POH6mu1Mtum2/KF2teoTcvAO4dyGhVSDJfufE/gPpJDYPKtcRFsh kzCIe5H3KDV20b0vM63TvYCWXuZKUF5/Bf0fBURYvJVIsyE5EwZLh8KD+MBZoOATwlYe gATQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=uioFA7kSJG6dSbTCJnInaEvZmY5RBbbDHDyMmMYCNBw=; b=U4ExzF9sBYxxrv8GS9PB75OUr+fLV7iKPlmXeysIwIEMUegK2UzKu/tB1JZ2DS0nys dSbfuJluK1xws7GCs4bQhL58KsmyIGOV3u30ZQ70aH8jNS9td84zj4GYW8YgERN3FOKl rtJox7V/L5yXKOgfauX1XNs50TPpM86bXwwfpf4gRJJTx1BkI+SGd6A0JMFpYll+5/gI ImvdJnDW0+Ix4WK61Siu8B/kpcNt4q2vMYaS1dABax9XZQ24jVsVn7X3zG25G1v7qBwH F1Ko/JS4uCUEDxznL7aOVSdxzW29vkwDMfedcvnKqBJ4gcRHHA3WoVwZWXyrcVMV4GeB NACw== X-Gm-Message-State: AOAM531Ev28dUetuDCwtTsxw7suMu35an25mtt+2S68SC9Q45bvgkHbZ SWDw/atwAykA5MopRCMLCmtEvNg7U8E= X-Google-Smtp-Source: ABdhPJxk9KhSVxE2bwFRuY54toyoFPcMdAojo2+p7B7nzXqnq2f8PiF7Typj0369QR4V9k69EYR9dA== X-Received: by 2002:a05:6402:2215:: with SMTP id cq21mr44600686edb.281.1616004016223; Wed, 17 Mar 2021 11:00:16 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 1/6] block/vdi: When writing new bmap entry fails, don't leak the buffer Date: Wed, 17 Mar 2021 19:00:08 +0100 Message-Id: <20210317180013.235231-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210317180013.235231-1-pbonzini@redhat.com> References: <20210317180013.235231-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2a00:1450:4864:20::536; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x536.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: david.edmondson@oracle.com, kwolf@redhat.com, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) 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 Message-Id: <20210309144015.557477-2-david.edmondson@oracle.com> Signed-off-by: Paolo Bonzini Reviewed-by: Max Reitz --- 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.29.2 From nobody Wed Nov 19 12:23:31 2025 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=1616004547; cv=none; d=zohomail.com; s=zohoarc; b=YJtgVmMYHjdauqMoXvqtfi3chrdED4I008HIupkDeolMqehvw4a9hUGxy1UkpiAcsl2N0/SgCdT480434YW1P7/ddiRgvmLn7OPv+F7enqS8w0N21psbnfkVLkfcEInKH1Q0HU0kvzH3rj55lq6VoDTx2L7r3Q3wVHnfxPnH578= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616004547; 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=sB3+w5MPiq5YtSsSHBffd76Yw89YjLssV56LPOeeU7o=; b=Q4rDTorgxDVJtSkSM6b+UBaOPftKA5u106cBzjRZ8Jl3SSUxUuV+g5VS+TtwHW87W7Pdw4OKj7iuhTbzJaScI76Inc0hSUl9XdwiHo/wqgC+QOhNcAzV1z9tEkMOUf7gqOqO6rd9v0eibdeHNLK4OivOm6098ckYijMnn9fTgUY= 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 1616004547053898.4268779178338; Wed, 17 Mar 2021 11:09:07 -0700 (PDT) Received: from localhost ([::1]:49106 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMabP-0006WC-CM for importer@patchew.org; Wed, 17 Mar 2021 14:09:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57248) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMaT3-0000kn-2X; Wed, 17 Mar 2021 14:00:25 -0400 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]:39451) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMaT1-0004wV-Hx; Wed, 17 Mar 2021 14:00:24 -0400 Received: by mail-ed1-x52a.google.com with SMTP id bf3so3397900edb.6; Wed, 17 Mar 2021 11:00:22 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id b22sm13159303edv.96.2021.03.17.11.00.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 11:00:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sB3+w5MPiq5YtSsSHBffd76Yw89YjLssV56LPOeeU7o=; b=JqxyAjNVsYWG8FD4nMDS2cjcip2DkQqlSsnCwTT1Wvxp0/DkaeI6Lfxiqd/oDEYxD1 i2x1hJTMR6kRWtofzH3hzgNSKXh6l/ogJgrP56Vwg3E75BNNSWNR6kxAjkpmGt3KQm++ DV4YS3FMwxm7k3QU6jTyNmI1V7asmL5KFHlkDXtU5erHBBEuJW9QdNx46Eyv11RFWpm3 lpe9XqNby+JL/f3oqtr4TU+Njo3WpMZzHGuttExc4l37jj3mbcr7vAm/rPth3Afp+295 Awpvg6X4eoQV/RNLK1JEFuH5JRpL+4eYNHNngJb4qyqoodOQflqgCoUHzdvspxFdypIz 8d/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=sB3+w5MPiq5YtSsSHBffd76Yw89YjLssV56LPOeeU7o=; b=pD27NPxv5vj/yRdy4VkkttgK2FozAhJh0T8VRW/zmMKMO1sbUTCWMr34TlyrJIw0rT mF964HtboHTFyaj1yL6guPTpu2oV+aGCzxdUUlgXQqSGv7zIme/njqp1BX2HXtpGlGKh WWvscbWVnuMoyrfuprX9U/BbJgUzAwXAANZviVsJ197Pwxmhdikk5Y+y3qL7eWEE2sYD /6Ub3o8S8gDtQj4Q2dwURljz56s7CpNsuWzFf78z9QWrjuDBXaQKAfsdVt1rAPvpBBXr fZdG5CKHPP/3ZkyDC3VesADsdNFCMC9IRZ4eu5ZYpeBr8V8ZLqkGjOp5KuHDVg8WAKdI pX/w== X-Gm-Message-State: AOAM532mG3IgkvUzK9o00xaGrzTnT9in6TFltXIMrgPXuiA2iQOyR2Uv XyN5xbZUEb1WuJhF7owXaaswSmx1Vyc= X-Google-Smtp-Source: ABdhPJwwtSAo5O4V66GDOrFLhHKQPgFAbNT6aSCZ622wdPQE3w2MkoURb6fGb7Shj8tmnBjxZ+80rA== X-Received: by 2002:aa7:dd4d:: with SMTP id o13mr10551709edw.53.1616004016916; Wed, 17 Mar 2021 11:00:16 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 2/6] block/vdi: Don't assume that blocks are larger than VdiHeader Date: Wed, 17 Mar 2021 19:00:09 +0100 Message-Id: <20210317180013.235231-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210317180013.235231-1-pbonzini@redhat.com> References: <20210317180013.235231-1-pbonzini@redhat.com> MIME-Version: 1.0 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=2a00:1450:4864:20::52a; envelope-from=paolo.bonzini@gmail.com; helo=mail-ed1-x52a.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: david.edmondson@oracle.com, kwolf@redhat.com, qemu-block@nongnu.org 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 Message-Id: <20210309144015.557477-3-david.edmondson@oracle.com> Signed-off-by: Paolo Bonzini Reviewed-by: Max Reitz --- 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.29.2 From nobody Wed Nov 19 12:23:31 2025 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=1616004230; cv=none; d=zohomail.com; s=zohoarc; b=hM+oPz3ONeHgBaNZgLTL+VtBHGkghBuqPPUS4mW+36SRPN/KU89H6V+ZrwHNclNOZStwQRXwM298efxwOCiBLuoJesELo23qCfKPACem7yVDzkr7/VCPKihlw8291I8PL3DC3brUHCUrIDWNvUm32mS2hRmxPrjvJSdUfxHBbxA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616004230; 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=Xgtdm62nkB77KDEB509RPm6RJ/Eax7jgJ8G9QzqE+DA=; b=HuxCdlLuBhYKa8bxx+LItB1dNwwiX+zZY0Z/zNXREsq1ux9SzNGfhvRJGPlKJQjcUKdSPQ1O4nxSW80EWOlXgg2/fW7whR0dcBHFknO7lL7rfSC0YJwcf9plLS9dYXrJVre9a6Nz/u251McdASHnldaaBat+t89Ms/6BNxNCNJU= 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 1616004230310919.3474708167154; Wed, 17 Mar 2021 11:03:50 -0700 (PDT) Received: from localhost ([::1]:40052 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMaWK-0002ay-Ub for importer@patchew.org; Wed, 17 Mar 2021 14:03:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57224) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMaT1-0000i9-6p; Wed, 17 Mar 2021 14:00:23 -0400 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]:33247) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMaSy-0004t5-Bi; Wed, 17 Mar 2021 14:00:22 -0400 Received: by mail-ej1-x633.google.com with SMTP id jt13so4109411ejb.0; Wed, 17 Mar 2021 11:00:19 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id b22sm13159303edv.96.2021.03.17.11.00.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 11:00:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Xgtdm62nkB77KDEB509RPm6RJ/Eax7jgJ8G9QzqE+DA=; b=d3B9zFF+hWfCt4GneGijwGdrShTr31SHNOXO72x/Tc9xOfjyslHitVe08SkrTPj0kd vnKoneF6cL+CmdMEESxn+P14kLPP5xI9hzW06MCp2Apj9b1xA2gLP7EebiRpAEuqOeFt CQ1/j7g8SdXm+FZvghYa5s7ftsz0aA7u6VuPRsCF2KxPJjQibcvk1H/hS548yBM5k1br hE8c7LIS82lf4AXvNq7utYyXPwhKjvtxUizVO2/9mzF1+B6nPHiY0AVoL3NXBT7fudgG 47RPvlgs2ubYU7010f2mzUvoLY39Mfat6Yed3IdBgMQwj+GJmuEreN7+EJLYyx3mx8V/ R5qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Xgtdm62nkB77KDEB509RPm6RJ/Eax7jgJ8G9QzqE+DA=; b=iFrgxDGgj57AjHoGqlris48ogFqZEuBa7ZMlbbq80RavEfBqTQpZwESk287NyWmGVX jNno+2hEwRm5wctpX4jF5J7hzZ+urqBvk9CRsfAMcNVu6uRtax3SPjIMypglKCLyXtKj AtW+dDRf8CMlVJh7df/4eKL6pFzk4a9S4MkjdwEbDLJ+KA1f/FoEots+y4V6hqN/rR22 rfnq8WYi3cX5qtL7F65r2mOoPREdlFHlRFv/bOwy+3AoGbOtPr1OHuUwvnHhRDHcsOsr zAYy+CvOoNgunPBxLe6bRTmmMwl+2+l2pB5i+fAMDkhy1pnHW0CTBh2w+MRH1xBVbwx7 sQIg== X-Gm-Message-State: AOAM531ENJT1vr3jLaAodV4UdP1ka0DtsKjdBGZAnRYSIhLcXwQ9RjmK XYDAnvehtoDPhQUJpQcC1yLJh8Ho7Ec= X-Google-Smtp-Source: ABdhPJyqt91cdCwgjAI1Gp7zDnee2nASdwo/DCaZw5dZfp1J8gOxbzY4NSetKVYyw2+IPE8omrpKDg== X-Received: by 2002:a17:906:5902:: with SMTP id h2mr36594916ejq.416.1616004017891; Wed, 17 Mar 2021 11:00:17 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 3/6] coroutine/mutex: Store the coroutine in the CoWaitRecord only once Date: Wed, 17 Mar 2021 19:00:10 +0100 Message-Id: <20210317180013.235231-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210317180013.235231-1-pbonzini@redhat.com> References: <20210317180013.235231-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2a00:1450:4864:20::633; envelope-from=paolo.bonzini@gmail.com; helo=mail-ej1-x633.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: david.edmondson@oracle.com, kwolf@redhat.com, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) 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 Message-Id: <20210309144015.557477-4-david.edmondson@oracle.com> Signed-off-by: Paolo Bonzini --- 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.29.2 From nobody Wed Nov 19 12:23:31 2025 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=1616005107; cv=none; d=zohomail.com; s=zohoarc; b=kM4p1aryids2/Yl2CGVJUpjSjO5tS2Ac3V2F0LKdUJXKC1UKO0ufBKS69AwPyhbEj9HvQ0NsHQ1iiRznTdkeZ5WfhetflmfrZYZQvU3xPbOgmGmaHMO2qBVAl6I5jrnq8120X3QgZneTOjDyASaw/9WBy7HKDlj41m6OHtNfrV8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616005107; 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=YblhuIkh5xNVfWBJe+b/yvre1lCOLi6tB8Zxn3pXVkg=; b=M3ma+q2vMqFxTNTfdNw8ZYleeoDlxZKpuwp2i8crKrRPIHjvfdSlgYzg9l6/a+plDQhyNOc5yf3pkSfoLiV2Uuj4YQ+Ne7LDBhmaQl8TEujJfYAKRxCJLgyM66sV87bmVqfvIpPvXLttT6MCFkTzI/f1AUMDg5Z0kTOhFHr/mMQ= 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 1616005107897714.789927858189; Wed, 17 Mar 2021 11:18:27 -0700 (PDT) Received: from localhost ([::1]:35052 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMakT-0004Ca-Bx for importer@patchew.org; Wed, 17 Mar 2021 14:18:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57272) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMaT5-0000nG-2A; Wed, 17 Mar 2021 14:00:31 -0400 Received: from mail-ej1-x62e.google.com ([2a00:1450:4864:20::62e]:39078) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMaT3-0004yx-7c; Wed, 17 Mar 2021 14:00:26 -0400 Received: by mail-ej1-x62e.google.com with SMTP id p7so4082828eju.6; Wed, 17 Mar 2021 11:00:24 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id b22sm13159303edv.96.2021.03.17.11.00.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 11:00:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YblhuIkh5xNVfWBJe+b/yvre1lCOLi6tB8Zxn3pXVkg=; b=THsbuA/PVy4DkRAbtb35HSaXihqRGgyfOVfgJ8PPV4PD9RU+DWDAgB3cxqxnCfdI5V xmy4Um4urSekTeogpBaBHUh3DIPc7OQoHwTFLLjlX2d67OWvgXPDqZlzVsX2YKQLuwin uWIEDnmubWMWWZjIq8WQqVOIhk8dCSH6NTWWdi2biKqF4TBlbXgKM2BlX9IEfrxRjYcZ pf6vNW2Wy4Tqdr5XExyS6XsB+J3/sYfo19FOymxKvI2pQlfpO9P3pYQmktB0uqpbq8CE gBDqhj8ED36vss3721yYoRKrhS01aU91ZGvDHOX5TEDuPGRV6i2FmMjAE+g/cpP7tGTI VNqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=YblhuIkh5xNVfWBJe+b/yvre1lCOLi6tB8Zxn3pXVkg=; b=JiPAZjkiXVo5A8WHhojENmVyiqOlNeIfbg2iWH8v2TYYjY9Hh0hYm479PnAHTuC3ZM aEOzjcnlAGfD6i6To2Uy/bNDjw9ferhaxYhAzOqdq2AOHeIJYOk1UuX2t2nFcEGLKZOb Nn9DLzMgV8R2ztx7FZP8QPio0PI9aEdn2yki3a4XaQF12vewaVflrQkTfAYjcHJsTXbs A4Bsvi/MO3zdgU4ObpsUWDV5curNBL0puYGK8XaNrM+mg4z3u85ng3mCSifzhoAKctuz 6KEabrYh7HpxFuC1u4HL+niN+sRk/y+3eeK/42E8mYbVnhYv0YlByLcPr/kPXiDylrtm rnxQ== X-Gm-Message-State: AOAM530LlHQd8QQaOIOcyEOWhPlqrEeXwCRU9ttuyXtlgMWgHcdBZCwf A2Y1pF6GmVkPZzr2g9DI5EflJEmMfzU= X-Google-Smtp-Source: ABdhPJxtgIVrvbIC/KkmIiPOAi0F9JQ5th1AVLEwCbhJNKEYT5xqdtPxVldt9Ou2gzv1WcMIm3A1zg== X-Received: by 2002:a17:906:5d06:: with SMTP id g6mr36392569ejt.216.1616004018721; Wed, 17 Mar 2021 11:00:18 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 4/6] coroutine-lock: reimplement CoRwlock to fix downgrade bug Date: Wed, 17 Mar 2021 19:00:11 +0100 Message-Id: <20210317180013.235231-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210317180013.235231-1-pbonzini@redhat.com> References: <20210317180013.235231-1-pbonzini@redhat.com> MIME-Version: 1.0 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=2a00:1450:4864:20::62e; envelope-from=paolo.bonzini@gmail.com; helo=mail-ej1-x62e.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: david.edmondson@oracle.com, kwolf@redhat.com, qemu-block@nongnu.org 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" 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 --- v3->v4: clean up the code and fix upgrade logic. Fix upgrade comment too. include/qemu/coroutine.h | 17 +++-- util/qemu-coroutine-lock.c | 148 ++++++++++++++++++++++++------------- 2 files changed, 106 insertions(+), 59 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 84eab6e3bf..7919d3bb62 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,13 +380,22 @@ 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); + if (lock->owners =3D=3D 0 || (lock->owners > 0 && QSIMPLEQ_EMPTY(&lock= ->tickets))) { + lock->owners++; + qemu_co_mutex_unlock(&lock->mutex); + } else { + CoRwTicket my_ticket =3D { true, self }; + + 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); + qemu_co_rwlock_maybe_wake_one(lock); } - lock->reader++; - qemu_co_mutex_unlock(&lock->mutex); =20 - /* The rest of the read-side critical section is run without the mutex= . */ self->locks_held++; } =20 @@ -355,69 +404,64 @@ void qemu_co_rwlock_unlock(CoRwlock *lock) Coroutine *self =3D qemu_coroutine_self(); =20 assert(qemu_in_coroutine()); - if (!lock->reader) { - /* The critical section started in qemu_co_rwlock_wrlock. */ - qemu_co_queue_restart_all(&lock->queue); - } else { - self->locks_held--; + self->locks_held--; =20 - 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_mutex_lock(&lock->mutex); + if (lock->owners > 0) { + lock->owners--; + } else { + assert(lock->owners =3D=3D -1); + lock->owners =3D 0; } - qemu_co_mutex_unlock(&lock->mutex); + + qemu_co_rwlock_maybe_wake_one(lock); } =20 void qemu_co_rwlock_downgrade(CoRwlock *lock) { - Coroutine *self =3D qemu_coroutine_self(); - - /* 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); + qemu_co_mutex_lock(&lock->mutex); + assert(lock->owners =3D=3D -1); + lock->owners =3D 1; =20 - /* 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); - } - lock->pending_writer--; + 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() }; =20 - /* 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--; + 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); + } } --=20 2.29.2 From nobody Wed Nov 19 12:23:31 2025 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=1616004238; cv=none; d=zohomail.com; s=zohoarc; b=ZfVffeIyIP/kEqIvW94KZlw4pL5oWCizcBm2YNNsilcRt3d9FnBKjixQ7F0x2rpH+GMI1TX+OvSFSJtjhJ/AyA0VDscFF/aocManJKkgslgt6P8/6DQtjRS+x5quHmc2YHdRACRlq6GIjDTYgHOU1Y1jbBk1Rq34Q/lobbdS2wc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616004238; 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=Xvvv2l70qQm9QCjFzsbGDAhjoM/FYrf9gqA1A6uPvNg=; b=i6zQgxEp/QY6nrPcg3BBi8HLibiHd6vwVIIOJB+W6uA0n387RJFlvu2z+fJMqigNqDI/ETFjyizwoJgnMK536FTnMJkXDz906jwW0vaOSxcYoziOmP1xylSq8ywTvJW1ChsEC95r+o++r8qgWIkDLFi9z8WQ9ZuUruEG/UagjuI= 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 1616004238499966.020799725659; Wed, 17 Mar 2021 11:03:58 -0700 (PDT) Received: from localhost ([::1]:40820 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMaWT-0002uR-5I for importer@patchew.org; Wed, 17 Mar 2021 14:03:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57228) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMaT1-0000j0-NV; Wed, 17 Mar 2021 14:00:23 -0400 Received: from mail-ej1-x62e.google.com ([2a00:1450:4864:20::62e]:39077) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMaT0-0004v0-39; Wed, 17 Mar 2021 14:00:23 -0400 Received: by mail-ej1-x62e.google.com with SMTP id p7so4082536eju.6; Wed, 17 Mar 2021 11:00:21 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id b22sm13159303edv.96.2021.03.17.11.00.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 11:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Xvvv2l70qQm9QCjFzsbGDAhjoM/FYrf9gqA1A6uPvNg=; b=KmIHz17quR+1SQWwuT/wJMbL1h0kpAqHKZqzpqyeKXxZIEodn+fjro+iQtUCjUTzXy WjkjFp8S70J7m5wp/v8f2jN2ua726gkUhAejUwPKka8JeSoa4WS7R+rP+A7Xu0DOcfT9 kQ11uILxYmmltmvBEK4ijwySPwWEOvWiG5CVkHXloESc250lyLG82WC0k2a2VyemkMQe sqzmS6ZBOfPPKv3b7xGsXsdS+bu6vsAr8ZU1PIf9XaFjPnK1m8/z01kwM3ktb/mP2btk Yx+Uls471Agr5aqIAJXplSPmDDFEYNFCn9ZxPDKDzuFX5bMcbh8JfW49NuMcpg2qwYsW 12VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Xvvv2l70qQm9QCjFzsbGDAhjoM/FYrf9gqA1A6uPvNg=; b=IMk+5vhI1HxuM+sHn0QQzLBQJgABlI5e7Ca1xDHRKhR0k08xoLVBvL8TnwQDqheypK JMqVktfs0vXzxbVX4+lvQ4byhgsU0SweUFzjf+jGBxhagDaiu4pa3pcnDgfenA9rCJH0 CSi485EmdHnfbKXyPvhRJSsV/xtM0v8x+xTuijuJPWWkSu/foq1xCu4AdYgAx5pR1xBU uh1G+uyjMdGD3lD2MkrN88gaP5LsGslf3ZXYcWQNgwdE2tgbhPH2hlwW1ir63VS7/edk kGG8jQx4me6JlHiuvNcJGUSLJbmDs3nXmLBxC9NEYYtFQv0lsGEdcU2A+euQ+49brIgD QJzw== X-Gm-Message-State: AOAM531a4PvfawJ0orOCpmKSjy15gilHVe2omTTFrLs8xSOKJLjD1Vi2 cUtrVhQPQpGb8Dfav/HNJe3p1oVILGI= X-Google-Smtp-Source: ABdhPJx3sp3/rQUD3QoX5on16Q1j5D9ki93BBFlMHWCVxpAH6htKFqAz5OhDZIEIF4pFfrYaPstnww== X-Received: by 2002:a17:906:3395:: with SMTP id v21mr36630356eja.322.1616004019581; Wed, 17 Mar 2021 11:00:19 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 5/6] test-coroutine: add rwlock upgrade test Date: Wed, 17 Mar 2021 19:00:12 +0100 Message-Id: <20210317180013.235231-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210317180013.235231-1-pbonzini@redhat.com> References: <20210317180013.235231-1-pbonzini@redhat.com> MIME-Version: 1.0 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=2a00:1450:4864:20::62e; envelope-from=paolo.bonzini@gmail.com; helo=mail-ej1-x62e.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: david.edmondson@oracle.com, kwolf@redhat.com, qemu-block@nongnu.org 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" Test that rwlock upgrade is fair, and readers go back to sleep if a writer is in line. Signed-off-by: Paolo Bonzini Reviewed-by: David Edmondson --- 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.29.2 From nobody Wed Nov 19 12:23:31 2025 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=1616004801; cv=none; d=zohomail.com; s=zohoarc; b=adZlSP4ezV3aq6qeCSEFllQ8N1rxfEgnDYZ4BtLnDLuOC0a5VE77LMnh4atqO/j4MS4H6BjCGfGkCXuSEUn2Xc0QIs/MllOrBFYgM32x8xZTmo1yDc/zB2ZnltFU5KNczVDdb7Wqr41QIX416HYDaHmUM31/zvc2zCRJ5Ujqnqg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616004801; 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=smfqshe5rWL1zIyiqGH7+GywYlbeI5eZY7MNQuJ+Muk=; b=NvbB7gY6iTMmd5zyYxtbKh8uyYICnRVJ17nVJc9mgIvfRj+2NCY8PKd0KjLqbLBvgi1ZFHiBa4S+EUpj3NcsQS2Wl20fRmkM/ajCVjbBST7PDidhtcXMtoxbVgD898OZdhFocQahIyYfT85nNFppDK9RSILYzG+b0C12IPebROg= 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 1616004801277436.9028746292788; Wed, 17 Mar 2021 11:13:21 -0700 (PDT) Received: from localhost ([::1]:56966 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMafX-0001SM-Q2 for importer@patchew.org; Wed, 17 Mar 2021 14:13:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57292) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMaT9-0000nL-S1; Wed, 17 Mar 2021 14:00:32 -0400 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]:36217) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMaT5-000515-3a; Wed, 17 Mar 2021 14:00:28 -0400 Received: by mail-ej1-x62f.google.com with SMTP id e19so4082178ejt.3; Wed, 17 Mar 2021 11:00:26 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:63a7:c72e:ea0e:6045]) by smtp.gmail.com with ESMTPSA id b22sm13159303edv.96.2021.03.17.11.00.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 11:00:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=smfqshe5rWL1zIyiqGH7+GywYlbeI5eZY7MNQuJ+Muk=; b=dVe479KW7cs+YM+4etiw8qYGFeTRtxiaLiqzVW/OI2RiFJ6F+bOL0lfPik7pBuzysB IC4o05+Yp3dWtvPrgNBrzumcZYzqV6uBjdCgVvaTqU5f05sQDsVV6S9vrliGUrubDkld MGmQq9m89kgr08nBoealG8nNQtj9X0HJJC1UBOc6mpHd3vOHKei1qdMCoIih+fShzvoN ZRSR+x2Sfo3ajBZvbulX3VQfdNWBdNo9rIwztSqvGVBhhNcNyKcgwM9AN3LcxQq76fIu Pl1WBLV3VKoQbYKDj4YscYxeZOWRug3acG7MXX60PKyLHMk8NYzkuBm7Loyq/6fw8WGj qjYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=smfqshe5rWL1zIyiqGH7+GywYlbeI5eZY7MNQuJ+Muk=; b=ScThh7Z/PmqU26sqtZYwJMOZIWFzoyz22Nfulz5Hd436ycF1eoJRyu9iOu02LFDIPu kDSOqVQ7sqJ6VbnefqoIyWmuDQsFy13c3+jCnjQUIUNm6khVv3811sCMNbEhKg7AWeL6 1HGatGh5P2mfUojYGxrEeDC86oDFSMCiiKk1SCIEhO4BUxrKNP+KY55yzGUckWFlYzq1 cTztSOl+3fByuxDDGg4oZdOejSYZrbep54wM5+j6y06+XbOfpDcc0y3jant8cTp+arXg b5m8JxzFOJsDPN2x9rgMOuVFRkLC9jZP4gMQbSCh5YbD41yydNRWJRQZF92cpjLMET/b 9GUw== X-Gm-Message-State: AOAM5331JTEKc5J1F9FOQFD/rxf7nck0Jnwqw0iqGZkLVFtHWxdv7HmB 8gU8mD4SUqFGGbzCjyLLQ2Bv53OPOWI= X-Google-Smtp-Source: ABdhPJxJXGHjqMl9sfZHGJqpILfCjPycyTwQczGRvF66HPxZWNsXIgNvWosngEcScoIhuUr3bVH3PQ== X-Received: by 2002:a17:906:ad4:: with SMTP id z20mr35893672ejf.496.1616004020495; Wed, 17 Mar 2021 11:00:20 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 6/6] test-coroutine: Add rwlock downgrade test Date: Wed, 17 Mar 2021 19:00:13 +0100 Message-Id: <20210317180013.235231-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210317180013.235231-1-pbonzini@redhat.com> References: <20210317180013.235231-1-pbonzini@redhat.com> MIME-Version: 1.0 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=2a00:1450:4864:20::62f; envelope-from=paolo.bonzini@gmail.com; helo=mail-ej1-x62f.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: david.edmondson@oracle.com, kwolf@redhat.com, qemu-block@nongnu.org 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 Message-Id: <20210309144015.557477-5-david.edmondson@oracle.com> Signed-off-by: Paolo Bonzini --- 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.29.2