@samitouri / QOSamiQemu / commits / 5a525dcb4d

system/memory: Constify various MemoryRegion arguments

Mark the MemoryRegion structure const when is only accessed read-only. Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org> Reviewed-by: Peter Xu <peterx@redhat.com> Link: https://lore.kernel.org/r/20260309183536.88976-3-philmd@linaro.org Signed-off-by: Peter Xu <peterx@redhat.com>

Philippe Mathieu-Daudé committed Mar 9, 2026 at 19:35 UTC 5a525dcb4d60c9e89d2873d7135ef798f1aef6bb
3 files changed +40 -38
include/system/memory.h
+19 -19
@@ -1690,14 +1690,14 @@ bool memory_region_init_rom_device(MemoryRegion *mr,
1690 *
1691 * @mr: the memory region being queried.
1692 */
1693 -Object *memory_region_owner(MemoryRegion *mr);
1693 +Object *memory_region_owner(const MemoryRegion *mr);
1694
1695 /**
1696 * memory_region_size: get a memory region's size.
1697 *
1698 * @mr: the memory region being queried.
1699 */
1700 -uint64_t memory_region_size(MemoryRegion *mr);
1700 +uint64_t memory_region_size(const MemoryRegion *mr);
1701
1702 /**
1703 * memory_region_is_ram: check whether a memory region is random access
@@ -1706,7 +1706,7 @@ uint64_t memory_region_size(MemoryRegion *mr);
1706 *
1707 * @mr: the memory region being queried
1708 */
1709 -static inline bool memory_region_is_ram(MemoryRegion *mr)
1709 +static inline bool memory_region_is_ram(const MemoryRegion *mr)
1710 {
1711 return mr->ram;
1712 }
@@ -1718,7 +1718,7 @@ static inline bool memory_region_is_ram(MemoryRegion *mr)
1718 *
1719 * @mr: the memory region being queried
1720 */
1721 -bool memory_region_is_ram_device(MemoryRegion *mr);
1721 +bool memory_region_is_ram_device(const MemoryRegion *mr);
1722
1723 /**
1724 * memory_region_is_romd: check whether a memory region is in ROMD mode
@@ -1728,7 +1728,7 @@ bool memory_region_is_ram_device(MemoryRegion *mr);
1728 *
1729 * @mr: the memory region being queried
1730 */
1731 -static inline bool memory_region_is_romd(MemoryRegion *mr)
1731 +static inline bool memory_region_is_romd(const MemoryRegion *mr)
1732 {
1733 return mr->rom_device && mr->romd_mode;
1734 }
@@ -1741,7 +1741,7 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)
1741 *
1742 * @mr: the memory region being queried
1743 */
1744 -bool memory_region_is_protected(MemoryRegion *mr);
1744 +bool memory_region_is_protected(const MemoryRegion *mr);
1745
1746 /**
1747 * memory_region_has_guest_memfd: check whether a memory region has guest_memfd
@@ -1751,7 +1751,7 @@ bool memory_region_is_protected(MemoryRegion *mr);
1751 *
1752 * @mr: the memory region being queried
1753 */
1754 -bool memory_region_has_guest_memfd(MemoryRegion *mr);
1754 +bool memory_region_has_guest_memfd(const MemoryRegion *mr);
1755
1756 /**
1757 * memory_region_get_iommu: check whether a memory region is an iommu
@@ -1761,7 +1761,7 @@ bool memory_region_has_guest_memfd(MemoryRegion *mr);
1761 *
1762 * @mr: the memory region being queried
1763 */
1764 -static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
1764 +static inline IOMMUMemoryRegion *memory_region_get_iommu(const MemoryRegion *mr)
1765 {
1766 if (mr->alias) {
1767 return memory_region_get_iommu(mr->alias);
@@ -1932,7 +1932,7 @@ const char *memory_region_name(const MemoryRegion *mr);
1932 * @mr: the memory region being queried
1933 * @client: the client being queried
1934 */
1935 -bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
1935 +bool memory_region_is_logging(const MemoryRegion *mr, uint8_t client);
1936
1937 /**
1938 * memory_region_get_dirty_log_mask: return the clients for which a
@@ -1943,7 +1943,7 @@ bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
1943 *
1944 * @mr: the memory region being queried
1945 */
1946 -uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
1946 +uint8_t memory_region_get_dirty_log_mask(const MemoryRegion *mr);
1947
1948 /**
1949 * memory_region_is_rom: check whether a memory region is ROM
@@ -1952,7 +1952,7 @@ uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
1952 *
1953 * @mr: the memory region being queried
1954 */
1955 -static inline bool memory_region_is_rom(MemoryRegion *mr)
1955 +static inline bool memory_region_is_rom(const MemoryRegion *mr)
1956 {
1957 return mr->ram && mr->readonly;
1958 }
@@ -1964,7 +1964,7 @@ static inline bool memory_region_is_rom(MemoryRegion *mr)
1964 *
1965 * @mr: the memory region being queried
1966 */
1967 -static inline bool memory_region_is_nonvolatile(MemoryRegion *mr)
1967 +static inline bool memory_region_is_nonvolatile(const MemoryRegion *mr)
1968 {
1969 return mr->nonvolatile;
1970 }
@@ -1977,7 +1977,7 @@ static inline bool memory_region_is_nonvolatile(MemoryRegion *mr)
1977 *
1978 * @mr: the RAM or alias memory region being queried.
1979 */
1980 -int memory_region_get_fd(MemoryRegion *mr);
1980 +int memory_region_get_fd(const MemoryRegion *mr);
1981
1982 /**
1983 * memory_region_from_host: Convert a pointer into a RAM memory region
@@ -2012,7 +2012,7 @@ MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
2012 *
2013 * @mr: the memory region being queried.
2014 */
2015 -void *memory_region_get_ram_ptr(MemoryRegion *mr);
2015 +void *memory_region_get_ram_ptr(const MemoryRegion *mr);
2016
2017 /* memory_region_ram_resize: Resize a RAM region.
2018 *
@@ -2362,7 +2362,7 @@ void memory_region_add_subregion_overlap(MemoryRegion *mr,
2362 *
2363 * @mr: the region to be queried
2364 */
2365 -ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
2365 +ram_addr_t memory_region_get_ram_addr(const MemoryRegion *mr);
2366
2367 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
2368 /**
@@ -2462,7 +2462,7 @@ bool memory_region_present(MemoryRegion *container, hwaddr addr);
2462 *
2463 * @mr: a #MemoryRegion which should be checked if it's mapped
2464 */
2465 -bool memory_region_is_mapped(MemoryRegion *mr);
2465 +bool memory_region_is_mapped(const MemoryRegion *mr);
2466
2467 /**
2468 * memory_region_get_ram_discard_manager: get the #RamDiscardManager for a
@@ -2908,7 +2908,7 @@ void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
2908 int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr);
2909 bool prepare_mmio_access(MemoryRegion *mr);
2910
2911 -static inline bool memory_region_supports_direct_access(MemoryRegion *mr)
2911 +static inline bool memory_region_supports_direct_access(const MemoryRegion *mr)
2912 {
2913 /* ROM DEVICE regions only allow direct access if in ROMD mode. */
2914 if (memory_region_is_romd(mr)) {
@@ -2925,8 +2925,8 @@ static inline bool memory_region_supports_direct_access(MemoryRegion *mr)
2925 return !memory_region_is_ram_device(mr);
2926 }
2927
2928 -static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write,
2929 - MemTxAttrs attrs)
2928 +static inline bool memory_access_is_direct(const MemoryRegion *mr,
2929 + bool is_write, MemTxAttrs attrs)
2930 {
2931 if (!memory_region_supports_direct_access(mr)) {
2932 return false;
system/memory.c
+20 -18
@@ -1750,9 +1750,9 @@ static void memory_region_finalize(Object *obj)
1750 g_free(mr->ioeventfds);
1751 }
1752
1753 -Object *memory_region_owner(MemoryRegion *mr)
1753 +Object *memory_region_owner(const MemoryRegion *mr)
1754 {
1755 - Object *obj = OBJECT(mr);
1755 + const Object *obj = OBJECT(mr);
1756 return obj->parent;
1757 }
1758
@@ -1780,7 +1780,7 @@ void memory_region_unref(MemoryRegion *mr)
1780 }
1781 }
1782
1783 -uint64_t memory_region_size(MemoryRegion *mr)
1783 +uint64_t memory_region_size(const MemoryRegion *mr)
1784 {
1785 if (int128_eq(mr->size, int128_2_64())) {
1786 return UINT64_MAX;
@@ -1797,25 +1797,25 @@ const char *memory_region_name(const MemoryRegion *mr)
1797 return mr->name;
1798 }
1799
1800 -bool memory_region_is_ram_device(MemoryRegion *mr)
1800 +bool memory_region_is_ram_device(const MemoryRegion *mr)
1801 {
1802 return mr->ram_device;
1803 }
1804
1805 -bool memory_region_is_protected(MemoryRegion *mr)
1805 +bool memory_region_is_protected(const MemoryRegion *mr)
1806 {
1807 return mr->ram && (mr->ram_block->flags & RAM_PROTECTED);
1808 }
1809
1810 -bool memory_region_has_guest_memfd(MemoryRegion *mr)
1810 +bool memory_region_has_guest_memfd(const MemoryRegion *mr)
1811 {
1812 return mr->ram_block && mr->ram_block->guest_memfd >= 0;
1813 }
1814
1815 -uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
1815 +uint8_t memory_region_get_dirty_log_mask(const MemoryRegion *mr)
1816 {
1817 uint8_t mask = mr->dirty_log_mask;
1818 - RAMBlock *rb = mr->ram_block;
1818 + const RAMBlock *rb = mr->ram_block;
1819
1820 if (global_dirty_tracking && ((rb && qemu_ram_is_migratable(rb)) ||
1821 memory_region_is_iommu(mr))) {
@@ -1829,7 +1829,7 @@ uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
1829 return mask;
1830 }
1831
1832 -bool memory_region_is_logging(MemoryRegion *mr, uint8_t client)
1832 +bool memory_region_is_logging(const MemoryRegion *mr, uint8_t client)
1833 {
1834 return memory_region_get_dirty_log_mask(mr) & (1 << client);
1835 }
@@ -2337,7 +2337,7 @@ void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
2337 memory_region_get_ram_addr(mr) + addr, size, client, NULL);
2338 }
2339
2340 -int memory_region_get_fd(MemoryRegion *mr)
2340 +int memory_region_get_fd(const MemoryRegion *mr)
2341 {
2342 RCU_READ_LOCK_GUARD();
2343 while (mr->alias) {
@@ -2346,7 +2346,7 @@ int memory_region_get_fd(MemoryRegion *mr)
2346 return mr->ram_block->fd;
2347 }
2348
2349 -void *memory_region_get_ram_ptr(MemoryRegion *mr)
2349 +void *memory_region_get_ram_ptr(const MemoryRegion *mr)
2350 {
2351 uint64_t offset = 0;
2352
@@ -2371,7 +2371,7 @@ MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset)
2371 return block->mr;
2372 }
2373
2374 -ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
2374 +ram_addr_t memory_region_get_ram_addr(const MemoryRegion *mr)
2375 {
2376 return mr->ram_block ? mr->ram_block->offset : RAM_ADDR_INVALID;
2377 }
@@ -2737,7 +2737,7 @@ static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
2737 sizeof(FlatRange), cmp_flatrange_addr);
2738 }
2739
2740 -bool memory_region_is_mapped(MemoryRegion *mr)
2740 +bool memory_region_is_mapped(const MemoryRegion *mr)
2741 {
2742 return !!mr->container || mr->mapped_via_alias;
2743 }
@@ -3221,7 +3221,7 @@ void address_space_destroy_free(AddressSpace *as)
3221 call_rcu(as, do_address_space_destroy_free, rcu);
3222 }
3223
3224 -static const char *memory_region_type(MemoryRegion *mr)
3224 +static const char *memory_region_type(const MemoryRegion *mr)
3225 {
3226 if (mr->alias) {
3227 return memory_region_type(mr->alias);
@@ -3414,7 +3414,6 @@ static void mtree_print_flatview(gpointer key, gpointer value,
3414 GArray *fv_address_spaces = value;
3415 struct FlatViewInfo *fvi = user_data;
3416 FlatRange *range = &view->ranges[0];
3417 - MemoryRegion *mr;
3417 int n = view->nr;
3418 int i;
3419 AddressSpace *as;
@@ -3441,7 +3440,8 @@ static void mtree_print_flatview(gpointer key, gpointer value,
3440 }
3441
3442 while (n--) {
3444 - mr = range->mr;
3443 + const MemoryRegion *mr = range->mr;
3444 +
3445 if (range->offset_in_region) {
3446 qemu_printf(MTREE_INDENT HWADDR_FMT_plx "-" HWADDR_FMT_plx
3447 " (prio %d, %s%s): %s @" HWADDR_FMT_plx,
@@ -3614,8 +3614,10 @@ static void mtree_info_as(bool dispatch_tree, bool owner, bool disabled)
3614
3615 /* print aliased regions */
3616 QTAILQ_FOREACH(ml, &ml_head, mrqueue) {
3617 - qemu_printf("memory-region: %s\n", memory_region_name(ml->mr));
3618 - mtree_print_mr(ml->mr, 1, 0, &ml_head, owner, disabled);
3617 + const MemoryRegion *mr = ml->mr;
3618 +
3619 + qemu_printf("memory-region: %s\n", memory_region_name(mr));
3620 + mtree_print_mr(mr, 1, 0, &ml_head, owner, disabled);
3621 qemu_printf("\n");
3622 }
3623
system/physmem.c
+1 -1
@@ -1975,7 +1975,7 @@ void qemu_ram_unset_idstr(RAMBlock *block)
1975 }
1976 }
1977
1978 -static char *cpr_name(MemoryRegion *mr)
1978 +static char *cpr_name(const MemoryRegion *mr)
1979 {
1980 const char *mr_name = memory_region_name(mr);
1981 g_autofree char *id = mr->dev ? qdev_get_dev_path(mr->dev) : NULL;