From nobody Tue Dec 2 02:04:20 2025 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA8EF377E9C; Thu, 20 Nov 2025 17:16:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763658978; cv=none; b=X2lrjKSOAuR6QiOEy6d5qK1o0mIomOB6//9pZ9YkX1jRoEzm67w7+xlTvq6uIBzYn0dEjwwfEfbL1Kx4jn8zYwzyWbVeaAGY/qaFDtcte4Eayoy66zNMNmFQpQkRaHKbzLPJSQcsOe3xkRJVtXjBVM1gb2j6XGHjQD05NmBD+vU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763658978; c=relaxed/simple; bh=YgWFM24ZgeoBOmj0T3wFA+RmJ7JyziVkHiE2wUwE9Nc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bfGuFoiqPFPI/y/f493M2ZQMHAOh87cx9KTZaPoMrh1bfx2I5cDvYbE6HKIPhE3ekRebyeinD61UoSHISQR17dawrcb8kgvlPm7iVMWiKGnnpTUORnPgefGSRX9UKlEvE6lpZGfLbDIkSOrBjFDBe6OWflwt993fjyFmBqO/rp8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=ZT0MELUx; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="ZT0MELUx" Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 5AKBRtgK021553; Thu, 20 Nov 2025 17:16:08 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=rjdyBPkqJpI6MWf8t mW68B0spU3vJ3MctslbddyriDo=; b=ZT0MELUxfvunQUoC/xSfSMB2GOVhSgFWU 119r2roIRS0gNJzYhmitEOh6iihnT5SFi8Bi02DmnmI25rVSNYDTLJLGYjFzjWjG 9AXUXpVG0rwZUXV4SS3FObDFoQicL/nFJLNeyQm5/LfDb4zSU9Kl2SXzhRbtFJXy 3jJPgmkYF+ZZunNP0bemrPCGhO8JIbyRfnR/ol4rE51y69hy7d1aYyeGlHXqk36v G1/JEWyhxWqsivXM1gs/9qU6DPEZcZCTVaDhJnFl9fmSa2QcOC588pB8ZbzQbMtd HUG/1JjKZlca4yP9F+3ILb1XcqMU9mq9fspJpqKuawOm8QMOkS3Xw== Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4aejjwfrbh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 20 Nov 2025 17:16:08 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 5AKEBuPm006959; Thu, 20 Nov 2025 17:16:07 GMT Received: from smtprelay03.fra02v.mail.ibm.com ([9.218.2.224]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 4af62jqdmw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 20 Nov 2025 17:16:07 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay03.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 5AKHG36Y38666620 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 20 Nov 2025 17:16:03 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 99FED20043; Thu, 20 Nov 2025 17:16:03 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F0BB42004D; Thu, 20 Nov 2025 17:16:01 +0000 (GMT) Received: from p-imbrenda.ibmuc.com (unknown [9.111.12.33]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 20 Nov 2025 17:16:01 +0000 (GMT) From: Claudio Imbrenda 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 09/23] KVM: s390: KVM page table management functions: clear and replace Date: Thu, 20 Nov 2025 18:15:30 +0100 Message-ID: <20251120171544.96841-10-imbrenda@linux.ibm.com> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20251120171544.96841-1-imbrenda@linux.ibm.com> References: <20251120171544.96841-1-imbrenda@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Authority-Analysis: v=2.4 cv=BanVE7t2 c=1 sm=1 tr=0 ts=691f4cd8 cx=c_pps a=AfN7/Ok6k8XGzOShvHwTGQ==:117 a=AfN7/Ok6k8XGzOShvHwTGQ==:17 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=VnNF1IyMAAAA:8 a=cTj5uL8xXfUXbJxEK6wA:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTE1MDAzMiBTYWx0ZWRfX4nlUcpShGNrL 1pmctcLqQWB258SbrTzf5Exap0VeGG+B1IFHvf5srevl8rGru/A3vs0M4nSTwIXvgDyFY+ini1v fAZSxHIFg5aSyPG4l3nEk7MOw1KAmQZsdgkHgRAEfWvjFJL6Bv4wcukS2AGxGrGO/jei7nI3XfR ZeaDlrzQWSmJAbnQiUvfJMNAbTpOtH7tPhq97AtI+qcwWEgAgGOkxATX+Lw5omfr3LBSbstd9fO dQEVfLt7TDFkKlhQD0Yn6YQo232F8wV2FEyk1WFn6RF60fgjeeW+Grrs+EioxJE4BDli6LQS32Y 2N+DrIQrMUhtj0XHJLL+Ol91VY04Acap7V/ROKTbfBRm6l83TQa/tW+hbh6WeHIPx89lO8gJatU qY+4yZLi0O51QzU2vUJ0O0SOBIWqzw== X-Proofpoint-GUID: dxMC7KIXmruieVjbKRoc4Bp0XFnzvVU9 X-Proofpoint-ORIG-GUID: dxMC7KIXmruieVjbKRoc4Bp0XFnzvVU9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-11-20_06,2025-11-20_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 lowpriorityscore=0 suspectscore=0 spamscore=0 impostorscore=0 priorityscore=1501 clxscore=1015 phishscore=0 bulkscore=0 adultscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2510240000 definitions=main-2511150032 Content-Type: text/plain; charset="utf-8" Add page table management functions to be used for KVM guest (gmap) page tables. This patch adds functions to clear, replace or exchange DAT table entries. Signed-off-by: Claudio Imbrenda --- arch/s390/kvm/dat.c | 118 ++++++++++++++++++++++++++++++++++++++++++++ arch/s390/kvm/dat.h | 40 +++++++++++++++ 2 files changed, 158 insertions(+) diff --git a/arch/s390/kvm/dat.c b/arch/s390/kvm/dat.c index c324a27f379f..a9d5b49ac411 100644 --- a/arch/s390/kvm/dat.c +++ b/arch/s390/kvm/dat.c @@ -101,3 +101,121 @@ void dat_free_level(struct crst_table *table, bool ow= ns_ptes) } dat_free_crst(table); } + +/** + * dat_crstep_xchg - exchange a gmap CRSTE with another + * @crstep: pointer to the CRST entry + * @new: replacement entry + * @gfn: the affected guest address + * @asce: the ASCE of the address space + * + * Context: This function is assumed to be called with kvm->mmu_lock held. + */ +void dat_crstep_xchg(union crste *crstep, union crste new, gfn_t gfn, unio= n asce asce) +{ + if (crstep->h.i) { + WRITE_ONCE(*crstep, new); + return; + } else if (cpu_has_edat2()) { + crdte_crste(crstep, *crstep, new, gfn, asce); + return; + } + + if (machine_has_tlb_guest()) + idte_crste(crstep, gfn, IDTE_GUEST_ASCE, asce, IDTE_GLOBAL); + else + idte_crste(crstep, gfn, 0, NULL_ASCE, IDTE_GLOBAL); + WRITE_ONCE(*crstep, new); +} + +/** + * dat_crstep_xchg_atomic - atomically exchange a gmap CRSTE with another + * @crstep: pointer to the CRST entry + * @old: expected old value + * @new: replacement entry + * @gfn: the affected guest address + * @asce: the asce of the address space + * + * This function should only be called on invalid crstes, or on crstes with + * FC =3D 1, as that guarantees the presence of CSPG. + * + * This function is needed to atomically exchange a CRSTE that potentially + * maps a prefix area, without having to invalidate it inbetween. + * + * Context: This function is assumed to be called with kvm->mmu_lock held. + * + * Return: true if the exchange was successful. + */ +bool dat_crstep_xchg_atomic(union crste *crstep, union crste old, union cr= ste new, gfn_t gfn, + union asce asce) +{ + if (old.h.i) + return arch_try_cmpxchg((long *)crstep, &old.val, new.val); + if (cpu_has_edat2()) + return crdte_crste(crstep, old, new, gfn, asce); + return cspg_crste(crstep, old, new); +} + +static void dat_set_storage_key_from_pgste(union pte pte, union pgste pgst= e) +{ + union skey nkey =3D { .acc =3D pgste.acc, .fp =3D pgste.fp }; + + page_set_storage_key(pte_origin(pte), nkey.skey, 0); +} + +static void dat_move_storage_key(union pte old, union pte new) +{ + page_set_storage_key(pte_origin(new), page_get_storage_key(pte_origin(old= )), 1); +} + +static union pgste dat_save_storage_key_into_pgste(union pte pte, union pg= ste pgste) +{ + union skey skey; + + skey.skey =3D page_get_storage_key(pte_origin(pte)); + + pgste.acc =3D skey.acc; + pgste.fp =3D skey.fp; + pgste.gr |=3D skey.r; + pgste.gc |=3D skey.c; + + return pgste; +} + +union pgste __dat_ptep_xchg(union pte *ptep, union pgste pgste, union pte = new, gfn_t gfn, + union asce asce, bool uses_skeys) +{ + union pte old =3D READ_ONCE(*ptep); + + /* Updating only the software bits while holding the pgste lock */ + if (!((ptep->val ^ new.val) & ~_PAGE_SW_BITS)) { + WRITE_ONCE(ptep->swbyte, new.swbyte); + return pgste; + } + + if (!old.h.i) { + unsigned long opts =3D IPTE_GUEST_ASCE | (pgste.nodat ? IPTE_NODAT : 0); + + if (machine_has_tlb_guest()) + __ptep_ipte(gfn_to_gpa(gfn), (void *)ptep, opts, asce.val, IPTE_GLOBAL); + else + __ptep_ipte(gfn_to_gpa(gfn), (void *)ptep, 0, 0, IPTE_GLOBAL); + } + + if (uses_skeys) { + if (old.h.i && !new.h.i) + /* Invalid to valid: restore storage keys from PGSTE */ + dat_set_storage_key_from_pgste(new, pgste); + else if (!old.h.i && new.h.i) + /* Valid to invalid: save storage keys to PGSTE */ + pgste =3D dat_save_storage_key_into_pgste(old, pgste); + else if (!old.h.i && !new.h.i) + /* Valid to valid: move storage keys */ + if (old.h.pfra !=3D new.h.pfra) + dat_move_storage_key(old, new); + /* Invalid to invalid: nothing to do */ + } + + WRITE_ONCE(*ptep, new); + return pgste; +} diff --git a/arch/s390/kvm/dat.h b/arch/s390/kvm/dat.h index 486b7dfc5df2..6a336c3c6f62 100644 --- a/arch/s390/kvm/dat.h +++ b/arch/s390/kvm/dat.h @@ -430,6 +430,12 @@ struct kvm_s390_mmu_cache { short int n_rmaps; }; =20 +union pgste __must_check __dat_ptep_xchg(union pte *ptep, union pgste pgst= e, union pte new, + gfn_t gfn, union asce asce, bool uses_skeys); +bool dat_crstep_xchg_atomic(union crste *crstep, union crste old, union cr= ste new, gfn_t gfn, + union asce asce); +void dat_crstep_xchg(union crste *crstep, union crste new, gfn_t gfn, unio= n asce asce); + void dat_free_level(struct crst_table *table, bool owns_ptes); struct crst_table *dat_alloc_crst_sleepable(unsigned long init); =20 @@ -757,6 +763,21 @@ static inline void pgste_set_unlock(union pte *ptep, u= nion pgste pgste) WRITE_ONCE(*pgste_of(ptep), pgste); } =20 +static inline void dat_ptep_xchg(union pte *ptep, union pte new, gfn_t gfn= , union asce asce, + bool has_skeys) +{ + union pgste pgste; + + pgste =3D pgste_get_lock(ptep); + pgste =3D __dat_ptep_xchg(ptep, pgste, new, gfn, asce, has_skeys); + pgste_set_unlock(ptep, pgste); +} + +static inline void dat_ptep_clear(union pte *ptep, gfn_t gfn, union asce a= sce, bool has_skeys) +{ + dat_ptep_xchg(ptep, _PTE_EMPTY, gfn, asce, has_skeys); +} + static inline void dat_free_pt(struct page_table *pt) { free_page((unsigned long)pt); @@ -794,4 +815,23 @@ static inline struct kvm_s390_mmu_cache *kvm_s390_new_= mmu_cache(void) return NULL; } =20 +static inline bool dat_pmdp_xchg_atomic(union pmd *pmdp, union pmd old, un= ion pmd new, + gfn_t gfn, union asce asce) +{ + return dat_crstep_xchg_atomic(_CRSTEP(pmdp), _CRSTE(old), _CRSTE(new), gf= n, asce); +} + +static inline bool dat_pudp_xchg_atomic(union pud *pudp, union pud old, un= ion pud new, + gfn_t gfn, union asce asce) +{ + return dat_crstep_xchg_atomic(_CRSTEP(pudp), _CRSTE(old), _CRSTE(new), gf= n, asce); +} + +static inline void dat_crstep_clear(union crste *crstep, gfn_t gfn, union = asce asce) +{ + union crste newcrste =3D _CRSTE_EMPTY(crstep->h.tt); + + dat_crstep_xchg(crstep, newcrste, gfn, asce); +} + #endif /* __KVM_S390_DAT_H */ --=20 2.51.1