From nobody Sat Apr 5 01:18:45 2025 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=1739314322; cv=none; d=zohomail.com; s=zohoarc; b=cGb3o86d/d5UZXVxHjqCDItirswbcrlYF4S3j+GU9HuratzGWGJN2jRzzZ30j1DuZDlewXWivHRm5HoCPwPyd2J3WHNr7jZp+frNzZoHCvqk8KjnUJzBEFZMgR1YVuqS0a0rKbbye9cDOs2bB3HKlVR5r0ptKggqfGeNP3PTeBM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1739314322; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Y5xSVhRiQUHuJeHocslUI3HV6Wv1hLUrZwIyEA1RiQE=; b=XOW5dJ2GmUlqW+8W4jEYuutRi/HIQEYK8BarVTGMw/XxZWGHhK7lwaQ2CgJlLRO1ruOBwlzY1A6E3ft2NeKoWaCNj0Vhw0n6SNOTzNe44SQvGbrg9znlv6BhvT/xdJwxfNBRjR3ViwJJhrYZzIftwM/ldnuTRuMEH637MTgmFXI= 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 173931432210627.00678005765053; Tue, 11 Feb 2025 14:52:02 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1thz6P-0005kr-Rs; Tue, 11 Feb 2025 17:51:37 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz6N-0005k8-FE for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:35 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1thz6L-00057S-Pl for qemu-devel@nongnu.org; Tue, 11 Feb 2025 17:51:35 -0500 Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-367-uBO6tYYcNi-xu47RL5k14g-1; Tue, 11 Feb 2025 17:51:30 -0500 Received: by mail-oo1-f69.google.com with SMTP id 006d021491bc7-5fc7f76cae8so1864735eaf.2 for ; Tue, 11 Feb 2025 14:51:30 -0800 (PST) Received: from x1.com ([2604:7a40:2041:2b00::1000]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fca0992fd0sm482515eaf.34.2025.02.11.14.51.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Feb 2025 14:51:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739314293; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y5xSVhRiQUHuJeHocslUI3HV6Wv1hLUrZwIyEA1RiQE=; b=VYrZd3c+hS+uywmhGxYixJ+5woeNlOSSRixipAQj7hf9amoDqqMHrucLvy5OiuapYbN5t8 NFD+sfUrAR5QFK1oBpvcriYHztoHWPcOH8qVnrnZJNTypA1C/wzAJysKG301IJpeepJeB5 WNWSBnCR03RXLOveiHFq2+R4eNwW2KE= X-MC-Unique: uBO6tYYcNi-xu47RL5k14g-1 X-Mimecast-MFC-AGG-ID: uBO6tYYcNi-xu47RL5k14g X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739314289; x=1739919089; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y5xSVhRiQUHuJeHocslUI3HV6Wv1hLUrZwIyEA1RiQE=; b=l655qWhAWybPccb0V79k1vEjGdEnw6XyUw/h+Lc6vMhkTs7p8B2NIT0hTDjV0miHrX Xoj/irexZIyvOhEyIkee2k2rufPVuRoE7M4kUiOB029Kwr3S7NVjhJKunZ8KjDSo0W+Y HJWgEWlK8EtNtXuBlMwZfYkSsCQB6NZk5WjPmRUEGcczcrJ/7Wb/v6V0fWk4GqVaf9w2 ZpIrN74dLu6G7zWeqTuLxXWXCRJBkc8txB6Sj+HCT1u7x8Fm9CSSsmFWFmOjiBFHMVRA u4QfSP0YdylNVXQIUqOhkGHpLuPTZHRBrNFJjbVgglpqIQkNxnsNyrL1y6/HR6vG/rMi cVRg== X-Gm-Message-State: AOJu0YzP7NEdNQthQyPdDfAtbYIhtN8zpTKZ3yTYdINf7VhdF5bjV6b6 1We1fnqT52gFwngNG/v8Iv47W3e7+HmRHxiwbk3GMHFJ5tH6p4f57eVhYIG6ij907LuRN6YRI8C xax1hrXvAGPpXMQ7MQnsKzlpN5yoaY6E9028CMHmqilT7ZYCohvrKMTDNw6E1HA1VaRnIpQRfXB flInMY7WEqaJG+lmlgXlRlfQt8rIniCPrKAQ== X-Gm-Gg: ASbGncu1WVR7TGXH4xccXpV3s3uW3T2P2rJ6TNsjBCVhOinkJI1CclzRAtigNOe26tU yEq/iyawim06yZ64eRDJ8GjJ7f5qerAtFbBRavi/65sg/Yk9uxq8gonN2dqIGlL5lmIK1sBRLNg ol8Rwm84TwDlc6E2P6DAW7AAxRgPojfEB4JSj+e+Posl78KOFXryzYOH0EPpeO1ErLbHKwb4Wnc lu7uHvKyMx1gqBokcBBm5WSx8nHxcc48CXTGII64OIHU/yaxwP7uglv X-Received: by 2002:a05:6820:1f07:b0:5fa:3bf1:f9df with SMTP id 006d021491bc7-5fca301f2b6mr321843eaf.8.1739314288836; Tue, 11 Feb 2025 14:51:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEa6jrKb3Gv58ndFAt0lsM7U7Kaesv3JiRWt8J6hqSNIqM0hfeScNvVAW30jnDEesyup1soiQ== X-Received: by 2002:a05:6820:1f07:b0:5fa:3bf1:f9df with SMTP id 006d021491bc7-5fca301f2b6mr321834eaf.8.1739314288496; Tue, 11 Feb 2025 14:51:28 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org, Stefan Hajnoczi Cc: peterx@redhat.com, Paolo Bonzini , David Hildenbrand , William Roche Subject: [PULL 14/14] system/physmem: poisoned memory discard on reboot Date: Tue, 11 Feb 2025 17:50:58 -0500 Message-ID: <20250211225059.182533-15-peterx@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250211225059.182533-1-peterx@redhat.com> References: <20250211225059.182533-1-peterx@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=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -35 X-Spam_score: -3.6 X-Spam_bar: --- X-Spam_report: (-3.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.54, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1739314323790019100 Content-Type: text/plain; charset="utf-8" From: William Roche Repair poisoned memory location(s), calling ram_block_discard_range(): punching a hole in the backend file when necessary and regenerating a usable memory. If the kernel doesn't support the madvise calls used by this function and we are dealing with anonymous memory, fall back to remapping the location(s). Signed-off-by: William Roche Acked-by: David Hildenbrand Link: https://lore.kernel.org/r/20250211212707.302391-3-william.roche@oracl= e.com Signed-off-by: Peter Xu --- system/physmem.c | 57 ++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/system/physmem.c b/system/physmem.c index af1175a57c..67bdf631e6 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -2275,6 +2275,23 @@ void qemu_ram_free(RAMBlock *block) } =20 #ifndef _WIN32 +/* Simply remap the given VM memory location from start to start+length */ +static int qemu_ram_remap_mmap(RAMBlock *block, uint64_t start, size_t len= gth) +{ + int flags, prot; + void *area; + void *host_startaddr =3D block->host + start; + + assert(block->fd < 0); + flags =3D MAP_FIXED | MAP_ANONYMOUS; + flags |=3D block->flags & RAM_SHARED ? MAP_SHARED : MAP_PRIVATE; + flags |=3D block->flags & RAM_NORESERVE ? MAP_NORESERVE : 0; + prot =3D PROT_READ; + prot |=3D block->flags & RAM_READONLY ? 0 : PROT_WRITE; + area =3D mmap(host_startaddr, length, prot, flags, -1, 0); + return area !=3D host_startaddr ? -errno : 0; +} + /* * qemu_ram_remap - remap a single RAM page * @@ -2292,9 +2309,7 @@ void qemu_ram_remap(ram_addr_t addr) { RAMBlock *block; uint64_t offset; - int flags; - void *area, *vaddr; - int prot; + void *vaddr; size_t page_size; =20 RAMBLOCK_FOREACH(block) { @@ -2310,24 +2325,24 @@ void qemu_ram_remap(ram_addr_t addr) } else if (xen_enabled()) { abort(); } else { - flags =3D MAP_FIXED; - flags |=3D block->flags & RAM_SHARED ? - MAP_SHARED : MAP_PRIVATE; - flags |=3D block->flags & RAM_NORESERVE ? MAP_NORESERVE : = 0; - prot =3D PROT_READ; - prot |=3D block->flags & RAM_READONLY ? 0 : PROT_WRITE; - if (block->fd >=3D 0) { - area =3D mmap(vaddr, page_size, prot, flags, block->fd, - offset + block->fd_offset); - } else { - flags |=3D MAP_ANONYMOUS; - area =3D mmap(vaddr, page_size, prot, flags, -1, 0); - } - if (area !=3D vaddr) { - error_report("Could not remap RAM %s:%" PRIx64 "+%" PR= Ix64 - " +%zx", block->idstr, offset, - block->fd_offset, page_size); - exit(1); + if (ram_block_discard_range(block, offset, page_size) !=3D= 0) { + /* + * Fall back to using mmap() only for anonymous mappin= g, + * as if a backing file is associated we may not be ab= le + * to recover the memory in all cases. + * So don't take the risk of using only mmap and fail = now. + */ + if (block->fd >=3D 0) { + error_report("Could not remap RAM %s:%" PRIx64 "+%" + PRIx64 " +%zx", block->idstr, offset, + block->fd_offset, page_size); + exit(1); + } + if (qemu_ram_remap_mmap(block, offset, page_size) !=3D= 0) { + error_report("Could not remap RAM %s:%" PRIx64 " += %zx", + block->idstr, offset, page_size); + exit(1); + } } memory_try_enable_merging(vaddr, page_size); qemu_ram_setup_dump(vaddr, page_size); --=20 2.47.0