diff mbox series

[Xen-devel,for-next,7/9] xen: Convert is_xen_heap_mfn to use typesafe MFN

Message ID 20190218113600.9540-8-julien.grall@arm.com
State Superseded
Headers show
Series xen/arm: Properly disable M2P on Arm. | expand

Commit Message

Julien Grall Feb. 18, 2019, 11:35 a.m. UTC
No functional changes.

Signed-off-by: Julien Grall <julien.grall@arm.com>
---
 xen/arch/x86/mm.c              | 9 +++++----
 xen/arch/x86/mm/p2m.c          | 2 +-
 xen/arch/x86/mm/shadow/multi.c | 2 +-
 xen/common/page_alloc.c        | 4 ++--
 xen/include/asm-arm/mm.h       | 8 ++++----
 xen/include/asm-x86/mm.h       | 2 +-
 6 files changed, 14 insertions(+), 13 deletions(-)

Comments

Jan Beulich March 13, 2019, 3:04 p.m. UTC | #1
>>> On 18.02.19 at 12:35, <julien.grall@arm.com> wrote:
> --- a/xen/arch/x86/mm.c
> +++ b/xen/arch/x86/mm.c
> @@ -4300,7 +4300,8 @@ int xenmem_add_to_physmap_one(
>  {
>      struct page_info *page = NULL;
>      unsigned long gfn = 0; /* gcc ... */
> -    unsigned long prev_mfn, old_gpfn;
> +    mfn_t prev_mfn;
> +    unsigned long old_gpfn;

Please can you put this together with "gfn"?

> --- a/xen/common/page_alloc.c
> +++ b/xen/common/page_alloc.c
> @@ -2121,9 +2121,9 @@ void init_xenheap_pages(paddr_t ps, paddr_t pe)
>       * Yuk! Ensure there is a one-page buffer between Xen and Dom zones, to
>       * prevent merging of power-of-two blocks across the zone boundary.
>       */
> -    if ( ps && !is_xen_heap_mfn(paddr_to_pfn(ps)-1) )
> +    if ( ps && !is_xen_heap_mfn(_mfn(paddr_to_pfn(ps)-1)) )

Please add the missing blanks around - at the same time.

>          ps += PAGE_SIZE;
> -    if ( !is_xen_heap_mfn(paddr_to_pfn(pe)) )
> +    if ( !is_xen_heap_mfn(maddr_to_mfn(pe)) )

Why maddr_to_mfn() here but still paddr_to_pfn() above? Oh,
we don't have any mfn_sub(), I see.

> --- a/xen/include/asm-x86/mm.h
> +++ b/xen/include/asm-x86/mm.h
> @@ -278,7 +278,7 @@ struct page_info
>  
>  #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
>  #define is_xen_heap_mfn(mfn) \
> -    (__mfn_valid(mfn) && is_xen_heap_page(mfn_to_page(_mfn(mfn))))
> +    (__mfn_valid(mfn_x(mfn)) && is_xen_heap_page(mfn_to_page(mfn)))

Please don't open code mfn_valid().

With these minor issues taken care of
Reviewed-by: Jan Beulich <jbeulich@suse.com>

Jan
Julien Grall March 13, 2019, 5:24 p.m. UTC | #2
Hi,

On 13/03/2019 15:04, Jan Beulich wrote:
>>>> On 18.02.19 at 12:35, <julien.grall@arm.com> wrote:
>> --- a/xen/arch/x86/mm.c
>> +++ b/xen/arch/x86/mm.c
>> @@ -4300,7 +4300,8 @@ int xenmem_add_to_physmap_one(
>>   {
>>       struct page_info *page = NULL;
>>       unsigned long gfn = 0; /* gcc ... */
>> -    unsigned long prev_mfn, old_gpfn;
>> +    mfn_t prev_mfn;
>> +    unsigned long old_gpfn;
> 
> Please can you put this together with "gfn"?

Sure.

> 
>> --- a/xen/common/page_alloc.c
>> +++ b/xen/common/page_alloc.c
>> @@ -2121,9 +2121,9 @@ void init_xenheap_pages(paddr_t ps, paddr_t pe)
>>        * Yuk! Ensure there is a one-page buffer between Xen and Dom zones, to
>>        * prevent merging of power-of-two blocks across the zone boundary.
>>        */
>> -    if ( ps && !is_xen_heap_mfn(paddr_to_pfn(ps)-1) )
>> +    if ( ps && !is_xen_heap_mfn(_mfn(paddr_to_pfn(ps)-1)) )
> 
> Please add the missing blanks around - at the same time.

Ok.

> 
>>           ps += PAGE_SIZE;
>> -    if ( !is_xen_heap_mfn(paddr_to_pfn(pe)) )
>> +    if ( !is_xen_heap_mfn(maddr_to_mfn(pe)) )
> 
> Why maddr_to_mfn() here but still paddr_to_pfn() above? Oh,
> we don't have any mfn_sub(), I see.

Yes we don't have mfn_sub() (or even gfn_sub()). I only found a couple of places 
where such helpers might be useful. I can introduce the 2 helpers if you think 
it is worth it.

> 
>> --- a/xen/include/asm-x86/mm.h
>> +++ b/xen/include/asm-x86/mm.h
>> @@ -278,7 +278,7 @@ struct page_info
>>   
>>   #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
>>   #define is_xen_heap_mfn(mfn) \
>> -    (__mfn_valid(mfn) && is_xen_heap_page(mfn_to_page(_mfn(mfn))))
>> +    (__mfn_valid(mfn_x(mfn)) && is_xen_heap_page(mfn_to_page(mfn)))
> 
> Please don't open code mfn_valid().

That's a mistake. Someone I thought there were place in the code that was 
overriding mfn_valid().

I will update it.

> 
> With these minor issues taken care of
> Reviewed-by: Jan Beulich <jbeulich@suse.com>

Thank you!

> 
> Jan
> 
>
Jan Beulich March 14, 2019, 7:52 a.m. UTC | #3
>>> On 13.03.19 at 18:24, <julien.grall@arm.com> wrote:
> On 13/03/2019 15:04, Jan Beulich wrote:
>>>>> On 18.02.19 at 12:35, <julien.grall@arm.com> wrote:
>>> --- a/xen/common/page_alloc.c
>>> +++ b/xen/common/page_alloc.c
>>> @@ -2121,9 +2121,9 @@ void init_xenheap_pages(paddr_t ps, paddr_t pe)
>>>        * Yuk! Ensure there is a one-page buffer between Xen and Dom zones, to
>>>        * prevent merging of power-of-two blocks across the zone boundary.
>>>        */
>>> -    if ( ps && !is_xen_heap_mfn(paddr_to_pfn(ps)-1) )
>>> +    if ( ps && !is_xen_heap_mfn(_mfn(paddr_to_pfn(ps)-1)) )
>>>           ps += PAGE_SIZE;
>>> -    if ( !is_xen_heap_mfn(paddr_to_pfn(pe)) )
>>> +    if ( !is_xen_heap_mfn(maddr_to_mfn(pe)) )
>> 
>> Why maddr_to_mfn() here but still paddr_to_pfn() above? Oh,
>> we don't have any mfn_sub(), I see.
> 
> Yes we don't have mfn_sub() (or even gfn_sub()). I only found a couple of places 
> where such helpers might be useful. I can introduce the 2 helpers if you think 
> it is worth it.

Well, I guess in the end I'm fine either way. It simply struck me
as odd at the first glance that you use maddr_to_mfn() in one
case but not the other.

Jan
Julien Grall March 14, 2019, 10:12 a.m. UTC | #4
Hi Jan,

On 3/14/19 7:52 AM, Jan Beulich wrote:
>>>> On 13.03.19 at 18:24, <julien.grall@arm.com> wrote:
>> On 13/03/2019 15:04, Jan Beulich wrote:
>>>>>> On 18.02.19 at 12:35, <julien.grall@arm.com> wrote:
>>>> --- a/xen/common/page_alloc.c
>>>> +++ b/xen/common/page_alloc.c
>>>> @@ -2121,9 +2121,9 @@ void init_xenheap_pages(paddr_t ps, paddr_t pe)
>>>>         * Yuk! Ensure there is a one-page buffer between Xen and Dom zones, to
>>>>         * prevent merging of power-of-two blocks across the zone boundary.
>>>>         */
>>>> -    if ( ps && !is_xen_heap_mfn(paddr_to_pfn(ps)-1) )
>>>> +    if ( ps && !is_xen_heap_mfn(_mfn(paddr_to_pfn(ps)-1)) )
>>>>            ps += PAGE_SIZE;
>>>> -    if ( !is_xen_heap_mfn(paddr_to_pfn(pe)) )
>>>> +    if ( !is_xen_heap_mfn(maddr_to_mfn(pe)) )
>>>
>>> Why maddr_to_mfn() here but still paddr_to_pfn() above? Oh,
>>> we don't have any mfn_sub(), I see.
>>
>> Yes we don't have mfn_sub() (or even gfn_sub()). I only found a couple of places
>> where such helpers might be useful. I can introduce the 2 helpers if you think
>> it is worth it.
> 
> Well, I guess in the end I'm fine either way. It simply struck me
> as odd at the first glance that you use maddr_to_mfn() in one
> case but not the other.

I wanted to avoid adding mfn_x(...) on the line:

_mfn(mfn_x(maddr_to_mfn(ps)) - 1)

I will look to introduce mfn_sub()/gfn_sub().

Cheers,
Andrew Cooper March 14, 2019, 10:14 a.m. UTC | #5
On 14/03/2019 10:12, Julien Grall wrote:
> Hi Jan,
>
> On 3/14/19 7:52 AM, Jan Beulich wrote:
>>>>> On 13.03.19 at 18:24, <julien.grall@arm.com> wrote:
>>> On 13/03/2019 15:04, Jan Beulich wrote:
>>>>>>> On 18.02.19 at 12:35, <julien.grall@arm.com> wrote:
>>>>> --- a/xen/common/page_alloc.c
>>>>> +++ b/xen/common/page_alloc.c
>>>>> @@ -2121,9 +2121,9 @@ void init_xenheap_pages(paddr_t ps, paddr_t pe)
>>>>>         * Yuk! Ensure there is a one-page buffer between Xen and
>>>>> Dom zones, to
>>>>>         * prevent merging of power-of-two blocks across the zone
>>>>> boundary.
>>>>>         */
>>>>> -    if ( ps && !is_xen_heap_mfn(paddr_to_pfn(ps)-1) )
>>>>> +    if ( ps && !is_xen_heap_mfn(_mfn(paddr_to_pfn(ps)-1)) )
>>>>>            ps += PAGE_SIZE;
>>>>> -    if ( !is_xen_heap_mfn(paddr_to_pfn(pe)) )
>>>>> +    if ( !is_xen_heap_mfn(maddr_to_mfn(pe)) )
>>>>
>>>> Why maddr_to_mfn() here but still paddr_to_pfn() above? Oh,
>>>> we don't have any mfn_sub(), I see.
>>>
>>> Yes we don't have mfn_sub() (or even gfn_sub()). I only found a
>>> couple of places
>>> where such helpers might be useful. I can introduce the 2 helpers if
>>> you think
>>> it is worth it.
>>
>> Well, I guess in the end I'm fine either way. It simply struck me
>> as odd at the first glance that you use maddr_to_mfn() in one
>> case but not the other.
>
> I wanted to avoid adding mfn_x(...) on the line:
>
> _mfn(mfn_x(maddr_to_mfn(ps)) - 1)
>
> I will look to introduce mfn_sub()/gfn_sub().

You do know that mfn_add(mfn, -1) will DTRT?

~Andrew
Julien Grall March 14, 2019, 10:19 a.m. UTC | #6
Hi Andrew,

On 3/14/19 10:14 AM, Andrew Cooper wrote:
> On 14/03/2019 10:12, Julien Grall wrote:
>> Hi Jan,
>>
>> On 3/14/19 7:52 AM, Jan Beulich wrote:
>>>>>> On 13.03.19 at 18:24, <julien.grall@arm.com> wrote:
>>>> On 13/03/2019 15:04, Jan Beulich wrote:
>>>>>>>> On 18.02.19 at 12:35, <julien.grall@arm.com> wrote:
>>>>>> --- a/xen/common/page_alloc.c
>>>>>> +++ b/xen/common/page_alloc.c
>>>>>> @@ -2121,9 +2121,9 @@ void init_xenheap_pages(paddr_t ps, paddr_t pe)
>>>>>>          * Yuk! Ensure there is a one-page buffer between Xen and
>>>>>> Dom zones, to
>>>>>>          * prevent merging of power-of-two blocks across the zone
>>>>>> boundary.
>>>>>>          */
>>>>>> -    if ( ps && !is_xen_heap_mfn(paddr_to_pfn(ps)-1) )
>>>>>> +    if ( ps && !is_xen_heap_mfn(_mfn(paddr_to_pfn(ps)-1)) )
>>>>>>             ps += PAGE_SIZE;
>>>>>> -    if ( !is_xen_heap_mfn(paddr_to_pfn(pe)) )
>>>>>> +    if ( !is_xen_heap_mfn(maddr_to_mfn(pe)) )
>>>>>
>>>>> Why maddr_to_mfn() here but still paddr_to_pfn() above? Oh,
>>>>> we don't have any mfn_sub(), I see.
>>>>
>>>> Yes we don't have mfn_sub() (or even gfn_sub()). I only found a
>>>> couple of places
>>>> where such helpers might be useful. I can introduce the 2 helpers if
>>>> you think
>>>> it is worth it.
>>>
>>> Well, I guess in the end I'm fine either way. It simply struck me
>>> as odd at the first glance that you use maddr_to_mfn() in one
>>> case but not the other.
>>
>> I wanted to avoid adding mfn_x(...) on the line:
>>
>> _mfn(mfn_x(maddr_to_mfn(ps)) - 1)
>>
>> I will look to introduce mfn_sub()/gfn_sub().
> 
> You do know that mfn_add(mfn, -1) will DTRT?

It didn't occur to me until you said it. I will use that then.

Cheers,
Jan Beulich March 14, 2019, 11:47 a.m. UTC | #7
>>> On 14.03.19 at 11:14, <andrew.cooper3@citrix.com> wrote:
> You do know that mfn_add(mfn, -1) will DTRT?

It will, but imo it looks slightly odd in particular in e.g.
for ( ...; ...; mfn_add(mfn, -1) ), hence I didn't suggest it. But
I don't object either.

Jan
Andrew Cooper March 14, 2019, 12:18 p.m. UTC | #8
On 14/03/2019 11:47, Jan Beulich wrote:
>>>> On 14.03.19 at 11:14, <andrew.cooper3@citrix.com> wrote:
>> You do know that mfn_add(mfn, -1) will DTRT?
> It will, but imo it looks slightly odd in particular in e.g.
> for ( ...; ...; mfn_add(mfn, -1) ), hence I didn't suggest it. But
> I don't object either.

Well - in mathematics, x - 1 is identical to x + (-1), so the construct
doesn't look overly odd to me.

Alternatively, we could rename mfn_add to something else, but nothing
comes to mind which would be as clear.

~Andrew
Stefano Stabellini April 15, 2019, 10:08 p.m. UTC | #9
On Mon, 18 Feb 2019, Julien Grall wrote:
> diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h
> index b56018aace..a9c8352b94 100644
> --- a/xen/include/asm-arm/mm.h
> +++ b/xen/include/asm-arm/mm.h
> @@ -138,16 +138,16 @@ extern vaddr_t xenheap_virt_start;
>  #endif
>  
>  #ifdef CONFIG_ARM_32
> -#define is_xen_heap_page(page) is_xen_heap_mfn(mfn_x(page_to_mfn(page)))
> +#define is_xen_heap_page(page) is_xen_heap_mfn(page_to_mfn(page))
>  #define is_xen_heap_mfn(mfn) ({                                 \
> -    unsigned long mfn_ = (mfn);                                 \
> +    unsigned long mfn_ = mfn_x(mfn);                            \
>      (mfn_ >= mfn_x(xenheap_mfn_start) &&                        \
>       mfn_ < mfn_x(xenheap_mfn_end));                            \
>  })
>  #else
>  #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
>  #define is_xen_heap_mfn(mfn) \
> -    (mfn_valid(_mfn(mfn)) && is_xen_heap_page(mfn_to_page(_mfn(mfn))))
> +    (mfn_valid(mfn) && is_xen_heap_page(mfn_to_page(mfn)))
>  #endif
>  
>  #define is_xen_fixed_mfn(mfn)                                   \
> @@ -246,7 +246,7 @@ static inline paddr_t __virt_to_maddr(vaddr_t va)
>  #ifdef CONFIG_ARM_32
>  static inline void *maddr_to_virt(paddr_t ma)
>  {
> -    ASSERT(is_xen_heap_mfn(ma >> PAGE_SHIFT));
> +    ASSERT(is_xen_heap_mfn(maddr_to_mfn(ma)));
>      ma -= mfn_to_maddr(xenheap_mfn_start);
>      return (void *)(unsigned long) ma + XENHEAP_VIRT_START;
>  }

For the ARM parts:

Acked-by: Stefano Stabellini <sstabellini@kernel.org>
diff mbox series

Patch

diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index df6e5bdd31..a1cd2fb421 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -4300,7 +4300,8 @@  int xenmem_add_to_physmap_one(
 {
     struct page_info *page = NULL;
     unsigned long gfn = 0; /* gcc ... */
-    unsigned long prev_mfn, old_gpfn;
+    mfn_t prev_mfn;
+    unsigned long old_gpfn;
     int rc = 0;
     mfn_t mfn = INVALID_MFN;
     p2m_type_t p2mt;
@@ -4349,12 +4350,12 @@  int xenmem_add_to_physmap_one(
     }
 
     /* Remove previously mapped page if it was present. */
-    prev_mfn = mfn_x(get_gfn(d, gfn_x(gpfn), &p2mt));
-    if ( mfn_valid(_mfn(prev_mfn)) )
+    prev_mfn = get_gfn(d, gfn_x(gpfn), &p2mt);
+    if ( mfn_valid(prev_mfn) )
     {
         if ( is_xen_heap_mfn(prev_mfn) )
             /* Xen heap frames are simply unhooked from this phys slot. */
-            rc = guest_physmap_remove_page(d, gpfn, _mfn(prev_mfn), PAGE_ORDER_4K);
+            rc = guest_physmap_remove_page(d, gpfn, prev_mfn, PAGE_ORDER_4K);
         else
             /* Normal domain memory is freed, to avoid leaking memory. */
             rc = guest_remove_page(d, gfn_x(gpfn));
diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c
index d14ce57dd5..40fed111b9 100644
--- a/xen/arch/x86/mm/p2m.c
+++ b/xen/arch/x86/mm/p2m.c
@@ -2910,7 +2910,7 @@  int p2m_add_foreign(struct domain *tdom, unsigned long fgfn,
     prev_mfn = get_gfn(tdom, gpfn, &p2mt_prev);
     if ( mfn_valid(prev_mfn) )
     {
-        if ( is_xen_heap_mfn(mfn_x(prev_mfn)) )
+        if ( is_xen_heap_mfn(prev_mfn) )
             /* Xen heap frames are simply unhooked from this phys slot */
             rc = guest_physmap_remove_page(tdom, _gfn(gpfn), prev_mfn, 0);
         else
diff --git a/xen/arch/x86/mm/shadow/multi.c b/xen/arch/x86/mm/shadow/multi.c
index 7e9cbc69be..2d25e356d3 100644
--- a/xen/arch/x86/mm/shadow/multi.c
+++ b/xen/arch/x86/mm/shadow/multi.c
@@ -602,7 +602,7 @@  _sh_propagate(struct vcpu *v,
      * caching attributes in the shadows to match what was asked for.
      */
     if ( (level == 1) && is_hvm_domain(d) &&
-         !is_xen_heap_mfn(mfn_x(target_mfn)) )
+         !is_xen_heap_mfn(target_mfn) )
     {
         int type;
 
diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c
index 5de3686d85..e43c98d5cb 100644
--- a/xen/common/page_alloc.c
+++ b/xen/common/page_alloc.c
@@ -2121,9 +2121,9 @@  void init_xenheap_pages(paddr_t ps, paddr_t pe)
      * Yuk! Ensure there is a one-page buffer between Xen and Dom zones, to
      * prevent merging of power-of-two blocks across the zone boundary.
      */
-    if ( ps && !is_xen_heap_mfn(paddr_to_pfn(ps)-1) )
+    if ( ps && !is_xen_heap_mfn(_mfn(paddr_to_pfn(ps)-1)) )
         ps += PAGE_SIZE;
-    if ( !is_xen_heap_mfn(paddr_to_pfn(pe)) )
+    if ( !is_xen_heap_mfn(maddr_to_mfn(pe)) )
         pe -= PAGE_SIZE;
 
     memguard_guard_range(maddr_to_virt(ps), pe - ps);
diff --git a/xen/include/asm-arm/mm.h b/xen/include/asm-arm/mm.h
index b56018aace..a9c8352b94 100644
--- a/xen/include/asm-arm/mm.h
+++ b/xen/include/asm-arm/mm.h
@@ -138,16 +138,16 @@  extern vaddr_t xenheap_virt_start;
 #endif
 
 #ifdef CONFIG_ARM_32
-#define is_xen_heap_page(page) is_xen_heap_mfn(mfn_x(page_to_mfn(page)))
+#define is_xen_heap_page(page) is_xen_heap_mfn(page_to_mfn(page))
 #define is_xen_heap_mfn(mfn) ({                                 \
-    unsigned long mfn_ = (mfn);                                 \
+    unsigned long mfn_ = mfn_x(mfn);                            \
     (mfn_ >= mfn_x(xenheap_mfn_start) &&                        \
      mfn_ < mfn_x(xenheap_mfn_end));                            \
 })
 #else
 #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
 #define is_xen_heap_mfn(mfn) \
-    (mfn_valid(_mfn(mfn)) && is_xen_heap_page(mfn_to_page(_mfn(mfn))))
+    (mfn_valid(mfn) && is_xen_heap_page(mfn_to_page(mfn)))
 #endif
 
 #define is_xen_fixed_mfn(mfn)                                   \
@@ -246,7 +246,7 @@  static inline paddr_t __virt_to_maddr(vaddr_t va)
 #ifdef CONFIG_ARM_32
 static inline void *maddr_to_virt(paddr_t ma)
 {
-    ASSERT(is_xen_heap_mfn(ma >> PAGE_SHIFT));
+    ASSERT(is_xen_heap_mfn(maddr_to_mfn(ma)));
     ma -= mfn_to_maddr(xenheap_mfn_start);
     return (void *)(unsigned long) ma + XENHEAP_VIRT_START;
 }
diff --git a/xen/include/asm-x86/mm.h b/xen/include/asm-x86/mm.h
index f124f57964..1ca4154382 100644
--- a/xen/include/asm-x86/mm.h
+++ b/xen/include/asm-x86/mm.h
@@ -278,7 +278,7 @@  struct page_info
 
 #define is_xen_heap_page(page) ((page)->count_info & PGC_xen_heap)
 #define is_xen_heap_mfn(mfn) \
-    (__mfn_valid(mfn) && is_xen_heap_page(mfn_to_page(_mfn(mfn))))
+    (__mfn_valid(mfn_x(mfn)) && is_xen_heap_page(mfn_to_page(mfn)))
 #define is_xen_fixed_mfn(mfn)                     \
     (((mfn_to_maddr(mfn)) >= __pa(&_stext)) &&    \
      ((mfn_to_maddr(mfn)) <= __pa(&__2M_rwdata_end)))