* [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments
@ 2026-03-19 19:10 Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 1/8] system/memory: Constify various AddressSpace arguments (flatview) Philippe Mathieu-Daudé
` (8 more replies)
0 siblings, 9 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-03-19 19:10 UTC (permalink / raw)
To: qemu-devel
Cc: Pierrick Bouvier, Paolo Bonzini, Peter Xu,
Philippe Mathieu-Daudé
When structures are only accessed read-only, it is safer to mark
them const to protect against invalid API (ab)uses. This is also
an useful hint to compilers.
Philippe Mathieu-Daudé (8):
system/memory: Constify various AddressSpace arguments (flatview)
system/memory: Constify various AddressSpace arguments (checks)
system/memory: Constify various AddressSpace arguments (xlat)
system/memory: Constify various AddressSpace arguments (flat-range)
system/memory: Constify various AddressSpace arguments (notify)
system/memory: Constify various AddressSpace arguments (cache)
system/memory: Constify various AddressSpace arguments (access)
system/memory: Constify various MemoryRegionCache arguments
include/system/memory.h | 32 +++++++++---------
include/system/memory_cached.h | 20 +++++------
system/memory-internal.h | 5 +--
include/system/memory_ldst_cached.h.inc | 15 +++++----
system/memory.c | 18 +++++-----
system/physmem.c | 44 ++++++++++++++-----------
6 files changed, 73 insertions(+), 61 deletions(-)
--
2.53.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH-for-11.1 1/8] system/memory: Constify various AddressSpace arguments (flatview)
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
@ 2026-03-19 19:10 ` Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 2/8] system/memory: Constify various AddressSpace arguments (checks) Philippe Mathieu-Daudé
` (7 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-03-19 19:10 UTC (permalink / raw)
To: qemu-devel
Cc: Pierrick Bouvier, Paolo Bonzini, Peter Xu,
Philippe Mathieu-Daudé
Mark the AddressSpace structure const when it is only accessed read-only.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/system/memory.h | 2 +-
system/memory-internal.h | 5 +++--
system/memory.c | 2 +-
3 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/include/system/memory.h b/include/system/memory.h
index d7b18b632d5..1417132f6d9 100644
--- a/include/system/memory.h
+++ b/include/system/memory.h
@@ -1201,7 +1201,7 @@ struct FlatView {
MemoryRegion *root;
};
-static inline FlatView *address_space_to_flatview(AddressSpace *as)
+static inline FlatView *address_space_to_flatview(const AddressSpace *as)
{
return qatomic_rcu_read(&as->current_map);
}
diff --git a/system/memory-internal.h b/system/memory-internal.h
index 5f0524756eb..0066ffdffb6 100644
--- a/system/memory-internal.h
+++ b/system/memory-internal.h
@@ -20,12 +20,13 @@ static inline AddressSpaceDispatch *flatview_to_dispatch(FlatView *fv)
return fv->dispatch;
}
-static inline AddressSpaceDispatch *address_space_to_dispatch(AddressSpace *as)
+static inline
+AddressSpaceDispatch *address_space_to_dispatch(const AddressSpace *as)
{
return flatview_to_dispatch(address_space_to_flatview(as));
}
-FlatView *address_space_get_flatview(AddressSpace *as);
+FlatView *address_space_get_flatview(const AddressSpace *as);
void flatview_unref(FlatView *view);
extern const MemoryRegionOps unassigned_mem_ops;
diff --git a/system/memory.c b/system/memory.c
index 17a7bcd9af7..bd12184a879 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -819,7 +819,7 @@ static void address_space_add_del_ioeventfds(AddressSpace *as,
}
}
-FlatView *address_space_get_flatview(AddressSpace *as)
+FlatView *address_space_get_flatview(const AddressSpace *as)
{
FlatView *view;
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH-for-11.1 2/8] system/memory: Constify various AddressSpace arguments (checks)
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 1/8] system/memory: Constify various AddressSpace arguments (flatview) Philippe Mathieu-Daudé
@ 2026-03-19 19:10 ` Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 3/8] system/memory: Constify various AddressSpace arguments (xlat) Philippe Mathieu-Daudé
` (6 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-03-19 19:10 UTC (permalink / raw)
To: qemu-devel
Cc: Pierrick Bouvier, Paolo Bonzini, Peter Xu,
Philippe Mathieu-Daudé
Mark the AddressSpace structure const when it is only accessed read-only.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/system/memory.h | 5 +++--
system/physmem.c | 4 ++--
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/include/system/memory.h b/include/system/memory.h
index 1417132f6d9..858dc40dc5f 100644
--- a/include/system/memory.h
+++ b/include/system/memory.h
@@ -2829,7 +2829,8 @@ static inline MemoryRegion *address_space_translate(AddressSpace *as,
* @is_write: indicates the transfer direction
* @attrs: memory attributes
*/
-bool address_space_access_valid(AddressSpace *as, hwaddr addr, hwaddr len,
+bool address_space_access_valid(const AddressSpace *as,
+ hwaddr addr, hwaddr len,
bool is_write, MemTxAttrs attrs);
/**
@@ -2839,7 +2840,7 @@ bool address_space_access_valid(AddressSpace *as, hwaddr addr, hwaddr len,
* @as: #AddressSpace to be accessed
* @addr: address within that address space
*/
-bool address_space_is_io(AddressSpace *as, hwaddr addr);
+bool address_space_is_io(const AddressSpace *as, hwaddr addr);
/* address_space_map: map a physical memory region into a host virtual address
*
diff --git a/system/physmem.c b/system/physmem.c
index 4e26f1a1d42..23ea6b69255 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -3652,7 +3652,7 @@ static bool flatview_access_valid(FlatView *fv, hwaddr addr, hwaddr len,
return true;
}
-bool address_space_access_valid(AddressSpace *as, hwaddr addr,
+bool address_space_access_valid(const AddressSpace *as, hwaddr addr,
hwaddr len, bool is_write,
MemTxAttrs attrs)
{
@@ -3663,7 +3663,7 @@ bool address_space_access_valid(AddressSpace *as, hwaddr addr,
return flatview_access_valid(fv, addr, len, is_write, attrs);
}
-bool address_space_is_io(AddressSpace *as, hwaddr addr)
+bool address_space_is_io(const AddressSpace *as, hwaddr addr)
{
MemoryRegion *mr;
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH-for-11.1 3/8] system/memory: Constify various AddressSpace arguments (xlat)
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 1/8] system/memory: Constify various AddressSpace arguments (flatview) Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 2/8] system/memory: Constify various AddressSpace arguments (checks) Philippe Mathieu-Daudé
@ 2026-03-19 19:10 ` Philippe Mathieu-Daudé
2026-03-20 14:12 ` Peter Xu
2026-03-19 19:10 ` [PATCH-for-11.1 4/8] system/memory: Constify various AddressSpace arguments (flat-range) Philippe Mathieu-Daudé
` (5 subsequent siblings)
8 siblings, 1 reply; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-03-19 19:10 UTC (permalink / raw)
To: qemu-devel
Cc: Pierrick Bouvier, Paolo Bonzini, Peter Xu,
Philippe Mathieu-Daudé
Mark the AddressSpace structure const when it is only accessed read-only.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/system/memory.h | 4 ++--
system/physmem.c | 10 ++++++----
2 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/include/system/memory.h b/include/system/memory.h
index 858dc40dc5f..38e4f7b25e5 100644
--- a/include/system/memory.h
+++ b/include/system/memory.h
@@ -2783,7 +2783,7 @@ void address_space_flush_icache_range(AddressSpace *as, hwaddr addr, hwaddr len)
/* address_space_get_iotlb_entry: translate an address into an IOTLB
* entry. Should be called from an RCU critical section.
*/
-IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
+IOMMUTLBEntry address_space_get_iotlb_entry(const AddressSpace *as, hwaddr addr,
bool is_write, MemTxAttrs attrs);
/* address_space_translate: translate an address range into an address space
@@ -2804,7 +2804,7 @@ MemoryRegion *flatview_translate(FlatView *fv,
hwaddr *len, bool is_write,
MemTxAttrs attrs);
-static inline MemoryRegion *address_space_translate(AddressSpace *as,
+static inline MemoryRegion *address_space_translate(const AddressSpace *as,
hwaddr addr, hwaddr *xlat,
hwaddr *len, bool is_write,
MemTxAttrs attrs)
diff --git a/system/physmem.c b/system/physmem.c
index 23ea6b69255..bbcbcedda15 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -528,19 +528,21 @@ static MemoryRegionSection flatview_do_translate(FlatView *fv,
}
/* Called from RCU critical section */
-IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
+IOMMUTLBEntry address_space_get_iotlb_entry(const AddressSpace *as, hwaddr addr,
bool is_write, MemTxAttrs attrs)
{
MemoryRegionSection section;
hwaddr xlat, page_mask;
+ AddressSpace target_as = *as;
+ AddressSpace *ptarget_as = &target_as;
/*
* This can never be MMIO, and we don't really care about plen,
* but page mask.
*/
section = flatview_do_translate(address_space_to_flatview(as), addr, &xlat,
- NULL, &page_mask, is_write, false, &as,
- attrs);
+ NULL, &page_mask, is_write, false,
+ &ptarget_as, attrs);
/* Illegal translation */
if (section.mr == &io_mem_unassigned) {
@@ -552,7 +554,7 @@ IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
section.offset_within_region;
return (IOMMUTLBEntry) {
- .target_as = as,
+ .target_as = &target_as,
.iova = addr & ~page_mask,
.translated_addr = xlat & ~page_mask,
.addr_mask = page_mask,
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH-for-11.1 4/8] system/memory: Constify various AddressSpace arguments (flat-range)
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
` (2 preceding siblings ...)
2026-03-19 19:10 ` [PATCH-for-11.1 3/8] system/memory: Constify various AddressSpace arguments (xlat) Philippe Mathieu-Daudé
@ 2026-03-19 19:10 ` Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 5/8] system/memory: Constify various AddressSpace arguments (notify) Philippe Mathieu-Daudé
` (4 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-03-19 19:10 UTC (permalink / raw)
To: qemu-devel
Cc: Pierrick Bouvier, Paolo Bonzini, Peter Xu,
Philippe Mathieu-Daudé
Mark the AddressSpace structure const when it is only accessed read-only.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
system/memory.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/system/memory.c b/system/memory.c
index bd12184a879..87ebeb93458 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -888,7 +888,8 @@ static void address_space_update_ioeventfds(AddressSpace *as)
* range `cmr'. Only the part that has intersection of the specified
* FlatRange will be sent.
*/
-static void flat_range_coalesced_io_notify(FlatRange *fr, AddressSpace *as,
+static void flat_range_coalesced_io_notify(FlatRange *fr,
+ const AddressSpace *as,
CoalescedMemoryRange *cmr, bool add)
{
AddrRange tmp;
@@ -912,7 +913,7 @@ static void flat_range_coalesced_io_notify(FlatRange *fr, AddressSpace *as,
}
}
-static void flat_range_coalesced_io_del(FlatRange *fr, AddressSpace *as)
+static void flat_range_coalesced_io_del(FlatRange *fr, const AddressSpace *as)
{
CoalescedMemoryRange *cmr;
@@ -921,7 +922,7 @@ static void flat_range_coalesced_io_del(FlatRange *fr, AddressSpace *as)
}
}
-static void flat_range_coalesced_io_add(FlatRange *fr, AddressSpace *as)
+static void flat_range_coalesced_io_add(FlatRange *fr, const AddressSpace *as)
{
MemoryRegion *mr = fr->mr;
CoalescedMemoryRange *cmr;
@@ -939,7 +940,8 @@ static void
flat_range_coalesced_io_notify_listener_add_del(FlatRange *fr,
MemoryRegionSection *mrs,
MemoryListener *listener,
- AddressSpace *as, bool add)
+ const AddressSpace *as,
+ bool add)
{
CoalescedMemoryRange *cmr;
MemoryRegion *mr = fr->mr;
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH-for-11.1 5/8] system/memory: Constify various AddressSpace arguments (notify)
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
` (3 preceding siblings ...)
2026-03-19 19:10 ` [PATCH-for-11.1 4/8] system/memory: Constify various AddressSpace arguments (flat-range) Philippe Mathieu-Daudé
@ 2026-03-19 19:10 ` Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 6/8] system/memory: Constify various AddressSpace arguments (cache) Philippe Mathieu-Daudé
` (3 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-03-19 19:10 UTC (permalink / raw)
To: qemu-devel
Cc: Pierrick Bouvier, Paolo Bonzini, Peter Xu,
Philippe Mathieu-Daudé
Mark the AddressSpace structure const when it is only accessed read-only.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/system/memory.h | 2 +-
system/memory.c | 6 +++---
system/physmem.c | 2 +-
3 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/include/system/memory.h b/include/system/memory.h
index 38e4f7b25e5..4c013593cae 100644
--- a/include/system/memory.h
+++ b/include/system/memory.h
@@ -2682,7 +2682,7 @@ void address_space_destroy_free(AddressSpace *as);
*
* @as: an initialized #AddressSpace
*/
-void address_space_remove_listeners(AddressSpace *as);
+void address_space_remove_listeners(const AddressSpace *as);
/**
* address_space_rw: read from or write to an address space.
diff --git a/system/memory.c b/system/memory.c
index 87ebeb93458..40e17ed22c6 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -2978,7 +2978,7 @@ void memory_global_dirty_log_stop(unsigned int flags)
}
static void listener_add_address_space(MemoryListener *listener,
- AddressSpace *as)
+ const AddressSpace *as)
{
unsigned i;
FlatView *view;
@@ -3043,7 +3043,7 @@ static void listener_add_address_space(MemoryListener *listener,
}
static void listener_del_address_space(MemoryListener *listener,
- AddressSpace *as)
+ const AddressSpace *as)
{
unsigned i;
FlatView *view;
@@ -3148,7 +3148,7 @@ void memory_listener_unregister(MemoryListener *listener)
listener->address_space = NULL;
}
-void address_space_remove_listeners(AddressSpace *as)
+void address_space_remove_listeners(const AddressSpace *as)
{
while (!QTAILQ_EMPTY(&as->listeners)) {
memory_listener_unregister(QTAILQ_FIRST(&as->listeners));
diff --git a/system/physmem.c b/system/physmem.c
index bbcbcedda15..c00f75bf591 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -3573,7 +3573,7 @@ address_space_unregister_map_client_do(AddressSpaceMapClient *client)
g_free(client);
}
-static void address_space_notify_map_clients_locked(AddressSpace *as)
+static void address_space_notify_map_clients_locked(const AddressSpace *as)
{
AddressSpaceMapClient *client;
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH-for-11.1 6/8] system/memory: Constify various AddressSpace arguments (cache)
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
` (4 preceding siblings ...)
2026-03-19 19:10 ` [PATCH-for-11.1 5/8] system/memory: Constify various AddressSpace arguments (notify) Philippe Mathieu-Daudé
@ 2026-03-19 19:10 ` Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 7/8] system/memory: Constify various AddressSpace arguments (access) Philippe Mathieu-Daudé
` (2 subsequent siblings)
8 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-03-19 19:10 UTC (permalink / raw)
To: qemu-devel
Cc: Pierrick Bouvier, Paolo Bonzini, Peter Xu,
Philippe Mathieu-Daudé
Mark the AddressSpace structure const when it is only accessed read-only.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/system/memory_cached.h | 2 +-
system/physmem.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/system/memory_cached.h b/include/system/memory_cached.h
index 6eb6179140b..760ecb38c19 100644
--- a/include/system/memory_cached.h
+++ b/include/system/memory_cached.h
@@ -117,7 +117,7 @@ void address_space_stb_cached(MemoryRegionCache *cache,
* are relative to @addr.
*/
int64_t address_space_cache_init(MemoryRegionCache *cache,
- AddressSpace *as,
+ const AddressSpace *as,
hwaddr addr,
hwaddr len,
bool is_write);
diff --git a/system/physmem.c b/system/physmem.c
index c00f75bf591..b7280e74aa2 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -3837,7 +3837,7 @@ void cpu_physical_memory_unmap(void *buffer, hwaddr len,
#include "memory_ldst.c.inc"
int64_t address_space_cache_init(MemoryRegionCache *cache,
- AddressSpace *as,
+ const AddressSpace *as,
hwaddr addr,
hwaddr len,
bool is_write)
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH-for-11.1 7/8] system/memory: Constify various AddressSpace arguments (access)
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
` (5 preceding siblings ...)
2026-03-19 19:10 ` [PATCH-for-11.1 6/8] system/memory: Constify various AddressSpace arguments (cache) Philippe Mathieu-Daudé
@ 2026-03-19 19:10 ` Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 8/8] system/memory: Constify various MemoryRegionCache arguments Philippe Mathieu-Daudé
2026-03-19 20:44 ` [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Pierrick Bouvier
8 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-03-19 19:10 UTC (permalink / raw)
To: qemu-devel
Cc: Pierrick Bouvier, Paolo Bonzini, Peter Xu,
Philippe Mathieu-Daudé
Mark the AddressSpace structure const when it is only accessed read-only.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/system/memory.h | 19 ++++++++++---------
system/physmem.c | 16 +++++++++-------
2 files changed, 19 insertions(+), 16 deletions(-)
diff --git a/include/system/memory.h b/include/system/memory.h
index 4c013593cae..564b94f1448 100644
--- a/include/system/memory.h
+++ b/include/system/memory.h
@@ -2698,7 +2698,7 @@ void address_space_remove_listeners(const AddressSpace *as);
* @len: the number of bytes to read or write
* @is_write: indicates the transfer direction
*/
-MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_rw(const AddressSpace *as, hwaddr addr,
MemTxAttrs attrs, void *buf,
hwaddr len, bool is_write);
@@ -2715,7 +2715,7 @@ MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
* @buf: buffer with the data transferred
* @len: the number of bytes to write
*/
-MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_write(const AddressSpace *as, hwaddr addr,
MemTxAttrs attrs,
const void *buf, hwaddr len);
@@ -2741,7 +2741,7 @@ MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
* @buf: buffer with the data transferred
* @len: the number of bytes to write
*/
-MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_write_rom(const AddressSpace *as, hwaddr addr,
MemTxAttrs attrs,
const void *buf, hwaddr len);
@@ -2768,17 +2768,18 @@ MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
#define SUFFIX
#define ARG1 as
-#define ARG1_DECL AddressSpace *as
+#define ARG1_DECL const AddressSpace *as
#include "system/memory_ldst.h.inc"
#ifndef TARGET_NOT_USING_LEGACY_LDST_PHYS_API
#define SUFFIX
#define ARG1 as
-#define ARG1_DECL AddressSpace *as
+#define ARG1_DECL const AddressSpace *as
#include "system/memory_ldst_phys.h.inc"
#endif
-void address_space_flush_icache_range(AddressSpace *as, hwaddr addr, hwaddr len);
+void address_space_flush_icache_range(const AddressSpace *as,
+ hwaddr addr, hwaddr len);
/* address_space_get_iotlb_entry: translate an address into an IOTLB
* entry. Should be called from an RCU critical section.
@@ -2898,7 +2899,7 @@ void address_space_register_map_client(AddressSpace *as, QEMUBH *bh);
void address_space_unregister_map_client(AddressSpace *as, QEMUBH *bh);
/* Internal functions, part of the implementation of address_space_read. */
-MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_read_full(const AddressSpace *as, hwaddr addr,
MemTxAttrs attrs, void *buf, hwaddr len);
MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
MemTxAttrs attrs, void *buf,
@@ -2953,7 +2954,7 @@ static inline bool memory_access_is_direct(const MemoryRegion *mr,
* @len: length of the data transferred
*/
static inline __attribute__((__always_inline__))
-MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_read(const AddressSpace *as, hwaddr addr,
MemTxAttrs attrs, void *buf,
hwaddr len)
{
@@ -2996,7 +2997,7 @@ MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
* @len: the number of bytes to fill with the constant byte
* @attrs: memory transaction attributes
*/
-MemTxResult address_space_set(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_set(const AddressSpace *as, hwaddr addr,
uint8_t c, hwaddr len, MemTxAttrs attrs);
/* Coalesced MMIO regions are areas where write operations can be reordered.
diff --git a/system/physmem.c b/system/physmem.c
index b7280e74aa2..8327b7c3de1 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -3424,7 +3424,7 @@ static MemTxResult flatview_read(FlatView *fv, hwaddr addr,
mr_addr, l, mr);
}
-MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_read_full(const AddressSpace *as, hwaddr addr,
MemTxAttrs attrs, void *buf, hwaddr len)
{
MemTxResult result = MEMTX_OK;
@@ -3439,7 +3439,7 @@ MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
return result;
}
-MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_write(const AddressSpace *as, hwaddr addr,
MemTxAttrs attrs,
const void *buf, hwaddr len)
{
@@ -3455,7 +3455,8 @@ MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
return result;
}
-MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
+MemTxResult address_space_rw(const AddressSpace *as,
+ hwaddr addr, MemTxAttrs attrs,
void *buf, hwaddr len, bool is_write)
{
if (is_write) {
@@ -3465,7 +3466,7 @@ MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
}
}
-MemTxResult address_space_set(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_set(const AddressSpace *as, hwaddr addr,
uint8_t c, hwaddr len, MemTxAttrs attrs)
{
#define FILLBUF_SIZE 512
@@ -3497,7 +3498,7 @@ void cpu_physical_memory_write(hwaddr addr, const void *buf, hwaddr len)
}
/* used for ROM loading : can write in RAM and ROM */
-MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
+MemTxResult address_space_write_rom(const AddressSpace *as, hwaddr addr,
MemTxAttrs attrs,
const void *buf, hwaddr len)
{
@@ -3522,7 +3523,8 @@ MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
return MEMTX_OK;
}
-void address_space_flush_icache_range(AddressSpace *as, hwaddr addr, hwaddr len)
+void address_space_flush_icache_range(const AddressSpace *as,
+ hwaddr addr, hwaddr len)
{
/*
* This function should do the same thing as an icache flush that was
@@ -3828,7 +3830,7 @@ void cpu_physical_memory_unmap(void *buffer, hwaddr len,
return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
}
-#define ARG1_DECL AddressSpace *as
+#define ARG1_DECL const AddressSpace *as
#define ARG1 as
#define SUFFIX
#define TRANSLATE(...) address_space_translate(as, __VA_ARGS__)
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH-for-11.1 8/8] system/memory: Constify various MemoryRegionCache arguments
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
` (6 preceding siblings ...)
2026-03-19 19:10 ` [PATCH-for-11.1 7/8] system/memory: Constify various AddressSpace arguments (access) Philippe Mathieu-Daudé
@ 2026-03-19 19:10 ` Philippe Mathieu-Daudé
2026-03-19 20:44 ` [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Pierrick Bouvier
8 siblings, 0 replies; 11+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-03-19 19:10 UTC (permalink / raw)
To: qemu-devel
Cc: Pierrick Bouvier, Paolo Bonzini, Peter Xu,
Philippe Mathieu-Daudé
Mark the MemoryRegionCache structure const when it is only
accessed read-only.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/system/memory_cached.h | 18 +++++++++---------
include/system/memory_ldst_cached.h.inc | 15 +++++++++------
system/physmem.c | 10 +++++-----
3 files changed, 23 insertions(+), 20 deletions(-)
diff --git a/include/system/memory_cached.h b/include/system/memory_cached.h
index 760ecb38c19..09d46821bec 100644
--- a/include/system/memory_cached.h
+++ b/include/system/memory_cached.h
@@ -51,12 +51,12 @@ struct MemoryRegionCache {
#define SUFFIX _cached_slow
#define ARG1 cache
-#define ARG1_DECL MemoryRegionCache *cache
+#define ARG1_DECL const MemoryRegionCache *cache
#include "system/memory_ldst.h.inc"
/* Inline fast path for direct RAM access. */
static inline
-uint8_t address_space_ldub_cached(MemoryRegionCache *cache, hwaddr addr,
+uint8_t address_space_ldub_cached(const MemoryRegionCache *cache, hwaddr addr,
MemTxAttrs attrs, MemTxResult *result)
{
assert(addr < cache->len);
@@ -68,7 +68,7 @@ uint8_t address_space_ldub_cached(MemoryRegionCache *cache, hwaddr addr,
}
static inline
-void address_space_stb_cached(MemoryRegionCache *cache,
+void address_space_stb_cached(const MemoryRegionCache *cache,
hwaddr addr, uint8_t val,
MemTxAttrs attrs, MemTxResult *result)
{
@@ -93,7 +93,7 @@ void address_space_stb_cached(MemoryRegionCache *cache,
#define SUFFIX _cached
#define ARG1 cache
-#define ARG1_DECL MemoryRegionCache *cache
+#define ARG1_DECL const MemoryRegionCache *cache
#include "system/memory_ldst_phys.h.inc"
/**
@@ -145,7 +145,7 @@ static inline void address_space_cache_init_empty(MemoryRegionCache *cache)
* address that was passed to @address_space_cache_init.
* @access_len: The number of bytes that were written starting at @addr.
*/
-void address_space_cache_invalidate(MemoryRegionCache *cache,
+void address_space_cache_invalidate(const MemoryRegionCache *cache,
hwaddr addr,
hwaddr access_len);
@@ -160,9 +160,9 @@ void address_space_cache_destroy(MemoryRegionCache *cache);
* Internal functions, part of the implementation of address_space_read_cached
* and address_space_write_cached.
*/
-MemTxResult address_space_read_cached_slow(MemoryRegionCache *cache,
+MemTxResult address_space_read_cached_slow(const MemoryRegionCache *cache,
hwaddr addr, void *buf, hwaddr len);
-MemTxResult address_space_write_cached_slow(MemoryRegionCache *cache,
+MemTxResult address_space_write_cached_slow(const MemoryRegionCache *cache,
hwaddr addr, const void *buf,
hwaddr len);
@@ -175,7 +175,7 @@ MemTxResult address_space_write_cached_slow(MemoryRegionCache *cache,
* @len: length of the data transferred
*/
static inline MemTxResult
-address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
+address_space_read_cached(const MemoryRegionCache *cache, hwaddr addr,
void *buf, hwaddr len)
{
assert(addr < cache->len && len <= cache->len - addr);
@@ -197,7 +197,7 @@ address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
* @len: length of the data transferred
*/
static inline MemTxResult
-address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
+address_space_write_cached(const MemoryRegionCache *cache, hwaddr addr,
const void *buf, hwaddr len)
{
assert(addr < cache->len && len <= cache->len - addr);
diff --git a/include/system/memory_ldst_cached.h.inc b/include/system/memory_ldst_cached.h.inc
index d7834f852c4..b4c696bff1f 100644
--- a/include/system/memory_ldst_cached.h.inc
+++ b/include/system/memory_ldst_cached.h.inc
@@ -24,7 +24,8 @@
#define LD_P(size) \
glue(glue(ld, size), glue(ENDIANNESS, _p))
-static inline uint16_t ADDRESS_SPACE_LD_CACHED(uw)(MemoryRegionCache *cache,
+static inline
+uint16_t ADDRESS_SPACE_LD_CACHED(uw)(const MemoryRegionCache *cache,
hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
{
assert(addr < cache->len && 2 <= cache->len - addr);
@@ -36,7 +37,8 @@ static inline uint16_t ADDRESS_SPACE_LD_CACHED(uw)(MemoryRegionCache *cache,
}
}
-static inline uint32_t ADDRESS_SPACE_LD_CACHED(l)(MemoryRegionCache *cache,
+static inline
+uint32_t ADDRESS_SPACE_LD_CACHED(l)(const MemoryRegionCache *cache,
hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
{
assert(addr < cache->len && 4 <= cache->len - addr);
@@ -48,7 +50,8 @@ static inline uint32_t ADDRESS_SPACE_LD_CACHED(l)(MemoryRegionCache *cache,
}
}
-static inline uint64_t ADDRESS_SPACE_LD_CACHED(q)(MemoryRegionCache *cache,
+static inline
+uint64_t ADDRESS_SPACE_LD_CACHED(q)(const MemoryRegionCache *cache,
hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
{
assert(addr < cache->len && 8 <= cache->len - addr);
@@ -71,7 +74,7 @@ static inline uint64_t ADDRESS_SPACE_LD_CACHED(q)(MemoryRegionCache *cache,
#define ST_P(size) \
glue(glue(st, size), glue(ENDIANNESS, _p))
-static inline void ADDRESS_SPACE_ST_CACHED(w)(MemoryRegionCache *cache,
+static inline void ADDRESS_SPACE_ST_CACHED(w)(const MemoryRegionCache *cache,
hwaddr addr, uint16_t val, MemTxAttrs attrs, MemTxResult *result)
{
assert(addr < cache->len && 2 <= cache->len - addr);
@@ -82,7 +85,7 @@ static inline void ADDRESS_SPACE_ST_CACHED(w)(MemoryRegionCache *cache,
}
}
-static inline void ADDRESS_SPACE_ST_CACHED(l)(MemoryRegionCache *cache,
+static inline void ADDRESS_SPACE_ST_CACHED(l)(const MemoryRegionCache *cache,
hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result)
{
assert(addr < cache->len && 4 <= cache->len - addr);
@@ -93,7 +96,7 @@ static inline void ADDRESS_SPACE_ST_CACHED(l)(MemoryRegionCache *cache,
}
}
-static inline void ADDRESS_SPACE_ST_CACHED(q)(MemoryRegionCache *cache,
+static inline void ADDRESS_SPACE_ST_CACHED(q)(const MemoryRegionCache *cache,
hwaddr addr, uint64_t val, MemTxAttrs attrs, MemTxResult *result)
{
assert(addr < cache->len && 8 <= cache->len - addr);
diff --git a/system/physmem.c b/system/physmem.c
index 8327b7c3de1..5f1be89649e 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -3886,7 +3886,7 @@ int64_t address_space_cache_init(MemoryRegionCache *cache,
return l;
}
-void address_space_cache_invalidate(MemoryRegionCache *cache,
+void address_space_cache_invalidate(const MemoryRegionCache *cache,
hwaddr addr,
hwaddr access_len)
{
@@ -3917,7 +3917,7 @@ void address_space_cache_destroy(MemoryRegionCache *cache)
* address_space_cache_init.
*/
static inline MemoryRegion *address_space_translate_cached(
- MemoryRegionCache *cache, hwaddr addr, hwaddr *xlat,
+ const MemoryRegionCache *cache, hwaddr addr, hwaddr *xlat,
hwaddr *plen, bool is_write, MemTxAttrs attrs)
{
MemoryRegionSection section;
@@ -3998,7 +3998,7 @@ static MemTxResult address_space_read_continue_cached(MemTxAttrs attrs,
* out of line function when the target is an MMIO or IOMMU region.
*/
MemTxResult
-address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr,
+address_space_read_cached_slow(const MemoryRegionCache *cache, hwaddr addr,
void *buf, hwaddr len)
{
hwaddr mr_addr, l;
@@ -4015,7 +4015,7 @@ address_space_read_cached_slow(MemoryRegionCache *cache, hwaddr addr,
* out of line function when the target is an MMIO or IOMMU region.
*/
MemTxResult
-address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr,
+address_space_write_cached_slow(const MemoryRegionCache *cache, hwaddr addr,
const void *buf, hwaddr len)
{
hwaddr mr_addr, l;
@@ -4028,7 +4028,7 @@ address_space_write_cached_slow(MemoryRegionCache *cache, hwaddr addr,
buf, len, mr_addr, l, mr);
}
-#define ARG1_DECL MemoryRegionCache *cache
+#define ARG1_DECL const MemoryRegionCache *cache
#define ARG1 cache
#define SUFFIX _cached_slow
#define TRANSLATE(...) address_space_translate_cached(cache, __VA_ARGS__)
--
2.53.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
` (7 preceding siblings ...)
2026-03-19 19:10 ` [PATCH-for-11.1 8/8] system/memory: Constify various MemoryRegionCache arguments Philippe Mathieu-Daudé
@ 2026-03-19 20:44 ` Pierrick Bouvier
8 siblings, 0 replies; 11+ messages in thread
From: Pierrick Bouvier @ 2026-03-19 20:44 UTC (permalink / raw)
To: Philippe Mathieu-Daudé, qemu-devel; +Cc: Paolo Bonzini, Peter Xu
On 3/19/26 12:10 PM, Philippe Mathieu-Daudé wrote:
> When structures are only accessed read-only, it is safer to mark
> them const to protect against invalid API (ab)uses. This is also
> an useful hint to compilers.
>
> Philippe Mathieu-Daudé (8):
> system/memory: Constify various AddressSpace arguments (flatview)
> system/memory: Constify various AddressSpace arguments (checks)
> system/memory: Constify various AddressSpace arguments (xlat)
> system/memory: Constify various AddressSpace arguments (flat-range)
> system/memory: Constify various AddressSpace arguments (notify)
> system/memory: Constify various AddressSpace arguments (cache)
> system/memory: Constify various AddressSpace arguments (access)
> system/memory: Constify various MemoryRegionCache arguments
>
> include/system/memory.h | 32 +++++++++---------
> include/system/memory_cached.h | 20 +++++------
> system/memory-internal.h | 5 +--
> include/system/memory_ldst_cached.h.inc | 15 +++++----
> system/memory.c | 18 +++++-----
> system/physmem.c | 44 ++++++++++++++-----------
> 6 files changed, 73 insertions(+), 61 deletions(-)
>
Series:
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH-for-11.1 3/8] system/memory: Constify various AddressSpace arguments (xlat)
2026-03-19 19:10 ` [PATCH-for-11.1 3/8] system/memory: Constify various AddressSpace arguments (xlat) Philippe Mathieu-Daudé
@ 2026-03-20 14:12 ` Peter Xu
0 siblings, 0 replies; 11+ messages in thread
From: Peter Xu @ 2026-03-20 14:12 UTC (permalink / raw)
To: Philippe Mathieu-Daudé; +Cc: qemu-devel, Pierrick Bouvier, Paolo Bonzini
On Thu, Mar 19, 2026 at 08:10:12PM +0100, Philippe Mathieu-Daudé wrote:
> Mark the AddressSpace structure const when it is only accessed read-only.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
> include/system/memory.h | 4 ++--
> system/physmem.c | 10 ++++++----
> 2 files changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/include/system/memory.h b/include/system/memory.h
> index 858dc40dc5f..38e4f7b25e5 100644
> --- a/include/system/memory.h
> +++ b/include/system/memory.h
> @@ -2783,7 +2783,7 @@ void address_space_flush_icache_range(AddressSpace *as, hwaddr addr, hwaddr len)
> /* address_space_get_iotlb_entry: translate an address into an IOTLB
> * entry. Should be called from an RCU critical section.
> */
> -IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
> +IOMMUTLBEntry address_space_get_iotlb_entry(const AddressSpace *as, hwaddr addr,
> bool is_write, MemTxAttrs attrs);
>
> /* address_space_translate: translate an address range into an address space
> @@ -2804,7 +2804,7 @@ MemoryRegion *flatview_translate(FlatView *fv,
> hwaddr *len, bool is_write,
> MemTxAttrs attrs);
>
> -static inline MemoryRegion *address_space_translate(AddressSpace *as,
> +static inline MemoryRegion *address_space_translate(const AddressSpace *as,
> hwaddr addr, hwaddr *xlat,
> hwaddr *len, bool is_write,
> MemTxAttrs attrs)
> diff --git a/system/physmem.c b/system/physmem.c
> index 23ea6b69255..bbcbcedda15 100644
> --- a/system/physmem.c
> +++ b/system/physmem.c
> @@ -528,19 +528,21 @@ static MemoryRegionSection flatview_do_translate(FlatView *fv,
> }
>
> /* Called from RCU critical section */
> -IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
> +IOMMUTLBEntry address_space_get_iotlb_entry(const AddressSpace *as, hwaddr addr,
> bool is_write, MemTxAttrs attrs)
> {
> MemoryRegionSection section;
> hwaddr xlat, page_mask;
> + AddressSpace target_as = *as;
Is this one an overkill? It'll deep copy everything of AS..
> + AddressSpace *ptarget_as = &target_as;
>
> /*
> * This can never be MMIO, and we don't really care about plen,
> * but page mask.
> */
> section = flatview_do_translate(address_space_to_flatview(as), addr, &xlat,
> - NULL, &page_mask, is_write, false, &as,
> - attrs);
> + NULL, &page_mask, is_write, false,
> + &ptarget_as, attrs);
>
> /* Illegal translation */
> if (section.mr == &io_mem_unassigned) {
> @@ -552,7 +554,7 @@ IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
> section.offset_within_region;
>
> return (IOMMUTLBEntry) {
> - .target_as = as,
> + .target_as = &target_as,
> .iova = addr & ~page_mask,
> .translated_addr = xlat & ~page_mask,
> .addr_mask = page_mask,
> --
> 2.53.0
>
--
Peter Xu
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-03-20 14:12 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-19 19:10 [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 1/8] system/memory: Constify various AddressSpace arguments (flatview) Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 2/8] system/memory: Constify various AddressSpace arguments (checks) Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 3/8] system/memory: Constify various AddressSpace arguments (xlat) Philippe Mathieu-Daudé
2026-03-20 14:12 ` Peter Xu
2026-03-19 19:10 ` [PATCH-for-11.1 4/8] system/memory: Constify various AddressSpace arguments (flat-range) Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 5/8] system/memory: Constify various AddressSpace arguments (notify) Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 6/8] system/memory: Constify various AddressSpace arguments (cache) Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 7/8] system/memory: Constify various AddressSpace arguments (access) Philippe Mathieu-Daudé
2026-03-19 19:10 ` [PATCH-for-11.1 8/8] system/memory: Constify various MemoryRegionCache arguments Philippe Mathieu-Daudé
2026-03-19 20:44 ` [PATCH-for-11.1 0/8] system/memory: Constify various AddressSpace/MemoryRegionCache arguments Pierrick Bouvier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox