iommu/amd: Use try_cmpxchg64() in v2_alloc_pte()
Subsystems (4)
PATCH 1/3 iommu/amd: Use try_cmpxchg64() in v2_alloc_pte()
Use try_cmpxchg64() instead of cmpxchg64 (*ptr, old, new) != old in v2_alloc_pte(). cmpxchg returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).
This is the same improvement as implemented for alloc_pte() in:
commit 0d10fe759117 ("iommu/amd: Use try_cmpxchg64 in alloc_pte and free_clear_pte")
--- drivers/iommu/amd/io_pgtable_v2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/amd/io_pgtable_v2.c +1 -1
@@ -158,7 +158,7 @@ static u64 *v2_alloc_pte(int nid, u64 *pgd, unsigned long iova, __npte =set_pgtable_attr (page);/* pte could have been changed somewhere. */ -if (cmpxchg64 (pte, __pte, __npte) != __pte) +if (!try_cmpxchg64 (pte, &__pte, __npte))iommu_free_page (page);else if (IOMMU_PTE_PRESENT (__pte)) *updated = true;
Signature · 1 line
2.45.1
PATCH 2/3 iommu/vt-d: Use try_cmpxchg64() in intel_pasid_get_entry()
Use try_cmpxchg64() instead of cmpxchg64 (*ptr, old, new) != old in intel_pasid_get_entry(). cmpxchg returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).
--- drivers/iommu/intel/pasid.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/iommu/intel/pasid.c +5 -2
@@ -146,6 +146,8 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) retry: entries =get_pasid_table_from_pde (&dir[dir_index]);if (!entries) { +u64 tmp; + entries =iommu_alloc_page_node (info->iommu->node,GFP_ATOMIC );if (!entries)return NULL ;
@@ -156,8 +158,9 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)* clear. However, this entry might be populated by others * while we are preparing it. Use theirs with a retry. */ -if (cmpxchg64 (&dir[dir_index].val,0ULL , - (u64 )virt_to_phys (entries) |PASID_PTE_PRESENT )) { + tmp =0ULL ; +if (!try_cmpxchg64 (&dir[dir_index].val, &tmp, + (u64 )virt_to_phys (entries) |PASID_PTE_PRESENT )) {iommu_free_page (entries); goto retry; }
Signature · 1 line
2.45.1
Baolu Lu
maintainerRe: [PATCH 2/3] iommu/vt-d: Use try_cmpxchg64() in intel_pasid_get_entry()
Quoted 29 lines · Uros Bizjak
Use
try_cmpxchg64()instead of cmpxchg64 (*ptr, old, new) != old inintel_pasid_get_entry(). cmpxchg returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).Signed-off-by Uros Bizjak <[email protected]>Cc David Woodhouse <[email protected]>Cc Lu Baolu <[email protected]>Cc Joerg Roedel <[email protected]>Cc Will Deacon <[email protected]>Cc Robin Murphy <[email protected]>--- drivers/iommu/intel/pasid.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/iommu/intel/pasid.c +2
-0@@ -146,6 +146,8 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) retry: entries =get_pasid_table_from_pde (&dir[dir_index]);if (!entries) { +u64 tmp; + entries =iommu_alloc_page_node (info->iommu->node,GFP_ATOMIC );if (!entries)return NULL ;
drivers/iommu/intel/pasid.c static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) · line 161
Quoted 6 lines
drivers/iommu/intel/pasid.c +1
-2@@ -156,8 +158,9 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)* clear. However, this entry might be populated by others * while we are preparing it. Use theirs with a retry. */ -if (cmpxchg64 (&dir[dir_index].val,0ULL , - (u64 )virt_to_phys (entries) |PASID_PTE_PRESENT )) { + tmp =0ULL ;
nit: can this simply be
tmp = 0;
?
drivers/iommu/intel/pasid.c static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) · line 163
Quoted 2 lines
drivers/iommu/intel/pasid.c +2
-0+if (!try_cmpxchg64 (&dir[dir_index].val, &tmp, + (u64 )virt_to_phys (entries) |PASID_PTE_PRESENT )) {
Above change will cause a dead loop during boot. It should be
if (try_cmpxchg64(&dir[dir_index].val, &tmp,
(u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) {drivers/iommu/intel/pasid.c static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) · line 166
Quoted 3 lines
drivers/iommu/intel/pasid.c +0
-0iommu_free_page (entries); goto retry; }
Best regards,
baolu
Re: [PATCH 2/3] iommu/vt-d: Use try_cmpxchg64() in intel_pasid_get_entry()
Quoted 39 lines · Baolu Lu
Quoted 29 lines · Uros Bizjak
Use
try_cmpxchg64()instead of cmpxchg64 (*ptr, old, new) != old inintel_pasid_get_entry(). cmpxchg returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).Signed-off-by Uros Bizjak <[email protected]>Cc David Woodhouse <[email protected]>Cc Lu Baolu <[email protected]>Cc Joerg Roedel <[email protected]>Cc Will Deacon <[email protected]>Cc Robin Murphy <[email protected]>--- drivers/iommu/intel/pasid.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)drivers/iommu/intel/pasid.c +2
-0@@ -146,6 +146,8 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) retry: entries =get_pasid_table_from_pde (&dir[dir_index]);if (!entries) { +u64 tmp; + entries =iommu_alloc_page_node (info->iommu->node,GFP_ATOMIC );if (!entries)return NULL ;drivers/iommu/intel/pasid.c static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) · line 161
Quoted 6 lines
drivers/iommu/intel/pasid.c +1
-2@@ -156,8 +158,9 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)* clear. However, this entry might be populated by others * while we are preparing it. Use theirs with a retry. */ -if (cmpxchg64 (&dir[dir_index].val,0ULL , - (u64 )virt_to_phys (entries) |PASID_PTE_PRESENT )) { + tmp =0ULL ;nit: can this simply be
Quoted 2 lines
tmp = 0;?
I just took the same suffix as it was in the original code, but for zero literal, it does not make any change. I can change it, it preferred.
Quoted 4 lines
drivers/iommu/intel/pasid.c static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) · line 163
Quoted 2 lines
drivers/iommu/intel/pasid.c +2
-0+if (!try_cmpxchg64 (&dir[dir_index].val, &tmp, + (u64 )virt_to_phys (entries) |PASID_PTE_PRESENT )) {Above change will cause a dead loop during boot. It should be
No, it is correct as written:
if (cmpxchg64(*ptr, 0, new))
can be written as:
if (cmpxchg64(*ptr, 0, new) != 0)
this is equivalent to:
tmp = 0ULL;
if (!try_cmpxchg64(*ptr, &tmp, new))
Thanks,
Uros.
Baolu Lu
maintainerRe: [PATCH 2/3] iommu/vt-d: Use try_cmpxchg64() in intel_pasid_get_entry()
Quoted 7 lines · Uros Bizjak
Quoted 3 lines
drivers/iommu/intel/pasid.c static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) · line 163
Quoted 2 lines
drivers/iommu/intel/pasid.c +2
-0+if (!try_cmpxchg64 (&dir[dir_index].val, &tmp, + (u64 )virt_to_phys (entries) |PASID_PTE_PRESENT )) {Above change will cause a dead loop during boot. It should be
No, it is correct as written:
if (
cmpxchg64(*ptr, 0, new))
Quoted 8 lines
can be written as:
if (
cmpxchg64(*ptr, 0, new)!= 0)this is equivalent to:
tmp = 0ULL;
if (!try_cmpxchg64(*ptr, &tmp, new))
The return value of both cmpxchg64() and try_cmpxchg64() is the old value that was loaded from the memory location, right?
If so,
if (cmpxchg64(*ptr, 0, new) != 0)
is not equivalent to
if (!try_cmpxchg64(*ptr, &tmp, new))
Best regards,
baolu
Re: [PATCH 2/3] iommu/vt-d: Use try_cmpxchg64() in intel_pasid_get_entry()
Quoted 13 lines · Baolu Lu
Quoted 11 lines · Uros Bizjak
Quoted 3 lines
drivers/iommu/intel/pasid.c static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) · line 163
Quoted 2 lines
drivers/iommu/intel/pasid.c +2
-0+if (!try_cmpxchg64 (&dir[dir_index].val, &tmp, + (u64 )virt_to_phys (entries) |PASID_PTE_PRESENT )) {Above change will cause a dead loop during boot. It should be
No, it is correct as written:
if (
cmpxchg64(*ptr, 0, new))can be written as:
if (
cmpxchg64(*ptr, 0, new)!= 0)
Quoted 7 lines
Quoted 4 lines
this is equivalent to:
tmp = 0ULL;
if (!try_cmpxchg64(*ptr, &tmp, new))The return value of both
cmpxchg64()andtry_cmpxchg64()is the old value that was loaded from the memory location, right?
Actually, try_cmpxchg() returns true if successful and false if it fails.
tmp = 0ULL;
if (!try_cmpxchg64(*ptr, &tmp, new))The logic in the above snippet can be interpreted as:
if we fail to compare *ptr with 0, then:
iommu_free_page(entries);
goto retry;as intended in the original code.
Thanks,
Uros.
Baolu Lu
maintainerRe: [PATCH 2/3] iommu/vt-d: Use try_cmpxchg64() in intel_pasid_get_entry()
Quoted 13 lines · Uros Bizjak
Quoted 12 lines · Baolu Lu
Quoted 11 lines · Uros Bizjak
Quoted 3 lines
drivers/iommu/intel/pasid.c static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) · line 163
Quoted 2 lines
drivers/iommu/intel/pasid.c +2
-0+if (!try_cmpxchg64 (&dir[dir_index].val, &tmp, + (u64 )virt_to_phys (entries) |PASID_PTE_PRESENT )) {Above change will cause a dead loop during boot. It should be
No, it is correct as written:
if (
cmpxchg64(*ptr, 0, new))can be written as:
if (
cmpxchg64(*ptr, 0, new)!= 0)
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
this is equivalent to:
tmp = 0ULL;
if (!try_cmpxchg64(*ptr, &tmp, new))The return value of both
cmpxchg64()andtry_cmpxchg64()is the old value that was loaded from the memory location, right?Actually,
try_cmpxchg()returns true if successful and false if it fails.
Oh! I misunderstood this.
Quoted 4 lines
tmp = 0ULL;
if (!try_cmpxchg64(*ptr, &tmp, new))Quoted 8 lines
The logic in the above snippet can be interpreted as:
if we fail to compare *ptr with 0, then:
iommu_free_page(entries); goto retry;as intended in the original code.
Okay, it's fine.
Best regards,
baolu
Re: [PATCH 2/3] iommu/vt-d: Use try_cmpxchg64() in intel_pasid_get_entry()
In reply to Uros BizjakQuoted 11 lines · Uros Bizjak
Use
try_cmpxchg64()instead of cmpxchg64 (*ptr, old, new) != old inintel_pasid_get_entry(). cmpxchg returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).Signed-off-by Uros Bizjak<[email protected]>Cc David Woodhouse<[email protected]>Cc Lu Baolu<[email protected]>Cc Joerg Roedel<[email protected]>Cc Will Deacon<[email protected]>Cc Robin Murphy<[email protected]>
Quoted 3 lines
--- drivers/iommu/intel/pasid.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
Best regards,
baolu
PATCH 3/3 iommufd: Use atomic_long_try_cmpxchg() in incr_user_locked_vm()
In reply to Uros BizjakUse atomic_long_try_cmpxchg() instead of atomic_long_cmpxchg (*ptr, old, new) != old in incr_user_locked_vm(). cmpxchg returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).
Also, atomic_long_try_cmpxchg() implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop.
--- drivers/iommu/iommufd/pages.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iommu/iommufd/pages.c +4 -3
@@ -809,13 +809,14 @@ static int incr_user_locked_vm(struct iopt_pages *pages, unsigned long npages) lock_limit =task_rlimit (pages->source_task,RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; + + cur_pages =atomic_long_read (&pages->source_user->locked_vm);do { - cur_pages =atomic_long_read (&pages->source_user->locked_vm); new_pages = cur_pages + npages;if (new_pages > lock_limit)return -ENOMEM ; - }while (atomic_long_cmpxchg (&pages->source_user->locked_vm, cur_pages, - new_pages) != cur_pages); + }while (!atomic_long_try_cmpxchg (&pages->source_user->locked_vm, + &cur_pages, new_pages));return 0 ; }
Signature · 1 line
2.45.1
Re: [PATCH 3/3] iommufd: Use atomic_long_try_cmpxchg() in incr_user_locked_vm()
Quoted 15 lines · Uros Bizjak
Use
atomic_long_try_cmpxchg()instead ofatomic_long_cmpxchg(*ptr, old, new) != old inincr_user_locked_vm(). cmpxchg returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).Also,
atomic_long_try_cmpxchg()implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop.Signed-off-by Uros Bizjak <[email protected]>Cc Jason Gunthorpe <[email protected]>Cc Kevin Tian <[email protected]>Cc Joerg Roedel <[email protected]>Cc Will Deacon <[email protected]>Cc Robin Murphy <[email protected]>
Quoted 3 lines
--- drivers/iommu/iommufd/pages.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
This patch may as well go through Joerg's tree with the whole series
Thanks,
Jason
Quoted 7 lines · Uros Bizjak
Use
try_cmpxchg64()instead of cmpxchg64 (*ptr, old, new) != old inv2_alloc_pte(). cmpxchg returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).This is the same improvement as implemented for
alloc_pte()in:
Quoted 7 lines
commit
0d10fe759117("iommu/amd: Usetry_cmpxchg64inalloc_pteandfree_clear_pte")Signed-off-by Uros Bizjak <[email protected]>Cc Joerg Roedel <[email protected]>Cc Suravee Suthikulpanit <[email protected]>Cc Will Deacon <[email protected]>Cc Robin Murphy <[email protected]>
Thanks for fixing it. Looks good to me.
-Vasant
Quoted 17 lines
--- drivers/iommu/amd/io_pgtable_v2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iommu/amd/io_pgtable_v2.c +1
-1@@ -158,7 +158,7 @@ static u64 *v2_alloc_pte(int nid, u64 *pgd, unsigned long iova, __npte =set_pgtable_attr (page);/* pte could have been changed somewhere. */ -if (cmpxchg64 (pte, __pte, __npte) != __pte) +if (!try_cmpxchg64 (pte, &__pte, __npte))iommu_free_page (page);else if (IOMMU_PTE_PRESENT (__pte)) *updated = true;