Skip to contentSkip to conversation

KVM: x86: use kvfree_rcu to simplify the code

PATCH Applied lirongqing 3 participants 3 messages Jan 22, 2025 – Feb 15, 2025

Subsystems (3)
lore
Share & tools
lore

lirongqing

author
#0 lore raw

PATCH 1/1 KVM: x86: use kvfree_rcu to simplify the code

From: Li RongQing <[email protected]>

The callback function of call_rcu() just calls kvfree(), so we can use kvfree_rcu() instead of call_rcu() + callback function.

---
 arch/x86/kvm/lapic.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
arch/x86/kvm/lapic.c +1 -8
index 3c83951..d6e62a2 100644
@@ -221,13 +221,6 @@ static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
 	}
 }
 
-static void kvm_apic_map_free(struct rcu_head *rcu)
-{
-	struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
-
-	kvfree(map);
-}
-
 static int kvm_recalculate_phys_map(struct kvm_apic_map *new,
 				    struct kvm_vcpu *vcpu,
 				    bool *xapic_id_mismatch)
@@ -489,7 +482,7 @@ static void kvm_recalculate_apic_map(struct kvm *kvm)
 	mutex_unlock(&kvm->arch.apic_map_lock);
 
 	if (old)
-		call_rcu(&old->rcu, kvm_apic_map_free);
+		kvfree_rcu(old, rcu);
 
 	kvm_make_scan_ioapic_request(kvm);
 }
Signature · 1 line
2.9.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · lirongqing

From: Li RongQing <[email protected]>

The callback function of call_rcu() just calls kvfree(), so we can use kvfree_rcu() instead of call_rcu() + callback function.

Reword to imperative style?

The callback function of call_rcu() just calls kvfree(). So, use kvfree_rcu() instead of call_rcu() + callback function.

Quoted 24 lines
---
 arch/x86/kvm/lapic.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
arch/x86/kvm/lapic.c +0 -7
index 3c83951..d6e62a2 100644
@@ -221,13 +221,6 @@ static inline bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map,
 	}
 }
 
-static void kvm_apic_map_free(struct rcu_head *rcu)
-{
-	struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
-
-	kvfree(map);
-}
-
 static int kvm_recalculate_phys_map(struct kvm_apic_map *new,
 				    struct kvm_vcpu *vcpu,
 				    bool *xapic_id_mismatch)

arch/x86/kvm/lapic.c static void kvm_recalculate_apic_map(struct kvm *kvm) · line 486

Quoted 6 lines
arch/x86/kvm/lapic.c +1 -1
@@ -489,7 +482,7 @@ static void kvm_recalculate_apic_map(struct kvm *kvm)
 	mutex_unlock(&kvm->arch.apic_map_lock);
 
 	if (old)
-		call_rcu(&old->rcu, kvm_apic_map_free);
+		kvfree_rcu(old, rcu);
 

Just a note: rcu_barrier() does not wait for kvfree_rcu() callbacks to complete. It only waits for call_rcu() callbacks. For kvfree_rcu() callbacks, kvfree_rcu_barrier() is required to wait for previously enqueued kvfree_rcu() callbacks to complete. As rcu_barrier() is not used for apic_map free callbacks, this looks fine.

- Neeraj

Quoted 2 lines
arch/x86/kvm/lapic.c +0 -0
 	kvm_make_scan_ioapic_request(kvm);
 }
maintainer Applied
#2 lore raw
In reply to lirongqing
Quoted 2 lines · lirongqing

The callback function of call_rcu() just calls kvfree(), so we can use kvfree_rcu() instead of call_rcu() + callback function.

Applied to kvm-x86 misc, with a heavily rewritten changelog to call out the rcu_barrier() wrinkles (I'm pretty sure it's, very technically, a bug fix).

Thanks!

[1/1] KVM: x86: use kvfree_rcu to simplify the code

      https://github.com/kvm-x86/linux/commit/82c470121c7b

--
https://github.com/kvm-x86/linux/tree/next