From patchwork Mon Nov 25 14:27:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?=E2=80=9CWilliam_Roche?= X-Patchwork-Id: 13885040 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7EBCCD58D50 for ; Mon, 25 Nov 2024 14:29:29 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tFa3v-0007ZO-5V; Mon, 25 Nov 2024 09:27:39 -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 1tFa3t-0007Ys-BU; Mon, 25 Nov 2024 09:27:37 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tFa3r-0001Ud-G5; Mon, 25 Nov 2024 09:27:37 -0500 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AP6fcpQ006728; Mon, 25 Nov 2024 14:27:28 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=5A679e/2eKPMHKee/atd42aBQUwZQOa2Q2ipO/drAnU=; b= fS8mnoCWDxPD4D7IbS6ZmTD6KMxswooS3NMRrr1PQHMrctCAQEZH6xQ30y6+s9pq 4w11xL8Po3/cDKdZAhaJpYY02BPDgBw+dPebsWbU7jPzwd9F86eWkr1dmgMIEwEx BQAso5q+TgKx0JFR6/aukiGYO1pVEzOjQZPGbHoWPB3EtKYmaHjo8hAMDRvHTvYv aG6x5Le0U6Dtt/O3lbnDIQHhibZ8iKpR7DZ6/ih2CnoU3JwQOgOTwSGAEfQPEGLY Y9DEnN0GZ4clcebfer7tSffEivJ1dVYIcT2VliJ2jtYMxiQt4FG8pGvDC33JmNls SakicwgcPTUM5VDBVWbk/A== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 43384au7a5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 25 Nov 2024 14:27:28 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4APE8bJ8023429; Mon, 25 Nov 2024 14:27:27 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2048.outbound.protection.outlook.com [104.47.66.48]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 4335g7k1m7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 25 Nov 2024 14:27:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=rg2v6gWs7BotvhmioxEi1gIjPugmiBiKb+OZamyKeNZ3IfTTcyvhI7fxMjjmUz9nbyQpI/fbc4QmOI7RpTQdp8Pb14IJes3CjjFjnrV6UL/YfLfIvOeB6+Kqs7EXWoq7QrfZC0TSFQ9Z8zwO6ezrEO1nnAcDyGrvOnxJqLyDv+yIaN4CRBsYnZdXXst1gusxVyJD6fk0o0s3aBSUQL1y2DxU/LI5p9VZCyu23HURMWq8skFUU/eFYpOOfzvvdsW/gnefZ7gzvS3Xw9kWCfT2o5fl6ADQaW1jze3nZYm1nPKJzfy0oofaiam8pYi5QnzowqoGvF7Tlypj9pKKXlPR0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5A679e/2eKPMHKee/atd42aBQUwZQOa2Q2ipO/drAnU=; b=neP/EE9CxwxiObHr43g5fCMzJ0WpVAThWUMMMgqR6MKSxEnK1lvGBLmyGJ+8Tqkf629ngDk0lGZfFv6V2nT2ceZ5X2xKHtD/+bvbvsLfeyizsHL6ZMqwFm21Pz6tI32FSaogPBHpzyF0V4sf0GV+bVNIUGOaCueYOwkwhH6oWMUOkeI9UWcQbHcaBrtNOKnEn3/CZBw6cILN+Hwj9V+XcF7qcMxfjHlXrYoAiFuVyhDvDsgn1gEhpVD9q+haxfrUvPPNp+Mcx50QlWugmjYkrE0f/oqV2eM6jtQBRUtGsenCUcPxp4bfNcy4jQSz8+V2AtQFGkmG2hUc//zNnBud3g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5A679e/2eKPMHKee/atd42aBQUwZQOa2Q2ipO/drAnU=; b=IKz8XjLna9T3HnN10McmnY2yJp4TpVz+FctYooKeA/ReBnYQpMi/g3sDQI9MCvfmDwCsNFGMO//ugATfmP6qOC9c3Irn2OJgOYLB/cXzUTUGEPaGVjqCVOr2xsONCj39jXiTDAom0sMeEGFcVyaibrJBR0EVxD1NRX5P/fEDL/I= Received: from CH3PR10MB7329.namprd10.prod.outlook.com (2603:10b6:610:12c::16) by IA1PR10MB5995.namprd10.prod.outlook.com (2603:10b6:208:3ed::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8182.21; Mon, 25 Nov 2024 14:27:23 +0000 Received: from CH3PR10MB7329.namprd10.prod.outlook.com ([fe80::f238:6143:104c:da23]) by CH3PR10MB7329.namprd10.prod.outlook.com ([fe80::f238:6143:104c:da23%4]) with mapi id 15.20.8182.018; Mon, 25 Nov 2024 14:27:23 +0000 From: =?utf-8?q?=E2=80=9CWilliam_Roche?= To: david@redhat.com, kvm@vger.kernel.org, qemu-devel@nongnu.org, qemu-arm@nongnu.org Cc: william.roche@oracle.com, peterx@redhat.com, pbonzini@redhat.com, richard.henderson@linaro.org, philmd@linaro.org, peter.maydell@linaro.org, mtosatti@redhat.com, imammedo@redhat.com, eduardo@habkost.net, marcel.apfelbaum@gmail.com, wangyanan55@huawei.com, zhao1.liu@intel.com, joao.m.martins@oracle.com Subject: [PATCH v3 2/7] system/physmem: poisoned memory discard on reboot Date: Mon, 25 Nov 2024 14:27:13 +0000 Message-ID: <20241125142718.3373203-3-william.roche@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241125142718.3373203-1-william.roche@oracle.com> References: <20241125142718.3373203-1-william.roche@oracle.com> X-ClientProxiedBy: CY5PR19CA0084.namprd19.prod.outlook.com (2603:10b6:930:69::20) To CH3PR10MB7329.namprd10.prod.outlook.com (2603:10b6:610:12c::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR10MB7329:EE_|IA1PR10MB5995:EE_ X-MS-Office365-Filtering-Correlation-Id: 09049a0e-94ea-4702-976f-08dd0d5d471e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: pilFt6GvHd3DCimyKy1A277mcVavihIo6hFA48TO/qct5or2QzGwW5ixmCpRE+h72N7k/c9/sGZLzbOv7abiIpLu8KlK21eLVxcgYCKQ6zy3JEUP+JVbraxwTnUXFBDe1Dn/sstMeGRzYsit9WFFA5An05WtvqFXrMl+rJO1lf4W8I3RNMXKhLrz/6X0IS+lSkzK5h2z7nMPzGMloOYqqr+nFgzIEZA5jixPUNCpUiTbHM3xzLZnnWO6a8KfAoYBMk7mYiYkfg/3nSgEhfgsp6fr5Ca+H6A9uNk+/3P0JSxHgii92tMdGPaEdPCZaQ0mrSNnJadLglzFS72Qp45BaaPHPl0HAewZ8D/gTYNzjPIljvpLzlHN+PYgCkpXpEQoWhIYlWpGoG9o+itIKuIJUqRoKldf60T8XTPaue9C5smBsdxM90zNjLr1NGP0sR1xmpGFJofxXGiAAsRuyEj5kw6eFHQ7YNcQ8X0EaNCVSQ6fc04mOHzGIc0pcAFk/aOPQZRScmmIiQllHOR1SX0utyXKjpdiVjEu6UR4wllOAiK30WvfXSxTQ5Af1Y+of0GvImOdX+RckrqJtGCOLfD7b6OM8Pr8j1w+luv5SBs91AT9EM26uPEVw/Jpl2BZer0yzuuv6wQ8uzYcT8vvIxwfglN3kl+OdtF35J2jqUX9S5nOMEi0T7aVMgbVnUCoElGFcVKHLr5u9wATnEz3TeTadnlEmBKw3MLDaKnDbDdnLrsa9kX2SPI5NvmmcJ7QGZ9doF2rnmp8ItoyFm5j/rDvwTbOBbvO8IHuBtvMftGYRS38OTR8n1n0VChoQPB6AJTSw4lqBPL3PHdH7oZwiNq8CT9XE3tnD84Tr90Hwmd8ncj9msTbztNk0Bj79ur54e4SKmJplNM5xgSwDsdUDoBOentq+KkffeygGRLvtA3gokCuMvMSreoelGscHd95xFKgEu8h/Hta/eOiFhHLZ4n02k0oUIRVEZrA5ZOK+xqTyMIyvUkNM0Nl8dtElCdVBlnb9cVrgcGuzbQJcbeUAe8Waw/9mg9Ne/Nth0Oogfq1tHSs7nSCOXAZV+m8jsWImBq2MEmRCTn0zuyoCQJi1JvxQnVM+OnCSYKQVcfFRb06XXb282W4cil/bWw4Zm+CyYMLjCoUvrSyAk6TNkgBS7MKjSe5W7ou43cPea12L8r3otKnnx2FxSp+T5jT5S2WFx2DPUFrQTcPJFPXWz86kytGRZ24YI27vN5rSlJZBCmuOjoQ15YV6wbwLOHcZtXYcCWtdkyFEpKp736qz337mjJbrdvIj36rBDy22lzXYiNDCZsVT89WgGqWhqqfLzmlCB40FbnhnULNdAI0h3m5A+SNwA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR10MB7329.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(7416014)(376014)(366016)(1800799024); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RPzxHquCn6+4M3wqKZWcYYoJzmx4M+iWN3Nf3ifR4QPHnlLKdiViWZpA0tRbDns/7VC9VEkHgvBYNZsmm3qWdRHByk7DsZ19MbgWOq7RaYPiHB7II6L0km7Nz/vWoj1BP09RM09YaRm2DsqwHdnXBUDVxjphioZAc1Ae3VY+VYQQ/8w7LuZW2t0csaGgvZlShrrqEpc4Lk8dZS9IgsJX7cnrpxBXjexOO9IV1rL5xfaaWWe2lVIZhIyrqTxpssSXi4Fxo97kY1sIW5JTkItibAVkhgnXtiOUC98aaqI2eYu2X+DjyJshjYRopWviG+TaPH6wTKFRduaBBu9VcT3GKjFV2xV6OiwoShZ5vjNfT2iEW0hiYLLrEP03HbyIa4qXRGq7EMY9WkM2lBQQoApzV6vn0rucBYLazTacY2vz/amefg8ME7tWIjUB9WpP7kNTYK3FzCLftHtO8nfIEzYS5w59yjWwCewxG3WvaSTvLL47E9D89WJeHD7rm+pFk94wsFxp+yL0/WgYji3owabT2t0nw47aDh6Tyb/cq/j3JGo3hkxnK1CZUvYU2MnpAkpb5o5C4av+NkL3xERX4aCSY3irc1X6v90+UGjcDJEOg5h1625i3IwSUKgYVekYwPJL3oJu8a170ee54MYThcS/10/DmC+re/vumDB9lj55orXa566Iqyr2F/0CdX7BXATDm4M6Apmxr0b75JB1vnqw6AFlUdsDNr1nsMJqoRihbSVJOz+EqYLnveUM526RKyxBuHmpEKeZ4FPNYf7IJHfO1BK++/MnfGHJPJfdH4jJiNKhGdXXQseqy7MQKE7xFeOeqEgEaJID+wvhBJg1ynnxDCjcwguo0x8Rw3Hq8w1LxgquFnvHiTMjGcLjz7XjuJADeXs9iubTNkUBga2TvuJpCrST7ZJqune8Tqxz50aYmERiWmlPZd/2rtOUhtpCs2oznlvudB6It9r734opVHnBomf36v17u3th1ImVmTpkluC48Q/q5ZqOj64xZE7n9kC7biCNcB2tVugCOmIisXg8ABIhS07rdYsm8lVUL4gBteYMoMYyKB3M7/kcZHOTkVuj58HdjpJUX0PIXBvuOAQ2aaYj37asoEqq/BW2QoL+5VC8eaUQordb3ZbPL+QfLJprZWWk1i/p3S3zVAw+6JJ1s8+PbMWTNDk/VSz+wumb6KM3MLwY5TfIyiF1BPAHUZkFTZN1LAf8knngW6qC2R3+sEQZVyqtSStzB7lMHXiEECIGA0HO03eWTcSUIzP6bB3HK/MVHAqqiqIwa6QNci+qU+/0oLJjl2iD7UUw0cVU+EHv0vb21cYFwPlYJBpKsp1vAD8ZknnFWR4tlWcK7B0665oMrjd4z0jfmZyvHUwq/Udh+eupVKAyTD8d1NkbsGlYeFS8FsYYUaH3jqguDAw5q3BW55haxlLg4/FBdVK/XywceGB1KWCTvZiOFY40xCmqfxvSgIzUXFLJBsIy6vxr8JXlfWQaWus825nbai+igr/Rc6IQZEQIopkU7bKdTnODQle0vYN5Rolu5qEKP3XPc8zXMvSwcM5ZinL6WhNx4Ll1aNUA8w+N+XYQDQNM8XRb1ksVm355JTM00mpRQE4ERw== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: eVXE1CAPJ7oIf4EtVqrt2wXfFVTkRftmd8Jd1+MUQbUJvhjH9nnuRWXMFzLkWUccz6QVFCwFrgjmpvjrZis1t0xq74rTmSKy3x9byvX96XCUC+20VlpbRDdrcFX7hkCPLf0XN3JXBWhbmixp117e63nZy/W/0pSE4s0lOohCUB/y7Lp4h6CFvtF0Vzvr3Si2TxWWa7KfL/7PQuCuoX4TEhAg1bFU1PIMGOKcXIwRvJTnYAVGOhVFFz/D2M27uJ3y3gI9txjIxlg4Pu8xLxlEJAypgmetpS4nwfvurONOns7xybCdsU/BJlTREUyKXhv4VmAHsfyfsUQPrdDdM/AT9ffJ7WCd4yB4Izc+2+paIsG7FiXhg67b++QQZcYvonD0omImDmKgwlpMK/Mnwi5B7A7F6vmfief1eKoPcxGdE1GHVwGFU2HOdMJoT80fBRRA2A5XL4N8pK6Wir4vgkypzXNqf9feoruyIjnEaMTv1R1qRRHWx5F21mHkhCbv+kDnroK+ixqDtCoNz0RXidlDTuj/grujxEFydlP/m9nWuGVdgtj4m7SCm2Gxetx3PKJenCBvDxVWUBesHip+J6wcGL79Yjx5n8CcDbYCh6qlsxQ= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 09049a0e-94ea-4702-976f-08dd0d5d471e X-MS-Exchange-CrossTenant-AuthSource: CH3PR10MB7329.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Nov 2024 14:27:23.4404 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: /AkAggE2991FepsoP8oGJdA8kOjL+cEl9UA6TzEm7W3qpP3mdljkVvPyycg5CGVr3GgkNBQ/kCG8vf+VlBq9+uVGeysPhELmZlu5+1iOkQ0= X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB5995 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-25_09,2024-11-25_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 spamscore=0 suspectscore=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411250122 X-Proofpoint-ORIG-GUID: lyZC0jhQefSxZsPpAel929IbpQeXg13r X-Proofpoint-GUID: lyZC0jhQefSxZsPpAel929IbpQeXg13r Received-SPF: pass client-ip=205.220.177.32; envelope-from=william.roche@oracle.com; helo=mx0b-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: William Roche Repair memory locations, calling ram_block_discard_range(), punching a hole in the backend file when necessary and regenerate a usable memory. Fall back to unmap/remap the memory location(s) if the kernel doesn't support the madvise calls used by ram_block_discard_range(). Signed-off-by: William Roche --- system/physmem.c | 69 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/system/physmem.c b/system/physmem.c index 410eabd29d..26711df2d2 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -2180,13 +2180,37 @@ void qemu_ram_free(RAMBlock *block) } #ifndef _WIN32 +/* Try to recover the given location using mmap */ +static void qemu_ram_remap_mmap(RAMBlock *block, void* vaddr, size_t size, + ram_addr_t offset) +{ + int flags, prot; + void *area; + + flags = MAP_FIXED; + flags |= block->flags & RAM_SHARED ? MAP_SHARED : MAP_PRIVATE; + flags |= block->flags & RAM_NORESERVE ? MAP_NORESERVE : 0; + prot = PROT_READ; + prot |= block->flags & RAM_READONLY ? 0 : PROT_WRITE; + if (block->fd >= 0) { + area = mmap(vaddr, size, prot, flags, block->fd, + offset + block->fd_offset); + } else { + flags |= MAP_ANONYMOUS; + area = mmap(vaddr, size, prot, flags, -1, 0); + } + if (area != vaddr) { + error_report("Could not remap addr: " RAM_ADDR_FMT "@" RAM_ADDR_FMT "", + size, addr); + exit(1); + } +} + void qemu_ram_remap(ram_addr_t addr) { RAMBlock *block; ram_addr_t offset; - int flags; - void *area, *vaddr; - int prot; + void *vaddr; size_t page_size; RAMBLOCK_FOREACH(block) { @@ -2202,27 +2226,26 @@ void qemu_ram_remap(ram_addr_t addr) } else if (xen_enabled()) { abort(); } else { - flags = MAP_FIXED; - flags |= block->flags & RAM_SHARED ? - MAP_SHARED : MAP_PRIVATE; - flags |= block->flags & RAM_NORESERVE ? MAP_NORESERVE : 0; - prot = PROT_READ; - prot |= block->flags & RAM_READONLY ? 0 : PROT_WRITE; - if (block->fd >= 0) { - area = mmap(vaddr, page_size, prot, flags, block->fd, - offset + block->fd_offset); - } else { - flags |= MAP_ANONYMOUS; - area = mmap(vaddr, page_size, prot, flags, -1, 0); - } - if (area != vaddr) { - error_report("Could not remap addr: " - RAM_ADDR_FMT "@" RAM_ADDR_FMT "", - page_size, addr); - exit(1); + if (ram_block_discard_range(block, offset + block->fd_offset, + length) != 0) { + /* + * Fold back to using mmap(), but it cannot zap pagecache + * pages, only anonymous pages. As soon as we might have + * pagecache pages involved (either private or shared + * mapping), we must be careful. + * We don't take the risk of using mmap and fail now. + */ + if (block->fd >= 0 && (qemu_ram_is_shared(block) || + (length > TARGET_PAGE_SIZE))) { + error_report("Memory poison recovery failure addr: " + RAM_ADDR_FMT "@" RAM_ADDR_FMT "", + length, addr); + exit(1); + } + qemu_ram_remap_mmap(block, vaddr, page_size, offset); + memory_try_enable_merging(vaddr, size); + qemu_ram_setup_dump(vaddr, size); } - memory_try_enable_merging(vaddr, page_size); - qemu_ram_setup_dump(vaddr, page_size); } break;