From: Uros Bizjak <ubizjak@gmail.com>
To: iommu@lists.linux.dev, linux-kernel@vger.kernel.org
Cc: Uros Bizjak <ubizjak@gmail.com>, Jason Gunthorpe <jgg@ziepe.ca>,
Kevin Tian <kevin.tian@intel.com>, Joerg Roedel <joro@8bytes.org>,
Will Deacon <will@kernel.org>,
Robin Murphy <robin.murphy@arm.com>
Subject: [PATCH 3/3] iommufd: Use atomic_long_try_cmpxchg() in incr_user_locked_vm()
Date: Wed, 22 May 2024 10:26:49 +0200 [thread overview]
Message-ID: <20240522082729.971123-3-ubizjak@gmail.com> (raw)
In-Reply-To: <20240522082729.971123-1-ubizjak@gmail.com>
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 <ubizjak@gmail.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Kevin Tian <kevin.tian@intel.com>
Cc: Joerg Roedel <joro@8bytes.org>
Cc: Will Deacon <will@kernel.org>
Cc: Robin Murphy <robin.murphy@arm.com>
---
drivers/iommu/iommufd/pages.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
index 528f356238b3..117f644a0c5b 100644
--- a/drivers/iommu/iommufd/pages.c
+++ b/drivers/iommu/iommufd/pages.c
@@ -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;
}
--
2.45.1
next prev parent reply other threads:[~2024-05-22 8:27 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-22 8:26 [PATCH 1/3] iommu/amd: Use try_cmpxchg64() in v2_alloc_pte() Uros Bizjak
2024-05-22 8:26 ` [PATCH 2/3] iommu/vt-d: Use try_cmpxchg64() in intel_pasid_get_entry() Uros Bizjak
2024-05-23 13:24 ` Baolu Lu
2024-05-23 13:34 ` Uros Bizjak
2024-05-23 13:44 ` Baolu Lu
2024-05-23 13:57 ` Uros Bizjak
2024-05-24 1:08 ` Baolu Lu
2024-05-24 1:09 ` Baolu Lu
2024-05-22 8:26 ` Uros Bizjak [this message]
2024-05-22 12:45 ` [PATCH 3/3] iommufd: Use atomic_long_try_cmpxchg() in incr_user_locked_vm() Jason Gunthorpe
2024-05-23 10:40 ` [PATCH 1/3] iommu/amd: Use try_cmpxchg64() in v2_alloc_pte() Vasant Hegde
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240522082729.971123-3-ubizjak@gmail.com \
--to=ubizjak@gmail.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=robin.murphy@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox