syscons - Avoid potential blocking issue.
[dragonfly.git] / sys / kern / lwkt_ipiq.c
CommitLineData
3b6b7bd1 1/*
8c10bfcf
MD
2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
3b6b7bd1
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
8c10bfcf 10 *
3b6b7bd1
MD
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
8c10bfcf
MD
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3b6b7bd1 32 * SUCH DAMAGE.
3b6b7bd1
MD
33 */
34
35/*
36 * This module implements IPI message queueing and the MI portion of IPI
37 * message processing.
38 */
39
e8f15168
MD
40#include "opt_ddb.h"
41
3b6b7bd1
MD
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/proc.h>
46#include <sys/rtprio.h>
47#include <sys/queue.h>
48#include <sys/thread2.h>
49#include <sys/sysctl.h>
ac72c7f4 50#include <sys/ktr.h>
3b6b7bd1
MD
51#include <sys/kthread.h>
52#include <machine/cpu.h>
53#include <sys/lock.h>
54#include <sys/caps.h>
55
56#include <vm/vm.h>
57#include <vm/vm_param.h>
58#include <vm/vm_kern.h>
59#include <vm/vm_object.h>
60#include <vm/vm_page.h>
61#include <vm/vm_map.h>
62#include <vm/vm_pager.h>
63#include <vm/vm_extern.h>
64#include <vm/vm_zone.h>
65
66#include <machine/stdarg.h>
3b6b7bd1
MD
67#include <machine/smp.h>
68#include <machine/atomic.h>
69
3b6b7bd1 70#ifdef SMP
4c9f5a7f
MD
71static __int64_t ipiq_count; /* total calls to lwkt_send_ipiq*() */
72static __int64_t ipiq_fifofull; /* number of fifo full conditions detected */
73static __int64_t ipiq_avoided; /* interlock with target avoids cpu ipi */
74static __int64_t ipiq_passive; /* passive IPI messages */
75static __int64_t ipiq_cscount; /* number of cpu synchronizations */
76static int ipiq_optimized = 1; /* XXX temporary sysctl */
d5b2d319 77static int ipiq_debug; /* set to 1 for debug */
e8f15168
MD
78#ifdef PANIC_DEBUG
79static int panic_ipiq_cpu = -1;
80static int panic_ipiq_count = 100;
81#endif
3b6b7bd1
MD
82#endif
83
3b6b7bd1 84#ifdef SMP
0c52fa62
SG
85SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_count, CTLFLAG_RW, &ipiq_count, 0,
86 "Number of IPI's sent");
87SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_fifofull, CTLFLAG_RW, &ipiq_fifofull, 0,
88 "Number of fifo full conditions detected");
89SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_avoided, CTLFLAG_RW, &ipiq_avoided, 0,
90 "Number of IPI's avoided by interlock with target cpu");
91SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_passive, CTLFLAG_RW, &ipiq_passive, 0,
92 "Number of passive IPI messages sent");
93SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_cscount, CTLFLAG_RW, &ipiq_cscount, 0,
94 "Number of cpu synchronizations");
95SYSCTL_INT(_lwkt, OID_AUTO, ipiq_optimized, CTLFLAG_RW, &ipiq_optimized, 0,
96 "");
d5b2d319
MD
97SYSCTL_INT(_lwkt, OID_AUTO, ipiq_debug, CTLFLAG_RW, &ipiq_debug, 0,
98 "");
e8f15168
MD
99#ifdef PANIC_DEBUG
100SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_cpu, CTLFLAG_RW, &panic_ipiq_cpu, 0, "");
101SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_count, CTLFLAG_RW, &panic_ipiq_count, 0, "");
102#endif
3b6b7bd1 103
a7adb95a 104#define IPIQ_STRING "func=%p arg1=%p arg2=%d scpu=%d dcpu=%d"
5118bbc4 105#define IPIQ_ARG_SIZE (sizeof(void *) * 2 + sizeof(int) * 3)
ac72c7f4
MD
106
107#if !defined(KTR_IPIQ)
108#define KTR_IPIQ KTR_ALL
3b6b7bd1 109#endif
ac72c7f4
MD
110KTR_INFO_MASTER(ipiq);
111KTR_INFO(KTR_IPIQ, ipiq, send_norm, 0, IPIQ_STRING, IPIQ_ARG_SIZE);
112KTR_INFO(KTR_IPIQ, ipiq, send_pasv, 1, IPIQ_STRING, IPIQ_ARG_SIZE);
113KTR_INFO(KTR_IPIQ, ipiq, send_nbio, 2, IPIQ_STRING, IPIQ_ARG_SIZE);
114KTR_INFO(KTR_IPIQ, ipiq, send_fail, 3, IPIQ_STRING, IPIQ_ARG_SIZE);
115KTR_INFO(KTR_IPIQ, ipiq, receive, 4, IPIQ_STRING, IPIQ_ARG_SIZE);
d7ed9e5e 116KTR_INFO(KTR_IPIQ, ipiq, sync_start, 5, "cpumask=%08x", sizeof(cpumask_t));
d5b2d319 117KTR_INFO(KTR_IPIQ, ipiq, sync_end, 6, "cpumask=%08x", sizeof(cpumask_t));
866b61fb 118KTR_INFO(KTR_IPIQ, ipiq, cpu_send, 7, IPIQ_STRING, IPIQ_ARG_SIZE);
c92e86f1 119KTR_INFO(KTR_IPIQ, ipiq, send_end, 8, IPIQ_STRING, IPIQ_ARG_SIZE);
ac72c7f4 120
a7adb95a
SZ
121#define logipiq(name, func, arg1, arg2, sgd, dgd) \
122 KTR_LOG(ipiq_ ## name, func, arg1, arg2, sgd->gd_cpuid, dgd->gd_cpuid)
d7ed9e5e
MD
123#define logipiq2(name, arg) \
124 KTR_LOG(ipiq_ ## name, arg)
ac72c7f4
MD
125
126#endif /* SMP */
3b6b7bd1
MD
127
128#ifdef SMP
129
b8a98473
MD
130static int lwkt_process_ipiq_core(globaldata_t sgd, lwkt_ipiq_t ip,
131 struct intrframe *frame);
d5b2d319
MD
132static void lwkt_cpusync_remote1(lwkt_cpusync_t cs);
133static void lwkt_cpusync_remote2(lwkt_cpusync_t cs);
3b6b7bd1
MD
134
135/*
136 * Send a function execution request to another cpu. The request is queued
137 * on the cpu<->cpu ipiq matrix. Each cpu owns a unique ipiq FIFO for every
138 * possible target cpu. The FIFO can be written.
139 *
4c9f5a7f
MD
140 * If the FIFO fills up we have to enable interrupts to avoid an APIC
141 * deadlock and process pending IPIQs while waiting for it to empty.
142 * Otherwise we may soft-deadlock with another cpu whos FIFO is also full.
3b6b7bd1
MD
143 *
144 * We can safely bump gd_intr_nesting_level because our crit_exit() at the
145 * end will take care of any pending interrupts.
146 *
4c9f5a7f
MD
147 * The actual hardware IPI is avoided if the target cpu is already processing
148 * the queue from a prior IPI. It is possible to pipeline IPI messages
149 * very quickly between cpus due to the FIFO hysteresis.
150 *
151 * Need not be called from a critical section.
3b6b7bd1
MD
152 */
153int
b8a98473 154lwkt_send_ipiq3(globaldata_t target, ipifunc3_t func, void *arg1, int arg2)
3b6b7bd1
MD
155{
156 lwkt_ipiq_t ip;
157 int windex;
158 struct globaldata *gd = mycpu;
159
a7adb95a 160 logipiq(send_norm, func, arg1, arg2, gd, target);
ac72c7f4 161
3b6b7bd1 162 if (target == gd) {
b8a98473 163 func(arg1, arg2, NULL);
c92e86f1 164 logipiq(send_end, func, arg1, arg2, gd, target);
3b6b7bd1
MD
165 return(0);
166 }
167 crit_enter();
168 ++gd->gd_intr_nesting_level;
169#ifdef INVARIANTS
170 if (gd->gd_intr_nesting_level > 20)
171 panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
172#endif
f9235b6d 173 KKASSERT(curthread->td_critcount);
3b6b7bd1
MD
174 ++ipiq_count;
175 ip = &gd->gd_ipiq[target->gd_cpuid];
176
177 /*
4c9f5a7f
MD
178 * Do not allow the FIFO to become full. Interrupts must be physically
179 * enabled while we liveloop to avoid deadlocking the APIC.
1b1e83e2
MD
180 *
181 * The target ipiq may have gotten filled up due to passive IPIs and thus
182 * not be aware that its queue is too full, so be sure to issue an
183 * ipiq interrupt to the target cpu.
4c9f5a7f
MD
184 */
185 if (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 2) {
46d4e165 186#if defined(__i386__)
4c9f5a7f 187 unsigned int eflags = read_eflags();
b2b3ffcd 188#elif defined(__x86_64__)
46d4e165
JG
189 unsigned long rflags = read_rflags();
190#endif
4c9f5a7f 191
4c9f5a7f
MD
192 cpu_enable_intr();
193 ++ipiq_fifofull;
cfaeae2a 194 DEBUG_PUSH_INFO("send_ipiq3");
4c9f5a7f 195 while (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 4) {
da0b0e8b
MD
196 if (atomic_poll_acquire_int(&ip->ip_npoll) || ipiq_optimized == 0) {
197 logipiq(cpu_send, func, arg1, arg2, gd, target);
198 cpu_send_ipiq(target->gd_cpuid);
199 }
4c9f5a7f
MD
200 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
201 lwkt_process_ipiq();
da0b0e8b 202 cpu_pause();
4c9f5a7f 203 }
cfaeae2a 204 DEBUG_POP_INFO();
46d4e165 205#if defined(__i386__)
4c9f5a7f 206 write_eflags(eflags);
b2b3ffcd 207#elif defined(__x86_64__)
46d4e165
JG
208 write_rflags(rflags);
209#endif
4c9f5a7f
MD
210 }
211
212 /*
213 * Queue the new message
3b6b7bd1 214 */
3b6b7bd1 215 windex = ip->ip_windex & MAXCPUFIFO_MASK;
b8a98473
MD
216 ip->ip_func[windex] = func;
217 ip->ip_arg1[windex] = arg1;
218 ip->ip_arg2[windex] = arg2;
35238fa5 219 cpu_sfence();
3b6b7bd1 220 ++ip->ip_windex;
4c9f5a7f
MD
221
222 /*
223 * signal the target cpu that there is work pending.
224 */
da0b0e8b 225 if (atomic_poll_acquire_int(&ip->ip_npoll) || ipiq_optimized == 0) {
866b61fb 226 logipiq(cpu_send, func, arg1, arg2, gd, target);
4c9f5a7f
MD
227 cpu_send_ipiq(target->gd_cpuid);
228 } else {
da0b0e8b 229 ++ipiq_avoided;
4c9f5a7f 230 }
da0b0e8b 231 --gd->gd_intr_nesting_level;
4c9f5a7f 232 crit_exit();
c92e86f1 233 logipiq(send_end, func, arg1, arg2, gd, target);
da0b0e8b 234
4c9f5a7f
MD
235 return(ip->ip_windex);
236}
237
238/*
239 * Similar to lwkt_send_ipiq() but this function does not actually initiate
240 * the IPI to the target cpu unless the FIFO has become too full, so it is
241 * very fast.
242 *
243 * This function is used for non-critical IPI messages, such as memory
244 * deallocations. The queue will typically be flushed by the target cpu at
245 * the next clock interrupt.
246 *
247 * Need not be called from a critical section.
248 */
249int
b8a98473
MD
250lwkt_send_ipiq3_passive(globaldata_t target, ipifunc3_t func,
251 void *arg1, int arg2)
4c9f5a7f
MD
252{
253 lwkt_ipiq_t ip;
254 int windex;
255 struct globaldata *gd = mycpu;
256
257 KKASSERT(target != gd);
258 crit_enter();
259 ++gd->gd_intr_nesting_level;
da0b0e8b 260 logipiq(send_pasv, func, arg1, arg2, gd, target);
4c9f5a7f
MD
261#ifdef INVARIANTS
262 if (gd->gd_intr_nesting_level > 20)
263 panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
264#endif
f9235b6d 265 KKASSERT(curthread->td_critcount);
4c9f5a7f
MD
266 ++ipiq_count;
267 ++ipiq_passive;
268 ip = &gd->gd_ipiq[target->gd_cpuid];
269
270 /*
271 * Do not allow the FIFO to become full. Interrupts must be physically
272 * enabled while we liveloop to avoid deadlocking the APIC.
273 */
3b6b7bd1 274 if (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 2) {
46d4e165 275#if defined(__i386__)
3b6b7bd1 276 unsigned int eflags = read_eflags();
b2b3ffcd 277#elif defined(__x86_64__)
46d4e165
JG
278 unsigned long rflags = read_rflags();
279#endif
4c9f5a7f 280
3b6b7bd1
MD
281 cpu_enable_intr();
282 ++ipiq_fifofull;
cfaeae2a 283 DEBUG_PUSH_INFO("send_ipiq3_passive");
3b6b7bd1 284 while (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 4) {
da0b0e8b
MD
285 if (atomic_poll_acquire_int(&ip->ip_npoll) || ipiq_optimized == 0) {
286 logipiq(cpu_send, func, arg1, arg2, gd, target);
287 cpu_send_ipiq(target->gd_cpuid);
288 }
3b6b7bd1
MD
289 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
290 lwkt_process_ipiq();
da0b0e8b 291 cpu_pause();
3b6b7bd1 292 }
cfaeae2a 293 DEBUG_POP_INFO();
46d4e165 294#if defined(__i386__)
3b6b7bd1 295 write_eflags(eflags);
b2b3ffcd 296#elif defined(__x86_64__)
46d4e165
JG
297 write_rflags(rflags);
298#endif
3b6b7bd1 299 }
4c9f5a7f
MD
300
301 /*
302 * Queue the new message
303 */
304 windex = ip->ip_windex & MAXCPUFIFO_MASK;
b8a98473
MD
305 ip->ip_func[windex] = func;
306 ip->ip_arg1[windex] = arg1;
307 ip->ip_arg2[windex] = arg2;
35238fa5 308 cpu_sfence();
4c9f5a7f 309 ++ip->ip_windex;
3b6b7bd1 310 --gd->gd_intr_nesting_level;
4c9f5a7f
MD
311
312 /*
313 * Do not signal the target cpu, it will pick up the IPI when it next
314 * polls (typically on the next tick).
315 */
3b6b7bd1 316 crit_exit();
c92e86f1 317 logipiq(send_end, func, arg1, arg2, gd, target);
da0b0e8b 318
3b6b7bd1
MD
319 return(ip->ip_windex);
320}
321
41a01a4d 322/*
4c9f5a7f
MD
323 * Send an IPI request without blocking, return 0 on success, ENOENT on
324 * failure. The actual queueing of the hardware IPI may still force us
325 * to spin and process incoming IPIs but that will eventually go away
326 * when we've gotten rid of the other general IPIs.
41a01a4d
MD
327 */
328int
b8a98473
MD
329lwkt_send_ipiq3_nowait(globaldata_t target, ipifunc3_t func,
330 void *arg1, int arg2)
41a01a4d
MD
331{
332 lwkt_ipiq_t ip;
333 int windex;
334 struct globaldata *gd = mycpu;
335
a7adb95a 336 logipiq(send_nbio, func, arg1, arg2, gd, target);
f9235b6d 337 KKASSERT(curthread->td_critcount);
41a01a4d 338 if (target == gd) {
b8a98473 339 func(arg1, arg2, NULL);
c92e86f1 340 logipiq(send_end, func, arg1, arg2, gd, target);
41a01a4d
MD
341 return(0);
342 }
da0b0e8b
MD
343 crit_enter();
344 ++gd->gd_intr_nesting_level;
41a01a4d
MD
345 ++ipiq_count;
346 ip = &gd->gd_ipiq[target->gd_cpuid];
347
ac72c7f4 348 if (ip->ip_windex - ip->ip_rindex >= MAXCPUFIFO * 2 / 3) {
a7adb95a 349 logipiq(send_fail, func, arg1, arg2, gd, target);
da0b0e8b
MD
350 --gd->gd_intr_nesting_level;
351 crit_exit();
41a01a4d 352 return(ENOENT);
ac72c7f4 353 }
41a01a4d 354 windex = ip->ip_windex & MAXCPUFIFO_MASK;
b8a98473
MD
355 ip->ip_func[windex] = func;
356 ip->ip_arg1[windex] = arg1;
357 ip->ip_arg2[windex] = arg2;
35238fa5 358 cpu_sfence();
41a01a4d 359 ++ip->ip_windex;
4c9f5a7f 360
41a01a4d 361 /*
4c9f5a7f 362 * This isn't a passive IPI, we still have to signal the target cpu.
41a01a4d 363 */
da0b0e8b 364 if (atomic_poll_acquire_int(&ip->ip_npoll) || ipiq_optimized == 0) {
866b61fb 365 logipiq(cpu_send, func, arg1, arg2, gd, target);
4c9f5a7f
MD
366 cpu_send_ipiq(target->gd_cpuid);
367 } else {
da0b0e8b 368 ++ipiq_avoided;
4c9f5a7f 369 }
da0b0e8b
MD
370 --gd->gd_intr_nesting_level;
371 crit_exit();
c92e86f1
SZ
372
373 logipiq(send_end, func, arg1, arg2, gd, target);
41a01a4d
MD
374 return(0);
375}
376
3b6b7bd1
MD
377/*
378 * deprecated, used only by fast int forwarding.
379 */
380int
b8a98473 381lwkt_send_ipiq3_bycpu(int dcpu, ipifunc3_t func, void *arg1, int arg2)
3b6b7bd1 382{
b8a98473 383 return(lwkt_send_ipiq3(globaldata_find(dcpu), func, arg1, arg2));
3b6b7bd1
MD
384}
385
386/*
387 * Send a message to several target cpus. Typically used for scheduling.
388 * The message will not be sent to stopped cpus.
389 */
390int
da23a592 391lwkt_send_ipiq3_mask(cpumask_t mask, ipifunc3_t func, void *arg1, int arg2)
3b6b7bd1
MD
392{
393 int cpuid;
394 int count = 0;
395
396 mask &= ~stopped_cpus;
397 while (mask) {
da23a592 398 cpuid = BSFCPUMASK(mask);
b8a98473 399 lwkt_send_ipiq3(globaldata_find(cpuid), func, arg1, arg2);
da23a592 400 mask &= ~CPUMASK(cpuid);
3b6b7bd1
MD
401 ++count;
402 }
403 return(count);
404}
405
406/*
407 * Wait for the remote cpu to finish processing a function.
408 *
409 * YYY we have to enable interrupts and process the IPIQ while waiting
410 * for it to empty or we may deadlock with another cpu. Create a CPU_*()
411 * function to do this! YYY we really should 'block' here.
412 *
413 * MUST be called from a critical section. This routine may be called
414 * from an interrupt (for example, if an interrupt wakes a foreign thread
415 * up).
416 */
417void
418lwkt_wait_ipiq(globaldata_t target, int seq)
419{
420 lwkt_ipiq_t ip;
421 int maxc = 100000000;
422
423 if (target != mycpu) {
424 ip = &mycpu->gd_ipiq[target->gd_cpuid];
425 if ((int)(ip->ip_xindex - seq) < 0) {
46d4e165 426#if defined(__i386__)
3b6b7bd1 427 unsigned int eflags = read_eflags();
b2b3ffcd 428#elif defined(__x86_64__)
46d4e165
JG
429 unsigned long rflags = read_rflags();
430#endif
3b6b7bd1 431 cpu_enable_intr();
cfaeae2a 432 DEBUG_PUSH_INFO("wait_ipiq");
3b6b7bd1 433 while ((int)(ip->ip_xindex - seq) < 0) {
41a01a4d 434 crit_enter();
3b6b7bd1 435 lwkt_process_ipiq();
41a01a4d 436 crit_exit();
3b6b7bd1 437 if (--maxc == 0)
6ea70f76 438 kprintf("LWKT_WAIT_IPIQ WARNING! %d wait %d (%d)\n", mycpu->gd_cpuid, target->gd_cpuid, ip->ip_xindex - seq);
3b6b7bd1
MD
439 if (maxc < -1000000)
440 panic("LWKT_WAIT_IPIQ");
35238fa5
MD
441 /*
442 * xindex may be modified by another cpu, use a load fence
443 * to ensure that the loop does not use a speculative value
444 * (which may improve performance).
445 */
446 cpu_lfence();
3b6b7bd1 447 }
cfaeae2a 448 DEBUG_POP_INFO();
46d4e165 449#if defined(__i386__)
3b6b7bd1 450 write_eflags(eflags);
b2b3ffcd 451#elif defined(__x86_64__)
46d4e165
JG
452 write_rflags(rflags);
453#endif
3b6b7bd1
MD
454 }
455 }
456}
457
41a01a4d
MD
458int
459lwkt_seq_ipiq(globaldata_t target)
460{
461 lwkt_ipiq_t ip;
462
463 ip = &mycpu->gd_ipiq[target->gd_cpuid];
464 return(ip->ip_windex);
465}
466
3b6b7bd1
MD
467/*
468 * Called from IPI interrupt (like a fast interrupt), which has placed
469 * us in a critical section. The MP lock may or may not be held.
470 * May also be called from doreti or splz, or be reentrantly called
471 * indirectly through the ip_func[] we run.
472 *
473 * There are two versions, one where no interrupt frame is available (when
474 * called from the send code and from splz, and one where an interrupt
475 * frame is available.
d5b2d319
MD
476 *
477 * When the current cpu is mastering a cpusync we do NOT internally loop
478 * on the cpusyncq poll. We also do not re-flag a pending ipi due to
479 * the cpusyncq poll because this can cause doreti/splz to loop internally.
480 * The cpusync master's own loop must be allowed to run to avoid a deadlock.
3b6b7bd1
MD
481 */
482void
483lwkt_process_ipiq(void)
484{
485 globaldata_t gd = mycpu;
ac72c7f4 486 globaldata_t sgd;
3b6b7bd1
MD
487 lwkt_ipiq_t ip;
488 int n;
489
da0b0e8b 490 ++gd->gd_processing_ipiq;
3b6b7bd1
MD
491again:
492 for (n = 0; n < ncpus; ++n) {
493 if (n != gd->gd_cpuid) {
ac72c7f4
MD
494 sgd = globaldata_find(n);
495 ip = sgd->gd_ipiq;
3b6b7bd1 496 if (ip != NULL) {
b8a98473 497 while (lwkt_process_ipiq_core(sgd, &ip[gd->gd_cpuid], NULL))
3b6b7bd1
MD
498 ;
499 }
500 }
501 }
da0b0e8b
MD
502 if (lwkt_process_ipiq_core(gd, &gd->gd_cpusyncq, NULL)) {
503 if (gd->gd_curthread->td_cscount == 0)
504 goto again;
505 /* need_ipiq(); do not reflag */
3b6b7bd1 506 }
da0b0e8b 507 --gd->gd_processing_ipiq;
3b6b7bd1
MD
508}
509
3b6b7bd1 510void
c7eb0589 511lwkt_process_ipiq_frame(struct intrframe *frame)
3b6b7bd1
MD
512{
513 globaldata_t gd = mycpu;
ac72c7f4 514 globaldata_t sgd;
3b6b7bd1
MD
515 lwkt_ipiq_t ip;
516 int n;
517
518again:
519 for (n = 0; n < ncpus; ++n) {
520 if (n != gd->gd_cpuid) {
ac72c7f4
MD
521 sgd = globaldata_find(n);
522 ip = sgd->gd_ipiq;
3b6b7bd1 523 if (ip != NULL) {
c7eb0589 524 while (lwkt_process_ipiq_core(sgd, &ip[gd->gd_cpuid], frame))
3b6b7bd1
MD
525 ;
526 }
527 }
528 }
529 if (gd->gd_cpusyncq.ip_rindex != gd->gd_cpusyncq.ip_windex) {
c7eb0589 530 if (lwkt_process_ipiq_core(gd, &gd->gd_cpusyncq, frame)) {
0f7a3396
MD
531 if (gd->gd_curthread->td_cscount == 0)
532 goto again;
da0b0e8b 533 /* need_ipiq(); do not reflag */
0f7a3396 534 }
3b6b7bd1
MD
535 }
536}
3b6b7bd1 537
cfaeae2a
MD
538#if 0
539static int iqticks[SMP_MAXCPU];
540static int iqcount[SMP_MAXCPU];
541#endif
542#if 0
543static int iqterm[SMP_MAXCPU];
544#endif
545
3b6b7bd1 546static int
b8a98473
MD
547lwkt_process_ipiq_core(globaldata_t sgd, lwkt_ipiq_t ip,
548 struct intrframe *frame)
3b6b7bd1 549{
2de4f77e 550 globaldata_t mygd = mycpu;
3b6b7bd1 551 int ri;
35238fa5 552 int wi;
b8a98473
MD
553 ipifunc3_t copy_func;
554 void *copy_arg1;
555 int copy_arg2;
35238fa5 556
cfaeae2a
MD
557#if 0
558 if (iqticks[mygd->gd_cpuid] != ticks) {
559 iqticks[mygd->gd_cpuid] = ticks;
560 iqcount[mygd->gd_cpuid] = 0;
561 }
562 if (++iqcount[mygd->gd_cpuid] > 3000000) {
563 kprintf("cpu %d ipiq maxed cscount %d spin %d\n",
564 mygd->gd_cpuid,
565 mygd->gd_curthread->td_cscount,
566 mygd->gd_spinlocks_wr);
567 iqcount[mygd->gd_cpuid] = 0;
568#if 0
569 if (++iqterm[mygd->gd_cpuid] > 10)
570 panic("cpu %d ipiq maxed", mygd->gd_cpuid);
571#endif
572 int i;
573 for (i = 0; i < ncpus; ++i) {
574 if (globaldata_find(i)->gd_infomsg)
575 kprintf(" %s", globaldata_find(i)->gd_infomsg);
576 }
577 kprintf("\n");
578 }
579#endif
580
35238fa5
MD
581 /*
582 * Obtain the current write index, which is modified by a remote cpu.
583 * Issue a load fence to prevent speculative reads of e.g. data written
584 * by the other cpu prior to it updating the index.
585 */
f9235b6d 586 KKASSERT(curthread->td_critcount);
35238fa5
MD
587 wi = ip->ip_windex;
588 cpu_lfence();
2de4f77e 589 ++mygd->gd_intr_nesting_level;
35238fa5 590
3b6b7bd1 591 /*
562273ea
MD
592 * NOTE: xindex is only updated after we are sure the function has
593 * finished execution. Beware lwkt_process_ipiq() reentrancy!
594 * The function may send an IPI which may block/drain.
d64a7617 595 *
562273ea
MD
596 * NOTE: Due to additional IPI operations that the callback function
597 * may make, it is possible for both rindex and windex to advance and
598 * thus for rindex to advance passed our cached windex.
599 *
d5b2d319 600 * NOTE: A load fence is required to prevent speculative loads prior
562273ea 601 * to the loading of ip_rindex. Even though stores might be
d5b2d319
MD
602 * ordered, loads are probably not. A memory fence is required
603 * to prevent reordering of the loads after the ip_rindex update.
3b6b7bd1 604 */
d64a7617 605 while (wi - (ri = ip->ip_rindex) > 0) {
3b6b7bd1 606 ri &= MAXCPUFIFO_MASK;
d5b2d319 607 cpu_lfence();
728f6208 608 copy_func = ip->ip_func[ri];
b8a98473
MD
609 copy_arg1 = ip->ip_arg1[ri];
610 copy_arg2 = ip->ip_arg2[ri];
d5b2d319 611 cpu_mfence();
728f6208 612 ++ip->ip_rindex;
562273ea
MD
613 KKASSERT((ip->ip_rindex & MAXCPUFIFO_MASK) ==
614 ((ri + 1) & MAXCPUFIFO_MASK));
a7adb95a 615 logipiq(receive, copy_func, copy_arg1, copy_arg2, sgd, mycpu);
d5b2d319
MD
616#ifdef INVARIANTS
617 if (ipiq_debug && (ip->ip_rindex & 0xFFFFFF) == 0) {
618 kprintf("cpu %d ipifunc %p %p %d (frame %p)\n",
619 mycpu->gd_cpuid,
620 copy_func, copy_arg1, copy_arg2,
621#if defined(__i386__)
622 (frame ? (void *)frame->if_eip : NULL));
623#elif defined(__amd64__)
624 (frame ? (void *)frame->if_rip : NULL));
625#else
626 NULL);
627#endif
628 }
629#endif
b8a98473 630 copy_func(copy_arg1, copy_arg2, frame);
35238fa5 631 cpu_sfence();
3b6b7bd1 632 ip->ip_xindex = ip->ip_rindex;
e8f15168
MD
633
634#ifdef PANIC_DEBUG
635 /*
636 * Simulate panics during the processing of an IPI
637 */
638 if (mycpu->gd_cpuid == panic_ipiq_cpu && panic_ipiq_count) {
639 if (--panic_ipiq_count == 0) {
640#ifdef DDB
641 Debugger("PANIC_DEBUG");
642#else
643 panic("PANIC_DEBUG");
644#endif
645 }
646 }
647#endif
3b6b7bd1 648 }
2de4f77e 649 --mygd->gd_intr_nesting_level;
4c9f5a7f
MD
650
651 /*
da0b0e8b
MD
652 * If the queue is empty release ip_npoll to enable the other cpu to
653 * send us an IPI interrupt again.
654 *
655 * Return non-zero if there is still more in the queue. Note that we
656 * must re-check the indexes after potentially releasing ip_npoll. The
657 * caller must loop or otherwise ensure that a loop will occur prior to
658 * blocking.
4c9f5a7f 659 */
09a364d7 660 if (ip->ip_rindex == ip->ip_windex)
da0b0e8b
MD
661 atomic_poll_release_int(&ip->ip_npoll);
662 cpu_lfence();
663 return (ip->ip_rindex != ip->ip_windex);
3b6b7bd1
MD
664}
665
6c92c1f2
SZ
666static void
667lwkt_sync_ipiq(void *arg)
668{
5a1a2253 669 volatile cpumask_t *cpumask = arg;
6c92c1f2 670
da23a592 671 atomic_clear_cpumask(cpumask, mycpu->gd_cpumask);
6c92c1f2
SZ
672 if (*cpumask == 0)
673 wakeup(cpumask);
674}
675
676void
677lwkt_synchronize_ipiqs(const char *wmesg)
678{
5a1a2253 679 volatile cpumask_t other_cpumask;
6c92c1f2
SZ
680
681 other_cpumask = mycpu->gd_other_cpus & smp_active_mask;
5a1a2253
SZ
682 lwkt_send_ipiq_mask(other_cpumask, lwkt_sync_ipiq,
683 __DEVOLATILE(void *, &other_cpumask));
6c92c1f2 684
6c92c1f2 685 while (other_cpumask != 0) {
ae8e83e6 686 tsleep_interlock(&other_cpumask, 0);
6c92c1f2 687 if (other_cpumask != 0)
d9345d3a 688 tsleep(&other_cpumask, PINTERLOCKED, wmesg, 0);
6c92c1f2 689 }
6c92c1f2
SZ
690}
691
0f7a3396
MD
692#endif
693
3b6b7bd1
MD
694/*
695 * CPU Synchronization Support
5c71a36a 696 *
d5b2d319
MD
697 * lwkt_cpusync_interlock() - Place specified cpus in a quiescent state.
698 * The current cpu is placed in a hard critical
699 * section.
5c71a36a 700 *
d5b2d319
MD
701 * lwkt_cpusync_deinterlock() - Execute cs_func on specified cpus, including
702 * current cpu if specified, then return.
3b6b7bd1 703 */
3b6b7bd1 704void
d5b2d319 705lwkt_cpusync_simple(cpumask_t mask, cpusync_func_t func, void *arg)
5c71a36a 706{
d5b2d319 707 struct lwkt_cpusync cs;
5c71a36a 708
d5b2d319
MD
709 lwkt_cpusync_init(&cs, mask, func, arg);
710 lwkt_cpusync_interlock(&cs);
711 lwkt_cpusync_deinterlock(&cs);
3b6b7bd1
MD
712}
713
d5b2d319 714
5c71a36a 715void
d5b2d319 716lwkt_cpusync_interlock(lwkt_cpusync_t cs)
3b6b7bd1 717{
d5b2d319 718#ifdef SMP
0f7a3396 719 globaldata_t gd = mycpu;
d5b2d319 720 cpumask_t mask;
0f7a3396 721
d5b2d319
MD
722 /*
723 * mask acknowledge (cs_mack): 0->mask for stage 1
724 *
725 * mack does not include the current cpu.
726 */
727 mask = cs->cs_mask & gd->gd_other_cpus & smp_active_mask;
728 cs->cs_mack = 0;
729 crit_enter_id("cpusync");
730 if (mask) {
cfaeae2a 731 DEBUG_PUSH_INFO("cpusync_interlock");
0f7a3396
MD
732 ++ipiq_cscount;
733 ++gd->gd_curthread->td_cscount;
d5b2d319
MD
734 lwkt_send_ipiq_mask(mask, (ipifunc1_t)lwkt_cpusync_remote1, cs);
735 logipiq2(sync_start, mask);
736 while (cs->cs_mack != mask) {
0f7a3396 737 lwkt_process_ipiq();
d5b2d319 738 cpu_pause();
0f7a3396 739 }
cfaeae2a 740 DEBUG_POP_INFO();
3b6b7bd1 741 }
d5b2d319
MD
742#else
743 cs->cs_mack = 0;
0f7a3396 744#endif
3b6b7bd1
MD
745}
746
747/*
d5b2d319
MD
748 * Interlocked cpus have executed remote1 and are polling in remote2.
749 * To deinterlock we clear cs_mack and wait for the cpus to execute
750 * the func and set their bit in cs_mack again.
0f7a3396 751 *
3b6b7bd1
MD
752 */
753void
d5b2d319 754lwkt_cpusync_deinterlock(lwkt_cpusync_t cs)
3b6b7bd1 755{
0f7a3396 756 globaldata_t gd = mycpu;
0f7a3396 757#ifdef SMP
d5b2d319
MD
758 cpumask_t mask;
759
760 /*
761 * mask acknowledge (cs_mack): mack->0->mack for stage 2
762 *
763 * Clearing cpu bits for polling cpus in cs_mack will cause them to
764 * execute stage 2, which executes the cs_func(cs_data) and then sets
765 * their bit in cs_mack again.
766 *
767 * mack does not include the current cpu.
768 */
769 mask = cs->cs_mack;
770 cpu_ccfence();
771 cs->cs_mack = 0;
772 if (cs->cs_func && (cs->cs_mask & gd->gd_cpumask))
773 cs->cs_func(cs->cs_data);
774 if (mask) {
cfaeae2a 775 DEBUG_PUSH_INFO("cpusync_deinterlock");
d5b2d319 776 while (cs->cs_mack != mask) {
0f7a3396 777 lwkt_process_ipiq();
d5b2d319 778 cpu_pause();
0f7a3396 779 }
cfaeae2a
MD
780 DEBUG_POP_INFO();
781 /*
782 * cpusyncq ipis may be left queued without the RQF flag set due to
783 * a non-zero td_cscount, so be sure to process any laggards after
784 * decrementing td_cscount.
785 */
0f7a3396 786 --gd->gd_curthread->td_cscount;
d5b2d319
MD
787 lwkt_process_ipiq();
788 logipiq2(sync_end, mask);
3b6b7bd1 789 }
d5b2d319
MD
790 crit_exit_id("cpusync");
791#else
792 if (cs->cs_func && (cs->cs_mask & gd->gd_cpumask))
793 cs->cs_func(cs->cs_data);
0f7a3396 794#endif
3b6b7bd1
MD
795}
796
0f7a3396
MD
797#ifdef SMP
798
3b6b7bd1
MD
799/*
800 * helper IPI remote messaging function.
801 *
802 * Called on remote cpu when a new cpu synchronization request has been
803 * sent to us. Execute the run function and adjust cs_count, then requeue
804 * the request so we spin on it.
805 */
806static void
d5b2d319 807lwkt_cpusync_remote1(lwkt_cpusync_t cs)
3b6b7bd1 808{
d5b2d319
MD
809 globaldata_t gd = mycpu;
810
811 atomic_set_cpumask(&cs->cs_mack, gd->gd_cpumask);
812 lwkt_cpusync_remote2(cs);
3b6b7bd1
MD
813}
814
815/*
816 * helper IPI remote messaging function.
817 *
818 * Poll for the originator telling us to finish. If it hasn't, requeue
d5b2d319 819 * our request so we spin on it.
3b6b7bd1
MD
820 */
821static void
d5b2d319 822lwkt_cpusync_remote2(lwkt_cpusync_t cs)
3b6b7bd1 823{
d5b2d319
MD
824 globaldata_t gd = mycpu;
825
826 if ((cs->cs_mack & gd->gd_cpumask) == 0) {
827 if (cs->cs_func)
828 cs->cs_func(cs->cs_data);
829 atomic_set_cpumask(&cs->cs_mack, gd->gd_cpumask);
3b6b7bd1 830 } else {
3b6b7bd1
MD
831 lwkt_ipiq_t ip;
832 int wi;
833
834 ip = &gd->gd_cpusyncq;
835 wi = ip->ip_windex & MAXCPUFIFO_MASK;
b8a98473 836 ip->ip_func[wi] = (ipifunc3_t)(ipifunc1_t)lwkt_cpusync_remote2;
d5b2d319 837 ip->ip_arg1[wi] = cs;
b8a98473 838 ip->ip_arg2[wi] = 0;
35238fa5 839 cpu_sfence();
3b6b7bd1 840 ++ip->ip_windex;
37494a7a 841 if (ipiq_debug && (ip->ip_windex & 0xFFFFFF) == 0) {
cfaeae2a
MD
842 kprintf("cpu %d cm=%016jx %016jx f=%p\n",
843 gd->gd_cpuid,
844 (intmax_t)cs->cs_mask, (intmax_t)cs->cs_mack,
845 cs->cs_func);
37494a7a 846 }
3b6b7bd1
MD
847 }
848}
849
3b6b7bd1 850#endif