diff mbox

[Xen-devel,06/22] xen/arm: p2m: Use the typesafe MFN in mfn_to_p2m_entry

Message ID 1469031064-23344-7-git-send-email-julien.grall@arm.com
State New
Headers show

Commit Message

Julien Grall July 20, 2016, 4:10 p.m. UTC
Signed-off-by: Julien Grall <julien.grall@arm.com>
---
 xen/arch/arm/p2m.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

Comments

Julien Grall July 27, 2016, 9:54 a.m. UTC | #1
Hi Stefano,

On 26/07/16 23:28, Stefano Stabellini wrote:
> On Wed, 20 Jul 2016, Julien Grall wrote:
>> @@ -411,7 +411,7 @@ static int p2m_create_table(struct domain *d, lpae_t *entry,
>>      if ( splitting )
>>      {
>>          p2m_type_t t = entry->p2m.type;
>> -        unsigned long base_pfn = entry->p2m.base;
>> +        mfn_t mfn = _mfn(entry->p2m.base);
>>          int i;
>>
>>          /*
>> @@ -420,8 +420,9 @@ static int p2m_create_table(struct domain *d, lpae_t *entry,
>>           */
>>           for ( i=0 ; i < LPAE_ENTRIES; i++ )
>>           {
>> -             pte = mfn_to_p2m_entry(base_pfn + (i<<(level_shift-LPAE_SHIFT)),
>> -                                    MATTR_MEM, t, p2m->default_access);
>> +             pte = mfn_to_p2m_entry(mfn, MATTR_MEM, t, p2m->default_access);
>> +
>> +             mfn = mfn_add(mfn, 1UL << (level_shift - LPAE_SHIFT));
>
> Should we be incrementing mfn before calling mfn_to_p2m_entry?

No. The base of the superpage is mfn, after splitting the first entry 
will be equal to the base, the second entry base + level_size...

Regards,
Julien Grall July 27, 2016, 8:14 p.m. UTC | #2
Hi Stefano,

On 27/07/2016 19:25, Stefano Stabellini wrote:
> On Wed, 27 Jul 2016, Julien Grall wrote:
>> Hi Stefano,
>>
>> On 26/07/16 23:28, Stefano Stabellini wrote:
>>> On Wed, 20 Jul 2016, Julien Grall wrote:
>>>> @@ -411,7 +411,7 @@ static int p2m_create_table(struct domain *d, lpae_t
>>>> *entry,
>>>>      if ( splitting )
>>>>      {
>>>>          p2m_type_t t = entry->p2m.type;
>>>> -        unsigned long base_pfn = entry->p2m.base;
>>>> +        mfn_t mfn = _mfn(entry->p2m.base);
>>>>          int i;
>>>>
>>>>          /*
>>>> @@ -420,8 +420,9 @@ static int p2m_create_table(struct domain *d, lpae_t
>>>> *entry,
>>>>           */
>>>>           for ( i=0 ; i < LPAE_ENTRIES; i++ )
>>>>           {
>>>> -             pte = mfn_to_p2m_entry(base_pfn +
>>>> (i<<(level_shift-LPAE_SHIFT)),
>>>> -                                    MATTR_MEM, t, p2m->default_access);
>>>> +             pte = mfn_to_p2m_entry(mfn, MATTR_MEM, t,
>>>> p2m->default_access);
>>>> +
>>>> +             mfn = mfn_add(mfn, 1UL << (level_shift - LPAE_SHIFT));
>>>
>>> Should we be incrementing mfn before calling mfn_to_p2m_entry?
>>
>> No. The base of the superpage is mfn, after splitting the first entry will be
>> equal to the base, the second entry base + level_size...
>
> I understand what the patch is doing now, I confused "1" with "i" :-)
> The patch is OK. It might be more obvious as the following:
>
>
>   for ( i=0 ; i < LPAE_ENTRIES; i++ )
>   {
>      pte = mfn_to_p2m_entry(mfn_add(mfn, (i<<(level_shift-LPAE_SHIFT))),
>                             MATTR_MEM, t, p2m->default_access);
>
>
> However it's just a matter of taste, so I'll let you choose the way you
> prefer.

I wanted to avoid shifting "i" at each loop (which should save an 
instruction). However, as it seems to be confusing, I will use your 
suggestion.

>
> Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>

Thank you!

Cheers,
diff mbox

Patch

diff --git a/xen/arch/arm/p2m.c b/xen/arch/arm/p2m.c
index d82349c..99be9be 100644
--- a/xen/arch/arm/p2m.c
+++ b/xen/arch/arm/p2m.c
@@ -324,7 +324,7 @@  static void p2m_set_permission(lpae_t *e, p2m_type_t t, p2m_access_t a)
     }
 }
 
-static lpae_t mfn_to_p2m_entry(unsigned long mfn, unsigned int mattr,
+static lpae_t mfn_to_p2m_entry(mfn_t mfn, unsigned int mattr,
                                p2m_type_t t, p2m_access_t a)
 {
     /*
@@ -358,9 +358,9 @@  static lpae_t mfn_to_p2m_entry(unsigned long mfn, unsigned int mattr,
 
     p2m_set_permission(&e, t, a);
 
-    ASSERT(!(pfn_to_paddr(mfn) & ~PADDR_MASK));
+    ASSERT(!(pfn_to_paddr(mfn_x(mfn)) & ~PADDR_MASK));
 
-    e.p2m.base = mfn;
+    e.p2m.base = mfn_x(mfn);
 
     return e;
 }
@@ -411,7 +411,7 @@  static int p2m_create_table(struct domain *d, lpae_t *entry,
     if ( splitting )
     {
         p2m_type_t t = entry->p2m.type;
-        unsigned long base_pfn = entry->p2m.base;
+        mfn_t mfn = _mfn(entry->p2m.base);
         int i;
 
         /*
@@ -420,8 +420,9 @@  static int p2m_create_table(struct domain *d, lpae_t *entry,
          */
          for ( i=0 ; i < LPAE_ENTRIES; i++ )
          {
-             pte = mfn_to_p2m_entry(base_pfn + (i<<(level_shift-LPAE_SHIFT)),
-                                    MATTR_MEM, t, p2m->default_access);
+             pte = mfn_to_p2m_entry(mfn, MATTR_MEM, t, p2m->default_access);
+
+             mfn = mfn_add(mfn, 1UL << (level_shift - LPAE_SHIFT));
 
              /*
               * First and second level super pages set p2m.table = 0, but
@@ -443,7 +444,7 @@  static int p2m_create_table(struct domain *d, lpae_t *entry,
 
     unmap_domain_page(p);
 
-    pte = mfn_to_p2m_entry(page_to_mfn(page), MATTR_MEM, p2m_invalid,
+    pte = mfn_to_p2m_entry(_mfn(page_to_mfn(page)), MATTR_MEM, p2m_invalid,
                            p2m->default_access);
 
     p2m_write_pte(entry, pte, flush_cache);
@@ -693,7 +694,7 @@  static int apply_one_level(struct domain *d,
                 return rc;
 
             /* New mapping is superpage aligned, make it */
-            pte = mfn_to_p2m_entry(*maddr >> PAGE_SHIFT, mattr, t, a);
+            pte = mfn_to_p2m_entry(_mfn(*maddr >> PAGE_SHIFT), mattr, t, a);
             if ( level < 3 )
                 pte.p2m.table = 0; /* Superpage entry */