From: Claudio Imbrenda <imbrenda@linux.ibm.com>
To: Heiko Carstens <hca@linux.ibm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>,
Janosch Frank <frankja@linux.ibm.com>,
David Hildenbrand <david@redhat.com>,
kvm@vger.kernel.org, linux-s390@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/3] KVM: s390: Use try_cmpxchg() instead of cmpxchg() loops
Date: Mon, 25 Nov 2024 13:18:28 +0100 [thread overview]
Message-ID: <20241125131828.6d49ad3f@p-imbrenda> (raw)
In-Reply-To: <20241125115039.1809353-2-hca@linux.ibm.com>
On Mon, 25 Nov 2024 12:50:37 +0100
Heiko Carstens <hca@linux.ibm.com> wrote:
> Convert all cmpxchg() loops to try_cmpxchg() loops. With gcc 14 and the
> usage of flag output operands in try_cmpxchg() this allows the compiler to
> generate slightly better code.
>
> Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
Looks straightforward
Acked-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
> ---
> arch/s390/kvm/gaccess.c | 16 ++++++++--------
> arch/s390/kvm/interrupt.c | 12 ++++++------
> arch/s390/kvm/kvm-s390.c | 4 ++--
> arch/s390/kvm/pci.c | 5 ++---
> 4 files changed, 18 insertions(+), 19 deletions(-)
>
> diff --git a/arch/s390/kvm/gaccess.c b/arch/s390/kvm/gaccess.c
> index a688351f4ab5..9816b0060fbe 100644
> --- a/arch/s390/kvm/gaccess.c
> +++ b/arch/s390/kvm/gaccess.c
> @@ -129,8 +129,8 @@ static void ipte_lock_simple(struct kvm *kvm)
> retry:
> read_lock(&kvm->arch.sca_lock);
> ic = kvm_s390_get_ipte_control(kvm);
> + old = READ_ONCE(*ic);
> do {
> - old = READ_ONCE(*ic);
> if (old.k) {
> read_unlock(&kvm->arch.sca_lock);
> cond_resched();
> @@ -138,7 +138,7 @@ static void ipte_lock_simple(struct kvm *kvm)
> }
> new = old;
> new.k = 1;
> - } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
> + } while (!try_cmpxchg(&ic->val, &old.val, new.val));
> read_unlock(&kvm->arch.sca_lock);
> out:
> mutex_unlock(&kvm->arch.ipte_mutex);
> @@ -154,11 +154,11 @@ static void ipte_unlock_simple(struct kvm *kvm)
> goto out;
> read_lock(&kvm->arch.sca_lock);
> ic = kvm_s390_get_ipte_control(kvm);
> + old = READ_ONCE(*ic);
> do {
> - old = READ_ONCE(*ic);
> new = old;
> new.k = 0;
> - } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
> + } while (!try_cmpxchg(&ic->val, &old.val, new.val));
> read_unlock(&kvm->arch.sca_lock);
> wake_up(&kvm->arch.ipte_wq);
> out:
> @@ -172,8 +172,8 @@ static void ipte_lock_siif(struct kvm *kvm)
> retry:
> read_lock(&kvm->arch.sca_lock);
> ic = kvm_s390_get_ipte_control(kvm);
> + old = READ_ONCE(*ic);
> do {
> - old = READ_ONCE(*ic);
> if (old.kg) {
> read_unlock(&kvm->arch.sca_lock);
> cond_resched();
> @@ -182,7 +182,7 @@ static void ipte_lock_siif(struct kvm *kvm)
> new = old;
> new.k = 1;
> new.kh++;
> - } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
> + } while (!try_cmpxchg(&ic->val, &old.val, new.val));
> read_unlock(&kvm->arch.sca_lock);
> }
>
> @@ -192,13 +192,13 @@ static void ipte_unlock_siif(struct kvm *kvm)
>
> read_lock(&kvm->arch.sca_lock);
> ic = kvm_s390_get_ipte_control(kvm);
> + old = READ_ONCE(*ic);
> do {
> - old = READ_ONCE(*ic);
> new = old;
> new.kh--;
> if (!new.kh)
> new.k = 0;
> - } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
> + } while (!try_cmpxchg(&ic->val, &old.val, new.val));
> read_unlock(&kvm->arch.sca_lock);
> if (!new.kh)
> wake_up(&kvm->arch.ipte_wq);
> diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
> index 4f0e7f61edf7..eff69018cbeb 100644
> --- a/arch/s390/kvm/interrupt.c
> +++ b/arch/s390/kvm/interrupt.c
> @@ -247,12 +247,12 @@ static inline int gisa_set_iam(struct kvm_s390_gisa *gisa, u8 iam)
> {
> u64 word, _word;
>
> + word = READ_ONCE(gisa->u64.word[0]);
> do {
> - word = READ_ONCE(gisa->u64.word[0]);
> if ((u64)gisa != word >> 32)
> return -EBUSY;
> _word = (word & ~0xffUL) | iam;
> - } while (cmpxchg(&gisa->u64.word[0], word, _word) != word);
> + } while (!try_cmpxchg(&gisa->u64.word[0], &word, _word));
>
> return 0;
> }
> @@ -270,10 +270,10 @@ static inline void gisa_clear_ipm(struct kvm_s390_gisa *gisa)
> {
> u64 word, _word;
>
> + word = READ_ONCE(gisa->u64.word[0]);
> do {
> - word = READ_ONCE(gisa->u64.word[0]);
> _word = word & ~(0xffUL << 24);
> - } while (cmpxchg(&gisa->u64.word[0], word, _word) != word);
> + } while (!try_cmpxchg(&gisa->u64.word[0], &word, _word));
> }
>
> /**
> @@ -291,14 +291,14 @@ static inline u8 gisa_get_ipm_or_restore_iam(struct kvm_s390_gisa_interrupt *gi)
> u8 pending_mask, alert_mask;
> u64 word, _word;
>
> + word = READ_ONCE(gi->origin->u64.word[0]);
> do {
> - word = READ_ONCE(gi->origin->u64.word[0]);
> alert_mask = READ_ONCE(gi->alert.mask);
> pending_mask = (u8)(word >> 24) & alert_mask;
> if (pending_mask)
> return pending_mask;
> _word = (word & ~0xffUL) | alert_mask;
> - } while (cmpxchg(&gi->origin->u64.word[0], word, _word) != word);
> + } while (!try_cmpxchg(&gi->origin->u64.word[0], &word, _word));
>
> return 0;
> }
> diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
> index 442d4a227c0e..d8080c27d45b 100644
> --- a/arch/s390/kvm/kvm-s390.c
> +++ b/arch/s390/kvm/kvm-s390.c
> @@ -1937,11 +1937,11 @@ static void kvm_s390_update_topology_change_report(struct kvm *kvm, bool val)
>
> read_lock(&kvm->arch.sca_lock);
> sca = kvm->arch.sca;
> + old = READ_ONCE(sca->utility);
> do {
> - old = READ_ONCE(sca->utility);
> new = old;
> new.mtcr = val;
> - } while (cmpxchg(&sca->utility.val, old.val, new.val) != old.val);
> + } while (!try_cmpxchg(&sca->utility.val, &old.val, new.val));
> read_unlock(&kvm->arch.sca_lock);
> }
>
> diff --git a/arch/s390/kvm/pci.c b/arch/s390/kvm/pci.c
> index a61518b549f0..9b9e7fdd5380 100644
> --- a/arch/s390/kvm/pci.c
> +++ b/arch/s390/kvm/pci.c
> @@ -208,13 +208,12 @@ static inline int account_mem(unsigned long nr_pages)
>
> page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
>
> + cur_pages = atomic_long_read(&user->locked_vm);
> do {
> - cur_pages = atomic_long_read(&user->locked_vm);
> new_pages = cur_pages + nr_pages;
> if (new_pages > page_limit)
> return -ENOMEM;
> - } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
> - new_pages) != cur_pages);
> + } while (!atomic_long_try_cmpxchg(&user->locked_vm, &cur_pages, new_pages));
>
> atomic64_add(nr_pages, ¤t->mm->pinned_vm);
>
next prev parent reply other threads:[~2024-11-25 12:18 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-25 11:50 [PATCH 0/3] KVM: s390: Couple of small cmpxchg() optimizations Heiko Carstens
2024-11-25 11:50 ` [PATCH 1/3] KVM: s390: Use try_cmpxchg() instead of cmpxchg() loops Heiko Carstens
2024-11-25 12:18 ` Claudio Imbrenda [this message]
2024-11-25 11:50 ` [PATCH 2/3] KVM: s390: Remove one byte cmpxchg() usage Heiko Carstens
2024-11-25 12:16 ` Claudio Imbrenda
2024-11-25 13:37 ` Heiko Carstens
2024-11-25 16:20 ` Claudio Imbrenda
2024-11-25 11:50 ` [PATCH 3/3] KVM: s390: Increase size of union sca_utility to four bytes Heiko Carstens
2024-11-25 12:20 ` Claudio Imbrenda
2024-11-25 13:40 ` Heiko Carstens
2024-11-25 16:17 ` Claudio Imbrenda
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=20241125131828.6d49ad3f@p-imbrenda \
--to=imbrenda@linux.ibm.com \
--cc=borntraeger@linux.ibm.com \
--cc=david@redhat.com \
--cc=frankja@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-s390@vger.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