public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Chenyi Qiang <chenyi.qiang@intel.com>
To: "David Hildenbrand" <david@redhat.com>,
	"Alexey Kardashevskiy" <aik@amd.com>,
	"Peter Xu" <peterx@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Michael Roth" <michael.roth@amd.com>
Cc: <qemu-devel@nongnu.org>, <kvm@vger.kernel.org>,
	Williams Dan J <dan.j.williams@intel.com>,
	Peng Chao P <chao.p.peng@intel.com>,
	Gao Chao <chao.gao@intel.com>, Xu Yilun <yilun.xu@intel.com>,
	Li Xiaoyao <xiaoyao.li@intel.com>
Subject: Re: [PATCH v3 6/7] memory: Attach MemoryAttributeManager to guest_memfd-backed RAMBlocks
Date: Fri, 14 Mar 2025 16:21:04 +0800	[thread overview]
Message-ID: <8d9ff645-cfc2-4789-9c13-9275103fbd8c@intel.com> (raw)
In-Reply-To: <20250310081837.13123-7-chenyi.qiang@intel.com>

Hi David & Alexey,

To keep the bitmap aligned, I add the undo operation for
set_memory_attributes() and use the bitmap + replay callback to do
set_memory_attributes(). Does this change make sense?

Alexey, I didn't add your Reivewed-by since this patch introduced some
new changes.

On 3/10/2025 4:18 PM, Chenyi Qiang wrote:
> Introduce a new field, memory_attribute_manager, in RAMBlock to link to
> an MemoryAttributeManager object. This change centralizes all
> guest_memfd state information (like fd and shared_bitmap) within a
> RAMBlock, making it easier to manage.
> 
> Use the realize()/unrealize() helpers to initialize/uninitialize the
> MemoryAttributeManager object. Register/unregister the object in the
> target RAMBlock's MemoryRegion when creating guest_memfd.
> 
> In the kvm_convert_memory() function, manage memory state changes by
> using the shared_bitmap to call set_attribute() only on the specific
> memory range. Additionally, use the
> memory_attribute_manager_state_change() helper to notify the reigstered
> RamDiscardListener of these changes.
> 
> Signed-off-by: Chenyi Qiang <chenyi.qiang@intel.com>
> ---
> Changes in v3:
>     - Use ram_discard_manager_reply_populated/discarded() to set the
>       memory attribute and add the undo support if state_change()
>       failed.
>     - Didn't add Reviewed-by from Alexey due to the new changes in this
>       commit.
> 
> Changes in v2:
>     - Introduce a new field memory_attribute_manager in RAMBlock.
>     - Move the state_change() handling during page conversion in this patch.
>     - Undo what we did if it fails to set.
>     - Change the order of close(guest_memfd) and memory_attribute_manager cleanup.
> ---
>  accel/kvm/kvm-all.c     | 50 +++++++++++++++++++++++++++++++++++++++--
>  include/exec/ramblock.h |  2 ++
>  system/physmem.c        | 13 +++++++++++
>  3 files changed, 63 insertions(+), 2 deletions(-)
> 
> diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c
> index c1fea69d58..a89c5655e8 100644
> --- a/accel/kvm/kvm-all.c
> +++ b/accel/kvm/kvm-all.c
> @@ -48,6 +48,7 @@
>  #include "kvm-cpus.h"
>  #include "system/dirtylimit.h"
>  #include "qemu/range.h"
> +#include "system/memory-attribute-manager.h"
>  
>  #include "hw/boards.h"
>  #include "system/stats.h"
> @@ -3018,6 +3019,25 @@ static void kvm_eat_signals(CPUState *cpu)
>      } while (sigismember(&chkset, SIG_IPI));
>  }
>  
> +typedef struct SetMemoryAttribute {
> +    bool to_private;
> +} SetMemoryAttribute;
> +
> +static int kvm_set_memory_attributes_cb(MemoryRegionSection *section,
> +                                        void *opaque)
> +{
> +    hwaddr start = section->offset_within_address_space;
> +    hwaddr size = section->size;
> +    SetMemoryAttribute *args = opaque;
> +    bool to_private = args->to_private;
> +
> +    if (to_private) {
> +        return kvm_set_memory_attributes_private(start, size);
> +    } else {
> +        return kvm_set_memory_attributes_shared(start, size);
> +    }
> +}
> +
>  int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)
>  {
>      MemoryRegionSection section;
> @@ -3026,6 +3046,7 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)
>      RAMBlock *rb;
>      void *addr;
>      int ret = -EINVAL;
> +    SetMemoryAttribute args = { .to_private = to_private };
>  
>      trace_kvm_convert_memory(start, size, to_private ? "shared_to_private" : "private_to_shared");
>  
> @@ -3077,9 +3098,13 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)
>      }
>  
>      if (to_private) {
> -        ret = kvm_set_memory_attributes_private(start, size);
> +        ret = ram_discard_manager_replay_populated(mr->rdm, &section,
> +                                                   kvm_set_memory_attributes_cb,
> +                                                   &args);
>      } else {
> -        ret = kvm_set_memory_attributes_shared(start, size);
> +        ret = ram_discard_manager_replay_discarded(mr->rdm, &section,
> +                                                   kvm_set_memory_attributes_cb,
> +                                                   &args);
>      }
>      if (ret) {
>          goto out_unref;
> @@ -3088,6 +3113,27 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private)
>      addr = memory_region_get_ram_ptr(mr) + section.offset_within_region;
>      rb = qemu_ram_block_from_host(addr, false, &offset);
>  
> +    ret = memory_attribute_manager_state_change(MEMORY_ATTRIBUTE_MANAGER(mr->rdm),
> +                                                offset, size, to_private);
> +    if (ret) {
> +        warn_report("Failed to notify the listener the state change of "
> +                    "(0x%"HWADDR_PRIx" + 0x%"HWADDR_PRIx") to %s",
> +                    start, size, to_private ? "private" : "shared");
> +        args.to_private = !to_private;
> +        if (to_private) {
> +            ret = ram_discard_manager_replay_populated(mr->rdm, &section,
> +                                                       kvm_set_memory_attributes_cb,
> +                                                       &args);
> +        } else {
> +            ret = ram_discard_manager_replay_discarded(mr->rdm, &section,
> +                                                       kvm_set_memory_attributes_cb,
> +                                                       &args);
> +        }
> +        if (ret) {
> +            goto out_unref;
> +        }
> +    }
> +
>      if (to_private) {
>          if (rb->page_size != qemu_real_host_page_size()) {
>              /*
> diff --git a/include/exec/ramblock.h b/include/exec/ramblock.h
> index 0babd105c0..06fd365326 100644
> --- a/include/exec/ramblock.h
> +++ b/include/exec/ramblock.h
> @@ -23,6 +23,7 @@
>  #include "cpu-common.h"
>  #include "qemu/rcu.h"
>  #include "exec/ramlist.h"
> +#include "system/memory-attribute-manager.h"
>  
>  struct RAMBlock {
>      struct rcu_head rcu;
> @@ -42,6 +43,7 @@ struct RAMBlock {
>      int fd;
>      uint64_t fd_offset;
>      int guest_memfd;
> +    MemoryAttributeManager *memory_attribute_manager;
>      size_t page_size;
>      /* dirty bitmap used during migration */
>      unsigned long *bmap;
> diff --git a/system/physmem.c b/system/physmem.c
> index c76503aea8..0ed394c5d2 100644
> --- a/system/physmem.c
> +++ b/system/physmem.c
> @@ -54,6 +54,7 @@
>  #include "system/hostmem.h"
>  #include "system/hw_accel.h"
>  #include "system/xen-mapcache.h"
> +#include "system/memory-attribute-manager.h"
>  #include "trace.h"
>  
>  #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
> @@ -1885,6 +1886,16 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
>              qemu_mutex_unlock_ramlist();
>              goto out_free;
>          }
> +
> +        new_block->memory_attribute_manager = MEMORY_ATTRIBUTE_MANAGER(object_new(TYPE_MEMORY_ATTRIBUTE_MANAGER));
> +        if (memory_attribute_manager_realize(new_block->memory_attribute_manager, new_block->mr)) {
> +            error_setg(errp, "Failed to realize memory attribute manager");
> +            object_unref(OBJECT(new_block->memory_attribute_manager));
> +            close(new_block->guest_memfd);
> +            ram_block_discard_require(false);
> +            qemu_mutex_unlock_ramlist();
> +            goto out_free;
> +        }
>      }
>  
>      ram_size = (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS;
> @@ -2138,6 +2149,8 @@ static void reclaim_ramblock(RAMBlock *block)
>      }
>  
>      if (block->guest_memfd >= 0) {
> +        memory_attribute_manager_unrealize(block->memory_attribute_manager);
> +        object_unref(OBJECT(block->memory_attribute_manager));
>          close(block->guest_memfd);
>          ram_block_discard_require(false);
>      }


  reply	other threads:[~2025-03-14  8:21 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-10  8:18 [PATCH v3 0/7] Enable shared device assignment Chenyi Qiang
2025-03-10  8:18 ` [PATCH v3 1/7] memory: Export a helper to get intersection of a MemoryRegionSection with a given range Chenyi Qiang
2025-03-10  8:18 ` [PATCH v3 2/7] memory: Change memory_region_set_ram_discard_manager() to return the result Chenyi Qiang
2025-03-10  8:18 ` [PATCH v3 3/7] memory: Unify the definiton of ReplayRamPopulate() and ReplayRamDiscard() Chenyi Qiang
2025-03-17  3:00   ` Kishen Maloor
2025-03-17  4:11     ` Chenyi Qiang
2025-03-10  8:18 ` [PATCH v3 4/7] memory-attribute-manager: Introduce MemoryAttributeManager to manage RAMBLock with guest_memfd Chenyi Qiang
2025-03-14 12:11   ` Gupta, Pankaj
2025-03-17  2:54     ` Chenyi Qiang
2025-03-17 10:36       ` David Hildenbrand
2025-03-17 17:01         ` Gupta, Pankaj
2025-03-18  1:54           ` Chenyi Qiang
2025-03-19  8:55             ` Gupta, Pankaj
2025-03-19 11:23               ` Chenyi Qiang
2025-03-19 11:56                 ` Gupta, Pankaj
2025-03-20  3:15                   ` Chenyi Qiang
2025-03-24  4:04                     ` Chenyi Qiang
2025-03-10  8:18 ` [PATCH v3 5/7] memory-attribute-manager: Introduce a callback to notify the shared/private state change Chenyi Qiang
2025-03-10  8:18 ` [PATCH v3 6/7] memory: Attach MemoryAttributeManager to guest_memfd-backed RAMBlocks Chenyi Qiang
2025-03-14  8:21   ` Chenyi Qiang [this message]
2025-03-14  9:00     ` David Hildenbrand
2025-03-14  9:30       ` Chenyi Qiang
2025-03-14  9:50         ` David Hildenbrand
2025-03-14 10:23           ` Chenyi Qiang
2025-03-31  8:55             ` Chenyi Qiang
2025-03-17  6:18   ` Tony Lindgren
2025-03-17  7:32     ` Chenyi Qiang
2025-03-17  9:45       ` Tony Lindgren
2025-03-17 10:21         ` Chenyi Qiang
2025-03-17 11:07           ` Tony Lindgren
2025-03-10  8:18 ` [PATCH v3 7/7] RAMBlock: Make guest_memfd require coordinate discard Chenyi Qiang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8d9ff645-cfc2-4789-9c13-9275103fbd8c@intel.com \
    --to=chenyi.qiang@intel.com \
    --cc=aik@amd.com \
    --cc=chao.gao@intel.com \
    --cc=chao.p.peng@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=david@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=michael.roth@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=xiaoyao.li@intel.com \
    --cc=yilun.xu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox