From: Claudio Imbrenda <imbrenda@linux.ibm.com>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org,
borntraeger@de.ibm.com, frankja@linux.ibm.com, nsg@linux.ibm.com,
nrb@linux.ibm.com, seiden@linux.ibm.com, gra@linux.ibm.com,
schlameuss@linux.ibm.com, hca@linux.ibm.com, svens@linux.ibm.com,
agordeev@linux.ibm.com, gor@linux.ibm.com, david@redhat.com,
gerald.schaefer@linux.ibm.com
Subject: [PATCH v4 18/23] KVM: s390: Storage key functions refactoring
Date: Thu, 20 Nov 2025 18:15:39 +0100 [thread overview]
Message-ID: <20251120171544.96841-19-imbrenda@linux.ibm.com> (raw)
In-Reply-To: <20251120171544.96841-1-imbrenda@linux.ibm.com>
Refactor some storage key functions to improve readability.
Introduce helper functions that will be used in the next patches.
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gaccess.c | 36 +++++++++---------
arch/s390/kvm/gaccess.h | 4 +-
arch/s390/kvm/kvm-s390.c | 80 +++++++++++++++-------------------------
arch/s390/kvm/kvm-s390.h | 8 ++++
4 files changed, 58 insertions(+), 70 deletions(-)
diff --git a/arch/s390/kvm/gaccess.c b/arch/s390/kvm/gaccess.c
index 05fd3ee4b20d..a054de80a5cc 100644
--- a/arch/s390/kvm/gaccess.c
+++ b/arch/s390/kvm/gaccess.c
@@ -989,9 +989,8 @@ int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
* * -EAGAIN: transient failure (len 1 or 2)
* * -EOPNOTSUPP: read-only memslot (should never occur)
*/
-int cmpxchg_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, int len,
- __uint128_t *old_addr, __uint128_t new,
- u8 access_key, bool *success)
+int cmpxchg_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, int len, union kvm_s390_quad *old_addr,
+ union kvm_s390_quad new, u8 acc, bool *success)
{
gfn_t gfn = gpa_to_gfn(gpa);
struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
@@ -1023,41 +1022,42 @@ int cmpxchg_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, int len,
case 1: {
u8 old;
- ret = cmpxchg_user_key((u8 __user *)hva, &old, *old_addr, new, access_key);
- *success = !ret && old == *old_addr;
- *old_addr = old;
+ ret = cmpxchg_user_key((u8 __user *)hva, &old, old_addr->one, new.one, acc);
+ *success = !ret && old == old_addr->one;
+ old_addr->one = old;
break;
}
case 2: {
u16 old;
- ret = cmpxchg_user_key((u16 __user *)hva, &old, *old_addr, new, access_key);
- *success = !ret && old == *old_addr;
- *old_addr = old;
+ ret = cmpxchg_user_key((u16 __user *)hva, &old, old_addr->two, new.two, acc);
+ *success = !ret && old == old_addr->two;
+ old_addr->two = old;
break;
}
case 4: {
u32 old;
- ret = cmpxchg_user_key((u32 __user *)hva, &old, *old_addr, new, access_key);
- *success = !ret && old == *old_addr;
- *old_addr = old;
+ ret = cmpxchg_user_key((u32 __user *)hva, &old, old_addr->four, new.four, acc);
+ *success = !ret && old == old_addr->four;
+ old_addr->four = old;
break;
}
case 8: {
u64 old;
- ret = cmpxchg_user_key((u64 __user *)hva, &old, *old_addr, new, access_key);
- *success = !ret && old == *old_addr;
- *old_addr = old;
+ ret = cmpxchg_user_key((u64 __user *)hva, &old, old_addr->eight, new.eight, acc);
+ *success = !ret && old == old_addr->eight;
+ old_addr->eight = old;
break;
}
case 16: {
__uint128_t old;
- ret = cmpxchg_user_key((__uint128_t __user *)hva, &old, *old_addr, new, access_key);
- *success = !ret && old == *old_addr;
- *old_addr = old;
+ ret = cmpxchg_user_key((__uint128_t __user *)hva, &old, old_addr->sixteen,
+ new.sixteen, acc);
+ *success = !ret && old == old_addr->sixteen;
+ old_addr->sixteen = old;
break;
}
default:
diff --git a/arch/s390/kvm/gaccess.h b/arch/s390/kvm/gaccess.h
index 3fde45a151f2..774cdf19998f 100644
--- a/arch/s390/kvm/gaccess.h
+++ b/arch/s390/kvm/gaccess.h
@@ -206,8 +206,8 @@ int access_guest_with_key(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar,
int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
void *data, unsigned long len, enum gacc_mode mode);
-int cmpxchg_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, int len, __uint128_t *old,
- __uint128_t new, u8 access_key, bool *success);
+int cmpxchg_guest_abs_with_key(struct kvm *kvm, gpa_t gpa, int len, union kvm_s390_quad *old_addr,
+ union kvm_s390_quad new, u8 access_key, bool *success);
/**
* write_guest_with_key - copy data from kernel space to guest space
diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
index d7eff75a53d0..ab69c9fd7926 100644
--- a/arch/s390/kvm/kvm-s390.c
+++ b/arch/s390/kvm/kvm-s390.c
@@ -2905,9 +2905,9 @@ static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_fla
static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop)
{
void __user *uaddr = (void __user *)mop->buf;
+ void *tmpbuf __free(kvfree) = NULL;
enum gacc_mode acc_mode;
- void *tmpbuf = NULL;
- int r, srcu_idx;
+ int r;
r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION |
KVM_S390_MEMOP_F_CHECK_ONLY);
@@ -2920,52 +2920,36 @@ static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop)
return -ENOMEM;
}
- srcu_idx = srcu_read_lock(&kvm->srcu);
+ acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE;
- if (!kvm_is_gpa_in_memslot(kvm, mop->gaddr)) {
- r = PGM_ADDRESSING;
- goto out_unlock;
- }
+ scoped_guard(srcu, &kvm->srcu) {
+ if (!kvm_is_gpa_in_memslot(kvm, mop->gaddr))
+ return PGM_ADDRESSING;
- acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE;
- if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
- r = check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key);
- goto out_unlock;
- }
- if (acc_mode == GACC_FETCH) {
+ if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)
+ return check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key);
+
+ if (acc_mode == GACC_STORE && copy_from_user(tmpbuf, uaddr, mop->size))
+ return -EFAULT;
r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
- mop->size, GACC_FETCH, mop->key);
+ mop->size, acc_mode, mop->key);
if (r)
- goto out_unlock;
- if (copy_to_user(uaddr, tmpbuf, mop->size))
- r = -EFAULT;
- } else {
- if (copy_from_user(tmpbuf, uaddr, mop->size)) {
- r = -EFAULT;
- goto out_unlock;
- }
- r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
- mop->size, GACC_STORE, mop->key);
+ return r;
+ if (acc_mode != GACC_STORE && copy_to_user(uaddr, tmpbuf, mop->size))
+ return -EFAULT;
}
-out_unlock:
- srcu_read_unlock(&kvm->srcu, srcu_idx);
-
- vfree(tmpbuf);
- return r;
+ return 0;
}
static int kvm_s390_vm_mem_op_cmpxchg(struct kvm *kvm, struct kvm_s390_mem_op *mop)
{
void __user *uaddr = (void __user *)mop->buf;
void __user *old_addr = (void __user *)mop->old_addr;
- union {
- __uint128_t quad;
- char raw[sizeof(__uint128_t)];
- } old = { .quad = 0}, new = { .quad = 0 };
- unsigned int off_in_quad = sizeof(new) - mop->size;
- int r, srcu_idx;
- bool success;
+ union kvm_s390_quad old = { .sixteen = 0 };
+ union kvm_s390_quad new = { .sixteen = 0 };
+ bool success = false;
+ int r;
r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION);
if (r)
@@ -2977,25 +2961,21 @@ static int kvm_s390_vm_mem_op_cmpxchg(struct kvm *kvm, struct kvm_s390_mem_op *m
*/
if (mop->size > sizeof(new))
return -EINVAL;
- if (copy_from_user(&new.raw[off_in_quad], uaddr, mop->size))
+ if (copy_from_user(&new, uaddr, mop->size))
return -EFAULT;
- if (copy_from_user(&old.raw[off_in_quad], old_addr, mop->size))
+ if (copy_from_user(&old, old_addr, mop->size))
return -EFAULT;
- srcu_idx = srcu_read_lock(&kvm->srcu);
+ scoped_guard(srcu, &kvm->srcu) {
+ if (!kvm_is_gpa_in_memslot(kvm, mop->gaddr))
+ return PGM_ADDRESSING;
- if (!kvm_is_gpa_in_memslot(kvm, mop->gaddr)) {
- r = PGM_ADDRESSING;
- goto out_unlock;
- }
-
- r = cmpxchg_guest_abs_with_key(kvm, mop->gaddr, mop->size, &old.quad,
- new.quad, mop->key, &success);
- if (!success && copy_to_user(old_addr, &old.raw[off_in_quad], mop->size))
- r = -EFAULT;
+ r = cmpxchg_guest_abs_with_key(kvm, mop->gaddr, mop->size, &old, new,
+ mop->key, &success);
-out_unlock:
- srcu_read_unlock(&kvm->srcu, srcu_idx);
+ if (!success && copy_to_user(old_addr, &old, mop->size))
+ return -EFAULT;
+ }
return r;
}
diff --git a/arch/s390/kvm/kvm-s390.h b/arch/s390/kvm/kvm-s390.h
index f89f9f698df5..495ee9caaa30 100644
--- a/arch/s390/kvm/kvm-s390.h
+++ b/arch/s390/kvm/kvm-s390.h
@@ -22,6 +22,14 @@
#define KVM_S390_UCONTROL_MEMSLOT (KVM_USER_MEM_SLOTS + 0)
+union kvm_s390_quad {
+ __uint128_t sixteen;
+ unsigned long eight;
+ unsigned int four;
+ unsigned short two;
+ unsigned char one;
+};
+
static inline void kvm_s390_fpu_store(struct kvm_run *run)
{
fpu_stfpc(&run->s.regs.fpc);
--
2.51.1
next prev parent reply other threads:[~2025-11-20 17:16 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-20 17:15 [PATCH v4 00/23] KVM: s390: gmap rewrite, the real deal Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 01/23] KVM: s390: Refactor pgste lock and unlock functions Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 02/23] KVM: s390: add P bit in table entry bitfields, move union vaddress Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 03/23] s390: Move sske_frame() to a header Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 04/23] KVM: s390: Add gmap_helper_set_unused() Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 05/23] KVM: s390: Enable KVM_GENERIC_MMU_NOTIFIER Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 06/23] KVM: s390: Rename some functions in gaccess.c Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 07/23] KVM: s390: KVM-specific bitfields and helper functions Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 08/23] KVM: s390: KVM page table management functions: allocation Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 09/23] KVM: s390: KVM page table management functions: clear and replace Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 10/23] KVM: s390: KVM page table management functions: walks Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 11/23] KVM: s390: KVM page table management functions: storage keys Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 12/23] KVM: s390: KVM page table management functions: lifecycle management Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 13/23] KVM: s390: KVM page table management functions: CMMA Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 14/23] KVM: s390: New gmap code Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 15/23] KVM: s390: Add helper functions for fault handling Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 16/23] KVM: s390: Add some helper functions needed for vSIE Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 17/23] KVM: s390: Stop using CONFIG_PGSTE Claudio Imbrenda
2025-11-20 17:15 ` Claudio Imbrenda [this message]
2025-11-20 17:15 ` [PATCH v4 19/23] KVM: s390: Switch to new gmap Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 20/23] KVM: s390: Remove gmap from s390/mm Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 21/23] KVM: S390: Remove PGSTE code from linux/s390 mm Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 22/23] KVM: s390: Enable 1M pages for gmap Claudio Imbrenda
2025-11-20 17:15 ` [PATCH v4 23/23] KVM: s390: Storage key manipulation IOCTL 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=20251120171544.96841-19-imbrenda@linux.ibm.com \
--to=imbrenda@linux.ibm.com \
--cc=agordeev@linux.ibm.com \
--cc=borntraeger@de.ibm.com \
--cc=david@redhat.com \
--cc=frankja@linux.ibm.com \
--cc=gerald.schaefer@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=gra@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 \
--cc=nrb@linux.ibm.com \
--cc=nsg@linux.ibm.com \
--cc=schlameuss@linux.ibm.com \
--cc=seiden@linux.ibm.com \
--cc=svens@linux.ibm.com \
/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