1 /* $Id: mpnotification-r0drv-linux.c $ */
3 * IPRT - Multiprocessor Event Notifications, Ring-0 Driver, Linux.
7 * Copyright (C) 2008-2016 Oracle Corporation
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
28 /*********************************************************************************************************************************
30 *********************************************************************************************************************************/
31 #include "the-linux-kernel.h"
32 #include "internal/iprt.h"
34 #include <iprt/asm-amd64-x86.h>
36 #include <iprt/cpuset.h>
37 #include <iprt/thread.h>
38 #include "r0drv/mp-r0drv.h"
40 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
42 static enum cpuhp_state g_rtR0MpOnline
;
45 * Linux 4.10 completely removed CPU notifiers. So let's switch to CPU hotplug
49 static int rtR0MpNotificationLinuxOnline(unsigned int cpu
)
51 RTCPUID idCpu
= RTMpCpuIdFromSetIndex(cpu
);
52 rtMpNotificationDoCallbacks(RTMPEVENT_ONLINE
, idCpu
);
56 static int rtR0MpNotificationLinuxOffline(unsigned int cpu
)
58 RTCPUID idCpu
= RTMpCpuIdFromSetIndex(cpu
);
59 rtMpNotificationDoCallbacks(RTMPEVENT_OFFLINE
, idCpu
);
63 DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
66 IPRT_LINUX_SAVE_EFL_AC();
67 rc
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
, "vboxdrv:online",
68 rtR0MpNotificationLinuxOnline
, rtR0MpNotificationLinuxOffline
);
69 IPRT_LINUX_RESTORE_EFL_AC();
71 * cpuhp_setup_state_nocalls() returns a positive state number for
72 * CPUHP_AP_ONLINE_DYN or -ENOSPC if there is no free slot available
73 * (see cpuhp_reserve_state / definition of CPUHP_AP_ONLINE_DYN).
75 AssertMsgReturn(rc
> 0, ("%d\n", rc
), RTErrConvertFromErrno(rc
));
81 DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
83 IPRT_LINUX_SAVE_EFL_AC();
84 cpuhp_remove_state_nocalls(g_rtR0MpOnline
);
85 IPRT_LINUX_RESTORE_EFL_AC();
88 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 71) && defined(CONFIG_SMP)
90 static int rtMpNotificationLinuxCallback(struct notifier_block
*pNotifierBlock
, unsigned long ulNativeEvent
, void *pvCpu
);
93 * The notifier block we use for registering the callback.
95 static struct notifier_block g_NotifierBlock
=
97 .notifier_call
= rtMpNotificationLinuxCallback
,
102 # ifdef CPU_DOWN_FAILED
104 * The set of CPUs we've seen going offline recently.
106 static RTCPUSET g_MpPendingOfflineSet
;
111 * The native callback.
113 * @returns NOTIFY_DONE.
114 * @param pNotifierBlock Pointer to g_NotifierBlock.
115 * @param ulNativeEvent The native event.
116 * @param pvCpu The cpu id cast into a pointer value.
118 * @remarks This can fire with preemption enabled and on any CPU.
120 static int rtMpNotificationLinuxCallback(struct notifier_block
*pNotifierBlock
, unsigned long ulNativeEvent
, void *pvCpu
)
122 bool fProcessEvent
= false;
123 RTCPUID idCpu
= (uintptr_t)pvCpu
;
124 NOREF(pNotifierBlock
);
127 * Note that redhat/CentOS ported _some_ of the FROZEN macros
128 * back to their 2.6.18-92.1.10.el5 kernel but actually don't
129 * use them. Thus we have to test for both CPU_TASKS_FROZEN and
130 * the individual event variants.
132 switch (ulNativeEvent
)
135 * Pick up online events or failures to go offline.
136 * Ignore failure events for CPUs we didn't see go offline.
138 # ifdef CPU_DOWN_FAILED
139 case CPU_DOWN_FAILED
:
140 # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_FAILED_FROZEN)
141 case CPU_DOWN_FAILED_FROZEN
:
143 if (!RTCpuSetIsMember(&g_MpPendingOfflineSet
, idCpu
))
144 break; /* fProcessEvents = false */
148 # if defined(CPU_TASKS_FROZEN) && defined(CPU_ONLINE_FROZEN)
149 case CPU_ONLINE_FROZEN
:
151 # ifdef CPU_DOWN_FAILED
152 RTCpuSetDel(&g_MpPendingOfflineSet
, idCpu
);
154 fProcessEvent
= true;
158 * Pick the earliest possible offline event.
159 * The only important thing here is that we get the event and that
162 # ifdef CPU_DOWN_PREPARE
163 case CPU_DOWN_PREPARE
:
164 # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_PREPARE_FROZEN)
165 case CPU_DOWN_PREPARE_FROZEN
:
167 fProcessEvent
= true;
170 # if defined(CPU_TASKS_FROZEN) && defined(CPU_DEAD_FROZEN)
171 case CPU_DEAD_FROZEN
:
173 /* Don't process CPU_DEAD notifications. */
175 # ifdef CPU_DOWN_FAILED
176 RTCpuSetAdd(&g_MpPendingOfflineSet
, idCpu
);
184 switch (ulNativeEvent
)
186 # ifdef CPU_DOWN_FAILED
187 case CPU_DOWN_FAILED
:
188 # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_FAILED_FROZEN)
189 case CPU_DOWN_FAILED_FROZEN
:
193 # if defined(CPU_TASKS_FROZEN) && defined(CPU_ONLINE_FROZEN)
194 case CPU_ONLINE_FROZEN
:
196 rtMpNotificationDoCallbacks(RTMPEVENT_ONLINE
, idCpu
);
199 # ifdef CPU_DOWN_PREPARE
200 case CPU_DOWN_PREPARE
:
201 # if defined(CPU_TASKS_FROZEN) && defined(CPU_DOWN_PREPARE_FROZEN)
202 case CPU_DOWN_PREPARE_FROZEN
:
204 rtMpNotificationDoCallbacks(RTMPEVENT_OFFLINE
, idCpu
);
213 DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
216 IPRT_LINUX_SAVE_EFL_AC();
218 # ifdef CPU_DOWN_FAILED
219 RTCpuSetEmpty(&g_MpPendingOfflineSet
);
222 rc
= register_cpu_notifier(&g_NotifierBlock
);
223 IPRT_LINUX_RESTORE_EFL_AC();
224 AssertMsgReturn(!rc
, ("%d\n", rc
), RTErrConvertFromErrno(rc
));
229 DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
231 IPRT_LINUX_SAVE_EFL_AC();
232 unregister_cpu_notifier(&g_NotifierBlock
);
233 IPRT_LINUX_RESTORE_EFL_AC();
236 #else /* Not supported / Not needed */
238 DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
243 DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
247 #endif /* Not supported / Not needed */