From nobody Sun May 19 06:50:46 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=1615911114; cv=none; d=zohomail.com; s=zohoarc; b=gjatXt1fe1wgmJZU75Apauis7sjrcdsz85buJUOABo1C/Pmat2/IeDJAZKgZX+c+Y2M3SeGpxfFftc0xVKX/Qhf3XSECdNlBEBF8J2jhznSm8JrvrCa/WjSq58M0Ej+XPz1ojXfEVCG7IR3Yffrxk3BSC/FgABYbrLj6e66ioC0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615911114; 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=B5xBX6kLMQLcNiaBR/0MeabUWk9immHw+MKL8IB1uSfqtxj+5e5WTux9eXfcXbZq11PilR8oJxyUoJ5Qzb5C9YUkm7JkNSeDFCdhnCUHN1r7WJIhhFwxEAxiROYW27c8WnOvaAhhdLLW2wJ9JcSFLgEWbaO/mFNw6pmYiIiH+YM= 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 1615911114093115.65518282127971; Tue, 16 Mar 2021 09:11:54 -0700 (PDT) Received: from localhost ([::1]:56664 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMCIT-0001c5-1v for importer@patchew.org; Tue, 16 Mar 2021 12:11:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36138) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMC7H-0000uN-A7; Tue, 16 Mar 2021 12:00:21 -0400 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]:50909) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMC79-0000IZ-IN; Tue, 16 Mar 2021 12:00:16 -0400 Received: by mail-wm1-x336.google.com with SMTP id g25so10372069wmh.0; Tue, 16 Mar 2021 09:00:10 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id j14sm22674851wrw.69.2021.03.16.09.00.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Mar 2021 09:00:09 -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=P5UPFlTg9J2Mh6ZgtUx5nExmAl6QCH7w3m2a6QOIBle1dwG43gw7+YC1+sUc9rPYzK F6zslCs6G6bBRftFBRed+YdluJqN0dOV+3AUI2wOEh58yDEfloxtt18FZKNxSWN6PhSZ M6Jtyvq9tKYsDyR43ukxI2LeRegqQUzAkuOIBrdQgxj7R0ipNmfScVzyujW3AKJK9263 j/5zMfTEK9kAeDWH0wbk6gZmU68znDSVXRiO1mBNDAyulbuaTHmd+6VllW5HYpYZ/TVn kDWJIdPoKyw5MrrEAfMynVDG1BUzkxIoOC2T6zRUY24nmgbRvoLFX8QL8t9NodCtx1hK /h0w== 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=q9P67Rl29/Xnmmc5Enhp4WIzN0HICgIuVZ59jx7cDhKe+SSHHhwTJf+U+AmaETbulI 9UnbFHo9fnSnIMR6lOE1qsAkojGFQszMVeM03L4sU2CgAX8GpDhEB/TcJcoFDbIdD+/p JTaSWLKCvUIJFcBByNAyZCU6i021uBy7g++nSEGhApuFRMIaQrRGqY9pwag2o4poJ/2y hl2evwUT3GRefGKbU9L+Aldq7x2jBzB8kiemT2+Id3RvM0Wl1SlLYWGlItF7rN+FeycB PTR9sxZaCRA0aN8ZEDT5BRYeCqwIrJjThZT3nLt2AnqGWfQtevlfQme56f8AXuWvHjDO VNIg== X-Gm-Message-State: AOAM530I5ljaRYfoXD/uYL+ehZeGxv6iwPdQmCyqkEJVsk5FxObFO7xI rL/YfsDIwBBTUFwOoQlMyO9agB7Avzg= X-Google-Smtp-Source: ABdhPJyehjFxt429d3wo7fS7eSFjbMjmHVGjSpL3X6uutyx62qaX1ERKTJVqutm2mUgHgrNkyWUAeQ== X-Received: by 2002:a1c:7209:: with SMTP id n9mr321093wmc.132.1615910409683; Tue, 16 Mar 2021 09:00:09 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 1/5] block/vdi: When writing new bmap entry fails, don't leak the buffer Date: Tue, 16 Mar 2021 17:00:03 +0100 Message-Id: <20210316160007.135459-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210316160007.135459-1-pbonzini@redhat.com> References: <20210316160007.135459-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::336; envelope-from=paolo.bonzini@gmail.com; helo=mail-wm1-x336.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 --- 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 Sun May 19 06:50:46 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=1615911487; cv=none; d=zohomail.com; s=zohoarc; b=E15xHq7joQfH0DxLn4DRnEJrCSrdcvJ9HZLZiRNcliDz/c2JukdYY+iDbT5eSPiLKkZrHRH8zp+wJeI0LA6Rfl9s52Bxwg1eFc2uAXuPPRfMQrcTZ4Q6B5SU+/6AZXIoqUlw3utnhTkiNepwor6WG3YZxL54CcwBMN+vrMLtF2o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615911487; 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=Ump7NI/yMfwTGZDP5ydGSR5sbpiY46mVVd0TStemQbg0GTTV3SawBwPsV9L/1yeO/XX3Y7tcz4MsE6hSs5dvdTZjtUxMYk4MtPFikdcjZxPEtBTcX+9aCli+esJwhLd0wAKVRCNw4g7GMrosQ2+DJKoQILIzCq6zBn5uFwdyuiM= 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 1615911487475231.28141019285783; Tue, 16 Mar 2021 09:18:07 -0700 (PDT) Received: from localhost ([::1]:40108 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMCOS-0007DQ-UN for importer@patchew.org; Tue, 16 Mar 2021 12:18:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36124) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMC7C-0000uD-TI; Tue, 16 Mar 2021 12:00:20 -0400 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]:44072) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMC7A-0000JD-0Z; Tue, 16 Mar 2021 12:00:14 -0400 Received: by mail-wr1-x430.google.com with SMTP id o14so6942390wrm.11; Tue, 16 Mar 2021 09:00:11 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id j14sm22674851wrw.69.2021.03.16.09.00.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Mar 2021 09:00:10 -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=GrZTqi7U3gcasp9/+Bg3Ygrl95eTyoYLpkTxp/aB/bkUBAqc5mnac4SJiJwjrd0ND0 Y/GhA7PUz1EvoMmdYetCSYT+kK4ghSNZ6O9nnUNRxtfSuKlvoGEACWeP/t3qXzciT/YE zIATpKGm9NMuAizxUq+JOGXp9WsITyVtgAks5XHmzsN+sEsEqUOECPpXEhcMIYTw4OXV roVv2ykskKBXvWNYX52XufJN3y84C7MP6T+xftlBN8lJ7+9edAcw207cRtur79xG2VP1 eMdKKp2w+G59MuW05TGLj9Cdz7uvfuxomH0JDINvwWuUjVZinsuYgsQcLNajVuIiT68n PQkw== 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=rcPD5BN9N7sOqUP/Hprh3PwAz3b4SGKShlbdpwRCuvkQm4/MQMeZQDB1CL6MRBAnOj AoZKbDTnlFaFGJGsyvufPNJ+0JVjyTMykOWDkPCgz7+6a/y0kRYYDetXPxLybzP4jQi8 Dzc4Q7/zjQYKEwt92Z5Apd70Kf8bk53GnNjeeoyE0YoUh7nwM9Z96B0itc/MSGlkq3E7 XfOBvDKA/7BR24UUFAJQYoPPF8fG2t5yaxhr3MJNEFzArK9+/TYE85bxNPv5Du/YMc7C qyt0uHoI+jFX+gBeYAyBlq2M0Of4V08i1zXwLxo+ft+9544G8FpjcfReDrUx5xy4Ry0q VIVA== X-Gm-Message-State: AOAM531fN9d9b733e5V45w76uLmcedTqz0r3cQgrsrQcRlRFpO71E7xG 91TiC44ejKd8EwvZuE9fvO6kl0sYizI= X-Google-Smtp-Source: ABdhPJzXj0/+GM1NpiLIhdnOYsqHh5e7gnntlvz2vlvZ7KapdtE7Yp/Vs/ZkPIByTs1aTk4KH97k0Q== X-Received: by 2002:adf:e4c7:: with SMTP id v7mr5741126wrm.245.1615910410440; Tue, 16 Mar 2021 09:00:10 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 2/5] block/vdi: Don't assume that blocks are larger than VdiHeader Date: Tue, 16 Mar 2021 17:00:04 +0100 Message-Id: <20210316160007.135459-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210316160007.135459-1-pbonzini@redhat.com> References: <20210316160007.135459-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::430; envelope-from=paolo.bonzini@gmail.com; helo=mail-wr1-x430.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 --- 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 Sun May 19 06:50:46 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=1615910691; cv=none; d=zohomail.com; s=zohoarc; b=LL2mB03uchHwwkqdv2+BwwZGE2JhHwJ4aABcN89isXncLYBFuRGWrt1Q/Rn9djdLoMcmV4BhUQB7ZLiemBVZF1ngSHvQvE7PGd67lXOAbLa4nDaEiVvZ8EWpbnT97WesK/FSYPOBBmJwDZbOMzHTJpClyhHPlSaI4WwMV/gOoZ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615910691; 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=PpK6zhBWA0zwPreMGWHLn2LVgoUbUeKqN+Ff0gnyRngRgBwG+wc10/TudBAq+LLaw85xqbdDKMSIiUI9ep6aINcx9aJt4nWXSQa/3LFaoBaC5F5rzvvOTc/kB8Q5rRdMElGUEAY989wAeko1Vbm/2arrhVdhpvttME3wp+SyrrA= 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 1615910691166784.6437933297827; Tue, 16 Mar 2021 09:04:51 -0700 (PDT) Received: from localhost ([::1]:42032 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMCBe-00032j-En for importer@patchew.org; Tue, 16 Mar 2021 12:04:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36136) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMC7H-0000uM-9m; Tue, 16 Mar 2021 12:00:21 -0400 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]:56270) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMC7B-0000JR-Av; Tue, 16 Mar 2021 12:00:14 -0400 Received: by mail-wm1-x32f.google.com with SMTP id 12so5368348wmf.5; Tue, 16 Mar 2021 09:00:12 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id j14sm22674851wrw.69.2021.03.16.09.00.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Mar 2021 09:00:10 -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=MOL3F8VoPm/CmozSQWlfZzjI14xU2FOHJytLITmThr3oXvvAGGjGBcFPUsGs91iFVC B/YV2qY8N3WehDrl7pBXzmr7T7nbeybbNNL9XWAsKp9RyOrysTUpPZA7qWaS7WDv+Zo/ G1AuHCL1M5fxbwjV8e2qkpq3bOkYL3o9R6bw98UsQbnUeoiRQGpSxAOklXDaMtfeGgt5 muR6WBnmRxkcfTMiwk2CoT1DdeATU5OjormMnkDr6cd/6M4Q1Pvfu2zvmNdI4PkR2LSw Yklp7hd4n7hfDhb9AqkFTcvRG7rPZnIZtJUO7X2PsXbpcHraga6J2sXYk1DI5Pal3pr/ ga6A== 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=BU8TTFC7EJJa6/dAf8vkDzeEN3H0j/6AKdz8AQg7mKttcJKREcrfWlbA8j3M0yaGqh p+SfT/+lJB1L4In3WLD6hMhm3gJBiuidW3PShXuMcZRfjEubeS1/b/38BHMVypLpVSIf k6qpajX9+S+Jezh0v1DOA6Z0Cgy7nqw/s7w4KA0+2p6hUuAB51rJhFUc3wmL2SFmN8B+ Tp22SUMgs9sEGrZG+anYQBUATBsmp6PlZBlHw0yNDd1dZbUyhWKd1crBWZaVGsg4UefP PxeFutaDJmVcAc3fUYDUdbTjLrUgCiVri/wTIdWOXHHX0XkSxl8CEd8DR8LDENhbGbr8 5OGw== X-Gm-Message-State: AOAM532ZYQoYmCHQrx9sOnBBNzMkP74ZDZRrfIn1HHqgrvdPzz5UnjGE vRuVv5aTfOmW8v5IM7vltNOaw3wzA08= X-Google-Smtp-Source: ABdhPJzKlqZyrBSK9V8hW9NSNEZOI17h0Ji4VdE4QUhe4vTstwNQgRGBQ4xq0QME4KVhpqcSuPdk0Q== X-Received: by 2002:a1c:a543:: with SMTP id o64mr297567wme.107.1615910411137; Tue, 16 Mar 2021 09:00:11 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 3/5] coroutine/mutex: Store the coroutine in the CoWaitRecord only once Date: Tue, 16 Mar 2021 17:00:05 +0100 Message-Id: <20210316160007.135459-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210316160007.135459-1-pbonzini@redhat.com> References: <20210316160007.135459-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::32f; envelope-from=paolo.bonzini@gmail.com; helo=mail-wm1-x32f.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 Sun May 19 06:50:46 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=1615910680; cv=none; d=zohomail.com; s=zohoarc; b=Bl90Po7tvVqLLU8VE46EPj3yy++hCow6Ml0MXRgwZYau96qMYvF3PXqP7ZRHxCJGC+AqiNNZt4p1iB1ovzd3md+qMmpFMzkf2JuCGdu8wN5WXonJsclBB4VUVSxxsOq+/fOrY5GmItCABCAY3n8YIQaf7yJRUlraMvlpUeRJwAE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615910680; 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=Mte8xjSStB0pUDuwPLi1AvR+FEhWFweLpTs50yZR/7A=; b=GPsrHgaSX0n5ZGIaPlxlOTi7d8ytCp9Lsv8tzIj1lqfuol59EA2H/XoQCSWfxPSkg9mJ1nMkpDzhmICiZLtmUg+e+OZp4SuHMBb4PI4zibBemrI5PSt4S7pKmqKVnrO4+SDu8Zk+Mmz9goQKZg57/kuNNDwgb9/H80xoq8waG58= 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 16159106806231008.1904274972601; Tue, 16 Mar 2021 09:04:40 -0700 (PDT) Received: from localhost ([::1]:41872 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMCBT-0002ym-TR for importer@patchew.org; Tue, 16 Mar 2021 12:04:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36140) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMC7H-0000uO-99; Tue, 16 Mar 2021 12:00:21 -0400 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]:33359) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMC7B-0000Jh-Ps; Tue, 16 Mar 2021 12:00:16 -0400 Received: by mail-wm1-x336.google.com with SMTP id w203-20020a1c49d40000b029010c706d0642so4074254wma.0; Tue, 16 Mar 2021 09:00:12 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id j14sm22674851wrw.69.2021.03.16.09.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Mar 2021 09:00:11 -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=Mte8xjSStB0pUDuwPLi1AvR+FEhWFweLpTs50yZR/7A=; b=b54mecIGxHgzGlYRLHx2Y8llFI02yW3xoB4CQknfO+YzJ8h+VqfyDZfoPaoF7m7vsg k4JPxKUg54HL6DmyoiQlCXUvFwcBS9VsPmSwNZf2fobJvJHtCLdDjtdH32KNUCIggA4C e/vRbnpe7TLC2OGbGdOoQba2NiIIpT05FXLoA/qtRH9EwMWfKwU48eY5I+fAYmtTWsxq hLjwA8P0WhQg2pxFiqeXptblL9xvyY7LtgUL7OmbG3Gau8C9pil5aeOHHUNZhFbTdcwu JEyz2opKHWkhTj9cZ++7d/GSvLblsRxTExZPAc1fAkvLtEFKMFpxN0vPtIVGmRpHto/V xS9w== 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=Mte8xjSStB0pUDuwPLi1AvR+FEhWFweLpTs50yZR/7A=; b=NmZw59i8E6i/Du7w22tFya51iVxUuX5rRRP6oUz4K1+v+nHdFiChVDg/jeOTVEbPrw JlFbemlC4FRhUHJTgZDhETpk4OLGx0T6mvogr8qvs0k22699lp2JhubGcu+i0x51A2Uc C6/k6WZsDKRcN00P+XVNmY1VQ+1lzx20or0GWRKbpKqF4snQF8R/JJVkhgU6tulQ1U0U h21Nt600dw+WUol1y0WbO7ikhBE5Kr8f+dyLdu5fcBAatdcWXSX/2HLjdkjf/jQTVo/j 8H1zwWdQ6UhnSi9WR5AbG65rsPW7Ru/s4H97kBwfB8T61bDdrify5JWKC+Td9iu1W8DF Pdtw== X-Gm-Message-State: AOAM532xo+cTF86zmdpGvPv+J0LzxFU1yrCfcPhGQ7/OLLo0CmgtLc71 pWCgDn0bRYIv5CyiEgNSdGC+mvZ3jsQ= X-Google-Smtp-Source: ABdhPJznGz++RljOF/YE94MhQ5mGC+Ua5xWDeVUJvco97Eayl987tAoj+cLtaExrxvIc7guXdW7RVQ== X-Received: by 2002:a7b:c047:: with SMTP id u7mr335074wmc.98.1615910411862; Tue, 16 Mar 2021 09:00:11 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 4/5] coroutine-lock: reimplement CoRwLock to fix downgrade bug Date: Tue, 16 Mar 2021 17:00:06 +0100 Message-Id: <20210316160007.135459-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210316160007.135459-1-pbonzini@redhat.com> References: <20210316160007.135459-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::336; envelope-from=paolo.bonzini@gmail.com; helo=mail-wm1-x336.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" A feature 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. 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 if it is a reader, causing all other readers to be released in turn. Reported-by: David Edmondson Signed-off-by: Paolo Bonzini --- include/qemu/coroutine.h | 10 ++- util/qemu-coroutine-lock.c | 150 ++++++++++++++++++++++++------------- 2 files changed, 106 insertions(+), 54 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 84eab6e3bf..ae62d4bc8d 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, of -1 if owned for writing. */ + int owner; + + /* Waiting coroutines. */ + QSIMPLEQ_HEAD(, CoRwTicket) tickets; } CoRwlock; =20 /** diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index eb73cf11dc..655634d185 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -327,11 +327,70 @@ 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->owner =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->owner here prevents rdlock and wrlock from + * sneaking in between unlock and wake. + */ + + if (tkt) { + if (tkt->read) { + if (lock->owner >=3D 0) { + lock->owner++; + co =3D tkt->co; + } + } else { + if (lock->owner =3D=3D 0) { + lock->owner =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); + } +} + +/* Releases the internal CoMutex. */ +static void qemu_co_rwlock_sleep(bool read, CoRwlock *lock) +{ + CoRwTicket my_ticket =3D { read, qemu_coroutine_self() }; + + QSIMPLEQ_INSERT_TAIL(&lock->tickets, &my_ticket, next); + qemu_co_mutex_unlock(&lock->mutex); + qemu_coroutine_yield(); + + if (read) { + /* Possibly wake another reader, which will wake the next in line.= */ + assert(lock->owner >=3D 1); + qemu_co_mutex_lock(&lock->mutex); + qemu_co_rwlock_maybe_wake_one(lock); + } else { + assert(lock->owner =3D=3D -1); + } } =20 void qemu_co_rwlock_rdlock(CoRwlock *lock) @@ -339,13 +398,13 @@ 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->owner =3D=3D 0 || (lock->owner > 0 && QSIMPLEQ_EMPTY(&lock->= tickets))) { + lock->owner++; + qemu_co_mutex_unlock(&lock->mutex); + } else { + qemu_co_rwlock_sleep(true, 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 +413,58 @@ 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); + self->locks_held--; + + qemu_co_mutex_lock(&lock->mutex); + if (lock->owner =3D=3D -1) { + lock->owner =3D 0; } else { - self->locks_held--; + lock->owner--; + } =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); - } + if (lock->owner =3D=3D 0) { + qemu_co_rwlock_maybe_wake_one(lock); + } else { + qemu_co_mutex_unlock(&lock->mutex); } - qemu_co_mutex_unlock(&lock->mutex); } =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->owner =3D=3D -1); + lock->owner =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->owner =3D=3D 0) { + lock->owner =3D -1; + qemu_co_mutex_unlock(&lock->mutex); + } else { + qemu_co_rwlock_sleep(false, lock); } - 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->owner > 0); + /* For fairness, wait if a writer is in line. */ + if (lock->owner =3D=3D 1 && QSIMPLEQ_EMPTY(&lock->tickets)) { + lock->owner =3D -1; + qemu_co_mutex_unlock(&lock->mutex); + } else { + lock->owner--; + qemu_co_rwlock_sleep(false, lock); } - lock->pending_writer--; =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--; } --=20 2.29.2 From nobody Sun May 19 06:50:46 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=1615910932; cv=none; d=zohomail.com; s=zohoarc; b=FCQcFvZ5KMXvMV7E0ofq2qwtq2lu/oSS/8HtBtzkSafE+FpiTNLzmnzheuukxFSpKTH7PtbbGPVDs4J16szJXY+HY3xQpWWPxECinQJz9E8pPsUX9pZYhc0ZmyQYj9cfmKehcoC3zZOBbOXz/tH1VtYhqCo5xfw+TYBKF16HYzU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615910932; 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=9UJ7bhSHpJmzJJvP40BdtCaFHKYlAuZrTu3mwldeJRg=; b=ccsY7BCO/4thhkk5pNlGbt3NPO2DdMCG/mFl5kJ6sqHrjJ40IK/4o5YbzxYATG46Xi2lmgYtDtu1H+orZYRom6nOTXCFc6rC0mg59XpGPBZ7ivAgCW3B4TgsjSk1n21Yvi9sdgWFQsx06Hfqm6AYi1G6dFQrKvqYSZUs5HvCg5Y= 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 1615910932327230.77326054818002; Tue, 16 Mar 2021 09:08:52 -0700 (PDT) Received: from localhost ([::1]:48332 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMCFW-0006Eb-Q2 for importer@patchew.org; Tue, 16 Mar 2021 12:08:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36172) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMC7J-0000uq-6e; Tue, 16 Mar 2021 12:00:21 -0400 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]:44076) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMC7D-0000LG-0s; Tue, 16 Mar 2021 12:00:20 -0400 Received: by mail-wr1-x434.google.com with SMTP id o14so6942489wrm.11; Tue, 16 Mar 2021 09:00:13 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id j14sm22674851wrw.69.2021.03.16.09.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Mar 2021 09:00:12 -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=9UJ7bhSHpJmzJJvP40BdtCaFHKYlAuZrTu3mwldeJRg=; b=WpEok9b17mvce8Fg7ZuBCtH6fIZ3nKYJWNC0jIx8MoTzCyFM0EVvJJZKp/pk9y3cy3 jUj6RNWoKJV4HWhk2mQWVGyc1xvUhr++vBlLfTSpDJrscGWZE6OuThHDPiVlk3wSV7P6 /FjgZ1HWwp5ks2qvREyS9+hL57Jo4Wcs+JjuWwrdvvRiQGmtanEi3TTIQBxetScIkErV LOKjuDir05/Qwa9FuW8NOysMb7c5KT2Qf85l2dCsFinnIM3UHc6FGtMcsaqv0gQVHBsO pkYBASoAKvJNJJvsLhy3DnH9DmljluxJIOSTO+7WPwD3iFPzg2kkllFF1kQojRy1ecSH Z8xQ== 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=9UJ7bhSHpJmzJJvP40BdtCaFHKYlAuZrTu3mwldeJRg=; b=BZASesew3Iy3SR0urRVJ/iLCoRM3SUGOx3Xl65054/RQVEji+NcwMLm3VDXBuVvfgu uU2jQyyzljGI/oMNRwll3ohlAwjyAeiNL6DsRfAHCeGuNVKy6zWznywH9J+nrWLLSg0b UUrroqTVWzMyNCUZIPkjopbiSExd9wdIzYCWZw/68UzcvpOTvZpyQdfc4FfYdr8maij3 hQLiNo64ha6k8M+C7NALsG3e5BQQgQoVeC6X41R/fH5bAz4d+CnMmHrftXxKVZ6X6hV+ LFvs5+8wVTQJ3yoCKUcg2w7EjOQDMw8dADeBB+8RY9+enMJwGhbQ22bEof4HTaaNxLKu Bf6w== X-Gm-Message-State: AOAM533+XY7NXzL3T1QMHsvzpaFAbdgFR+T52gMu8Mly+AD2XsLiUuD+ bFJ5htkLZLeBA6/Ule/Da9LKAdG5uHA= X-Google-Smtp-Source: ABdhPJz/oXgd8O4cXRfCwEvmtFEggqR6sWYUidVX0sRV8J9tVK6+CA5p/wa5ce3rEFPGmJEgcGRzTg== X-Received: by 2002:adf:e60a:: with SMTP id p10mr5672004wrm.291.1615910412610; Tue, 16 Mar 2021 09:00:12 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 5/5] test-coroutine: Add rwlock downgrade test Date: Tue, 16 Mar 2021 17:00:07 +0100 Message-Id: <20210316160007.135459-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210316160007.135459-1-pbonzini@redhat.com> References: <20210316160007.135459-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::434; envelope-from=paolo.bonzini@gmail.com; helo=mail-wr1-x434.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 --- Unlike in David's solution, here c3 is run as soon as c2 is downgraded. This only affects the drawing and not the test code. tests/unit/test-coroutine.c | 109 ++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/tests/unit/test-coroutine.c b/tests/unit/test-coroutine.c index e946d93a65..62b0092721 100644 --- a/tests/unit/test-coroutine.c +++ b/tests/unit/test-coroutine.c @@ -264,6 +264,114 @@ static void test_co_mutex_lockable(void) g_assert(QEMU_MAKE_LOCKABLE(null_pointer) =3D=3D NULL); } =20 +static bool c1_done; +static bool c2_done; +static bool c3_done; +static bool c4_done; + +static void coroutine_fn rwlock_c1(void *opaque) +{ + CoRwlock *l =3D opaque; + + qemu_co_rwlock_rdlock(l); + qemu_coroutine_yield(); + + qemu_co_rwlock_unlock(l); + qemu_coroutine_yield(); + + c1_done =3D true; +} + +static void coroutine_fn rwlock_c2(void *opaque) +{ + CoRwlock *l =3D opaque; + + qemu_co_rwlock_wrlock(l); + + qemu_co_rwlock_downgrade(l); + qemu_co_rwlock_unlock(l); + c2_done =3D true; +} + +static void coroutine_fn rwlock_c3(void *opaque) +{ + CoRwlock *l =3D opaque; + + qemu_co_rwlock_rdlock(l); + + qemu_co_rwlock_unlock(l); + c3_done =3D true; +} + +static void coroutine_fn rwlock_c4(void *opaque) +{ + CoRwlock *l =3D opaque; + + qemu_co_rwlock_wrlock(l); + + qemu_co_rwlock_unlock(l); + c4_done =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) +{ + CoRwlock l; + Coroutine *c1, *c2, *c3, *c4; + + qemu_co_rwlock_init(&l); + + c1 =3D qemu_coroutine_create(rwlock_c1, &l); + c2 =3D qemu_coroutine_create(rwlock_c2, &l); + c3 =3D qemu_coroutine_create(rwlock_c3, &l); + c4 =3D qemu_coroutine_create(rwlock_c4, &l); + + 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 */ @@ -501,6 +612,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/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