On 5/14/26 10:26 AM, HAGIO KAZUHITO(萩尾 一仁) wrote:
> On 2026/05/06 20:57, Rui Qi wrote:
>> When a page table entry has _PAGE_LEAF set at PGD/P4D/PUD/PMD level,
>> the current code incorrectly falls through to the next level instead
>> of computing the correct physical address. Fix this by calculating
>> the paddr with the proper level shift and returning directly.
>>
>> Co-developed-by: Rui Qi <[email protected]>
>> Signed-off-by: Shuan He <[email protected]>
>> ---
>>   arch/riscv64.c | 26 ++++++++++++++++++--------
>>   1 file changed, 18 insertions(+), 8 deletions(-)
>>
>> diff --git a/arch/riscv64.c b/arch/riscv64.c
>> index 28365fa0cc5b..67755331de24 100644
>> --- a/arch/riscv64.c
>> +++ b/arch/riscv64.c
>> @@ -107,8 +107,12 @@ vtop_riscv64(pgd_t * pgd, unsigned long vaddr, long 
>> va_bits)
>>   
>>      pt_phys = (pt_val >> _PAGE_PFN_SHIFT) << PAGESHIFT();
>>   
>> -    if (pt_val & _PAGE_LEAF)
>> -            goto out;
> 
> the following warning is observed:
> 
> cc  -g -O2 -Wall -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE 
> -D_LARGEFILE64_SOURCE -D__riscv64__ -U__x86_64__ -c -o ./arch/riscv64.o 
> arch/riscv64.c
> arch/riscv64.c: In function 'vtop_riscv64':
> arch/riscv64.c:203:1: warning: label 'out' defined but not used 
> [-Wunused-label]
>   out:
>   ^~~

Good catch. The out: label is now unused since we replaced all goto out
with goto invalid. We will remove it in v2.

> 
>> +    if (pt_val & _PAGE_LEAF) {
>> +            unsigned long shift = (va_bits == VA_BITS_SV57) ? PGD_SHIFT_L5 :
>> +                                  (va_bits == VA_BITS_SV48) ? PGD_SHIFT_L4 
>> : PGD_SHIFT_L3;
>> +            paddr = (pt_phys & ~((1ULL << shift) - 1)) + (vaddr & ((1ULL << 
>> shift) - 1));
>> +            goto invalid;
> 
> just a style point, if this path is not invalid, please do no use the 
> 'invalid'
> label and better to return here instead.
> 

Agreed, using goto invalid for a valid huge page translation path is
indeed misleading. We will replace it with a direct return paddr in v2.

Will send v2 shortly with both issues addressed.
Thanks,
Rui Qi

> Thanks,
> Kazu
> 
>> +    }
>>   
>>      if (va_bits == VA_BITS_SV57)
>>              goto p4d;
>> @@ -133,8 +137,10 @@ p4d:
>>   
>>      pt_phys = (pt_val >> _PAGE_PFN_SHIFT) << PAGESHIFT();
>>   
>> -    if (pt_val & _PAGE_LEAF)
>> -            goto out;
>> +    if (pt_val & _PAGE_LEAF) {
>> +            paddr = (pt_phys & ~((1ULL << P4D_SHIFT) - 1)) + (vaddr & 
>> ((1ULL << P4D_SHIFT) - 1));
>> +            goto invalid;
>> +    }
>>   pud:
>>      /* PUD */
>>      puda = (pud_t *)(pt_phys) + pud_index(vaddr);
>> @@ -152,8 +158,10 @@ pud:
>>   
>>      pt_phys = (pt_val >> _PAGE_PFN_SHIFT) << PAGESHIFT();
>>   
>> -    if(pt_val & _PAGE_LEAF)
>> -            goto out;
>> +    if (pt_val & _PAGE_LEAF) {
>> +            paddr = (pt_phys & ~((1ULL << PUD_SHIFT) - 1)) + (vaddr & 
>> ((1ULL << PUD_SHIFT) - 1));
>> +            goto invalid;
>> +    }
>>   pmd:
>>      /* PMD */
>>      pmda = (pmd_t *)(pt_phys) + pmd_index(vaddr);
>> @@ -171,8 +179,10 @@ pmd:
>>   
>>      pt_phys = (pt_val >> _PAGE_PFN_SHIFT) << PAGESHIFT();
>>   
>> -    if (pt_val & _PAGE_LEAF)
>> -            goto out;
>> +    if (pt_val & _PAGE_LEAF) {
>> +            paddr = (pt_phys & ~((1ULL << PMD_SHIFT) - 1)) + (vaddr & 
>> ((1ULL << PMD_SHIFT) - 1));
>> +            goto invalid;
>> +    }
>>   
>>      /* PTE */
>>      ptea = (pte_t *)(pt_phys) + pte_index(vaddr);

Reply via email to