From nobody Mon May 20 02:56:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661433825; cv=none; d=zohomail.com; s=zohoarc; b=Z475i3Lc2RDomAUeNOxXK1iUdNUYggWcgwiqZaZmO8oW2TeqQ/MSuQuf0f5JnSCoTd8siZeWZNw18whY95XH4Pr9JymT6rdoz9I5z8S6St1/crTuCv3AIINJ6aKPEr1rOs7Cds4B7Y/vpDs0uhpbtjS8O98rwcNSbkNSt4YizCo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661433825; 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=6RsaNSlRnxhrIkdYMtpbZcR2E/o687z00sVkGUTKAmg=; b=U7zYc38Ljv0uCo/o7U81wLJIHUc/1rFOQfV3nKMmREOsRLgD/mtuTYakk1GDU/fBpF3GpQP/ozrgHZWlGf03Sovho7QJ3I9ygB3wDHsA+tTmZWZPyzL9tLHCayAJP1FW0z0fKlb5hrClthnrOClMMWFR3yza3HGTeQXzF69tn6k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661433825945864.6739159698818; Thu, 25 Aug 2022 06:23:45 -0700 (PDT) Received: from localhost ([::1]:34634 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oRCpk-00040t-DK for importer@patchew.org; Thu, 25 Aug 2022 09:23:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46196) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oRCnW-0002Jp-NZ for qemu-devel@nongnu.org; Thu, 25 Aug 2022 09:21:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:54871) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oRCnT-0000rI-Kt for qemu-devel@nongnu.org; Thu, 25 Aug 2022 09:21:24 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-172-6q4ISBs7OF6DoZSaPlAU3g-1; Thu, 25 Aug 2022 09:21:20 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 37B0F38149A8; Thu, 25 Aug 2022 13:21:20 +0000 (UTC) Received: from localhost (unknown [10.39.208.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 44239492C3B; Thu, 25 Aug 2022 13:21:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661433681; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6RsaNSlRnxhrIkdYMtpbZcR2E/o687z00sVkGUTKAmg=; b=F3Dr8XR2UvToxWuFj/ZeIaljZafknNmectYyiYUAaHAb+TqVrl/GR78fzBIFliZ1uIQXa5 fCEFEeFFqRpfTavUMGH305CyxxnN0dBQVdSWy87geN/3HzpMKa2vBRgJNaoxhsdgOIYmhw qk8dgi2i5artaNy/pU3oSqT+k9QcK/M= X-MC-Unique: 6q4ISBs7OF6DoZSaPlAU3g-1 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Cc: Paolo Bonzini , qiaonuohan@cn.fujitsu.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Peter Maydell , David Hildenbrand , Stefan Berger Subject: [PATCH 1/2] dump: simplify a bit kdump get_next_page() Date: Thu, 25 Aug 2022 17:21:09 +0400 Message-Id: <20220825132110.1500330-2-marcandre.lureau@redhat.com> In-Reply-To: <20220825132110.1500330-1-marcandre.lureau@redhat.com> References: <20220825132110.1500330-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661433827119100001 From: Marc-Andr=C3=A9 Lureau This should be functionally equivalent, but slightly easier to read, with simplified paths and checks at the end of the function. The following patch is a major rewrite to get rid of the assert(). Signed-off-by: Marc-Andr=C3=A9 Lureau --- dump/dump.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/dump/dump.c b/dump/dump.c index 4d9658ffa2..18f06cffe2 100644 --- a/dump/dump.c +++ b/dump/dump.c @@ -1107,37 +1107,31 @@ static bool get_next_page(GuestPhysBlock **blockptr= , uint64_t *pfnptr, uint8_t *buf; =20 /* block =3D=3D NULL means the start of the iteration */ - if (!block) { - block =3D QTAILQ_FIRST(&s->guest_phys_blocks.head); - *blockptr =3D block; - assert((block->target_start & ~target_page_mask) =3D=3D 0); - assert((block->target_end & ~target_page_mask) =3D=3D 0); - *pfnptr =3D dump_paddr_to_pfn(s, block->target_start); - if (bufptr) { - *bufptr =3D block->host_addr; - } - return true; + if (block =3D=3D NULL) { + *blockptr =3D block =3D QTAILQ_FIRST(&s->guest_phys_blocks.head); + addr =3D block->target_start; + } else { + addr =3D dump_pfn_to_paddr(s, *pfnptr + 1); } - - *pfnptr =3D *pfnptr + 1; - addr =3D dump_pfn_to_paddr(s, *pfnptr); + assert(block !=3D NULL); =20 if ((addr >=3D block->target_start) && (addr + s->dump_info.page_size <=3D block->target_end)) { buf =3D block->host_addr + (addr - block->target_start); } else { /* the next page is in the next block */ - block =3D QTAILQ_NEXT(block, next); - *blockptr =3D block; + *blockptr =3D block =3D QTAILQ_NEXT(block, next); if (!block) { return false; } - assert((block->target_start & ~target_page_mask) =3D=3D 0); - assert((block->target_end & ~target_page_mask) =3D=3D 0); - *pfnptr =3D dump_paddr_to_pfn(s, block->target_start); + addr =3D block->target_start; buf =3D block->host_addr; } =20 + /* those checks are going away next */ + assert((block->target_start & ~target_page_mask) =3D=3D 0); + assert((block->target_end & ~target_page_mask) =3D=3D 0); + *pfnptr =3D dump_paddr_to_pfn(s, addr); if (bufptr) { *bufptr =3D buf; } --=20 2.37.2 From nobody Mon May 20 02:56:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1661433997; cv=none; d=zohomail.com; s=zohoarc; b=oHP3Vz0bgyNgX/O9aCUUMLI9uFZwfpiZDRH6vsnyHPkMa66Hu83wTKMzPTJBxEV5SHZpTYV+xToW90xevJ/JnYexvPgTmA8FsJTewH2/3h7pfEZi78QC1KOTP7B2QXtnbPOVHnSyujm6kWEnkVj0xprc9YkpEXrSJUzH4SFvH90= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661433997; 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=wzGsCWdlvNsNp9At+1/QNe2WO4Un1pBycwc68LIA0R8=; b=HDR0gS4dj2QGaQaqkCG8mFX1DVbDoJJsogKMfr9jqBTZXlR3eBUlzkGf0Cf35ZYuzps/7iOTuupsgx1rLeRNEQKJgh9S988Ga9iKoieSUv8+XAe91QA1Y4j68yUAbFdx7QWaQH5jKb6fmVgkgEwN+Jsmn6XxHR1I6tIPy8vaPZE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1661433997688934.109137669164; Thu, 25 Aug 2022 06:26:37 -0700 (PDT) Received: from localhost ([::1]:42270 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oRCsW-00070B-OJ for importer@patchew.org; Thu, 25 Aug 2022 09:26:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54288) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oRCnl-0002LR-7s for qemu-devel@nongnu.org; Thu, 25 Aug 2022 09:21:41 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:40587) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oRCnd-0000sZ-0q for qemu-devel@nongnu.org; Thu, 25 Aug 2022 09:21:34 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-564-mcDGCLQQOZizef06RdlvBA-1; Thu, 25 Aug 2022 09:21:28 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DE4B3185A7BA; Thu, 25 Aug 2022 13:21:27 +0000 (UTC) Received: from localhost (unknown [10.39.208.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2BBB0141510F; Thu, 25 Aug 2022 13:21:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661433691; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wzGsCWdlvNsNp9At+1/QNe2WO4Un1pBycwc68LIA0R8=; b=GuFKXk/tb4UYWt6W9xIx3hFEPq0YEg8Mw9QLAG9Gto79a61Vgl7HKmcH7PXQBLdVL/38/3 38+1+gF6ENNtKVsrnvlCAMm47z4VZMXqtjVi6PLXeWAnC04WnnzraPoqcjo6gQAj4Ht4zY qdmgOr5l346heI5ZkEnWM3vV2zthack= X-MC-Unique: mcDGCLQQOZizef06RdlvBA-1 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Cc: Paolo Bonzini , qiaonuohan@cn.fujitsu.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Peter Maydell , David Hildenbrand , Stefan Berger Subject: [PATCH 2/2] dump: fix kdump to work over non-aligned blocks Date: Thu, 25 Aug 2022 17:21:10 +0400 Message-Id: <20220825132110.1500330-3-marcandre.lureau@redhat.com> In-Reply-To: <20220825132110.1500330-1-marcandre.lureau@redhat.com> References: <20220825132110.1500330-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1661433998291100001 From: Marc-Andr=C3=A9 Lureau Rewrite get_next_page() to work over non-aligned blocks. When it encounters non aligned addresses, it will allocate a zero-page and try to fill it. This solves a kdump crash with "tpm-crb-cmd" RAM memory region, qemu-kvm: ../dump/dump.c:1162: _Bool get_next_page(GuestPhysBlock **, uint64_t *, uint8_t **, DumpState *): Assertion `(block->target_start & ~target_page_mask) =3D=3D 0' failed. because: guest_phys_block_add_section: target_start=3D00000000fed40080 target_end=3D= 00000000fed41000: added (count: 4) Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=3D2120480 Signed-off-by: Marc-Andr=C3=A9 Lureau --- dump/dump.c | 89 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 25 deletions(-) diff --git a/dump/dump.c b/dump/dump.c index 18f06cffe2..9aeed88688 100644 --- a/dump/dump.c +++ b/dump/dump.c @@ -1094,49 +1094,79 @@ static uint64_t dump_pfn_to_paddr(DumpState *s, uin= t64_t pfn) } =20 /* - * exam every page and return the page frame number and the address of the= page. - * bufptr can be NULL. note: the blocks here is supposed to reflect guest-= phys - * blocks, so block->target_start and block->target_end should be interal - * multiples of the target page size. + * Return the page frame number and the page content in *bufptr. + * bufptr and allocptr can be NULL. If alloced, *bufptr must be freed. */ static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr, - uint8_t **bufptr, DumpState *s) + uint8_t **bufptr, bool *allocptr, DumpState *s) { GuestPhysBlock *block =3D *blockptr; - hwaddr addr, target_page_mask =3D ~((hwaddr)s->dump_info.page_size - 1= ); - uint8_t *buf; + uint32_t page_size =3D s->dump_info.page_size; + bool alloced =3D false; + uint8_t *buf =3D NULL, *hbuf; + hwaddr addr; =20 /* block =3D=3D NULL means the start of the iteration */ if (block =3D=3D NULL) { *blockptr =3D block =3D QTAILQ_FIRST(&s->guest_phys_blocks.head); addr =3D block->target_start; + *pfnptr =3D dump_paddr_to_pfn(s, addr); } else { - addr =3D dump_pfn_to_paddr(s, *pfnptr + 1); + assert(block !=3D NULL); + *pfnptr +=3D 1; + addr =3D dump_pfn_to_paddr(s, *pfnptr); } assert(block !=3D NULL); =20 - if ((addr >=3D block->target_start) && - (addr + s->dump_info.page_size <=3D block->target_end)) { - buf =3D block->host_addr + (addr - block->target_start); - } else { - /* the next page is in the next block */ - *blockptr =3D block =3D QTAILQ_NEXT(block, next); - if (!block) { - return false; + while (1) { + if (addr >=3D block->target_start && addr < block->target_end) { + size_t n =3D MIN(block->target_end - addr, page_size - addr % = page_size); + hbuf =3D block->host_addr + (addr - block->target_start); + if (!alloced) { + if (n =3D=3D page_size) { + /* this is a whole host page, go for it */ + assert(addr % page_size =3D=3D 0); + buf =3D hbuf; + break; + } else { + buf =3D g_malloc0(page_size); + alloced =3D true; + } + } + + memcpy(buf + addr % page_size, hbuf, n); + addr +=3D n; + if (addr % page_size =3D=3D 0) { + /* we filled up the alloc page */ + break; + } + } else { + /* the next page is in the next block */ + *blockptr =3D block =3D QTAILQ_NEXT(block, next); + if (!block) { + break; + } + + addr =3D block->target_start; + /* are we still in the same page? */ + if (dump_paddr_to_pfn(s, addr) !=3D *pfnptr) { + if (alloced) { + /* no, but we already filled something earlier, return= it */ + break; + } else { + /* else continue from there */ + *pfnptr =3D dump_paddr_to_pfn(s, addr); + } + } } - addr =3D block->target_start; - buf =3D block->host_addr; } =20 - /* those checks are going away next */ - assert((block->target_start & ~target_page_mask) =3D=3D 0); - assert((block->target_end & ~target_page_mask) =3D=3D 0); - *pfnptr =3D dump_paddr_to_pfn(s, addr); if (bufptr) { *bufptr =3D buf; + *allocptr =3D alloced; } =20 - return true; + return buf !=3D NULL; } =20 static void write_dump_bitmap(DumpState *s, Error **errp) @@ -1159,7 +1189,7 @@ static void write_dump_bitmap(DumpState *s, Error **e= rrp) * exam memory page by page, and set the bit in dump_bitmap correspond= ed * to the existing page. */ - while (get_next_page(&block_iter, &pfn, NULL, s)) { + while (get_next_page(&block_iter, &pfn, NULL, NULL, s)) { ret =3D set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s); if (ret < 0) { error_setg(errp, "dump: failed to set dump_bitmap"); @@ -1274,6 +1304,7 @@ static void write_dump_pages(DumpState *s, Error **er= rp) uint8_t *buf; GuestPhysBlock *block_iter =3D NULL; uint64_t pfn_iter; + bool freebuf =3D false; =20 /* get offset of page_desc and page_data in dump file */ offset_desc =3D s->offset_page; @@ -1314,7 +1345,7 @@ static void write_dump_pages(DumpState *s, Error **er= rp) * dump memory to vmcore page by page. zero page will all be resided i= n the * first page of page section */ - while (get_next_page(&block_iter, &pfn_iter, &buf, s)) { + while (get_next_page(&block_iter, &pfn_iter, &buf, &freebuf, s)) { /* check zero page */ if (buffer_is_zero(buf, s->dump_info.page_size)) { ret =3D write_cache(&page_desc, &pd_zero, sizeof(PageDescripto= r), @@ -1403,6 +1434,10 @@ static void write_dump_pages(DumpState *s, Error **e= rrp) error_setg(errp, "dump: failed to write page desc"); goto out; } + if (freebuf) { + g_free(buf); + freebuf =3D false; + } } s->written_size +=3D s->dump_info.page_size; } @@ -1419,6 +1454,10 @@ static void write_dump_pages(DumpState *s, Error **e= rrp) } =20 out: + if (freebuf) { + g_free(buf); + } + free_data_cache(&page_desc); free_data_cache(&page_data); =20 --=20 2.37.2