59bde2081fc1ee4f05169d29d3fb277cea2d11d5
[dragonfly.git] / sys / kern / lwkt_ipiq.c
1 /*
2  * Copyright (c) 2003-2016 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  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
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
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * This module implements IPI message queueing and the MI portion of IPI
37  * message processing.
38  */
39
40 #include "opt_ddb.h"
41
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>
50 #include <sys/ktr.h>
51 #include <sys/kthread.h>
52 #include <machine/cpu.h>
53 #include <sys/lock.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_param.h>
57 #include <vm/vm_kern.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_pager.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_zone.h>
64
65 #include <machine/stdarg.h>
66 #include <machine/smp.h>
67 #include <machine/clock.h>
68 #include <machine/atomic.h>
69
70 #ifdef _KERNEL_VIRTUAL
71 #include <pthread.h>
72 #endif
73
74 struct ipiq_stats {
75     int64_t ipiq_count;         /* total calls to lwkt_send_ipiq*() */
76     int64_t ipiq_fifofull;      /* number of fifo full conditions detected */
77     int64_t ipiq_avoided;       /* interlock with target avoids cpu ipi */
78     int64_t ipiq_passive;       /* passive IPI messages */
79     int64_t ipiq_cscount;       /* number of cpu synchronizations */
80 } __cachealign;
81
82 static struct ipiq_stats ipiq_stats_percpu[MAXCPU];
83 #define ipiq_stat(gd)   ipiq_stats_percpu[(gd)->gd_cpuid]
84
85 static int ipiq_debug;          /* set to 1 for debug */
86 #ifdef PANIC_DEBUG
87 static int      panic_ipiq_cpu = -1;
88 static int      panic_ipiq_count = 100;
89 #endif
90
91 SYSCTL_INT(_lwkt, OID_AUTO, ipiq_debug, CTLFLAG_RW, &ipiq_debug, 0,
92     "");
93 #ifdef PANIC_DEBUG
94 SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_cpu, CTLFLAG_RW, &panic_ipiq_cpu, 0, "");
95 SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_count, CTLFLAG_RW, &panic_ipiq_count, 0, "");
96 #endif
97
98 #define IPIQ_STRING     "func=%p arg1=%p arg2=%d scpu=%d dcpu=%d"
99 #define IPIQ_ARGS       void *func, void *arg1, int arg2, int scpu, int dcpu
100
101 #if !defined(KTR_IPIQ)
102 #define KTR_IPIQ        KTR_ALL
103 #endif
104 KTR_INFO_MASTER(ipiq);
105 KTR_INFO(KTR_IPIQ, ipiq, send_norm, 0, IPIQ_STRING, IPIQ_ARGS);
106 KTR_INFO(KTR_IPIQ, ipiq, send_pasv, 1, IPIQ_STRING, IPIQ_ARGS);
107 KTR_INFO(KTR_IPIQ, ipiq, send_nbio, 2, IPIQ_STRING, IPIQ_ARGS);
108 KTR_INFO(KTR_IPIQ, ipiq, send_fail, 3, IPIQ_STRING, IPIQ_ARGS);
109 KTR_INFO(KTR_IPIQ, ipiq, receive, 4, IPIQ_STRING, IPIQ_ARGS);
110 KTR_INFO(KTR_IPIQ, ipiq, sync_start, 5, "cpumask=%08lx", unsigned long mask);
111 KTR_INFO(KTR_IPIQ, ipiq, sync_end, 6, "cpumask=%08lx", unsigned long mask);
112 KTR_INFO(KTR_IPIQ, ipiq, cpu_send, 7, IPIQ_STRING, IPIQ_ARGS);
113 KTR_INFO(KTR_IPIQ, ipiq, send_end, 8, IPIQ_STRING, IPIQ_ARGS);
114 KTR_INFO(KTR_IPIQ, ipiq, sync_quick, 9, "cpumask=%08lx", unsigned long mask);
115
116 #define logipiq(name, func, arg1, arg2, sgd, dgd)       \
117         KTR_LOG(ipiq_ ## name, func, arg1, arg2, sgd->gd_cpuid, dgd->gd_cpuid)
118 #define logipiq2(name, arg)     \
119         KTR_LOG(ipiq_ ## name, arg)
120
121 static int lwkt_process_ipiq_core(globaldata_t sgd, lwkt_ipiq_t ip, 
122                                   struct intrframe *frame);
123 static void lwkt_cpusync_remote1(lwkt_cpusync_t cs);
124 static void lwkt_cpusync_remote2(lwkt_cpusync_t cs);
125
126 #define IPIQ_SYSCTL(name) \
127 static int \
128 sysctl_##name(SYSCTL_HANDLER_ARGS) \
129 { \
130     int64_t val = 0; \
131     int cpu, error; \
132  \
133     for (cpu = 0; cpu < ncpus; ++cpu) \
134         val += ipiq_stats_percpu[cpu].name; \
135  \
136     error = sysctl_handle_quad(oidp, &val, 0, req); \
137     if (error || req->newptr == NULL) \
138         return error; \
139  \
140     for (cpu = 0; cpu < ncpus; ++cpu) \
141         ipiq_stats_percpu[cpu].name = val; \
142  \
143     return 0; \
144 }
145
146 IPIQ_SYSCTL(ipiq_count);
147 IPIQ_SYSCTL(ipiq_fifofull);
148 IPIQ_SYSCTL(ipiq_avoided);
149 IPIQ_SYSCTL(ipiq_passive);
150 IPIQ_SYSCTL(ipiq_cscount);
151
152 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_count, (CTLTYPE_QUAD | CTLFLAG_RW),
153     0, 0, sysctl_ipiq_count, "Q", "Number of IPI's sent");
154 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_fifofull, (CTLTYPE_QUAD | CTLFLAG_RW),
155     0, 0, sysctl_ipiq_fifofull, "Q",
156     "Number of fifo full conditions detected");
157 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_avoided, (CTLTYPE_QUAD | CTLFLAG_RW),
158     0, 0, sysctl_ipiq_avoided, "Q",
159     "Number of IPI's avoided by interlock with target cpu");
160 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_passive, (CTLTYPE_QUAD | CTLFLAG_RW),
161     0, 0, sysctl_ipiq_passive, "Q",
162     "Number of passive IPI messages sent");
163 SYSCTL_PROC(_lwkt, OID_AUTO, ipiq_cscount, (CTLTYPE_QUAD | CTLFLAG_RW),
164     0, 0, sysctl_ipiq_cscount, "Q",
165     "Number of cpu synchronizations");
166
167 /*
168  * Send a function execution request to another cpu.  The request is queued
169  * on the cpu<->cpu ipiq matrix.  Each cpu owns a unique ipiq FIFO for every
170  * possible target cpu.  The FIFO can be written.
171  *
172  * If the FIFO fills up we have to enable interrupts to avoid an APIC
173  * deadlock and process pending IPIQs while waiting for it to empty.   
174  * Otherwise we may soft-deadlock with another cpu whos FIFO is also full.
175  *
176  * We can safely bump gd_intr_nesting_level because our crit_exit() at the
177  * end will take care of any pending interrupts.
178  *
179  * The actual hardware IPI is avoided if the target cpu is already processing
180  * the queue from a prior IPI.  It is possible to pipeline IPI messages
181  * very quickly between cpus due to the FIFO hysteresis.
182  *
183  * Need not be called from a critical section.
184  */
185 int
186 lwkt_send_ipiq3(globaldata_t target, ipifunc3_t func, void *arg1, int arg2)
187 {
188     lwkt_ipiq_t ip;
189     int windex;
190     int level1;
191     int level2;
192     struct globaldata *gd = mycpu;
193
194     logipiq(send_norm, func, arg1, arg2, gd, target);
195
196     if (target == gd) {
197         func(arg1, arg2, NULL);
198         logipiq(send_end, func, arg1, arg2, gd, target);
199         return(0);
200     } 
201     crit_enter();
202     ++gd->gd_intr_nesting_level;
203 #ifdef INVARIANTS
204     if (gd->gd_intr_nesting_level > 20)
205         panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
206 #endif
207     KKASSERT(curthread->td_critcount);
208     ++ipiq_stat(gd).ipiq_count;
209     ip = &gd->gd_ipiq[target->gd_cpuid];
210
211     /*
212      * Do not allow the FIFO to become full.  Interrupts must be physically
213      * enabled while we liveloop to avoid deadlocking the APIC.
214      *
215      * If we are nested we want to queue the IPI without processing incoming
216      * IPIs, if possible, to avoid excessive stack recursion.  As long as
217      * the IPI callback does not itself try to send more than a few IPIs to
218      * any single target, it should not be possible to excessively nest because
219      * the unested send code always leaves at least 1/2 the fifo available.
220      */
221     if (gd->gd_processing_ipiq) {
222         level1 = MAXCPUFIFO - 2;
223         level2 = MAXCPUFIFO - 4;
224     } else {
225         level1 = MAXCPUFIFO / 2;
226         level2 = MAXCPUFIFO / 4;
227     }
228
229     if (ip->ip_windex - ip->ip_rindex > level1) {
230 #if defined(__x86_64__)
231         unsigned long rflags = read_rflags();
232 #else
233 #error "no read_*flags"
234 #endif
235 #ifndef _KERNEL_VIRTUAL
236         uint64_t tsc_base = rdtsc();
237 #endif
238         int repeating = 0;
239
240         cpu_enable_intr();
241         ++ipiq_stat(gd).ipiq_fifofull;
242         DEBUG_PUSH_INFO("send_ipiq3");
243         while (ip->ip_windex - ip->ip_rindex > level2) {
244 #if 0
245             if (atomic_swap_int(&target->gd_npoll, 1) == 0) {
246                 logipiq(cpu_send, func, arg1, arg2, gd, target);
247                 cpu_send_ipiq(target->gd_cpuid);
248             }
249 #endif
250             KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
251             lwkt_process_ipiq();
252             cpu_pause();
253
254             /*
255              * Check for target not draining issue.  This should be fixed but
256              * leave the code in-place anyway as it can recover an otherwise
257              * dead system.
258              */
259 #ifdef _KERNEL_VIRTUAL
260             if (repeating++ > 10)
261                     pthread_yield();
262 #else
263             if (rdtsc() - tsc_base > tsc_frequency) {
264                 ++repeating;
265                 if (repeating > 10) {
266                         smp_sniff();
267                         ATOMIC_CPUMASK_ORBIT(target->gd_ipimask, gd->gd_cpuid);
268                         cpu_send_ipiq(target->gd_cpuid);
269                         kprintf("send_ipiq %d->%d tgt not draining (%d) sniff=%p,%p\n",
270                                 gd->gd_cpuid, target->gd_cpuid, repeating,
271                                 target->gd_sample_pc, target->gd_sample_sp);
272                 } else {
273                         smp_sniff();
274                         kprintf("send_ipiq %d->%d tgt not draining (%d)\n",
275                                 gd->gd_cpuid, target->gd_cpuid, repeating);
276                 }
277                 tsc_base = rdtsc();
278             }
279 #endif
280         }
281         DEBUG_POP_INFO();
282 #if defined(__x86_64__)
283         write_rflags(rflags);
284 #else
285 #error "no write_*flags"
286 #endif
287     }
288
289     /*
290      * Queue the new message
291      */
292     windex = ip->ip_windex & MAXCPUFIFO_MASK;
293     ip->ip_info[windex].func = func;
294     ip->ip_info[windex].arg1 = arg1;
295     ip->ip_info[windex].arg2 = arg2;
296     cpu_sfence();
297     ++ip->ip_windex;
298     ATOMIC_CPUMASK_ORBIT(target->gd_ipimask, gd->gd_cpuid);
299
300     /*
301      * signal the target cpu that there is work pending.
302      */
303     if (atomic_swap_int(&target->gd_npoll, 1) == 0) {
304         logipiq(cpu_send, func, arg1, arg2, gd, target);
305         cpu_send_ipiq(target->gd_cpuid);
306     } else {
307         ++ipiq_stat(gd).ipiq_avoided;
308     }
309     --gd->gd_intr_nesting_level;
310     crit_exit();
311     logipiq(send_end, func, arg1, arg2, gd, target);
312
313     return(ip->ip_windex);
314 }
315
316 /*
317  * Similar to lwkt_send_ipiq() but this function does not actually initiate
318  * the IPI to the target cpu unless the FIFO has become too full, so it is
319  * very fast.
320  *
321  * This function is used for non-critical IPI messages, such as memory
322  * deallocations.  The queue will typically be flushed by the target cpu at
323  * the next clock interrupt.
324  *
325  * Need not be called from a critical section.
326  */
327 int
328 lwkt_send_ipiq3_passive(globaldata_t target, ipifunc3_t func,
329                         void *arg1, int arg2)
330 {
331     lwkt_ipiq_t ip;
332     int windex;
333     int level1;
334     int level2;
335     struct globaldata *gd = mycpu;
336
337     KKASSERT(target != gd);
338     crit_enter();
339     ++gd->gd_intr_nesting_level;
340     logipiq(send_pasv, func, arg1, arg2, gd, target);
341 #ifdef INVARIANTS
342     if (gd->gd_intr_nesting_level > 20)
343         panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
344 #endif
345     KKASSERT(curthread->td_critcount);
346     ++ipiq_stat(gd).ipiq_count;
347     ++ipiq_stat(gd).ipiq_passive;
348     ip = &gd->gd_ipiq[target->gd_cpuid];
349
350     /*
351      * Do not allow the FIFO to become full.  Interrupts must be physically
352      * enabled while we liveloop to avoid deadlocking the APIC.
353      *
354      * If we are nested we want to queue the IPI without processing incoming
355      * IPIs, if possible, to avoid excessive stack recursion.
356      */
357     if (gd->gd_processing_ipiq) {
358         level1 = MAXCPUFIFO - 2;
359         level2 = MAXCPUFIFO - 4;
360     } else {
361         level1 = MAXCPUFIFO / 2;
362         level2 = MAXCPUFIFO / 4;
363     }
364     if (ip->ip_windex - ip->ip_rindex > level1) {
365 #if defined(__x86_64__)
366         unsigned long rflags = read_rflags();
367 #else
368 #error "no read_*flags"
369 #endif
370 #ifndef _KERNEL_VIRTUAL
371         uint64_t tsc_base = rdtsc();
372 #endif
373         int repeating = 0;
374
375         cpu_enable_intr();
376         ++ipiq_stat(gd).ipiq_fifofull;
377         DEBUG_PUSH_INFO("send_ipiq3_passive");
378         while (ip->ip_windex - ip->ip_rindex > level2) {
379 #if 0
380             if (atomic_swap_int(&target->gd_npoll, 1) == 0) {
381                 logipiq(cpu_send, func, arg1, arg2, gd, target);
382                 cpu_send_ipiq(target->gd_cpuid);
383             }
384 #endif
385             KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
386             lwkt_process_ipiq();
387             cpu_pause();
388
389             /*
390              * Check for target not draining issue.  This should be fixed but
391              * leave the code in-place anyway as it can recover an otherwise
392              * dead system.
393              */
394 #ifdef _KERNEL_VIRTUAL
395             if (repeating++ > 10)
396                     pthread_yield();
397 #else
398             if (rdtsc() - tsc_base > tsc_frequency) {
399                 ++repeating;
400                 if (repeating > 10) {
401                         smp_sniff();
402                         ATOMIC_CPUMASK_ORBIT(target->gd_ipimask, gd->gd_cpuid);
403                         cpu_send_ipiq(target->gd_cpuid);
404                         kprintf("send_ipiq %d->%d tgt not draining (%d) sniff=%p,%p\n",
405                                 gd->gd_cpuid, target->gd_cpuid, repeating,
406                                 target->gd_sample_pc, target->gd_sample_sp);
407                 } else {
408                         smp_sniff();
409                         kprintf("send_ipiq %d->%d tgt not draining (%d)\n",
410                                 gd->gd_cpuid, target->gd_cpuid, repeating);
411                 }
412                 tsc_base = rdtsc();
413             }
414 #endif
415         }
416         DEBUG_POP_INFO();
417 #if defined(__x86_64__)
418         write_rflags(rflags);
419 #else
420 #error "no write_*flags"
421 #endif
422     }
423
424     /*
425      * Queue the new message
426      */
427     windex = ip->ip_windex & MAXCPUFIFO_MASK;
428     ip->ip_info[windex].func = func;
429     ip->ip_info[windex].arg1 = arg1;
430     ip->ip_info[windex].arg2 = arg2;
431     cpu_sfence();
432     ++ip->ip_windex;
433     ATOMIC_CPUMASK_ORBIT(target->gd_ipimask, gd->gd_cpuid);
434
435     /*
436      * We normally do not signal the target cpu, it will pick up the IPI when it
437      * next polls (typically on the next tick).  However, we do not want to allow
438      * the FIFO to get too full without signaling.  Make sure the target cpu is
439      * signalled once the FIFO is greater than 1/4 full.  This also ensures that
440      * the target cpu will be signalled in order to allow the drain wait to function
441      * without also signalling.
442      */
443     if ((ip->ip_windex - ip->ip_rindex) > MAXCPUFIFO / 4 &&
444         atomic_swap_int(&target->gd_npoll, 1) == 0) {
445         logipiq(cpu_send, func, arg1, arg2, gd, target);
446         cpu_send_ipiq(target->gd_cpuid);
447     } else {
448         ++ipiq_stat(gd).ipiq_avoided;
449     }
450     --gd->gd_intr_nesting_level;
451     crit_exit();
452     logipiq(send_end, func, arg1, arg2, gd, target);
453
454     return(ip->ip_windex);
455 }
456
457 /*
458  * Send an IPI request without blocking, return 0 on success, ENOENT on 
459  * failure.  The actual queueing of the hardware IPI may still force us
460  * to spin and process incoming IPIs but that will eventually go away
461  * when we've gotten rid of the other general IPIs.
462  */
463 int
464 lwkt_send_ipiq3_nowait(globaldata_t target, ipifunc3_t func, 
465                        void *arg1, int arg2)
466 {
467     lwkt_ipiq_t ip;
468     int windex;
469     struct globaldata *gd = mycpu;
470
471     logipiq(send_nbio, func, arg1, arg2, gd, target);
472     KKASSERT(curthread->td_critcount);
473     if (target == gd) {
474         func(arg1, arg2, NULL);
475         logipiq(send_end, func, arg1, arg2, gd, target);
476         return(0);
477     } 
478     crit_enter();
479     ++gd->gd_intr_nesting_level;
480     ++ipiq_stat(gd).ipiq_count;
481     ip = &gd->gd_ipiq[target->gd_cpuid];
482
483     if (ip->ip_windex - ip->ip_rindex >= MAXCPUFIFO * 2 / 3) {
484         logipiq(send_fail, func, arg1, arg2, gd, target);
485         --gd->gd_intr_nesting_level;
486         crit_exit();
487         return(ENOENT);
488     }
489     windex = ip->ip_windex & MAXCPUFIFO_MASK;
490     ip->ip_info[windex].func = func;
491     ip->ip_info[windex].arg1 = arg1;
492     ip->ip_info[windex].arg2 = arg2;
493     cpu_sfence();
494     ++ip->ip_windex;
495     ATOMIC_CPUMASK_ORBIT(target->gd_ipimask, gd->gd_cpuid);
496
497     /*
498      * This isn't a passive IPI, we still have to signal the target cpu.
499      */
500     if (atomic_swap_int(&target->gd_npoll, 1) == 0) {
501         logipiq(cpu_send, func, arg1, arg2, gd, target);
502         cpu_send_ipiq(target->gd_cpuid);
503     } else {
504         ++ipiq_stat(gd).ipiq_avoided;
505     }
506     --gd->gd_intr_nesting_level;
507     crit_exit();
508
509     logipiq(send_end, func, arg1, arg2, gd, target);
510     return(0);
511 }
512
513 /*
514  * deprecated, used only by fast int forwarding.
515  */
516 int
517 lwkt_send_ipiq3_bycpu(int dcpu, ipifunc3_t func, void *arg1, int arg2)
518 {
519     return(lwkt_send_ipiq3(globaldata_find(dcpu), func, arg1, arg2));
520 }
521
522 /*
523  * Send a message to several target cpus.  Typically used for scheduling.
524  * The message will not be sent to stopped cpus.
525  */
526 int
527 lwkt_send_ipiq3_mask(cpumask_t mask, ipifunc3_t func, void *arg1, int arg2)
528 {
529     int cpuid;
530     int count = 0;
531
532     CPUMASK_NANDMASK(mask, stopped_cpus);
533     while (CPUMASK_TESTNZERO(mask)) {
534         cpuid = BSFCPUMASK(mask);
535         lwkt_send_ipiq3(globaldata_find(cpuid), func, arg1, arg2);
536         CPUMASK_NANDBIT(mask, cpuid);
537         ++count;
538     }
539     return(count);
540 }
541
542 /*
543  * Wait for the remote cpu to finish processing a function.
544  *
545  * YYY we have to enable interrupts and process the IPIQ while waiting
546  * for it to empty or we may deadlock with another cpu.  Create a CPU_*()
547  * function to do this!  YYY we really should 'block' here.
548  *
549  * MUST be called from a critical section.  This routine may be called
550  * from an interrupt (for example, if an interrupt wakes a foreign thread
551  * up).
552  */
553 void
554 lwkt_wait_ipiq(globaldata_t target, int seq)
555 {
556     lwkt_ipiq_t ip;
557
558     if (target != mycpu) {
559         ip = &mycpu->gd_ipiq[target->gd_cpuid];
560         if ((int)(ip->ip_xindex - seq) < 0) {
561 #if defined(__x86_64__)
562             unsigned long rflags = read_rflags();
563 #else
564 #error "no read_*flags"
565 #endif
566             int64_t time_tgt = tsc_get_target(1000000000LL);
567             int time_loops = 10;
568             int benice = 0;
569 #ifdef _KERNEL_VIRTUAL
570             int repeating = 0;
571 #endif
572
573             cpu_enable_intr();
574             DEBUG_PUSH_INFO("wait_ipiq");
575             while ((int)(ip->ip_xindex - seq) < 0) {
576                 crit_enter();
577                 lwkt_process_ipiq();
578                 crit_exit();
579 #ifdef _KERNEL_VIRTUAL
580                 if (repeating++ > 10)
581                         pthread_yield();
582 #endif
583
584                 /*
585                  * IPIQs must be handled within 10 seconds and this code
586                  * will warn after one second.
587                  */
588                 if ((benice & 255) == 0 && tsc_test_target(time_tgt) > 0) {
589                         kprintf("LWKT_WAIT_IPIQ WARNING! %d wait %d (%d)\n",
590                                 mycpu->gd_cpuid, target->gd_cpuid,
591                                 ip->ip_xindex - seq);
592                         if (--time_loops == 0)
593                                 panic("LWKT_WAIT_IPIQ");
594                         time_tgt = tsc_get_target(1000000000LL);
595                 }
596                 ++benice;
597
598                 /*
599                  * xindex may be modified by another cpu, use a load fence
600                  * to ensure that the loop does not use a speculative value
601                  * (which may improve performance).
602                  */
603                 cpu_pause();
604                 cpu_lfence();
605             }
606             DEBUG_POP_INFO();
607 #if defined(__x86_64__)
608             write_rflags(rflags);
609 #else
610 #error "no write_*flags"
611 #endif
612         }
613     }
614 }
615
616 int
617 lwkt_seq_ipiq(globaldata_t target)
618 {
619     lwkt_ipiq_t ip;
620
621     ip = &mycpu->gd_ipiq[target->gd_cpuid];
622     return(ip->ip_windex);
623 }
624
625 /*
626  * Called from IPI interrupt (like a fast interrupt), which has placed
627  * us in a critical section.  The MP lock may or may not be held.
628  * May also be called from doreti or splz, or be reentrantly called
629  * indirectly through the ip_info[].func we run.
630  *
631  * There are two versions, one where no interrupt frame is available (when
632  * called from the send code and from splz, and one where an interrupt
633  * frame is available.
634  *
635  * When the current cpu is mastering a cpusync we do NOT internally loop
636  * on the cpusyncq poll.  We also do not re-flag a pending ipi due to
637  * the cpusyncq poll because this can cause doreti/splz to loop internally.
638  * The cpusync master's own loop must be allowed to run to avoid a deadlock.
639  */
640 void
641 lwkt_process_ipiq(void)
642 {
643     globaldata_t gd = mycpu;
644     globaldata_t sgd;
645     lwkt_ipiq_t ip;
646     cpumask_t mask;
647     int n;
648
649     /*
650      * We must process the entire cpumask if we are reentrant because it might
651      * have been partially cleared.
652      */
653     if (++gd->gd_processing_ipiq > 1)
654         ATOMIC_CPUMASK_COPY(gd->gd_ipimask, smp_active_mask);
655 again:
656     atomic_swap_int(&gd->gd_npoll, 0);
657     mask = gd->gd_ipimask;
658     cpu_ccfence();
659     ATOMIC_CPUMASK_NANDMASK(gd->gd_ipimask, mask);
660     while (CPUMASK_TESTNZERO(mask)) {
661         n = BSFCPUMASK(mask);
662         if (n != gd->gd_cpuid) {
663             sgd = globaldata_find(n);
664             ip = sgd->gd_ipiq;
665             if (ip != NULL) {
666                 while (lwkt_process_ipiq_core(sgd, &ip[gd->gd_cpuid], NULL))
667                     ;
668             }
669         }
670         CPUMASK_NANDBIT(mask, n);
671     }
672
673     /*
674      * Process pending cpusyncs.  If the current thread has a cpusync
675      * active cpusync we only run the list once and do not re-flag
676      * as the thread itself is processing its interlock.
677      */
678     if (lwkt_process_ipiq_core(gd, &gd->gd_cpusyncq, NULL)) {
679         if (gd->gd_curthread->td_cscount == 0)
680             goto again;
681         /* need_ipiq(); do not reflag */
682     }
683
684     /*
685      * Interlock to allow more IPI interrupts.  Recheck ipimask after
686      * releasing gd_npoll.
687      */
688     if (atomic_swap_int(&gd->gd_npoll, 0))
689         goto again;
690     --gd->gd_processing_ipiq;
691 }
692
693 void
694 lwkt_process_ipiq_frame(struct intrframe *frame)
695 {
696     globaldata_t gd = mycpu;
697     globaldata_t sgd;
698     lwkt_ipiq_t ip;
699     cpumask_t mask;
700     int n;
701
702     /*
703      * We must process the entire cpumask if we are reentrant because it might
704      * have been partially cleared.
705      */
706     if (++gd->gd_processing_ipiq > 1)
707         ATOMIC_CPUMASK_COPY(gd->gd_ipimask, smp_active_mask);
708 again:
709     atomic_swap_int(&gd->gd_npoll, 0);
710     mask = gd->gd_ipimask;
711     cpu_ccfence();
712     ATOMIC_CPUMASK_NANDMASK(gd->gd_ipimask, mask);
713     while (CPUMASK_TESTNZERO(mask)) {
714         n = BSFCPUMASK(mask);
715         if (n != gd->gd_cpuid) {
716             sgd = globaldata_find(n);
717             ip = sgd->gd_ipiq;
718             if (ip != NULL) {
719                 while (lwkt_process_ipiq_core(sgd, &ip[gd->gd_cpuid], frame))
720                     ;
721             }
722         }
723         CPUMASK_NANDBIT(mask, n);
724     }
725     if (gd->gd_cpusyncq.ip_rindex != gd->gd_cpusyncq.ip_windex) {
726         if (lwkt_process_ipiq_core(gd, &gd->gd_cpusyncq, frame)) {
727             if (gd->gd_curthread->td_cscount == 0)
728                 goto again;
729             /* need_ipiq(); do not reflag */
730         }
731     }
732
733     /*
734      * Interlock to allow more IPI interrupts.  Recheck ipimask after
735      * releasing gd_npoll.
736      */
737     if (atomic_swap_int(&gd->gd_npoll, 0))
738         goto again;
739     --gd->gd_processing_ipiq;
740 }
741
742 #if 0
743 static int iqticks[SMP_MAXCPU];
744 static int iqcount[SMP_MAXCPU];
745 #endif
746 #if 0
747 static int iqterm[SMP_MAXCPU];
748 #endif
749
750 static int
751 lwkt_process_ipiq_core(globaldata_t sgd, lwkt_ipiq_t ip, 
752                        struct intrframe *frame)
753 {
754     globaldata_t mygd = mycpu;
755     int ri;
756     int wi;
757     ipifunc3_t copy_func;
758     void *copy_arg1;
759     int copy_arg2;
760
761 #if 0
762     if (iqticks[mygd->gd_cpuid] != ticks) {
763             iqticks[mygd->gd_cpuid] = ticks;
764             iqcount[mygd->gd_cpuid] = 0;
765     }
766     if (++iqcount[mygd->gd_cpuid] > 3000000) {
767         kprintf("cpu %d ipiq maxed cscount %d spin %d\n",
768                 mygd->gd_cpuid,
769                 mygd->gd_curthread->td_cscount,
770                 mygd->gd_spinlocks);
771         iqcount[mygd->gd_cpuid] = 0;
772 #if 0
773         if (++iqterm[mygd->gd_cpuid] > 10)
774                 panic("cpu %d ipiq maxed", mygd->gd_cpuid);
775 #endif
776         int i;
777         for (i = 0; i < ncpus; ++i) {
778                 if (globaldata_find(i)->gd_infomsg)
779                         kprintf(" %s", globaldata_find(i)->gd_infomsg);
780         }
781         kprintf("\n");
782     }
783 #endif
784
785     /*
786      * Clear the originating core from our ipimask, we will process all
787      * incoming messages.
788      *
789      * Obtain the current write index, which is modified by a remote cpu.
790      * Issue a load fence to prevent speculative reads of e.g. data written
791      * by the other cpu prior to it updating the index.
792      */
793     KKASSERT(curthread->td_critcount);
794     wi = ip->ip_windex;
795     cpu_lfence();
796     ++mygd->gd_intr_nesting_level;
797
798     /*
799      * NOTE: xindex is only updated after we are sure the function has
800      *       finished execution.  Beware lwkt_process_ipiq() reentrancy!
801      *       The function may send an IPI which may block/drain.
802      *
803      * NOTE: Due to additional IPI operations that the callback function
804      *       may make, it is possible for both rindex and windex to advance and
805      *       thus for rindex to advance passed our cached windex.
806      *
807      * NOTE: A load fence is required to prevent speculative loads prior
808      *       to the loading of ip_rindex.  Even though stores might be
809      *       ordered, loads are probably not.  A memory fence is required
810      *       to prevent reordering of the loads after the ip_rindex update.
811      *
812      * NOTE: Single pass only.  Returns non-zero if the queue is not empty
813      *       on return.
814      */
815     while (wi - (ri = ip->ip_rindex) > 0) {
816         ri &= MAXCPUFIFO_MASK;
817         cpu_lfence();
818         copy_func = ip->ip_info[ri].func;
819         copy_arg1 = ip->ip_info[ri].arg1;
820         copy_arg2 = ip->ip_info[ri].arg2;
821         cpu_mfence();
822         ++ip->ip_rindex;
823         KKASSERT((ip->ip_rindex & MAXCPUFIFO_MASK) ==
824                  ((ri + 1) & MAXCPUFIFO_MASK));
825         logipiq(receive, copy_func, copy_arg1, copy_arg2, sgd, mycpu);
826 #ifdef INVARIANTS
827         if (ipiq_debug && (ip->ip_rindex & 0xFFFFFF) == 0) {
828                 kprintf("cpu %d ipifunc %p %p %d (frame %p)\n",
829                         mycpu->gd_cpuid,
830                         copy_func, copy_arg1, copy_arg2,
831 #if defined(__x86_64__)
832                         (frame ? (void *)frame->if_rip : NULL));
833 #else
834                         NULL);
835 #endif
836         }
837 #endif
838         copy_func(copy_arg1, copy_arg2, frame);
839         cpu_sfence();
840         ip->ip_xindex = ip->ip_rindex;
841
842 #ifdef PANIC_DEBUG
843         /*
844          * Simulate panics during the processing of an IPI
845          */
846         if (mycpu->gd_cpuid == panic_ipiq_cpu && panic_ipiq_count) {
847                 if (--panic_ipiq_count == 0) {
848 #ifdef DDB
849                         Debugger("PANIC_DEBUG");
850 #else
851                         panic("PANIC_DEBUG");
852 #endif
853                 }
854         }
855 #endif
856     }
857     --mygd->gd_intr_nesting_level;
858
859     /*
860      * Return non-zero if there is still more in the queue.
861      */
862     cpu_lfence();
863     return (ip->ip_rindex != ip->ip_windex);
864 }
865
866 static void
867 lwkt_sync_ipiq(void *arg)
868 {
869     volatile cpumask_t *cpumask = arg;
870
871     ATOMIC_CPUMASK_NANDBIT(*cpumask, mycpu->gd_cpuid);
872     if (CPUMASK_TESTZERO(*cpumask))
873         wakeup(cpumask);
874 }
875
876 void
877 lwkt_synchronize_ipiqs(const char *wmesg)
878 {
879     volatile cpumask_t other_cpumask;
880
881     other_cpumask = smp_active_mask;
882     CPUMASK_ANDMASK(other_cpumask, mycpu->gd_other_cpus);
883     lwkt_send_ipiq_mask(other_cpumask, lwkt_sync_ipiq,
884                         __DEVOLATILE(void *, &other_cpumask));
885
886     while (CPUMASK_TESTNZERO(other_cpumask)) {
887         tsleep_interlock(&other_cpumask, 0);
888         if (CPUMASK_TESTNZERO(other_cpumask))
889             tsleep(&other_cpumask, PINTERLOCKED, wmesg, 0);
890     }
891 }
892
893 /*
894  * CPU Synchronization Support
895  *
896  * lwkt_cpusync_interlock()     - Place specified cpus in a quiescent state.
897  *                                The current cpu is placed in a hard critical
898  *                                section.
899  *
900  * lwkt_cpusync_deinterlock()   - Execute cs_func on specified cpus, including
901  *                                current cpu if specified, then return.
902  */
903 void
904 lwkt_cpusync_simple(cpumask_t mask, cpusync_func_t func, void *arg)
905 {
906     struct lwkt_cpusync cs;
907
908     lwkt_cpusync_init(&cs, mask, func, arg);
909     lwkt_cpusync_interlock(&cs);
910     lwkt_cpusync_deinterlock(&cs);
911 }
912
913
914 void
915 lwkt_cpusync_interlock(lwkt_cpusync_t cs)
916 {
917     globaldata_t gd = mycpu;
918     cpumask_t mask;
919
920     /*
921      * mask acknowledge (cs_mack):  0->mask for stage 1
922      *
923      * mack does not include the current cpu.
924      */
925     mask = cs->cs_mask;
926     CPUMASK_ANDMASK(mask, gd->gd_other_cpus);
927     CPUMASK_ANDMASK(mask, smp_active_mask);
928     CPUMASK_ASSZERO(cs->cs_mack);
929
930     crit_enter_id("cpusync");
931     if (CPUMASK_TESTNZERO(mask)) {
932         DEBUG_PUSH_INFO("cpusync_interlock");
933         ++ipiq_stat(gd).ipiq_cscount;
934         ++gd->gd_curthread->td_cscount;
935         lwkt_send_ipiq_mask(mask, (ipifunc1_t)lwkt_cpusync_remote1, cs);
936         logipiq2(sync_start, (long)CPUMASK_LOWMASK(mask));
937         while (CPUMASK_CMPMASKNEQ(cs->cs_mack, mask)) {
938             lwkt_process_ipiq();
939             cpu_pause();
940 #ifdef _KERNEL_VIRTUAL
941             pthread_yield();
942 #endif
943         }
944         DEBUG_POP_INFO();
945     }
946 }
947
948 /*
949  * Interlocked cpus have executed remote1 and are polling in remote2.
950  * To deinterlock we clear cs_mack and wait for the cpus to execute
951  * the func and set their bit in cs_mack again.
952  *
953  */
954 void
955 lwkt_cpusync_deinterlock(lwkt_cpusync_t cs)
956 {
957     globaldata_t gd = mycpu;
958     cpumask_t mask;
959
960     /*
961      * mask acknowledge (cs_mack):  mack->0->mack for stage 2
962      *
963      * Clearing cpu bits for polling cpus in cs_mack will cause them to
964      * execute stage 2, which executes the cs_func(cs_data) and then sets
965      * their bit in cs_mack again.
966      *
967      * mack does not include the current cpu.
968      */
969     mask = cs->cs_mack;
970     cpu_ccfence();
971     CPUMASK_ASSZERO(cs->cs_mack);
972     cpu_ccfence();
973     if (cs->cs_func && CPUMASK_TESTBIT(cs->cs_mask, gd->gd_cpuid))
974             cs->cs_func(cs->cs_data);
975     if (CPUMASK_TESTNZERO(mask)) {
976         DEBUG_PUSH_INFO("cpusync_deinterlock");
977         while (CPUMASK_CMPMASKNEQ(cs->cs_mack, mask)) {
978             lwkt_process_ipiq();
979             cpu_pause();
980 #ifdef _KERNEL_VIRTUAL
981             pthread_yield();
982 #endif
983         }
984         DEBUG_POP_INFO();
985         /*
986          * cpusyncq ipis may be left queued without the RQF flag set due to
987          * a non-zero td_cscount, so be sure to process any laggards after
988          * decrementing td_cscount.
989          */
990         --gd->gd_curthread->td_cscount;
991         lwkt_process_ipiq();
992         logipiq2(sync_end, (long)CPUMASK_LOWMASK(mask));
993     }
994     crit_exit_id("cpusync");
995 }
996
997 /*
998  * The quick version does not quiesce the target cpu(s) but instead executes
999  * the function on the target cpu(s) and waits for all to acknowledge.  This
1000  * avoids spinning on the target cpus.
1001  *
1002  * This function is typically only used for kernel_pmap updates.  User pmaps
1003  * have to be quiesced.
1004  */
1005 void
1006 lwkt_cpusync_quick(lwkt_cpusync_t cs)
1007 {
1008     globaldata_t gd = mycpu;
1009     cpumask_t mask;
1010
1011     /*
1012      * stage-2 cs_mack only.
1013      */
1014     mask = cs->cs_mask;
1015     CPUMASK_ANDMASK(mask, gd->gd_other_cpus);
1016     CPUMASK_ANDMASK(mask, smp_active_mask);
1017     CPUMASK_ASSZERO(cs->cs_mack);
1018
1019     crit_enter_id("cpusync");
1020     if (CPUMASK_TESTNZERO(mask)) {
1021         DEBUG_PUSH_INFO("cpusync_interlock");
1022         ++ipiq_stat(gd).ipiq_cscount;
1023         ++gd->gd_curthread->td_cscount;
1024         lwkt_send_ipiq_mask(mask, (ipifunc1_t)lwkt_cpusync_remote2, cs);
1025         logipiq2(sync_quick, (long)CPUMASK_LOWMASK(mask));
1026         while (CPUMASK_CMPMASKNEQ(cs->cs_mack, mask)) {
1027             lwkt_process_ipiq();
1028             cpu_pause();
1029 #ifdef _KERNEL_VIRTUAL
1030             pthread_yield();
1031 #endif
1032         }
1033
1034         /*
1035          * cpusyncq ipis may be left queued without the RQF flag set due to
1036          * a non-zero td_cscount, so be sure to process any laggards after
1037          * decrementing td_cscount.
1038          */
1039         DEBUG_POP_INFO();
1040         --gd->gd_curthread->td_cscount;
1041         lwkt_process_ipiq();
1042     }
1043     if (cs->cs_func && CPUMASK_TESTBIT(cs->cs_mask, gd->gd_cpuid))
1044             cs->cs_func(cs->cs_data);
1045     crit_exit_id("cpusync");
1046 }
1047
1048 /*
1049  * helper IPI remote messaging function.
1050  * 
1051  * Called on remote cpu when a new cpu synchronization request has been
1052  * sent to us.  Execute the run function and adjust cs_count, then requeue
1053  * the request so we spin on it.
1054  */
1055 static void
1056 lwkt_cpusync_remote1(lwkt_cpusync_t cs)
1057 {
1058     globaldata_t gd = mycpu;
1059
1060     ATOMIC_CPUMASK_ORBIT(cs->cs_mack, gd->gd_cpuid);
1061     lwkt_cpusync_remote2(cs);
1062 }
1063
1064 /*
1065  * helper IPI remote messaging function.
1066  *
1067  * Poll for the originator telling us to finish.  If it hasn't, requeue
1068  * our request so we spin on it.
1069  */
1070 static void
1071 lwkt_cpusync_remote2(lwkt_cpusync_t cs)
1072 {
1073     globaldata_t gd = mycpu;
1074
1075     if (CPUMASK_TESTMASK(cs->cs_mack, gd->gd_cpumask) == 0) {
1076         if (cs->cs_func)
1077                 cs->cs_func(cs->cs_data);
1078         ATOMIC_CPUMASK_ORBIT(cs->cs_mack, gd->gd_cpuid);
1079         /* cs can be ripped out at this point */
1080     } else {
1081         lwkt_ipiq_t ip;
1082         int wi;
1083
1084         cpu_pause();
1085 #ifdef _KERNEL_VIRTUAL
1086         pthread_yield();
1087 #endif
1088         cpu_lfence();
1089
1090         /*
1091          * Requeue our IPI to avoid a deep stack recursion.  If no other
1092          * IPIs are pending we can just loop up, which should help VMs
1093          * better-detect spin loops.
1094          */
1095         ip = &gd->gd_cpusyncq;
1096 #if 0
1097         if (ip->ip_rindex == ip->ip_windex) {
1098                 __asm __volatile("cli");
1099                 if (ip->ip_rindex == ip->ip_windex) {
1100                         __asm __volatile("sti; hlt");
1101                 } else {
1102                         __asm __volatile("sti");
1103                 }
1104         }
1105 #endif
1106
1107         wi = ip->ip_windex & MAXCPUFIFO_MASK;
1108         ip->ip_info[wi].func = (ipifunc3_t)(ipifunc1_t)lwkt_cpusync_remote2;
1109         ip->ip_info[wi].arg1 = cs;
1110         ip->ip_info[wi].arg2 = 0;
1111         cpu_sfence();
1112         KKASSERT(ip->ip_windex - ip->ip_rindex < MAXCPUFIFO);
1113         ++ip->ip_windex;
1114         if (ipiq_debug && (ip->ip_windex & 0xFFFFFF) == 0) {
1115                 kprintf("cpu %d cm=%016jx %016jx f=%p\n",
1116                         gd->gd_cpuid,
1117                         (intmax_t)CPUMASK_LOWMASK(cs->cs_mask),
1118                         (intmax_t)CPUMASK_LOWMASK(cs->cs_mack),
1119                         cs->cs_func);
1120         }
1121     }
1122 }
1123
1124 #define LWKT_IPIQ_NLATENCY      8
1125 #define LWKT_IPIQ_NLATENCY_MASK (LWKT_IPIQ_NLATENCY - 1)
1126
1127 struct lwkt_ipiq_latency_log {
1128         int             idx;    /* unmasked index */
1129         int             pad;
1130         uint64_t        latency[LWKT_IPIQ_NLATENCY];
1131 };
1132
1133 static struct lwkt_ipiq_latency_log     lwkt_ipiq_latency_logs[MAXCPU];
1134 static uint64_t save_tsc;
1135
1136 /*
1137  * IPI callback (already in a critical section)
1138  */
1139 static void
1140 lwkt_ipiq_latency_testfunc(void *arg __unused)
1141 {
1142         uint64_t delta_tsc;
1143         struct globaldata *gd;
1144         struct lwkt_ipiq_latency_log *lat;
1145
1146         /*
1147          * Get delta TSC (assume TSCs are synchronized) as quickly as
1148          * possible and then convert to nanoseconds.
1149          */
1150         delta_tsc = rdtsc_ordered() - save_tsc;
1151         delta_tsc = delta_tsc * 1000000000LU / tsc_frequency;
1152
1153         /*
1154          * Record in our save array.
1155          */
1156         gd = mycpu;
1157         lat = &lwkt_ipiq_latency_logs[gd->gd_cpuid];
1158         lat->latency[lat->idx & LWKT_IPIQ_NLATENCY_MASK] = delta_tsc;
1159         ++lat->idx;
1160 }
1161
1162 /*
1163  * Send IPI from cpu0 to other cpus
1164  *
1165  * NOTE: Machine must be idle for test to run dependably, and also probably
1166  *       a good idea not to be running powerd.
1167  *
1168  * NOTE: Caller should use 'usched :1 <command>' to lock itself to cpu 0.
1169  *       See 'ipitest' script in /usr/src/test/sysperf/ipitest
1170  */
1171 static int
1172 lwkt_ipiq_latency_test(SYSCTL_HANDLER_ARGS)
1173 {
1174         struct globaldata *gd;
1175         int cpu = 0, orig_cpu, error;
1176
1177         error = sysctl_handle_int(oidp, &cpu, arg2, req);
1178         if (error || req->newptr == NULL)
1179                 return error;
1180
1181         if (cpu == 0)
1182                 return 0;
1183         else if (cpu >= ncpus || cpu < 0)
1184                 return EINVAL;
1185
1186         orig_cpu = mycpuid;
1187         lwkt_migratecpu(0);
1188
1189         gd = globaldata_find(cpu);
1190
1191         save_tsc = rdtsc_ordered();
1192         lwkt_send_ipiq(gd, lwkt_ipiq_latency_testfunc, NULL);
1193
1194         lwkt_migratecpu(orig_cpu);
1195         return 0;
1196 }
1197
1198 SYSCTL_NODE(_debug, OID_AUTO, ipiq, CTLFLAG_RW, 0, "");
1199 SYSCTL_PROC(_debug_ipiq, OID_AUTO, latency_test, CTLTYPE_INT | CTLFLAG_RW,
1200     NULL, 0, lwkt_ipiq_latency_test, "I",
1201     "ipi latency test, arg: remote cpuid");
1202
1203 static int
1204 lwkt_ipiq_latency(SYSCTL_HANDLER_ARGS)
1205 {
1206         struct lwkt_ipiq_latency_log *latency = arg1;
1207         uint64_t lat[LWKT_IPIQ_NLATENCY];
1208         int i;
1209
1210         for (i = 0; i < LWKT_IPIQ_NLATENCY; ++i)
1211                 lat[i] = latency->latency[i];
1212
1213         return sysctl_handle_opaque(oidp, lat, sizeof(lat), req);
1214 }
1215
1216 static void
1217 lwkt_ipiq_latency_init(void *dummy __unused)
1218 {
1219         int cpu;
1220
1221         for (cpu = 0; cpu < ncpus; ++cpu) {
1222                 char name[32];
1223
1224                 ksnprintf(name, sizeof(name), "latency%d", cpu);
1225                 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_debug_ipiq),
1226                     OID_AUTO, name, CTLTYPE_OPAQUE | CTLFLAG_RD,
1227                     &lwkt_ipiq_latency_logs[cpu], 0, lwkt_ipiq_latency,
1228                     "LU", "7 latest ipi latency measurement results");
1229         }
1230 }
1231 SYSINIT(lwkt_ipiq_latency, SI_SUB_CONFIGURE, SI_ORDER_ANY,
1232     lwkt_ipiq_latency_init, NULL);