From nobody Sat May 4 04:35:01 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=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1677755415; cv=none; d=zohomail.com; s=zohoarc; b=P5wcP+qLnFVyoIoJVrBAjJjdX3INkM9Fd0hqSWofAOT2BNLxnPUFJEQ7oQfXnFWoNMdzfWyuiZlUwGdQMblo1tUYvmjVuTcMNAohAWs10BU1qcFgTvDUl0w4fNYjJa/XyY8tsdLOLgorTUaUAOCw1vhENQgNTzUI4rtJiJ/OsXk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677755415; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=ZudvcLwMvUbYZ9rjSI6+sx+hNXnP93f2AUoNGYExEG8=; b=e4YfbfOoKhuJm5nipRQiKLQmRiARuMJz7eWvfDJlkmVFMZOLgWzLu0lKHOeqgffomukjn3vNv1btPKOfd4Xw1lNME+RtsUQaonGmrBv6FDilK4QPhm74kixjsocABJyw1r1apvmpnE2URxBSy6V9aCUptS6hPKHCgTOap3ZRUTM= 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 1677755415352274.02523664002956; Thu, 2 Mar 2023 03:10:15 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pXgoZ-0000PB-Ko; Thu, 02 Mar 2023 06:09:35 -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 1pXgoX-0000Ip-By for qemu-devel@nongnu.org; Thu, 02 Mar 2023 06:09:33 -0500 Received: from mail-qt1-x832.google.com ([2607:f8b0:4864:20::832]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pXgoV-0001m5-Bg for qemu-devel@nongnu.org; Thu, 02 Mar 2023 06:09:33 -0500 Received: by mail-qt1-x832.google.com with SMTP id r5so17546578qtp.4 for ; Thu, 02 Mar 2023 03:09:27 -0800 (PST) Received: from localhost ([93.115.195.2]) by smtp.gmail.com with ESMTPSA id g186-20020a37b6c3000000b007430494ab92sm1498404qkf.67.2023.03.02.03.09.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 03:09:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZudvcLwMvUbYZ9rjSI6+sx+hNXnP93f2AUoNGYExEG8=; b=D2RcFYPotMEfg7lRsVR1AzKKaNu/QrUO9yMo7J25O1O15bkkLUFF7BIhn2lluGaA/O uqfujbZPJjXxpb3iG/+m8xpv7X/9jXRZSyu8jnDQlgy0SQB+v8Wu/9GulFk4zRbfs3Sb rVZ2pXC9KKAjklzix70nNs2Rwz0N+DlE4tBsKMzNOEA5sMUzI1rTg2G+gSu4VloG1XzL OjVR9YsDc9CrFAV2OGSniL0CR6dG7aAI3PxucctQOd3V5KOx3fW/aAVZlYgZNmUXaywm 2QAry1gcNZHvqPyztgC/LYOgn+WrY6NYNWTA3m53FuSklnQn+R8toTVwh3JnqVBxhwZ7 86aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZudvcLwMvUbYZ9rjSI6+sx+hNXnP93f2AUoNGYExEG8=; b=W5o4g/pLLbuonUo5qbiO38lLk8EcCAXwPPJqA0J/CsvYzA44K1iHluDSchRfS+xXqc 8RLSUDhfwQ8qHGjdh42hgqIH9JvJXtwgbEfnafbqSnX14yRS+YOtU2eb4p3AzE+43Q+G qZCrpxmWAeS42+bYKInd6ZxJ11nBiCobVK9+r0QqTvqfKpvdr74mT9gy5eRBOv1KzZ7y v0uIGXdGq9kHt5il6LoSfJxelEzvxVbUOn/Fj18UkLX6AKeQYrGL/nS4iggvaYcSwFh2 AhEXLa3cXbSprOLOuKV3YyeRy6wzJQhgmSaV5bWPgS3ey0kscku/A4r7MHT4EWoRYpHl yzwg== X-Gm-Message-State: AO0yUKVDR1Cba418bBfR+8T64848x51jrBL1mmknI7W8WYlpJ0GLFcOx AQLuiJnBPdeGCjRFUfQs+xaPG7PyiZVcdhxelZU= X-Google-Smtp-Source: AK7set9aGs/LaqeyW8wd6ORIGsUjO323cTL+h5JOeZMszHsoF+OVeD79Gc72diHr3YJ6zlzz/k3I6g== X-Received: by 2002:a05:622a:514:b0:3b9:bc8c:c213 with SMTP id l20-20020a05622a051400b003b9bc8cc213mr2313861qtx.30.1677755366478; Thu, 02 Mar 2023 03:09:26 -0800 (PST) From: Fam Zheng To: qemu-devel@nongnu.org Cc: Eduardo Habkost , Peter Xu , fam@euphon.net, Igor Mammedov , Markus Armbruster , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , David Hildenbrand , Eric Blake , Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , stefanha@redhat.com, Fam Zheng Subject: [PATCH] hostmem: Add clear option to file backend Date: Thu, 2 Mar 2023 11:09:25 +0000 Message-Id: <20230302110925.4680-1-fam.zheng@bytedance.com> X-Mailer: git-send-email 2.20.1 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=2607:f8b0:4864:20::832; envelope-from=fam.zheng@bytedance.com; helo=mail-qt1-x832.google.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, 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 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 @bytedance.com) X-ZM-MESSAGEID: 1677755415727100002 Content-Type: text/plain; charset="utf-8" This adds a memset to clear the backing memory. This is useful in the case of PMEM DAX to drop dirty data, if the backing memory is handed over from a previous application or firmware which didn't clean up before exiting. Signed-off-by: Fam Zheng --- backends/hostmem-file.c | 20 ++++++++++++++++++++ include/exec/memory.h | 3 +++ qapi/qom.json | 3 +++ qemu-options.hx | 4 +++- softmmu/physmem.c | 6 +++++- 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/backends/hostmem-file.c b/backends/hostmem-file.c index 25141283c4..f7468d24ce 100644 --- a/backends/hostmem-file.c +++ b/backends/hostmem-file.c @@ -30,6 +30,7 @@ struct HostMemoryBackendFile { bool discard_data; bool is_pmem; bool readonly; + bool clear; }; =20 static void @@ -56,6 +57,7 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Err= or **errp) ram_flags =3D backend->share ? RAM_SHARED : 0; ram_flags |=3D backend->reserve ? 0 : RAM_NORESERVE; ram_flags |=3D fb->is_pmem ? RAM_PMEM : 0; + ram_flags |=3D fb->clear ? RAM_CLEAR : 0; memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name, backend->size, fb->align, ram_flags, fb->mem_path, fb->readonly, errp); @@ -168,6 +170,21 @@ static void file_memory_backend_set_readonly(Object *o= bj, bool value, fb->readonly =3D value; } =20 +static bool file_memory_backend_get_clear(Object *obj, Error **errp) +{ + HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(obj); + + return fb->clear; +} + +static void file_memory_backend_set_clear(Object *obj, bool value, + Error **errp) +{ + HostMemoryBackendFile *fb =3D MEMORY_BACKEND_FILE(obj); + + fb->clear =3D value; +} + static void file_backend_unparent(Object *obj) { HostMemoryBackend *backend =3D MEMORY_BACKEND(obj); @@ -204,6 +221,9 @@ file_backend_class_init(ObjectClass *oc, void *data) object_class_property_add_bool(oc, "readonly", file_memory_backend_get_readonly, file_memory_backend_set_readonly); + object_class_property_add_bool(oc, "clear", + file_memory_backend_get_clear, + file_memory_backend_set_clear); } =20 static void file_backend_instance_finalize(Object *o) diff --git a/include/exec/memory.h b/include/exec/memory.h index 2e602a2fad..3345db5241 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -232,6 +232,9 @@ typedef struct IOMMUTLBEvent { /* RAM that isn't accessible through normal means. */ #define RAM_PROTECTED (1 << 8) =20 +/* Clear these pages when mapping */ +#define RAM_CLEAR (1 << 9) + static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, IOMMUNotifierFlag flags, hwaddr start, hwaddr end, diff --git a/qapi/qom.json b/qapi/qom.json index 30e76653ad..2c4aa5b0d5 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -629,6 +629,8 @@ # specify the required alignment via this option. # 0 selects a default alignment (currently the page size). (defaul= t: 0) # +# @clear: if true, the memory is memset to 0 during init. (default: false) +# # @discard-data: if true, the file contents can be destroyed when QEMU exi= ts, # to avoid unnecessarily flushing data to the backing file.= Note # that ``discard-data`` is only an optimization, and QEMU m= ight @@ -649,6 +651,7 @@ { 'struct': 'MemoryBackendFileProperties', 'base': 'MemoryBackendProperties', 'data': { '*align': 'size', + '*clear': 'bool', '*discard-data': 'bool', 'mem-path': 'str', '*pmem': { 'type': 'bool', 'if': 'CONFIG_LIBPMEM' }, diff --git a/qemu-options.hx b/qemu-options.hx index beeb4475ba..6c8345c62e 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -4859,7 +4859,7 @@ SRST they are specified. Note that the 'id' property must be set. These objects are placed in the '/objects' path. =20 - ``-object memory-backend-file,id=3Did,size=3Dsize,mem-path=3Ddir,share= =3Don|off,discard-data=3Don|off,merge=3Don|off,dump=3Don|off,prealloc=3Don|= off,host-nodes=3Dhost-nodes,policy=3Ddefault|preferred|bind|interleave,alig= n=3Dalign,readonly=3Don|off`` + ``-object memory-backend-file,id=3Did,size=3Dsize,mem-path=3Ddir,share= =3Don|off,clear=3Don|off,discard-data=3Don|off,merge=3Don|off,dump=3Don|off= ,prealloc=3Don|off,host-nodes=3Dhost-nodes,policy=3Ddefault|preferred|bind|= interleave,align=3Dalign,readonly=3Don|off`` Creates a memory file backend object, which can be used to back the guest RAM with huge pages. =20 @@ -4886,6 +4886,8 @@ SRST Documentation/vm/numa\_memory\_policy.txt on the Linux kernel source tree for additional details. =20 + Setting clear=3Don will make QEMU memset the backend to 0. + Setting the ``discard-data`` boolean option to on indicates that file contents can be destroyed when QEMU exits, to avoid unnecessarily flushing data to the backing file. Note that diff --git a/softmmu/physmem.c b/softmmu/physmem.c index df54b917a9..573c686fd1 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -1409,6 +1409,10 @@ static void *file_ram_alloc(RAMBlock *block, return NULL; } =20 + if (block->flags & RAM_CLEAR) { + memset(area, 0, memory); + } + block->fd =3D fd; return area; } @@ -1868,7 +1872,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Mem= oryRegion *mr, =20 /* Just support these ram flags by now. */ assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE | - RAM_PROTECTED)) =3D=3D 0); + RAM_PROTECTED | RAM_CLEAR)) =3D=3D 0); =20 if (xen_enabled()) { error_setg(errp, "-mem-path not supported with Xen"); --=20 2.20.1