From: Furkan Caliskan <frn1furkan10@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org, x86@kernel.org, seanjc@google.com,
pbonzini@redhat.com, tglx@kernel.org, mingo@redhat.com,
bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com,
dwmw2@infradead.org, paul@xen.org,
xen-devel@lists.xenproject.org,
Furkan Caliskan <frn1furkan10@gmail.com>
Subject: [PATCH] KVM: x86/xen: Convert evtchn_ports from IDR to XArray
Date: Mon, 6 Jul 2026 11:13:11 +0300 [thread overview]
Message-ID: <20260706081311.13633-1-frn1furkan10@gmail.com> (raw)
IDR is deprecated in favor of XArray: see
Documentation/core-api/idr.rst. Convert evtchn_ports accordingly.
kvm_xen_eventfd_assign()'s single-slot idr_alloc() becomes
xa_insert(), since it was really an insert-at-index, not an
allocation: -EBUSY replaces -ENOSPC, still mapped to -EEXIST.
kvm_xen_hcall_evtchn_send() drops its explicit rcu_read_lock(),
since xa_load() takes its own RCU read-side section internally.
evtchnfd's lifetime is still guaranteed by kvm->srcu.
xen_lock is left in place: it protects state beyond the map itself.
Signed-off-by: Furkan Caliskan <frn1furkan10@gmail.com>
---
arch/x86/include/asm/kvm_host.h | 3 ++-
arch/x86/kvm/xen.c | 34 ++++++++++++++++-----------------
2 files changed, 18 insertions(+), 19 deletions(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index d8700eb848b4..6c8542b32313 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -16,6 +16,7 @@
#include <linux/irq_work.h>
#include <linux/irq.h>
#include <linux/workqueue.h>
+#include <linux/xarray.h>
#include <linux/kvm.h>
#include <linux/kvm_para.h>
@@ -1290,7 +1291,7 @@ struct kvm_xen {
bool runstate_update_flag;
u8 upcall_vector;
struct gfn_to_pfn_cache shinfo_cache;
- struct idr evtchn_ports;
+ struct xarray evtchn_ports;
unsigned long poll_mask[BITS_TO_LONGS(KVM_MAX_VCPUS)];
struct kvm_xen_hvm_config hvm_config;
diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
index 694b31c1fcc9..754191e3bef7 100644
--- a/arch/x86/kvm/xen.c
+++ b/arch/x86/kvm/xen.c
@@ -2072,7 +2072,7 @@ static int kvm_xen_eventfd_update(struct kvm *kvm,
/* Protect writes to evtchnfd as well as the idr lookup. */
mutex_lock(&kvm->arch.xen.xen_lock);
- evtchnfd = idr_find(&kvm->arch.xen.evtchn_ports, port);
+ evtchnfd = xa_load(&kvm->arch.xen.evtchn_ports, port);
ret = -ENOENT;
if (!evtchnfd)
@@ -2166,13 +2166,13 @@ static int kvm_xen_eventfd_assign(struct kvm *kvm,
}
mutex_lock(&kvm->arch.xen.xen_lock);
- ret = idr_alloc(&kvm->arch.xen.evtchn_ports, evtchnfd, port, port + 1,
+ ret = xa_insert(&kvm->arch.xen.evtchn_ports, port, evtchnfd,
GFP_KERNEL);
mutex_unlock(&kvm->arch.xen.xen_lock);
- if (ret >= 0)
+ if (!ret)
return 0;
- if (ret == -ENOSPC)
+ if (ret == -EBUSY)
ret = -EEXIST;
out:
if (eventfd)
@@ -2187,7 +2187,7 @@ static int kvm_xen_eventfd_deassign(struct kvm *kvm, u32 port)
struct evtchnfd *evtchnfd;
mutex_lock(&kvm->arch.xen.xen_lock);
- evtchnfd = idr_remove(&kvm->arch.xen.evtchn_ports, port);
+ evtchnfd = xa_erase(&kvm->arch.xen.evtchn_ports, port);
mutex_unlock(&kvm->arch.xen.xen_lock);
if (!evtchnfd)
@@ -2203,7 +2203,7 @@ static int kvm_xen_eventfd_deassign(struct kvm *kvm, u32 port)
static int kvm_xen_eventfd_reset(struct kvm *kvm)
{
struct evtchnfd *evtchnfd, **all_evtchnfds;
- int i;
+ unsigned long i;
int n = 0;
mutex_lock(&kvm->arch.xen.xen_lock);
@@ -2213,7 +2213,7 @@ static int kvm_xen_eventfd_reset(struct kvm *kvm)
* critical section, first collect all the evtchnfd objects
* in an array as they are removed from evtchn_ports.
*/
- idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i)
+ xa_for_each(&kvm->arch.xen.evtchn_ports, i, evtchnfd)
n++;
all_evtchnfds = kmalloc_objs(struct evtchnfd *, n);
@@ -2223,9 +2223,9 @@ static int kvm_xen_eventfd_reset(struct kvm *kvm)
}
n = 0;
- idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i) {
+ xa_for_each(&kvm->arch.xen.evtchn_ports, i, evtchnfd) {
all_evtchnfds[n++] = evtchnfd;
- idr_remove(&kvm->arch.xen.evtchn_ports, evtchnfd->send_port);
+ xa_erase(&kvm->arch.xen.evtchn_ports, evtchnfd->send_port);
}
mutex_unlock(&kvm->arch.xen.xen_lock);
@@ -2276,12 +2276,10 @@ static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r)
}
/*
- * evtchnfd is protected by kvm->srcu; the idr lookup instead
- * is protected by RCU.
+ * evtchnfd is protected by kvm->srcu; the xa_load is RCU-safe
+ * internally, no explicit rcu_read_lock() needed.
*/
- rcu_read_lock();
- evtchnfd = idr_find(&vcpu->kvm->arch.xen.evtchn_ports, send.port);
- rcu_read_unlock();
+ evtchnfd = xa_load(&vcpu->kvm->arch.xen.evtchn_ports, send.port);
if (!evtchnfd)
return false;
@@ -2328,23 +2326,23 @@ void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
void kvm_xen_init_vm(struct kvm *kvm)
{
mutex_init(&kvm->arch.xen.xen_lock);
- idr_init(&kvm->arch.xen.evtchn_ports);
+ xa_init(&kvm->arch.xen.evtchn_ports);
kvm_gpc_init(&kvm->arch.xen.shinfo_cache, kvm);
}
void kvm_xen_destroy_vm(struct kvm *kvm)
{
struct evtchnfd *evtchnfd;
- int i;
+ unsigned long i;
kvm_gpc_deactivate(&kvm->arch.xen.shinfo_cache);
- idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i) {
+ xa_for_each(&kvm->arch.xen.evtchn_ports, i, evtchnfd) {
if (!evtchnfd->deliver.port.port)
eventfd_ctx_put(evtchnfd->deliver.eventfd.ctx);
kfree(evtchnfd);
}
- idr_destroy(&kvm->arch.xen.evtchn_ports);
+ xa_destroy(&kvm->arch.xen.evtchn_ports);
if (kvm->arch.xen.hvm_config.msr)
static_branch_slow_dec_deferred(&kvm_xen_enabled);
--
2.34.1
next reply other threads:[~2026-07-06 8:13 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-06 8:13 Furkan Caliskan [this message]
2026-07-07 9:16 ` David Woodhouse
2026-08-31 5:59 ` Furkan Çalışkan
2026-08-31 8:33 ` David Woodhouse
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=20260706081311.13633-1-frn1furkan10@gmail.com \
--to=frn1furkan10@gmail.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=dwmw2@infradead.org \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=paul@xen.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
--cc=xen-devel@lists.xenproject.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
all inboxes | Powered by JetHome®