Setup memory management for RISC-V
v5 cover Setup memory management for RISC-V
Finish initializing the memory subsystem by mapping the direct map and frame table.
The frametable_virt_start variable is introduced with substracting
of an offest ( taking into account that RAM start could be not 0 ) to save virtual address space of FRAMETABLE. Also it simplifies the
calculations and thereby improves performance for page_to_mfn(), mfn_to_page() as there is no frametable_base_pdx or frametable_base_mfn ( if CONFIG_PDX_COMPRESSION=n);
The directmap_virt_start is introduced and shifted lower in the VA space to (DIRECTMAP_VIRT_START - masked_low_bits_of_ram_start_address) to avoid wasting a large portion of the directmap space, this also allows for simple VA <-> PA translations. Also aligns DIRECTMAP_VIRT_START to a GB boundary (for Sv39; for other MMU mode boundaries will be bigger ) by masking the higher bits of the RAM start address to enable the use of superpages in map_pages_to_xen().
In addition, initialize the VMAP_DEFAULT region, finalize the boot allocator, and update the system state from early_boot to boot.
Introduce share_xen_page_with_guest() to deal with linkage error which occurs because of DCE:
riscv64-linux-gnu-ld: prelink.o: in function `tasklet_kill':
/build/xen/common/tasklet.c:176: undefined reference to
`share_xen_page_with_guest'
riscv64-linux-gnu-ld: ./.xen-syms.0: hidden symbol `share_xen_page_with_guest'
isn't defined riscv64-linux-gnu-ld: final link failed: bad value
The function maddr_to_virt() is introduced as part of this patch series.
virt_to_maddr() is updated as it is started to use direct_virt_start which is introduced in this patch series.
---
Changes in V5:
- Rebase on top of current staging.
- Drop unnecessary local variables.
- Fix issue with virt_to_maddr() ( used DIRECTMAP_VIRT_START in if-condition instead of direct_virt_start ).
- Update setup_directmap_mappings() and setup_frametable_mappings() ( details look in patch 1).
---
Changes in V4:
- Rebase on top of current staging.
- Fix issues with setup_frametable_mappings() and setup_directmap_mappings().
- Drop unnecessary local variables.
- Minor log messsages fixes.
---
Changes in V3:
- Update the cover letter message as DIRECTMAP connected things were changed.
- Update setup_mm(), setup_{directmap,frametable}_mappings.
- Introduce ROUNDDOWN in <xen/macros.h>
- All other details please look at the specific patch.
---
Changes in V2:
- update the cover letter message.
- merge first 3 patches to "introduce setup_mm()" patch as after setup_mm() rework all the things in first two patches of v1 started to use changes introduced in "introduce setup_mm()" patch.
- add Acked-by for some patch series.
- All other details please look at the specific patch.
---
Oleksii Kurochko (3):
xen/riscv: introduce setup_mm()
xen/riscv: initialize the VMAP_DEFAULT virtual range xen/riscv: finalize boot allocator and transition to boot state
xen/arch/riscv/include/asm/mm.h | 17 ++- xen/arch/riscv/include/asm/setup.h | 2 + xen/arch/riscv/mm.c | 161 +++++++++++++++++++++++++++-- xen/arch/riscv/pt.c | 6 ++ xen/arch/riscv/setup.c | 14 +++ xen/include/xen/macros.h | 1 + 6 files changed, 190 insertions(+), 11 deletions(-)
Signature · 1 line
2.47.0
PATCH v5 1/3 xen/riscv: introduce setup_mm()
Introduce the implementation of setup_mm(), which includes:
1. Adding all free regions to the boot allocator, as memory is needed to allocate page tables used for frame table mapping.
2. Calculating RAM size and the RAM end address.
3. Setting up direct map mappings from each RAM bank and initialize directmap_virt_start to keep simple VA <-> PA translation and if RAM_start isn't properly aligned then add an additional alignment to directmap_virt_start to be properly aligned with RAM start to use more superpages to reduce pressure on the TLB.
4. Setting up frame table mappings for range [ram_start, ram_end) and initialize properly frametable_virt_start to have simplified version of mfn_to_page() and page_to_mfn().
5. Setting up max_page.
Update virt_to_maddr() to use introduced directmap_virt_start.
Implement maddr_to_virt() function to convert a machine address to a virtual address. This function is specifically designed to be used only for the DIRECTMAP region, so a check has been added to ensure that the address does not exceed DIRECTMAP_SIZE.
After the introduction of maddr_to_virt() the following linkage error starts to occur and to avoid it share_xen_page_with_guest() stub is added:
riscv64-linux-gnu-ld: prelink.o: in function `tasklet_kill':
/build/xen/common/tasklet.c:176: undefined reference to
`share_xen_page_with_guest'
riscv64-linux-gnu-ld: ./.xen-syms.0: hidden symbol `share_xen_page_with_guest'
isn't defined riscv64-linux-gnu-ld: final link failed: bad value
Despite the linkger fingering tasklet.c, it's trace.o which has the undefined refenrece:
$ find . -name \*.o | while read F; do nm $F | grep share_xen_page_with_guest &&
echo $F; done
U share_xen_page_with_guest
./xen/common/built_in.o
U share_xen_page_with_guest
./xen/common/trace.o
U share_xen_page_with_guest
./xen/prelink.oLooking at trace.i, there is call of share_xen_page_with_guest() but in case of when maddr_to_virt() is defined as stub ("BUG_ON(); return NULL;") DCE happens and the code is just eliminated.
---
Changes in V5:
- correct the if-condition in virt_to_maddr():
directmap_virt_start -> DIRECTMAP_VIRT_START
as va argument of virt_to_maddr() is DIRECTMAP_VIRT_START-relative.
- add initializer to definition/declaration of frametable_virt_start.
- setup_frametable_mappings() update:
- add check that setup_frametable_mappings() is called once.
- add staic variable frametable_mfn_start inside setup_frametable_mappings() to handle the case when setup_frametable_mappings() is called more then once.
- setup_directmap_mappings():
- Update initialization of directmap_virt_start when setup_directmap_mappings() is called for the first time.
- Introduce local variable "mfn_t base_mfn_t = _mfn(base_mfn);" to have less mfn_x(...) in the code.
- Add logging of error code of map_pages_to_xen().
- Drop ram_size variable in setup_mm() as there is no users of it.
---
Change in V4:
- s/heap/directmap in log message in setup_directmap_mappings().
- drop local variable total_pages in setup_mm() as it is not used.
- call setup_frametable_mappings() for [ram_start,ram_end) range.
- setup_frametable_mappings(ps, pe):
- update initialization ( validation/invalidation ) of frame table.
- add and initialize frameframetable_virt_start variable to cover
the case that ps ( RAM start ) could be not eqaul 0 so the proper calculations are needed in mfn_to_page() and page_to_mfn().
- setup_directmap_mapping():
- update the value of directmap_virt_start: add an alignment to the size of HYP_PT_ROOT_LEVEL to have proper alignment so more superpages will be used for mapping.
- re-use indirectly directmap_virt_start ( by using mfn_to_virt(base_mfn) ) during mapping of directmap region for RAM bank to not face an issue with possible mapping overlapping during the 2nd invocation of setup_directmap_mapping().
- Update the commit message.
---
Changes in V3:
- Update the comment the part where DCE should be mentioned and directmap-related things are touched.
- Move ROUNDDOWN to <xen/macros.h>.
- s/sizeof(struct page_info)/sizeof(*frame_table) in setup_frametable_mapping().
- Updates in setup_frametable_mapping():
- align phys_start (ps) up to a page boundary and align phys_end (pe) down to a page boundary.
- Update panic message.
- Add the comment about alignment of frametable_size and base_mfn.
- invalidate all frame_table entries and then just init with 0 only valid ones.
- Double blank lines removing.
- Initialize directmap_virt_start with DIRECTMAP_VIRT_START.
- Updates in setup_directmap_mapping():
- Drop local variable rc as it is used only once.
- Move directmap_mfn_start definition to setup_directmap_mapping() and change __ro_after_init to __initdata.
- Update the commit message about alignment of directmap_virt_start.
- Move down directmap_virt_start for (base_addr & ~XEN_PT_LEVEL_SIZE(HYP_PT_ROOT_LEVEL)) to not waste a lot of directmap space.
- Map DIRECTMAP_VIRT_START + (base_addr & XEN_PT_LEVEL_SIZE(HYP_PT_ROOT_LEVEL)) to _mfn(base_mfn).
- Add log of the range in case directmap mapping failure.
- Drop XENHEAP_VIRT_START and use directmap_virt_start instead.
- Update the comment above setup_mm().
- Update the calculation of bank_start and bank_end in setup_mm() to cover the case when a bank doesn't cover full pages.
- Move share_xen_page_with_guest() to riscv/mm.c instead of riscv/stub.c.
- Drop inclusion of <xen/pdx.h> in riscv/mm.c as thery is nothing used anymore in riscv/mm.c.
- Move variable directmap_virt_start and setup_mm() outside
#ifndef CONFIG_RISCV_32 ... #endif as they are likely to be common.
---
Changes in V2:
- merge patch 2 ( xen/riscv: implement maddr_to_virt() ) to the current one as maddr_to_virt() started to use the thing which are introduced in the current patch.
- merge with patch 1 ( xen/riscv: add stub for share_xen_page_with_guest() ) as this linkage issue happens during introduction of maddr_to_virt().
- use mathematical range expressions for log messages.
- calculate properly amount of mfns in setup_frametable_mapping() taking into account that ps and pe can be not properly aligned.
- drop full stop at the end of debug message.
- use PFN_DOWN(framsetable_size) instead of frametable_size >> PAGE_SHIFT.
- round down ram_size when it is being accumulated in setup_mm() to guarantee that banks can never have partial pages at their start/end.
- call setup_directmap_mappings() only for ram bank regions instead of mapping [0, ram_end] region.
- drop directmap_virt_end for now as it isn't used at the moment.
- update the commit message.
--- xen/arch/riscv/include/asm/mm.h | 17 +++- xen/arch/riscv/include/asm/setup.h | 2 + xen/arch/riscv/mm.c | 150 +++++++++++++++++++++++++++++ xen/arch/riscv/setup.c | 3 + xen/include/xen/macros.h | 1 + 5 files changed, 168 insertions(+), 5 deletions(-)
xen/arch/riscv/include/asm/mm.h +12 -5
@@ -12,6 +12,8 @@#include <asm/page-bits.h> +extern vaddr_t directmap_virt_start; +#define pfn_to_paddr (pfn) ((paddr_t)(pfn) << PAGE_SHIFT)#define paddr_to_pfn (pa) ((unsigned long)((pa) >> PAGE_SHIFT))
@@ -25,8 +27,11 @@static inline void *maddr_to_virt (paddr_t ma) { -BUG_ON ("unimplemented" ); -return NULL ; +unsigned long va_offset =maddr_to_directmapoff (ma); + +ASSERT (va_offset <DIRECTMAP_SIZE ); + +return (void *)(directmap_virt_start + va_offset); }/*
@@ -39,7 +44,7 @@ static inline unsigned long virt_to_maddr(unsigned long va) {if ((va >=DIRECTMAP_VIRT_START ) && (va < (DIRECTMAP_VIRT_START +DIRECTMAP_SIZE ))) -return directmapoff_to_maddr (va -DIRECTMAP_VIRT_START ); +return directmapoff_to_maddr (va - directmap_virt_start);BUILD_BUG_ON (XEN_VIRT_SIZE !=MB (2 )); ASSERT((va >> (PAGETABLE_ORDER + PAGE_SHIFT)) ==
@@ -127,11 +132,13 @@ struct page_info }; }; +extern struct page_info *frametable_virt_start; +#define frame_table ((struct page_info *)FRAMETABLE_VIRT_START)/* Convert between machine frame numbers and page-info structures. */ -#define mfn_to_page (mfn) (frame_table + mfn_x(mfn)) -#define page_to_mfn (pg) _mfn((pg) - frame_table) +#define mfn_to_page (mfn) (frametable_virt_start + mfn_x(mfn)) +#define page_to_mfn (pg) _mfn((pg) - frametable_virt_start)static inline void *page_to_virt (const struct page_info *pg) {
xen/arch/riscv/include/asm/setup.h +2 -0
@@ -5,6 +5,8 @@#define max_init_domid (0) +void setup_mm (void ); +#endif /* ASM__RISCV__SETUP_H */ /*
xen/arch/riscv/mm.c +150 -0
159 lines not on this page · raw message
xen/arch/riscv/setup.c +3 -0
@@ -12,6 +12,7 @@#include <asm/early_printk.h> #include <asm/sbi.h> +#include <asm/setup.h> #include <asm/smp.h> #include <asm/traps.h>
@@ -59,6 +60,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,printk ("Command line: %s\n" , cmdline);cmdline_parse (cmdline); +setup_mm (); +printk ("All set up\n" );machine_halt ();
xen/include/xen/macros.h +1 -0
@@ -2,6 +2,7 @@#define __MACROS_H__#define ROUNDUP (x, a) (((x) + (a) - 1) & ~((a) - 1)) +#define ROUNDDOWN (x, a) ((x) & ~((a) - 1))#define IS_ALIGNED (val, align) (!((val) & ((align) - 1)))
Signature · 1 line
2.47.0
Re: [PATCH v5 1/3] xen/riscv: introduce setup_mm()
xen/arch/riscv/include/asm/mm.h line 32
Quoted 9 lines · Oleksii Kurochko
xen/arch/riscv/include/asm/mm.h +3
-2@@ -25,8 +27,11 @@static inline void *maddr_to_virt (paddr_t ma) { -BUG_ON ("unimplemented" ); -return NULL ; +unsigned long va_offset =maddr_to_directmapoff (ma); + +ASSERT (va_offset <DIRECTMAP_SIZE );
Much like with the consideration towards virt_to_maddr() that was corrected from v4, I think this one also needs adjusting:
ASSERT(va_offset < DIRECTMAP_SIZE + (DIRECTMAP_VIRT_START -
directmap_virt_start));
This is because ...
xen/arch/riscv/include/asm/mm.h line 34
Quoted 1 line
xen/arch/riscv/include/asm/mm.h +1
-0+return (void *)(directmap_virt_start + va_offset);
... you're offsetting the VA here. It may then want accompanying by
ASSERT(va_offset >= DIRECTMAP_VIRT_START - directmap_virt_start);
(probably to go first).
Quoted 20 lines
xen/arch/riscv/mm.c +16
-0@@ -423,3 +429,147 @@ void * __init early_fdt_map(paddr_t fdt_paddr)return fdt_virt; } + +vaddr_t __ro_after_init directmap_virt_start =DIRECTMAP_VIRT_START ; + +struct page_info *__ro_after_init frametable_virt_start = frame_table; + +#ifndef CONFIG_RISCV_32 + +/* Map a frame table to cover physical addresses ps through pe */ +static void __init setup_frametable_mappings (paddr_t ps,paddr_t pe) +{ +static mfn_t __initdata frametable_mfn_start =INVALID_MFN_INITIALIZER ; + +paddr_t aligned_ps =ROUNDUP (ps,PAGE_SIZE ); +paddr_t aligned_pe =ROUNDDOWN (pe,PAGE_SIZE ); +unsigned long nr_mfns =PFN_DOWN (aligned_pe - aligned_ps); +unsigned long frametable_size = nr_mfns *sizeof (*frame_table);
xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 457
Quoted 10 lines
xen/arch/riscv/mm.c +10
-0+mfn_t base_mfn; + +if (mfn_eq (frametable_mfn_start,INVALID_MFN ) ) + { + frametable_mfn_start =maddr_to_mfn (aligned_ps); + + frametable_virt_start -=paddr_to_pfn (aligned_ps); + } +else +panic ("%s shouldn't be called twice\n" , __func__);
As said on the v4 thread - I don't think this is needed. Aiui Misra would actually dislike it, as it's unreachable code. Just to re-iterate: My complaint there wasn't about this missing check, but about the function partly giving the impression of expecting to be called more than once.
xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 468
Quoted 10 lines
xen/arch/riscv/mm.c +10
-0+if ( frametable_size >FRAMETABLE_SIZE ) +panic ("The frametable cannot cover [%#" PRIpaddr", %#" PRIpaddr")\n" , + ps, pe); + +/* +* align base_mfn and frametable_size to MB(2) to have superpage mapping +* in map_pages_to_xen() +*/ + frametable_size =ROUNDUP (frametable_size,MB (2 )); + base_mfn =alloc_boot_pages (frametable_size >>PAGE_SHIFT ,PFN_DOWN (MB (2 )));
As you already use PFN_DOWN() once, why do you open-code it for the other argument? You also use it ...
xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 471
Quoted 2 lines
xen/arch/riscv/mm.c +2
-0+ if ( map_pages_to_xen(FRAMETABLE_VIRT_START, base_mfn, + PFN_DOWN(frametable_size),
... here, where the purpose of the argument is exactly the same.
Quoted 20 lines
xen/arch/riscv/mm.c +20
-0+void __init setup_mm (void ) +{ +const struct membanks *banks =bootinfo_get_mem (); +paddr_t ram_start =INVALID_PADDR ; +paddr_t ram_end =0 ; +unsigned int i; + +/* +* We need some memory to allocate the page-tables used for the directmap +* mappings. But some regions may contain memory already allocated +* for other uses (e.g. modules, reserved-memory...). +* +* For simplicity, add all the free regions in the boot allocator. +*/ +populate_boot_allocator (); + +for ( i =0 ; i < banks->nr_banks; i++ ) + { +const struct membank *bank = &banks->bank[i]; +paddr_t bank_start =ROUNDUP (bank->start,PAGE_SIZE );
xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 573
Quoted 10 lines
xen/arch/riscv/mm.c +10
-0+paddr_t bank_end =ROUNDDOWN (bank->start + bank->size,PAGE_SIZE ); +unsigned long bank_size = bank_end - bank_start; + + ram_start =min (ram_start, bank_start); + ram_end =max (ram_end, bank_end); + +setup_directmap_mappings (PFN_DOWN (bank_start),PFN_DOWN (bank_size)); + } + +setup_frametable_mappings (ram_start, ram_end);
Just to double check: There is a guarantee that ->nr_banks isn't going to be zero? Else the setup_frametable_mappings() invocation here would badly degenerate.
Jan
Re: [PATCH v5 1/3] xen/riscv: introduce setup_mm()
Quoted 19 lines · Jan Beulich
Quoted 9 lines · Oleksii Kurochko
(quoted diff) +0
-0@@ -25,8 +27,11 @@
static inline void *maddr_to_virt(paddr_t ma)
{
-BUG_ON("unimplemented");
- return NULL;
+ unsigned longva_offset=maddr_to_directmapoff(ma); +
+ASSERT(va_offset < DIRECTMAP_SIZE);Much like with the consideration towards
virt_to_maddr()that was corrected from v4, I think this one also needs adjusting:
ASSERT(va_offset < DIRECTMAP_SIZE + (DIRECTMAP_VIRT_START - directmap_virt_start));This is because ...
Quoted 8 lines
Quoted 1 line
+ return (void *)(directmap_virt_start +
va_offset);... you're offsetting the VA here. It may then want accompanying by
ASSERT(va_offset >= DIRECTMAP_VIRT_START - directmap_virt_start);(probably to go first).
I might be misunderstanding something, but shouldn't the assertions be as follows?
DIRECTMAP_VIRT_START <= (directmap_virt_start + va_offset) < DIRECTMAP_VIRT_SIZE
So, va_offset should be in the range:
DIRECTMAP_VIRT_START - direcmap_virt_start <= va_offset < DIRECTMAP_VIRT_SIZE - directmap_virt_start
This would make the assertions look like:
ASSERT(va_offset < DIRECTMAP_VIRT_SIZE - directmap_virt_tart) ASSERT(va_offset >= DIRECTMAP_VIRT_START - directmap_virt_start)
So, we have different check for the first ASSERT. Could you please clarify where my calculations are wrong?
Quoted 38 lines
Quoted 28 lines
(quoted diff) +0
-0@@ -423,3 +429,147 @@ void * __init early_fdt_map(paddr_t fdt_paddr)
returnfdt_virt;
}(quoted diff) +2
-0+ +vaddr_t __ro_after_init directmap_virt_start =
DIRECTMAP_VIRT_START;(quoted diff) +2
-0+ +struct page_info *__ro_after_init frametable_virt_start =
frame_table;xen/arch/riscv/mm.c +5
-0+ +#ifndef CONFIG_RISCV_32 + +/* Map a frame table to cover physical addresses ps through pe */ +static void __init setup_frametable_mappings(paddr_t ps, paddr_tpe)
+{
+ staticmfn_t__initdataframetable_mfn_start=INVALID_MFN_INITIALIZER;
+
+paddr_taligned_ps=ROUNDUP(ps, PAGE_SIZE);
+paddr_taligned_pe=ROUNDDOWN(pe, PAGE_SIZE);
+ unsigned longnr_mfns=PFN_DOWN(aligned_pe - aligned_ps); + unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
+mfn_tbase_mfn;xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 457
Quoted 9 lines
xen/arch/riscv/mm.c +9
-0+ + if ( mfn_eq(frametable_mfn_start, INVALID_MFN) ) + { + frametable_mfn_start = maddr_to_mfn(aligned_ps); + + frametable_virt_start -= paddr_to_pfn(aligned_ps); + } + else + panic("%s shouldn't be called twice\n", __func__);
Quoted 7 lines
As said on the v4 thread - I don't think this is needed. Aiui Misra would
actually dislike it, as it's unreachable code. Just to re-iterate: My complaint there wasn't about this missing check, but about the function
partly giving the impression of expecting to be called more than once.
I’ll revert this check, then. Would it be better—and sufficient—to add a comment before setup_frametable_mappings() indicating that this function should only be called once, to avoid any impression that it might be expected to be called multiple times?
Quoted 15 lines
Quoted 14 lines
+ if (
frametable_size>FRAMETABLE_SIZE)
+panic("The frametable cannot cover [%#"PRIpaddr", %#"PRIpaddr")\n",
+ ps, pe);
+
+ /*
+ * alignbase_mfnandframetable_sizeto MB(2) to have superpage mapping
+ * inmap_pages_to_xen()
+ */
+frametable_size=ROUNDUP(frametable_size, MB(2)); +base_mfn=alloc_boot_pages(frametable_size >> PAGE_SHIFT, PFN_DOWN(MB(2)));
Quoted 3 lines
As you already use
PFN_DOWN()once, why do you open-code it for the other
argument?
Just overlooked that PFN_DOWN() could be used here.
Quoted 40 lines
You also use it ...
Quoted 2 lines
+ if ( map_pages_to_xen(FRAMETABLE_VIRT_START,
base_mfn, +PFN_DOWN(frametable_size),... here, where the purpose of the argument is exactly the same.
Quoted 33 lines
xen/arch/riscv/mm.c +9
-0+void __init setup_mm (void ) +{ + const struct membanks *banks = bootinfo_get_mem(); + paddr_t ram_start = INVALID_PADDR; + paddr_t ram_end = 0; + unsigned int i; + + /* +* We need some memory to allocate the page-tables used for the directmap
+ * mappings. But some regions may contain memory already allocated
+ * for other uses (e.g. modules, reserved-memory...). + *
+ * For simplicity, add all the free regions in the boot allocator.
+ */
+populate_boot_allocator();
+
+ for ( i = 0; i <banks->nr_banks; i++ )
+ {
+ const struct membank *bank = &banks->bank[i]; +paddr_tbank_start=ROUNDUP(bank->start, PAGE_SIZE); +paddr_tbank_end=ROUNDDOWN(bank->start + bank->size, PAGE_SIZE);
+ unsigned longbank_size=bank_end-bank_start;xen/arch/riscv/mm.c +5
-0+ + ram_start = min(ram_start, bank_start); + ram_end = max(ram_end, bank_end); + + setup_directmap_mappings(PFN_DOWN(bank_start),
PFN_DOWN(bank_size));
+ }
Quoted 8 lines
Quoted 2 lines
+
+setup_frametable_mappings(ram_start, ram_end);Just to double check: There is a guarantee that ->nr_banks isn't going to
be zero? Else thesetup_frametable_mappings()invocation here would badly
degenerate.
Based on the what is mentioned in the device tree spec I would say yes but based on the code we have in Xen ( and if I am not confusing something ) it is still a chance that ->nr_banks could be zero.
From the device tree spec:
``` A memory device node is required for all devicetrees and describes the physical memory layout for the system. If a system has multiple ranges of memory, multiple memory nodes can be created, or the ranges can be specified in the reg property of a single memory node. Property Name Usage Value Type Definition device_type R <string> Value shall be “memory” reg R <prop-encoded-array> ... Usage legend: R=Required, O=Optional, OR=Optional but Recommended, SD=See Definition ```
So the memory node should present in devicetree ( interesting that few years ago I remember that I could build devicetree w/o memory node, anyway, Xen cheking a reg property of memory node and panic if something wrong ).
In the same time, based on the code of device_tree_get_meminfo() ( xen/common/device-tree/bootfdt.c:169 ):
prop = fdt_get_property(fdt, node, prop_name, NULL);
if ( !prop )
return -ENOENT;
cell = (const __be32 *)prop->data;
banks = fdt32_to_cpu(prop->len) / (reg_cells * sizeof (u32));
for ( i = 0; i < banks && mem->nr_banks < mem->max_banks; i++ )
{
device_tree_get_reg(&cell, address_cells, size_cells,
&start, &size);
if ( mem == bootinfo_get_reserved_mem() &&
check_reserved_regions_overlap(start, size) )
return -EINVAL;
/* Some DT may describe empty bank, ignore them */
if ( !size )
continue;
mem->bank[mem->nr_banks].start = start;
mem->bank[mem->nr_banks].size = size;
mem->bank[mem->nr_banks].type = type;
mem->nr_banks++;
}It is possible that mem->nr_banks is equal to 0 ( when DT describe only empty banks? ) but then it sounds to me like something wrong with DT.
Just to be sure that everything is okay with ->nr_banks I could suggest to add BUG_ON(banks->nr_banks) in setup_mm() before the for cycle. Does it make sense?
Thanks.
~ Oleksii
Re: [PATCH v5 1/3] xen/riscv: introduce setup_mm()
Quoted 37 lines · On 13.11.2024 12:39, [email protected] wrote:
Quoted 19 lines · Jan Beulich
Quoted 9 lines · Oleksii Kurochko
(quoted diff) +0
-0@@ -25,8 +27,11 @@
static inline void *maddr_to_virt(paddr_t ma)
{
-BUG_ON("unimplemented");
- return NULL;
+ unsigned longva_offset=maddr_to_directmapoff(ma); +
+ASSERT(va_offset < DIRECTMAP_SIZE);Much like with the consideration towards
virt_to_maddr()that was corrected from v4, I think this one also needs adjusting:
ASSERT(va_offset < DIRECTMAP_SIZE + (DIRECTMAP_VIRT_START - directmap_virt_start));This is because ...
Quoted 8 lines
Quoted 1 line
+ return (void *)(directmap_virt_start +
va_offset);... you're offsetting the VA here. It may then want accompanying by
ASSERT(va_offset >= DIRECTMAP_VIRT_START - directmap_virt_start);(probably to go first).
I might be misunderstanding something, but shouldn't the assertions be as follows?
DIRECTMAP_VIRT_START<= (directmap_virt_start+va_offset) <DIRECTMAP_VIRT_SIZESo,
va_offsetshould be in the range:
DIRECTMAP_VIRT_START-direcmap_virt_start<=va_offset<DIRECTMAP_VIRT_SIZE-directmap_virt_start
Quoted 6 lines
This would make the assertions look like:
ASSERT(va_offset < DIRECTMAP_VIRT_SIZE - directmap_virt_tart)ASSERT(va_offset >= DIRECTMAP_VIRT_START - directmap_virt_start)So, we have different check for the first ASSERT. Could you please clarify where my calculations are wrong?
First and foremost - you can't compare an address (offset or not) against a size. You can only ever compare it against start / end (again with possible offsets applied).
Quoted 44 lines
Quoted 37 lines
Quoted 27 lines
(quoted diff) +0
-0@@ -423,3 +429,147 @@ void * __init early_fdt_map(paddr_t fdt_paddr)
returnfdt_virt;
}(quoted diff) +2
-0+ +vaddr_t __ro_after_init directmap_virt_start =
DIRECTMAP_VIRT_START;(quoted diff) +2
-0+ +struct page_info *__ro_after_init frametable_virt_start =
frame_table;xen/arch/riscv/mm.c +5
-0+ +#ifndef CONFIG_RISCV_32 + +/* Map a frame table to cover physical addresses ps through pe */ +static void __init setup_frametable_mappings(paddr_t ps, paddr_tpe)
+{
+ staticmfn_t__initdataframetable_mfn_start=INVALID_MFN_INITIALIZER;
+
+paddr_taligned_ps=ROUNDUP(ps, PAGE_SIZE);
+paddr_taligned_pe=ROUNDDOWN(pe, PAGE_SIZE);
+ unsigned longnr_mfns=PFN_DOWN(aligned_pe - aligned_ps); + unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
+mfn_tbase_mfn;xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 457
Quoted 9 lines
xen/arch/riscv/mm.c +9
-0+ + if ( mfn_eq(frametable_mfn_start, INVALID_MFN) ) + { + frametable_mfn_start = maddr_to_mfn(aligned_ps); + + frametable_virt_start -= paddr_to_pfn(aligned_ps); + } + else + panic("%s shouldn't be called twice\n", __func__);Quoted 7 lines
As said on the v4 thread - I don't think this is needed. Aiui Misra would
actually dislike it, as it's unreachable code. Just to re-iterate: My complaint there wasn't about this missing check, but about the function
partly giving the impression of expecting to be called more than once.
Quoted 4 lines
I’ll revert this check, then. Would it be better—and sufficient—to add a comment before
setup_frametable_mappings()indicating that this function should only be called once, to avoid any impression that it might be expected to be called multiple times?
You can add such a comment if you like, we we have many functions of this kind. The important aspect - as said before - is that the function should not - nowhere - give the impression of possibly expecting to be called more than once.
Quoted 91 lines
Quoted 33 lines
Quoted 33 lines
xen/arch/riscv/mm.c +9
-0+void __init setup_mm (void ) +{ + const struct membanks *banks = bootinfo_get_mem(); + paddr_t ram_start = INVALID_PADDR; + paddr_t ram_end = 0; + unsigned int i; + + /* +* We need some memory to allocate the page-tables used for the directmap
+ * mappings. But some regions may contain memory already allocated
+ * for other uses (e.g. modules, reserved-memory...). + *
+ * For simplicity, add all the free regions in the boot allocator.
+ */
+populate_boot_allocator();
+
+ for ( i = 0; i <banks->nr_banks; i++ )
+ {
+ const struct membank *bank = &banks->bank[i]; +paddr_tbank_start=ROUNDUP(bank->start, PAGE_SIZE); +paddr_tbank_end=ROUNDDOWN(bank->start + bank->size, PAGE_SIZE);
+ unsigned longbank_size=bank_end-bank_start;xen/arch/riscv/mm.c +5
-0+ + ram_start = min(ram_start, bank_start); + ram_end = max(ram_end, bank_end); + + setup_directmap_mappings(PFN_DOWN(bank_start),
PFN_DOWN(bank_size));
+ }Quoted 8 lines
Quoted 2 lines
+
+setup_frametable_mappings(ram_start, ram_end);Just to double check: There is a guarantee that ->nr_banks isn't going to
be zero? Else thesetup_frametable_mappings()invocation here would badly
degenerate.Based on the what is mentioned in the device tree spec I would say yes but based on the code we have in Xen ( and if I am not confusing something ) it is still a chance that ->nr_banks could be zero.
From the device tree spec:
``` A memory device node is required for all devicetrees and describes the physical memory layout for the system. If a system has multiple ranges of memory, multiple memory nodes can be created, or the ranges can be specified in the reg property of a single memory node. Property Name Usage Value Type Definition device_type R <string> Value shall be “memory” reg R <prop-encoded-array> ... Usage legend: R=Required, O=Optional, OR=Optional but Recommended, SD=See Definition ```So the memory node should present in devicetree ( interesting that few years ago I remember that I could build devicetree w/o memory node, anyway, Xen cheking a reg property of memory node and panic if something wrong ).
In the same time, based on the code of
device_tree_get_meminfo()(xen/common/device-tree/bootfdt.c:169):prop = fdt_get_property(fdt, node, prop_name, NULL); if ( !prop ) return -ENOENT; cell = (const __be32 *)prop->data; banks = fdt32_to_cpu(prop->len) / (reg_cells * sizeof (u32)); for ( i = 0; i < banks && mem->nr_banks < mem->max_banks; i++ ) { device_tree_get_reg(&cell, address_cells, size_cells, &start, &size); if ( mem == bootinfo_get_reserved_mem() && check_reserved_regions_overlap(start, size) ) return -EINVAL; /* Some DT may describe empty bank, ignore them */ if ( !size ) continue; mem->bank[mem->nr_banks].start = start; mem->bank[mem->nr_banks].size = size; mem->bank[mem->nr_banks].type = type; mem->nr_banks++; }
Quoted 6 lines
It is possible that
mem->nr_banksis equal to 0 ( when DT describe only empty banks? ) but then it sounds to me like something wrong with DT.Just to be sure that everything is okay with ->nr_banks I could suggest to add
BUG_ON(banks->nr_banks)insetup_mm()before theforcycle. Does it make sense?
As you likely mean BUG_ON(!banks->nr_banks) - yes. Still this is again a case where I think panic() is preferable over BUG_ON().
Jan
Re: [PATCH v5 1/3] xen/riscv: introduce setup_mm()
Quoted 55 lines · Jan Beulich
Quoted 48 lines
Quoted 40 lines
Quoted 29 lines
(quoted diff) +0
-0@@ -423,3 +429,147 @@ void * __init early_fdt_map(paddr_t fdt_paddr)
returnfdt_virt;
}(quoted diff) +2
-0+ +vaddr_t __ro_after_init directmap_virt_start =
DIRECTMAP_VIRT_START;(quoted diff) +2
-0+ +struct page_info *__ro_after_init frametable_virt_start =
frame_table;xen/arch/riscv/mm.c +4
-0+ +#ifndef CONFIG_RISCV_32 + +/* Map a frame table to cover physical addresses ps through pe */
+static void__initsetup_frametable_mappings(paddr_t ps,paddr_t
pe)
+{
+ staticmfn_t__initdataframetable_mfn_start=INVALID_MFN_INITIALIZER;
+
+paddr_taligned_ps=ROUNDUP(ps, PAGE_SIZE);
+paddr_taligned_pe=ROUNDDOWN(pe, PAGE_SIZE);
+ unsigned longnr_mfns=PFN_DOWN(aligned_pe - aligned_ps); + unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
+mfn_tbase_mfn;xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 457
Quoted 9 lines
xen/arch/riscv/mm.c +9
-0+ + if ( mfn_eq(frametable_mfn_start, INVALID_MFN) ) + { + frametable_mfn_start = maddr_to_mfn(aligned_ps); + + frametable_virt_start -= paddr_to_pfn(aligned_ps); + } + else + panic("%s shouldn't be called twice\n", __func__);As said on the v4 thread - I don't think this is needed. Aiui
Quoted 8 lines
Misra
would
actually dislike it, as it's unreachable code. Just to re-
iterate: My
complaint there wasn't about this missing check, but about the function
partly giving the impression of expecting to be called more than once.Quoted 6 lines
I’ll revert this check, then. Would it be better—and sufficient—to add
a comment beforesetup_frametable_mappings()indicating that this function should only be called once, to avoid any impression that it
might be expected to be called multiple times?
Quoted 7 lines
You can add such a comment if you like, we we have many functions of this
kind. The important aspect - as said before - is that the function should
not - nowhere - give the impression of possibly expecting to be called
more than once.
Then I am not 100% sure how to deal with this impression specifically in the case of setup_frametable_mapping() which should be called once.
The only options I have in my head are:
Option 1:
static void __init setup_frametable_mappings(paddr_t ps, paddr_t
pe)
{
+ static bool __read_mostly once;
+
paddr_t aligned_ps = ROUNDUP(ps, PAGE_SIZE);
paddr_t aligned_pe = ROUNDDOWN(pe, PAGE_SIZE);
unsigned long nr_mfns = PFN_DOWN(aligned_pe - aligned_ps);
unsigned long frametable_size = nr_mfns *
sizeof(*frame_table);
mfn_t base_mfn;
+ ASSERT(!once);
+
+ once = true;
+
frametable_virt_start -= paddr_to_pfn(aligned_ps);
if ( frametable_size > FRAMETABLE_SIZE )Option 2:
-struct page_info *__ro_after_init frametable_virt_start =
frame_table;
+struct page_info *__ro_after_init frametable_virt_start;
#ifndef CONFIG_RISCV_32
@@ -442,7 +442,9 @@ static void __init
setup_frametable_mappings(paddr_t ps, paddr_t pe)
unsigned long frametable_size = nr_mfns * sizeof(*frame_table);
mfn_t base_mfn;
- frametable_virt_start -= paddr_to_pfn(aligned_ps);
+ ASSERT(!frametable_virt_start);
+
+ frametable_virt_start = frame_table -
paddr_to_pfn(aligned_ps);
if ( frametable_size > FRAMETABLE_SIZE )
panic("The frametable cannot cover [%#"PRIpaddr",
%#"PRIpaddr")\n",
~ Oleksii
Re: [PATCH v5 1/3] xen/riscv: introduce setup_mm()
Quoted 63 lines · On 14.11.2024 17:30, [email protected] wrote:
Quoted 55 lines · Jan Beulich
Quoted 48 lines
Quoted 40 lines
Quoted 29 lines
(quoted diff) +0
-0@@ -423,3 +429,147 @@ void * __init early_fdt_map(paddr_t fdt_paddr)
returnfdt_virt;
}(quoted diff) +2
-0+ +vaddr_t __ro_after_init directmap_virt_start =
DIRECTMAP_VIRT_START;(quoted diff) +2
-0+ +struct page_info *__ro_after_init frametable_virt_start =
frame_table;xen/arch/riscv/mm.c +4
-0+ +#ifndef CONFIG_RISCV_32 + +/* Map a frame table to cover physical addresses ps through pe */
+static void__initsetup_frametable_mappings(paddr_t ps,paddr_t
pe)
+{
+ staticmfn_t__initdataframetable_mfn_start=INVALID_MFN_INITIALIZER;
+
+paddr_taligned_ps=ROUNDUP(ps, PAGE_SIZE);
+paddr_taligned_pe=ROUNDDOWN(pe, PAGE_SIZE);
+ unsigned longnr_mfns=PFN_DOWN(aligned_pe - aligned_ps); + unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
+mfn_tbase_mfn;xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 457
Quoted 9 lines
xen/arch/riscv/mm.c +9
-0+ + if ( mfn_eq(frametable_mfn_start, INVALID_MFN) ) + { + frametable_mfn_start = maddr_to_mfn(aligned_ps); + + frametable_virt_start -= paddr_to_pfn(aligned_ps); + } + else + panic("%s shouldn't be called twice\n", __func__);As said on the v4 thread - I don't think this is needed. Aiui
Quoted 8 lines
Misra
would
actually dislike it, as it's unreachable code. Just to re-
iterate: My
complaint there wasn't about this missing check, but about the function
partly giving the impression of expecting to be called more than once.Quoted 6 lines
I’ll revert this check, then. Would it be better—and sufficient—to add
a comment beforesetup_frametable_mappings()indicating that this function should only be called once, to avoid any impression that it
might be expected to be called multiple times?Quoted 7 lines
You can add such a comment if you like, we we have many functions of this
kind. The important aspect - as said before - is that the function should
not - nowhere - give the impression of possibly expecting to be called
more than once.
Quoted 4 lines
Then I am not 100% sure how to deal with this impression specifically in the case of
setup_frametable_mapping()which should be called once.The only options I have in my head are:
Let's try to avoid adding such extra baggage. In the v5 form it looked reasonably okay, iirc.
Jan
Quoted 48 lines
Option 1: static void __init setup_frametable_mappings(paddr_t ps, paddr_t pe) { + static bool __read_mostly once; + paddr_t aligned_ps = ROUNDUP(ps, PAGE_SIZE); paddr_t aligned_pe = ROUNDDOWN(pe, PAGE_SIZE); unsigned long nr_mfns = PFN_DOWN(aligned_pe - aligned_ps); unsigned long frametable_size = nr_mfns * sizeof(*frame_table); mfn_t base_mfn;+ ASSERT(!once);
+
+ once = true;
+
frametable_virt_start-=paddr_to_pfn(aligned_ps);if (
frametable_size>FRAMETABLE_SIZE)Option 2: -struct page_info *__ro_after_init frametable_virt_start = frame_table; +struct page_info *__ro_after_init frametable_virt_start; #ifndef CONFIG_RISCV_32 @@ -442,7 +442,9 @@ static void __init setup_frametable_mappings(paddr_t ps, paddr_t pe) unsigned long frametable_size = nr_mfns * sizeof(*frame_table); mfn_t base_mfn; - frametable_virt_start -= paddr_to_pfn(aligned_ps); + ASSERT(!frametable_virt_start); + + frametable_virt_start = frame_table - paddr_to_pfn(aligned_ps); if ( frametable_size > FRAMETABLE_SIZE ) panic("The frametable cannot cover [%#"PRIpaddr", %#"PRIpaddr")\n",~ Oleksii
Re: [PATCH v5 1/3] xen/riscv: introduce setup_mm()
In reply to [email protected]Quoted 112 lines · On Thu, 2024-11-14 at 17:30 +0100, [email protected] wrote:
Quoted 59 lines · Jan Beulich
Quoted 51 lines
Quoted 43 lines
Quoted 31 lines
(quoted diff) +0
-0@@ -423,3 +429,147 @@ void * __init early_fdt_map(paddr_t fdt_paddr)
returnfdt_virt;
}(quoted diff) +2
-0+ +vaddr_t __ro_after_init directmap_virt_start =
DIRECTMAP_VIRT_START;(quoted diff) +2
-0+ +struct page_info *__ro_after_init frametable_virt_start =
frame_table;xen/arch/riscv/mm.c +4
-0+ +#ifndef CONFIG_RISCV_32 + +/* Map a frame table to cover physical addresses ps through pe
*/
+static void__initsetup_frametable_mappings(paddr_t ps,paddr_t
pe)
+{
+ staticmfn_t__initdataframetable_mfn_start=INVALID_MFN_INITIALIZER;
+
+paddr_taligned_ps=ROUNDUP(ps, PAGE_SIZE); +paddr_taligned_pe=ROUNDDOWN(pe, PAGE_SIZE); + unsigned longnr_mfns=PFN_DOWN(aligned_pe - aligned_ps);
+ unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
+mfn_tbase_mfn;xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 457
Quoted 9 lines
xen/arch/riscv/mm.c +9
-0+ + if ( mfn_eq(frametable_mfn_start, INVALID_MFN) ) + { + frametable_mfn_start = maddr_to_mfn(aligned_ps); + + frametable_virt_start -= paddr_to_pfn(aligned_ps); + } + else + panic("%s shouldn't be called twice\n", __func__);As said on the v4 thread - I don't think this is needed. Aiui Misra
Quoted 8 lines
would
actually dislike it, as it's unreachable code. Just to re-
iterate: My
complaint there wasn't about this missing check, but about the function
partly giving the impression of expecting to be called more than
once.Quoted 7 lines
I’ll revert this check, then. Would it be better—and sufficient—to
add
a comment beforesetup_frametable_mappings()indicating that this function should only be called once, to avoid any impression that it
might be expected to be called multiple times?Quoted 8 lines
You can add such a comment if you like, we we have many functions of
this
kind. The important aspect - as said before - is that the function should
not - nowhere - give the impression of possibly expecting to be called
more than once.Then I am not 100% sure how to deal with this impression specifically in the case of
setup_frametable_mapping()which should be called once.The only options I have in my head are:
Option 1:
static void__initsetup_frametable_mappings(paddr_t ps,paddr_t
pe)
{
+ static bool__read_mostlyonce;
+
paddr_taligned_ps=ROUNDUP(ps, PAGE_SIZE);paddr_taligned_pe=ROUNDDOWN(pe, PAGE_SIZE); unsigned longnr_mfns=PFN_DOWN(aligned_pe - aligned_ps); unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
mfn_tbase_mfn;
+ ASSERT(!once);
+
+ once = true;
+
frametable_virt_start-=paddr_to_pfn(aligned_ps);
if (frametable_size>FRAMETABLE_SIZE)Option 2:
-structpage_info*__ro_after_initframetable_virt_start=frame_table;
+structpage_info*__ro_after_initframetable_virt_start;
#ifndefCONFIG_RISCV_32
@@ -442,7 +442,9 @@ static void__initsetup_frametable_mappings(paddr_t ps, paddr_t pe)unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
mfn_tbase_mfn;
-frametable_virt_start-=paddr_to_pfn(aligned_ps); +ASSERT(!frametable_virt_start);
Quoted 8 lines
+
+frametable_virt_start=frame_table-paddr_to_pfn(aligned_ps);
if (frametable_size>FRAMETABLE_SIZE)panic("The frametable cannot cover [%#"PRIpaddr", %#"PRIpaddr")\n",
Or your intention was that I can drop any check at all in setup_frametable_mappings():
static void __init setup_frametable_mappings(paddr_t ps, paddr_t pe)
{
paddr_t aligned_ps = ROUNDUP(ps, PAGE_SIZE);
paddr_t aligned_pe = ROUNDDOWN(pe, PAGE_SIZE);
unsigned long nr_mfns = PFN_DOWN(aligned_pe - aligned_ps);
unsigned long frametable_size = nr_mfns * sizeof(*frame_table);
mfn_t base_mfn;
frametable_virt_start -= paddr_to_pfn(aligned_ps);... }
and it should be enough as this function by its nature shouldn't be called twice.
~ Oleksii
Re: [PATCH v5 1/3] xen/riscv: introduce setup_mm()
Quoted 136 lines · On 14.11.2024 17:49, [email protected] wrote:
Quoted 112 lines · On Thu, 2024-11-14 at 17:30 +0100, [email protected] wrote:
Quoted 59 lines · Jan Beulich
Quoted 51 lines
Quoted 43 lines
Quoted 31 lines
(quoted diff) +0
-0@@ -423,3 +429,147 @@ void * __init early_fdt_map(paddr_t fdt_paddr)
returnfdt_virt;
}(quoted diff) +2
-0+ +vaddr_t __ro_after_init directmap_virt_start =
DIRECTMAP_VIRT_START;(quoted diff) +2
-0+ +struct page_info *__ro_after_init frametable_virt_start =
frame_table;xen/arch/riscv/mm.c +4
-0+ +#ifndef CONFIG_RISCV_32 + +/* Map a frame table to cover physical addresses ps through pe
*/
+static void__initsetup_frametable_mappings(paddr_t ps,paddr_t
pe)
+{
+ staticmfn_t__initdataframetable_mfn_start=INVALID_MFN_INITIALIZER;
+
+paddr_taligned_ps=ROUNDUP(ps, PAGE_SIZE); +paddr_taligned_pe=ROUNDDOWN(pe, PAGE_SIZE); + unsigned longnr_mfns=PFN_DOWN(aligned_pe - aligned_ps);
+ unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
+mfn_tbase_mfn;xen/arch/riscv/mm.c void * __init early_fdt_map(paddr_t fdt_paddr) · line 457
Quoted 9 lines
xen/arch/riscv/mm.c +9
-0+ + if ( mfn_eq(frametable_mfn_start, INVALID_MFN) ) + { + frametable_mfn_start = maddr_to_mfn(aligned_ps); + + frametable_virt_start -= paddr_to_pfn(aligned_ps); + } + else + panic("%s shouldn't be called twice\n", __func__);As said on the v4 thread - I don't think this is needed. Aiui Misra
Quoted 8 lines
would
actually dislike it, as it's unreachable code. Just to re-
iterate: My
complaint there wasn't about this missing check, but about the function
partly giving the impression of expecting to be called more than
once.Quoted 7 lines
I’ll revert this check, then. Would it be better—and sufficient—to
add
a comment beforesetup_frametable_mappings()indicating that this function should only be called once, to avoid any impression that it
might be expected to be called multiple times?Quoted 8 lines
You can add such a comment if you like, we we have many functions of
this
kind. The important aspect - as said before - is that the function should
not - nowhere - give the impression of possibly expecting to be called
more than once.Then I am not 100% sure how to deal with this impression specifically in the case of
setup_frametable_mapping()which should be called once.The only options I have in my head are:
Option 1:
static void__initsetup_frametable_mappings(paddr_t ps,paddr_t
pe)
{
+ static bool__read_mostlyonce;
+
paddr_taligned_ps=ROUNDUP(ps, PAGE_SIZE);paddr_taligned_pe=ROUNDDOWN(pe, PAGE_SIZE); unsigned longnr_mfns=PFN_DOWN(aligned_pe - aligned_ps); unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
mfn_tbase_mfn;
+ ASSERT(!once);
+
+ once = true;
+
frametable_virt_start-=paddr_to_pfn(aligned_ps);
if (frametable_size>FRAMETABLE_SIZE)Option 2:
-structpage_info*__ro_after_initframetable_virt_start=frame_table;
+structpage_info*__ro_after_initframetable_virt_start;
#ifndefCONFIG_RISCV_32
@@ -442,7 +442,9 @@ static void__initsetup_frametable_mappings(paddr_t ps, paddr_t pe)unsigned longframetable_size=nr_mfns*sizeof(*frame_table);
mfn_tbase_mfn;
-frametable_virt_start-=paddr_to_pfn(aligned_ps); +ASSERT(!frametable_virt_start);Quoted 8 lines
+
+frametable_virt_start=frame_table-paddr_to_pfn(aligned_ps);
if (frametable_size>FRAMETABLE_SIZE)panic("The frametable cannot cover [%#"PRIpaddr", %#"PRIpaddr")\n",Or your intention was that I can drop any check at all in
setup_frametable_mappings():static void __init setup_frametable_mappings(paddr_t ps, paddr_t pe) { paddr_t aligned_ps = ROUNDUP(ps, PAGE_SIZE); paddr_t aligned_pe = ROUNDDOWN(pe, PAGE_SIZE); unsigned long nr_mfns = PFN_DOWN(aligned_pe - aligned_ps); unsigned long frametable_size = nr_mfns * sizeof(*frame_table); mfn_t base_mfn; frametable_virt_start -= paddr_to_pfn(aligned_ps); ... }
Quoted 2 lines
and it should be enough as this function by its nature shouldn't be called twice.
Indeed, that's what I was after.
Jan
PATCH v5 2/3 xen/riscv: initialize the VMAP_DEFAULT virtual range
In reply to Oleksii KurochkoCall vm_init() to initialize the VMAP_DEFAULT virtual range.
To support this, introduce the populate_pt_range() and arch_vmap_virt_end() functions, which are used by vm_init()->vm_init_type().
---
Change in V2-V5:
- Acked-by: Jan Beulich <[email protected]>
--- xen/arch/riscv/mm.c | 11 +++++------ xen/arch/riscv/pt.c | 6 ++++++ xen/arch/riscv/setup.c | 3 +++ 3 files changed, 14 insertions(+), 6 deletions(-)
xen/arch/riscv/mm.c +5 -6
@@ -351,12 +351,6 @@ void arch_dump_shared_mem_info(void)BUG_ON ("unimplemented" ); } -int populate_pt_range (unsigned long virt,unsigned long nr_mfns) -{ -BUG_ON ("unimplemented" ); -return -1 ; -} -int xenmem_add_to_physmap_one (struct domain *d,unsigned int space,union add_to_physmap_extra extra,unsigned long idx,gfn_t gfn)
@@ -573,3 +567,8 @@ void __init setup_mm(void)setup_frametable_mappings (ram_start, ram_end); max_page =PFN_DOWN (ram_end); } + +void *__init arch_vmap_virt_end (void ) +{ +return (void *)(VMAP_VIRT_START +VMAP_VIRT_SIZE ); +}
xen/arch/riscv/pt.c +6 -0
@@ -1,6 +1,7 @@#include <xen/bug.h> #include <xen/domain_page.h> #include <xen/errno.h> +#include <xen/init.h> #include <xen/lib.h> #include <xen/mm.h> #include <xen/pfn.h>
@@ -419,3 +420,8 @@ int map_pages_to_xen(unsigned long virt,return pt_update (virt, mfn, nr_mfns, flags); } + +int __init populate_pt_range (unsigned long virt,unsigned long nr_mfns) +{ +return pt_update (virt,INVALID_MFN , nr_mfns,PTE_POPULATE ); +}
xen/arch/riscv/setup.c +3 -0
@@ -7,6 +7,7 @@#include <xen/init.h> #include <xen/mm.h> #include <xen/shutdown.h> +#include <xen/vmap.h> #include <public/version.h>
@@ -62,6 +63,8 @@ void __init noreturn start_xen(unsigned long bootcpu_id,setup_mm (); +vm_init (); +printk ("All set up\n" );machine_halt ();
Signature · 1 line
2.47.0
PATCH v5 3/3 xen/riscv: finalize boot allocator and transition to boot state
In reply to Oleksii KurochkoAdd a call to end_boot_allocator() in start_xen() to finalize the boot memory allocator, moving free pages to the domain sub-allocator.
After initializing the memory subsystem, update system_state from SYS_STATE_early_boot to SYS_STATE_boot, signifying the end of the early boot phase.
---
Change in V2-V5:
- Acked-by: Jan Beulich <[email protected]>
--- xen/arch/riscv/setup.c | 8 ++++++++ 1 file changed, 8 insertions(+)
xen/arch/riscv/setup.c +8 -0
@@ -65,6 +65,14 @@ void __init noreturn start_xen(unsigned long bootcpu_id,vm_init (); +end_boot_allocator (); + +/* +* The memory subsystem has been initialized, we can now switch from +* early_boot -> boot. +*/ + system_state = SYS_STATE_boot; +printk ("All set up\n" );machine_halt ();
Signature · 1 line
2.47.0