Hello Lu and Moritz,
I think it's better to keep "if (level > 1)" because level_to_offset_bits() returns (unsigned int) -1 * LEVEL_STRIDE if level becomes 0.
static inline unsigned int level_to_offset_bits(int level)
{
 return (level - 1) * LEVEL_STRIDE;
}
On Tue, Feb 18, 2020 at 8:42 PM Moritz Fischer <mdf@xxxxxxxxxx <mailto:mdf@xxxxxxxxxx>> wrote:
Hi Baolu, Yonghyun
On Wed, Feb 19, 2020 at 11:15:36AM +0800, Lu Baolu wrote:
> Hi Yonghyun,
>
> Thanks for the patch.
>
> On 2020/2/19 6:23, Yonghyun Hwang wrote:
> > intel_iommu_iova_to_phys() has a bug when it translates an IOVA
for a huge
> > page onto its corresponding physical address. This commit fixes
the bug by
> > accomodating the level of page entry for the IOVA and adds
IOVA's lower
> > address to the physical address. >
> > Signed-off-by: Yonghyun Hwang <yonghyun@xxxxxxxxxx
<mailto:yonghyun@xxxxxxxxxx>>
> > ---
> >Â Âdrivers/iommu/intel-iommu.c | 11 +++++++++--
> >Â Â1 file changed, 9 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/iommu/intel-iommu.c
b/drivers/iommu/intel-iommu.c
> > index 0c8d81f56a30..ed6e69adb578 100644
> > --- a/drivers/iommu/intel-iommu.c
> > +++ b/drivers/iommu/intel-iommu.c
> > @@ -5555,13 +5555,20 @@ static phys_addr_t
intel_iommu_iova_to_phys(struct iommu_domain *domain,
> >Â Â Âstruct dma_pte *pte;
> >Â Â Âint level = 0;
> >Â Â Âu64 phys = 0;
> > +Â Âconst unsigned long pfn = iova >> VTD_PAGE_SHIFT;
>
> Why do you need a "const unsigned long" here?
It's because pfn_to_dma_pte() takes pfn for its second parameter as "unsigned int" type and pfn is not changed inÂintel_iommu_iova_to_phys().
>
> >Â Â Âif (dmar_domain->flags & DOMAIN_FLAG_LOSE_CHILDREN)
> >Â Â Â Â Â Â Âreturn 0;
> > -Â Âpte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT,
&level);
> > -Â Âif (pte)
> > +Â Âpte = pfn_to_dma_pte(dmar_domain, pfn, &level);
> > +Â Âif (pte) {
> >Â Â Â Â Â Â Âphys = dma_pte_addr(pte);
> > +Â Â Â Â Â Âif (level > 1)
> > +Â Â Â Â Â Â Â Â Â Âphys += (pfn &
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â((1UL <<
level_to_offset_bits(level)) - 1))
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â<< VTD_PAGE_SHIFT;
> > +Â Â Â Â Â Âphys += iova & (VTD_PAGE_SIZE - 1);
>
> How about
>
> diff --git a/drivers/iommu/intel-iommu.c
b/drivers/iommu/intel-iommu.c
> index 9dc37672bf89..bd17c2510bb2 100644
> --- a/drivers/iommu/intel-iommu.c
> +++ b/drivers/iommu/intel-iommu.c
> @@ -5693,8 +5693,14 @@ static phys_addr_t
intel_iommu_iova_to_phys(struct
> iommu_domain *domain,
>Â Â Â Â Âu64 phys = 0;
>
>Â Â Â Â Âpte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT,
&level);
> -Â Â Â Âif (pte)
> +Â Â Â Âif (pte) {
> +Â Â Â Â Â Â Â Âunsigned long offset_mask;
> +
> +Â Â Â Â Â Â Â Âoffset_mask = BIT_MASK(level_to_offset_bits(level) +
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â VTD_PAGE_SHIFT) - 1;
>Â Â Â Â Â Â Â Â Âphys = dma_pte_addr(pte);
> +Â Â Â Â Â Â Â Âphys += iova & (bitmask - 1);
Did you mean:
phys += iova & (offset_mask - 1);
> +Â Â Â Â}
At that point why not define a helper:
static inline unsigned long offset_mask(int level)
{
    return BIT_MASK(level_to_offset_bits(level) +
VTD_PAGE_SHIFT) - 1;
}
At which point the above would reduce to:
if (pte)
    phys = dma_pte_addr(pte) + iova & offset_mask(level) - 1;
Let me introduce the helper function and upstream the patch again. Thank you Lu and Moritz.
I might've fatfingered something here, but you get the idea :)
Cheers,
Moritz
On Tue, Feb 18, 2020 at 8:42 PM Moritz Fischer <mdf@xxxxxxxxxx <mailto:mdf@xxxxxxxxxx>> wrote:
Hi Baolu, Yonghyun
On Wed, Feb 19, 2020 at 11:15:36AM +0800, Lu Baolu wrote:
> Hi Yonghyun,
>
> Thanks for the patch.
>
> On 2020/2/19 6:23, Yonghyun Hwang wrote:
> > intel_iommu_iova_to_phys() has a bug when it translates an IOVA
for a huge
> > page onto its corresponding physical address. This commit fixes
the bug by
> > accomodating the level of page entry for the IOVA and adds
IOVA's lower
> > address to the physical address. >
> > Signed-off-by: Yonghyun Hwang <yonghyun@xxxxxxxxxx
<mailto:yonghyun@xxxxxxxxxx>>
> > ---
> >Â Âdrivers/iommu/intel-iommu.c | 11 +++++++++--
> >Â Â1 file changed, 9 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/iommu/intel-iommu.c
b/drivers/iommu/intel-iommu.c
> > index 0c8d81f56a30..ed6e69adb578 100644
> > --- a/drivers/iommu/intel-iommu.c
> > +++ b/drivers/iommu/intel-iommu.c
> > @@ -5555,13 +5555,20 @@ static phys_addr_t
intel_iommu_iova_to_phys(struct iommu_domain *domain,
> >Â Â Âstruct dma_pte *pte;
> >Â Â Âint level = 0;
> >Â Â Âu64 phys = 0;
> > +Â Âconst unsigned long pfn = iova >> VTD_PAGE_SHIFT;
>
> Why do you need a "const unsigned long" here?
>
> >Â Â Âif (dmar_domain->flags & DOMAIN_FLAG_LOSE_CHILDREN)
> >Â Â Â Â Â Â Âreturn 0;
> > -Â Âpte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT,
&level);
> > -Â Âif (pte)
> > +Â Âpte = pfn_to_dma_pte(dmar_domain, pfn, &level);
> > +Â Âif (pte) {
> >Â Â Â Â Â Â Âphys = dma_pte_addr(pte);
> > +Â Â Â Â Â Âif (level > 1)
> > +Â Â Â Â Â Â Â Â Â Âphys += (pfn &
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â((1UL <<
level_to_offset_bits(level)) - 1))
> > +Â Â Â Â Â Â Â Â Â Â Â Â Â Â<< VTD_PAGE_SHIFT;
> > +Â Â Â Â Â Âphys += iova & (VTD_PAGE_SIZE - 1);
>
> How about
>
> diff --git a/drivers/iommu/intel-iommu.c
b/drivers/iommu/intel-iommu.c
> index 9dc37672bf89..bd17c2510bb2 100644
> --- a/drivers/iommu/intel-iommu.c
> +++ b/drivers/iommu/intel-iommu.c
> @@ -5693,8 +5693,14 @@ static phys_addr_t
intel_iommu_iova_to_phys(struct
> iommu_domain *domain,
>Â Â Â Â Âu64 phys = 0;
>
>Â Â Â Â Âpte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT,
&level);
> -Â Â Â Âif (pte)
> +Â Â Â Âif (pte) {
> +Â Â Â Â Â Â Â Âunsigned long offset_mask;
> +
> +Â Â Â Â Â Â Â Âoffset_mask = BIT_MASK(level_to_offset_bits(level) +
> +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â VTD_PAGE_SHIFT) - 1;
>Â Â Â Â Â Â Â Â Âphys = dma_pte_addr(pte);
> +Â Â Â Â Â Â Â Âphys += iova & (bitmask - 1);
Did you mean:
phys += iova & (offset_mask - 1);
> +Â Â Â Â}
At that point why not define a helper:
static inline unsigned long offset_mask(int level)
{
    return BIT_MASK(level_to_offset_bits(level) +
VTD_PAGE_SHIFT) - 1;
}
At which point the above would reduce to:
if (pte)
    phys = dma_pte_addr(pte) + iova & offset_mask(level) - 1;
I might've fatfingered something here, but you get the idea :)
Cheers,
Moritz