x86/kvm: Avoid freeing stack-allocated node in kvm_async_pf_queue_task
Subsystems (3)
PATCH 1/1 x86/kvm: Avoid freeing stack-allocated node in kvm_async_pf_queue_task
kvm_async_pf_queue_task() can incorrectly remove a node allocated on the stack of kvm_async_pf_task_wait_schedule(). This occurs when a task request a PF while another task's PF request with the same token is still pending. Currently, kvm_async_pf_queue_task() assumes that any entry in the list is a dummy entry and tries to kfree(). To fix this, add a dummy flag to the node structure and the function should check this flag and kfree() only if it is a dummy entry.
--- arch/x86/kernel/kvm.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/kernel/kvm.c +11 -2
@@ -88,6 +88,7 @@ struct kvm_task_sleep_node {struct swait_queue_head wq;u32 token;int cpu; +bool dummy; }; static struct kvm_task_sleep_head {
@@ -119,10 +120,17 @@ static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n)raw_spin_lock (&b->lock); e =_find_apf_task (b, token);if (e) { +struct kvm_task_sleep_node *dummy =NULL ; +/* dummy entry exist -> wake up was delivered ahead of PF */ -hlist_del (&e->link); +/* Otherwise it should not be freed here. */ +if (e->dummy) { +hlist_del (&e->link); + dummy = e; + } +raw_spin_unlock (&b->lock); -kfree (e); +kfree (dummy);return false; }
@@ -230,6 +238,7 @@ static void kvm_async_pf_task_wake(u32 token) } dummy->token = token; dummy->cpu =smp_processor_id (); + dummy->dummy = true;init_swait_queue_head (&dummy->wq);hlist_add_head (&dummy->link, &b->list); dummy =NULL ;
base-commit: 2eba5e05d9bcf4cdea995ed51b0f07ba0275794a
Signature · 1 line
2.51.0
Quoted 54 lines · Ryosuke Yasuoka
kvm_async_pf_queue_task()can incorrectly remove a node allocated on the stack ofkvm_async_pf_task_wait_schedule(). This occurs when a task request a PF while another task's PF request with the same token is still pending. Currently,kvm_async_pf_queue_task()assumes that any entry in the list is a dummy entry and tries tokfree(). To fix this, add a dummy flag to the node structure and the function should check this flag andkfree()only if it is a dummy entry.Signed-off-by Ryosuke Yasuoka <[email protected]>--- arch/x86/kernel/kvm.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)arch/x86/kernel/kvm.c +11
-2@@ -88,6 +88,7 @@ struct kvm_task_sleep_node {struct swait_queue_head wq;u32 token;int cpu; +bool dummy; }; static struct kvm_task_sleep_head {@@ -119,10 +120,17 @@ static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n)raw_spin_lock (&b->lock); e =_find_apf_task (b, token);if (e) { +struct kvm_task_sleep_node *dummy =NULL ; +/* dummy entry exist -> wake up was delivered ahead of PF */ -hlist_del (&e->link); +/* Otherwise it should not be freed here. */ +if (e->dummy) { +hlist_del (&e->link); + dummy = e; + } +raw_spin_unlock (&b->lock); -kfree (e); +kfree (dummy);return false; }@@ -230,6 +238,7 @@ static void kvm_async_pf_task_wake(u32 token) } dummy->token = token; dummy->cpu =smp_processor_id (); + dummy->dummy = true;init_swait_queue_head (&dummy->wq);hlist_add_head (&dummy->link, &b->list); dummy =NULL ;
Quoted 3 lines
base-commit:
2eba5e05d9bcf4cdea995ed51b0f07ba0275794aSignature · 1 line
2.51.0
Hi all,
It's just a gentle ping on this patch.
Please let me know if thare any changes required. Any feedbacks are welcome.
Here is a original patch's link:
https://lore.kernel.org/all/[email protected]/
Thank you
Ryosuke
Vitaly Kuznetsov
reviewerQuoted 4 lines · Ryosuke Yasuoka
kvm_async_pf_queue_task()can incorrectly remove a node allocated on the stack ofkvm_async_pf_task_wait_schedule(). This occurs when a task request a PF while another task's PF request with the same token is still pending.
The important missing part here is what the 'token' is. exc_page_fault() sets it to (u32)read_cr2() so indeed I see possibilities that two different tasks will generate the same token.
Quoted 23 lines
Currently,
kvm_async_pf_queue_task()assumes that any entry in the list is a dummy entry and tries tokfree(). To fix this, add a dummy flag to the node structure and the function should check this flag andkfree()only if it is a dummy entry.Signed-off-by Ryosuke Yasuoka <[email protected]>--- arch/x86/kernel/kvm.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)arch/x86/kernel/kvm.c +1
-0@@ -88,6 +88,7 @@ struct kvm_task_sleep_node {struct swait_queue_head wq;u32 token;int cpu; +bool dummy; }; static struct kvm_task_sleep_head {
arch/x86/kernel/kvm.c static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n) · line 126
Quoted 8 lines
arch/x86/kernel/kvm.c +3
-1@@ -119,10 +120,17 @@ static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n)raw_spin_lock (&b->lock); e =_find_apf_task (b, token);if (e) { +struct kvm_task_sleep_node *dummy =NULL ; +/* dummy entry exist -> wake up was delivered ahead of PF */ -hlist_del (&e->link); +/* Otherwise it should not be freed here. */
I think we can merge these two comments together and write something better, e.g.
/*
* The entry can either be a 'dummy' entry (which is put on the
* list when wake-up happens ahead of APF handling completion)
* or a token from another task which should not be touched. */
arch/x86/kernel/kvm.c static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n) · line 135
Quoted 10 lines
arch/x86/kernel/kvm.c +6
-1+if (e->dummy) { +hlist_del (&e->link); + dummy = e; + } +raw_spin_unlock (&b->lock); -kfree (e); +kfree (dummy);return false; }
I think you also need to do
arch/x86/kernel/kvm.c +1 -0
@@ -128,6 +128,7 @@ static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n) n->token = token; n->cpu =smp_processor_id (); + n->dummy = false;init_swait_queue_head (&n->wq);hlist_add_head (&n->link, &b->list);raw_spin_unlock (&b->lock);
now as kvm_async_pf_task_wait_schedule() allocates struct kvm_task_sleep_node() on the stack and thus 'n->dummy' can be anything.
Quoted 11 lines
arch/x86/kernel/kvm.c +1
-0@@ -230,6 +238,7 @@ static void kvm_async_pf_task_wake(u32 token) } dummy->token = token; dummy->cpu =smp_processor_id (); + dummy->dummy = true;init_swait_queue_head (&dummy->wq);hlist_add_head (&dummy->link, &b->list); dummy =NULL ;base-commit:
2eba5e05d9bcf4cdea995ed51b0f07ba0275794a
Sorry for delayed reply!
Signature · 1 line
Vitaly