Skip to content

Commit b95a401

Browse files
ubizjakjoergroedel
authored andcommitted
iommufd: Use atomic_long_try_cmpxchg() in incr_user_locked_vm()
Use 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. 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]> Reviewed-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent c94ad1d commit b95a401

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

drivers/iommu/iommufd/pages.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,13 +809,14 @@ static int incr_user_locked_vm(struct iopt_pages *pages, unsigned long npages)
809809

810810
lock_limit = task_rlimit(pages->source_task, RLIMIT_MEMLOCK) >>
811811
PAGE_SHIFT;
812+
813+
cur_pages = atomic_long_read(&pages->source_user->locked_vm);
812814
do {
813-
cur_pages = atomic_long_read(&pages->source_user->locked_vm);
814815
new_pages = cur_pages + npages;
815816
if (new_pages > lock_limit)
816817
return -ENOMEM;
817-
} while (atomic_long_cmpxchg(&pages->source_user->locked_vm, cur_pages,
818-
new_pages) != cur_pages);
818+
} while (!atomic_long_try_cmpxchg(&pages->source_user->locked_vm,
819+
&cur_pages, new_pages));
819820
return 0;
820821
}
821822

0 commit comments

Comments
 (0)