1 | /* $Id: mpnotification-r0drv-linux.c 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Multiprocessor Event Notifications, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2020 Oracle Corporation
|
---|
8 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
22 | *
|
---|
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.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "the-linux-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/asm-amd64-x86.h>
|
---|
35 | #include <iprt/errcore.h>
|
---|
36 | #include <iprt/cpuset.h>
|
---|
37 | #include <iprt/thread.h>
|
---|
38 | #include "r0drv/mp-r0drv.h"
|
---|
39 |
|
---|
40 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
|
---|
41 |
|
---|
42 | static enum cpuhp_state g_rtR0MpOnline;
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Linux 4.10 completely removed CPU notifiers. So let's switch to CPU hotplug
|
---|
46 | * notification.
|
---|
47 | */
|
---|
48 |
|
---|
49 | static int rtR0MpNotificationLinuxOnline(unsigned int cpu)
|
---|
50 | {
|
---|
51 | RTCPUID idCpu = RTMpCpuIdFromSetIndex(cpu);
|
---|
52 | rtMpNotificationDoCallbacks(RTMPEVENT_ONLINE, idCpu);
|
---|
53 | return 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 | static int rtR0MpNotificationLinuxOffline(unsigned int cpu)
|
---|
57 | {
|
---|
58 | RTCPUID idCpu = RTMpCpuIdFromSetIndex(cpu);
|
---|
59 | rtMpNotificationDoCallbacks(RTMPEVENT_OFFLINE, idCpu);
|
---|
60 | return 0;
|
---|
61 | }
|
---|
62 |
|
---|
63 | DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
|
---|
64 | {
|
---|
65 | int rc;
|
---|
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();
|
---|
70 | /*
|
---|
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).
|
---|
74 | */
|
---|
75 | AssertMsgReturn(rc > 0, ("%d\n", rc), RTErrConvertFromErrno(rc));
|
---|
76 | g_rtR0MpOnline = rc;
|
---|
77 | return VINF_SUCCESS;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
|
---|
82 | {
|
---|
83 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
84 | cpuhp_remove_state_nocalls(g_rtR0MpOnline);
|
---|
85 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
86 | }
|
---|
87 |
|
---|
88 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 71) && defined(CONFIG_SMP)
|
---|
89 |
|
---|
90 | static int rtMpNotificationLinuxCallback(struct notifier_block *pNotifierBlock, unsigned long ulNativeEvent, void *pvCpu);
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * The notifier block we use for registering the callback.
|
---|
94 | */
|
---|
95 | static struct notifier_block g_NotifierBlock =
|
---|
96 | {
|
---|
97 | .notifier_call = rtMpNotificationLinuxCallback,
|
---|
98 | .next = NULL,
|
---|
99 | .priority = 0
|
---|
100 | };
|
---|
101 |
|
---|
102 | # ifdef CPU_DOWN_FAILED
|
---|
103 | /**
|
---|
104 | * The set of CPUs we've seen going offline recently.
|
---|
105 | */
|
---|
106 | static RTCPUSET g_MpPendingOfflineSet;
|
---|
107 | # endif
|
---|
108 |
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * The native callback.
|
---|
112 | *
|
---|
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.
|
---|
117 | *
|
---|
118 | * @remarks This can fire with preemption enabled and on any CPU.
|
---|
119 | */
|
---|
120 | static int rtMpNotificationLinuxCallback(struct notifier_block *pNotifierBlock, unsigned long ulNativeEvent, void *pvCpu)
|
---|
121 | {
|
---|
122 | bool fProcessEvent = false;
|
---|
123 | RTCPUID idCpu = (uintptr_t)pvCpu;
|
---|
124 | NOREF(pNotifierBlock);
|
---|
125 |
|
---|
126 | /*
|
---|
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.
|
---|
131 | */
|
---|
132 | switch (ulNativeEvent)
|
---|
133 | {
|
---|
134 | /*
|
---|
135 | * Pick up online events or failures to go offline.
|
---|
136 | * Ignore failure events for CPUs we didn't see go offline.
|
---|
137 | */
|
---|
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:
|
---|
142 | # endif
|
---|
143 | if (!RTCpuSetIsMember(&g_MpPendingOfflineSet, idCpu))
|
---|
144 | break; /* fProcessEvents = false */
|
---|
145 | /* fall thru */
|
---|
146 | # endif
|
---|
147 | case CPU_ONLINE:
|
---|
148 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_ONLINE_FROZEN)
|
---|
149 | case CPU_ONLINE_FROZEN:
|
---|
150 | # endif
|
---|
151 | # ifdef CPU_DOWN_FAILED
|
---|
152 | RTCpuSetDel(&g_MpPendingOfflineSet, idCpu);
|
---|
153 | # endif
|
---|
154 | fProcessEvent = true;
|
---|
155 | break;
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Pick the earliest possible offline event.
|
---|
159 | * The only important thing here is that we get the event and that
|
---|
160 | * it's exactly one.
|
---|
161 | */
|
---|
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:
|
---|
166 | # endif
|
---|
167 | fProcessEvent = true;
|
---|
168 | # else
|
---|
169 | case CPU_DEAD:
|
---|
170 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_DEAD_FROZEN)
|
---|
171 | case CPU_DEAD_FROZEN:
|
---|
172 | # endif
|
---|
173 | /* Don't process CPU_DEAD notifications. */
|
---|
174 | # endif
|
---|
175 | # ifdef CPU_DOWN_FAILED
|
---|
176 | RTCpuSetAdd(&g_MpPendingOfflineSet, idCpu);
|
---|
177 | # endif
|
---|
178 | break;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (!fProcessEvent)
|
---|
182 | return NOTIFY_DONE;
|
---|
183 |
|
---|
184 | switch (ulNativeEvent)
|
---|
185 | {
|
---|
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:
|
---|
190 | # endif
|
---|
191 | # endif
|
---|
192 | case CPU_ONLINE:
|
---|
193 | # if defined(CPU_TASKS_FROZEN) && defined(CPU_ONLINE_FROZEN)
|
---|
194 | case CPU_ONLINE_FROZEN:
|
---|
195 | # endif
|
---|
196 | rtMpNotificationDoCallbacks(RTMPEVENT_ONLINE, idCpu);
|
---|
197 | break;
|
---|
198 |
|
---|
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:
|
---|
203 | # endif
|
---|
204 | rtMpNotificationDoCallbacks(RTMPEVENT_OFFLINE, idCpu);
|
---|
205 | break;
|
---|
206 | # endif
|
---|
207 | }
|
---|
208 |
|
---|
209 | return NOTIFY_DONE;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
|
---|
214 | {
|
---|
215 | int rc;
|
---|
216 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
217 |
|
---|
218 | # ifdef CPU_DOWN_FAILED
|
---|
219 | RTCpuSetEmpty(&g_MpPendingOfflineSet);
|
---|
220 | # endif
|
---|
221 |
|
---|
222 | rc = register_cpu_notifier(&g_NotifierBlock);
|
---|
223 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
224 | AssertMsgReturn(!rc, ("%d\n", rc), RTErrConvertFromErrno(rc));
|
---|
225 | return VINF_SUCCESS;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
|
---|
230 | {
|
---|
231 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
232 | unregister_cpu_notifier(&g_NotifierBlock);
|
---|
233 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
234 | }
|
---|
235 |
|
---|
236 | #else /* Not supported / Not needed */
|
---|
237 |
|
---|
238 | DECLHIDDEN(int) rtR0MpNotificationNativeInit(void)
|
---|
239 | {
|
---|
240 | return VINF_SUCCESS;
|
---|
241 | }
|
---|
242 |
|
---|
243 | DECLHIDDEN(void) rtR0MpNotificationNativeTerm(void)
|
---|
244 | {
|
---|
245 | }
|
---|
246 |
|
---|
247 | #endif /* Not supported / Not needed */
|
---|
248 |
|
---|