1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Preemptible hypercalls
4  *
5  * Copyright (C) 2014 Citrix Systems R&D ltd.
6  */
7 
8 #include <linux/sched.h>
9 #include <xen/xen-ops.h>
10 
11 #ifndef CONFIG_PREEMPT
12 
13 /*
14  * Some hypercalls issued by the toolstack can take many 10s of
15  * seconds. Allow tasks running hypercalls via the privcmd driver to
16  * be voluntarily preempted even if full kernel preemption is
17  * disabled.
18  *
19  * Such preemptible hypercalls are bracketed by
20  * xen_preemptible_hcall_begin() and xen_preemptible_hcall_end()
21  * calls.
22  */
23 
24 DEFINE_PER_CPU(bool, xen_in_preemptible_hcall);
25 EXPORT_SYMBOL_GPL(xen_in_preemptible_hcall);
26 
xen_maybe_preempt_hcall(void)27 asmlinkage __visible void xen_maybe_preempt_hcall(void)
28 {
29 	if (unlikely(__this_cpu_read(xen_in_preemptible_hcall)
30 		     && need_resched())) {
31 		/*
32 		 * Clear flag as we may be rescheduled on a different
33 		 * cpu.
34 		 */
35 		__this_cpu_write(xen_in_preemptible_hcall, false);
36 		_cond_resched();
37 		__this_cpu_write(xen_in_preemptible_hcall, true);
38 	}
39 }
40 #endif /* CONFIG_PREEMPT */
41