smp_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Intel SMP support routines.
  3. *
  4. * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
  5. * (c) 1998-99, 2000 Ingo Molnar <mingo@redhat.com>
  6. * (c) 2002,2003 Andi Kleen, SuSE Labs.
  7. *
  8. * This code is released under the GNU General Public License version 2 or
  9. * later.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/delay.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/smp.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/mc146818rtc.h>
  18. #include <linux/interrupt.h>
  19. #include <asm/mtrr.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/mach_apic.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/proto.h>
  25. #include <asm/apicdef.h>
  26. #include <asm/idle.h>
  27. /*
  28. * Smarter SMP flushing macros.
  29. * c/o Linus Torvalds.
  30. *
  31. * These mean you can really definitely utterly forget about
  32. * writing to user space from interrupts. (Its not allowed anyway).
  33. *
  34. * Optimizations Manfred Spraul <manfred@colorfullife.com>
  35. *
  36. * More scalable flush, from Andi Kleen
  37. *
  38. * To avoid global state use 8 different call vectors.
  39. * Each CPU uses a specific vector to trigger flushes on other
  40. * CPUs. Depending on the received vector the target CPUs look into
  41. * the right per cpu variable for the flush data.
  42. *
  43. * With more than 8 CPUs they are hashed to the 8 available
  44. * vectors. The limited global vector space forces us to this right now.
  45. * In future when interrupts are split into per CPU domains this could be
  46. * fixed, at the cost of triggering multiple IPIs in some cases.
  47. */
  48. union smp_flush_state {
  49. struct {
  50. cpumask_t flush_cpumask;
  51. struct mm_struct *flush_mm;
  52. unsigned long flush_va;
  53. spinlock_t tlbstate_lock;
  54. };
  55. char pad[SMP_CACHE_BYTES];
  56. } ____cacheline_aligned;
  57. /* State is put into the per CPU data section, but padded
  58. to a full cache line because other CPUs can access it and we don't
  59. want false sharing in the per cpu data segment. */
  60. static DEFINE_PER_CPU(union smp_flush_state, flush_state);
  61. /*
  62. * We cannot call mmdrop() because we are in interrupt context,
  63. * instead update mm->cpu_vm_mask.
  64. */
  65. static inline void leave_mm(int cpu)
  66. {
  67. if (read_pda(mmu_state) == TLBSTATE_OK)
  68. BUG();
  69. cpu_clear(cpu, read_pda(active_mm)->cpu_vm_mask);
  70. load_cr3(swapper_pg_dir);
  71. }
  72. /*
  73. *
  74. * The flush IPI assumes that a thread switch happens in this order:
  75. * [cpu0: the cpu that switches]
  76. * 1) switch_mm() either 1a) or 1b)
  77. * 1a) thread switch to a different mm
  78. * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask);
  79. * Stop ipi delivery for the old mm. This is not synchronized with
  80. * the other cpus, but smp_invalidate_interrupt ignore flush ipis
  81. * for the wrong mm, and in the worst case we perform a superfluous
  82. * tlb flush.
  83. * 1a2) set cpu mmu_state to TLBSTATE_OK
  84. * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
  85. * was in lazy tlb mode.
  86. * 1a3) update cpu active_mm
  87. * Now cpu0 accepts tlb flushes for the new mm.
  88. * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask);
  89. * Now the other cpus will send tlb flush ipis.
  90. * 1a4) change cr3.
  91. * 1b) thread switch without mm change
  92. * cpu active_mm is correct, cpu0 already handles
  93. * flush ipis.
  94. * 1b1) set cpu mmu_state to TLBSTATE_OK
  95. * 1b2) test_and_set the cpu bit in cpu_vm_mask.
  96. * Atomically set the bit [other cpus will start sending flush ipis],
  97. * and test the bit.
  98. * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
  99. * 2) switch %%esp, ie current
  100. *
  101. * The interrupt must handle 2 special cases:
  102. * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
  103. * - the cpu performs speculative tlb reads, i.e. even if the cpu only
  104. * runs in kernel space, the cpu could load tlb entries for user space
  105. * pages.
  106. *
  107. * The good news is that cpu mmu_state is local to each cpu, no
  108. * write/read ordering problems.
  109. */
  110. /*
  111. * TLB flush IPI:
  112. *
  113. * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
  114. * 2) Leave the mm if we are in the lazy tlb mode.
  115. *
  116. * Interrupts are disabled.
  117. */
  118. asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
  119. {
  120. int cpu;
  121. int sender;
  122. union smp_flush_state *f;
  123. cpu = smp_processor_id();
  124. /*
  125. * orig_rax contains the negated interrupt vector.
  126. * Use that to determine where the sender put the data.
  127. */
  128. sender = ~regs->orig_ax - INVALIDATE_TLB_VECTOR_START;
  129. f = &per_cpu(flush_state, sender);
  130. if (!cpu_isset(cpu, f->flush_cpumask))
  131. goto out;
  132. /*
  133. * This was a BUG() but until someone can quote me the
  134. * line from the intel manual that guarantees an IPI to
  135. * multiple CPUs is retried _only_ on the erroring CPUs
  136. * its staying as a return
  137. *
  138. * BUG();
  139. */
  140. if (f->flush_mm == read_pda(active_mm)) {
  141. if (read_pda(mmu_state) == TLBSTATE_OK) {
  142. if (f->flush_va == TLB_FLUSH_ALL)
  143. local_flush_tlb();
  144. else
  145. __flush_tlb_one(f->flush_va);
  146. } else
  147. leave_mm(cpu);
  148. }
  149. out:
  150. ack_APIC_irq();
  151. cpu_clear(cpu, f->flush_cpumask);
  152. add_pda(irq_tlb_count, 1);
  153. }
  154. void native_flush_tlb_others(const cpumask_t *cpumaskp, struct mm_struct *mm,
  155. unsigned long va)
  156. {
  157. int sender;
  158. union smp_flush_state *f;
  159. cpumask_t cpumask = *cpumaskp;
  160. /* Caller has disabled preemption */
  161. sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
  162. f = &per_cpu(flush_state, sender);
  163. /*
  164. * Could avoid this lock when
  165. * num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
  166. * probably not worth checking this for a cache-hot lock.
  167. */
  168. spin_lock(&f->tlbstate_lock);
  169. f->flush_mm = mm;
  170. f->flush_va = va;
  171. cpus_or(f->flush_cpumask, cpumask, f->flush_cpumask);
  172. /*
  173. * We have to send the IPI only to
  174. * CPUs affected.
  175. */
  176. send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR_START + sender);
  177. while (!cpus_empty(f->flush_cpumask))
  178. cpu_relax();
  179. f->flush_mm = NULL;
  180. f->flush_va = 0;
  181. spin_unlock(&f->tlbstate_lock);
  182. }
  183. int __cpuinit init_smp_flush(void)
  184. {
  185. int i;
  186. for_each_cpu_mask(i, cpu_possible_map) {
  187. spin_lock_init(&per_cpu(flush_state, i).tlbstate_lock);
  188. }
  189. return 0;
  190. }
  191. core_initcall(init_smp_flush);
  192. void flush_tlb_current_task(void)
  193. {
  194. struct mm_struct *mm = current->mm;
  195. cpumask_t cpu_mask;
  196. preempt_disable();
  197. cpu_mask = mm->cpu_vm_mask;
  198. cpu_clear(smp_processor_id(), cpu_mask);
  199. local_flush_tlb();
  200. if (!cpus_empty(cpu_mask))
  201. flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
  202. preempt_enable();
  203. }
  204. void flush_tlb_mm (struct mm_struct * mm)
  205. {
  206. cpumask_t cpu_mask;
  207. preempt_disable();
  208. cpu_mask = mm->cpu_vm_mask;
  209. cpu_clear(smp_processor_id(), cpu_mask);
  210. if (current->active_mm == mm) {
  211. if (current->mm)
  212. local_flush_tlb();
  213. else
  214. leave_mm(smp_processor_id());
  215. }
  216. if (!cpus_empty(cpu_mask))
  217. flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
  218. preempt_enable();
  219. }
  220. void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
  221. {
  222. struct mm_struct *mm = vma->vm_mm;
  223. cpumask_t cpu_mask;
  224. preempt_disable();
  225. cpu_mask = mm->cpu_vm_mask;
  226. cpu_clear(smp_processor_id(), cpu_mask);
  227. if (current->active_mm == mm) {
  228. if(current->mm)
  229. __flush_tlb_one(va);
  230. else
  231. leave_mm(smp_processor_id());
  232. }
  233. if (!cpus_empty(cpu_mask))
  234. flush_tlb_others(cpu_mask, mm, va);
  235. preempt_enable();
  236. }
  237. static void do_flush_tlb_all(void* info)
  238. {
  239. unsigned long cpu = smp_processor_id();
  240. __flush_tlb_all();
  241. if (read_pda(mmu_state) == TLBSTATE_LAZY)
  242. leave_mm(cpu);
  243. }
  244. void flush_tlb_all(void)
  245. {
  246. on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
  247. }
  248. /*
  249. * this function sends a 'reschedule' IPI to another CPU.
  250. * it goes straight through and wastes no time serializing
  251. * anything. Worst case is that we lose a reschedule ...
  252. */
  253. void smp_send_reschedule(int cpu)
  254. {
  255. send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
  256. }
  257. /*
  258. * Structure and data for smp_call_function(). This is designed to minimise
  259. * static memory requirements. It also looks cleaner.
  260. */
  261. static DEFINE_SPINLOCK(call_lock);
  262. struct call_data_struct {
  263. void (*func) (void *info);
  264. void *info;
  265. atomic_t started;
  266. atomic_t finished;
  267. int wait;
  268. };
  269. static struct call_data_struct * call_data;
  270. void lock_ipi_call_lock(void)
  271. {
  272. spin_lock_irq(&call_lock);
  273. }
  274. void unlock_ipi_call_lock(void)
  275. {
  276. spin_unlock_irq(&call_lock);
  277. }
  278. /*
  279. * this function sends a 'generic call function' IPI to all other CPU
  280. * of the system defined in the mask.
  281. */
  282. static int __smp_call_function_mask(cpumask_t mask,
  283. void (*func)(void *), void *info,
  284. int wait)
  285. {
  286. struct call_data_struct data;
  287. cpumask_t allbutself;
  288. int cpus;
  289. allbutself = cpu_online_map;
  290. cpu_clear(smp_processor_id(), allbutself);
  291. cpus_and(mask, mask, allbutself);
  292. cpus = cpus_weight(mask);
  293. if (!cpus)
  294. return 0;
  295. data.func = func;
  296. data.info = info;
  297. atomic_set(&data.started, 0);
  298. data.wait = wait;
  299. if (wait)
  300. atomic_set(&data.finished, 0);
  301. call_data = &data;
  302. wmb();
  303. /* Send a message to other CPUs */
  304. if (cpus_equal(mask, allbutself))
  305. send_IPI_allbutself(CALL_FUNCTION_VECTOR);
  306. else
  307. send_IPI_mask(mask, CALL_FUNCTION_VECTOR);
  308. /* Wait for response */
  309. while (atomic_read(&data.started) != cpus)
  310. cpu_relax();
  311. if (!wait)
  312. return 0;
  313. while (atomic_read(&data.finished) != cpus)
  314. cpu_relax();
  315. return 0;
  316. }
  317. /**
  318. * smp_call_function_mask(): Run a function on a set of other CPUs.
  319. * @mask: The set of cpus to run on. Must not include the current cpu.
  320. * @func: The function to run. This must be fast and non-blocking.
  321. * @info: An arbitrary pointer to pass to the function.
  322. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  323. *
  324. * Returns 0 on success, else a negative status code.
  325. *
  326. * If @wait is true, then returns once @func has returned; otherwise
  327. * it returns just before the target cpu calls @func.
  328. *
  329. * You must not call this function with disabled interrupts or from a
  330. * hardware interrupt handler or from a bottom half handler.
  331. */
  332. int smp_call_function_mask(cpumask_t mask,
  333. void (*func)(void *), void *info,
  334. int wait)
  335. {
  336. int ret;
  337. /* Can deadlock when called with interrupts disabled */
  338. WARN_ON(irqs_disabled());
  339. spin_lock(&call_lock);
  340. ret = __smp_call_function_mask(mask, func, info, wait);
  341. spin_unlock(&call_lock);
  342. return ret;
  343. }
  344. EXPORT_SYMBOL(smp_call_function_mask);
  345. /*
  346. * smp_call_function_single - Run a function on a specific CPU
  347. * @func: The function to run. This must be fast and non-blocking.
  348. * @info: An arbitrary pointer to pass to the function.
  349. * @nonatomic: Currently unused.
  350. * @wait: If true, wait until function has completed on other CPUs.
  351. *
  352. * Retrurns 0 on success, else a negative status code.
  353. *
  354. * Does not return until the remote CPU is nearly ready to execute <func>
  355. * or is or has executed.
  356. */
  357. int smp_call_function_single (int cpu, void (*func) (void *info), void *info,
  358. int nonatomic, int wait)
  359. {
  360. /* prevent preemption and reschedule on another processor */
  361. int ret, me = get_cpu();
  362. /* Can deadlock when called with interrupts disabled */
  363. WARN_ON(irqs_disabled());
  364. if (cpu == me) {
  365. local_irq_disable();
  366. func(info);
  367. local_irq_enable();
  368. put_cpu();
  369. return 0;
  370. }
  371. ret = smp_call_function_mask(cpumask_of_cpu(cpu), func, info, wait);
  372. put_cpu();
  373. return ret;
  374. }
  375. EXPORT_SYMBOL(smp_call_function_single);
  376. /*
  377. * smp_call_function - run a function on all other CPUs.
  378. * @func: The function to run. This must be fast and non-blocking.
  379. * @info: An arbitrary pointer to pass to the function.
  380. * @nonatomic: currently unused.
  381. * @wait: If true, wait (atomically) until function has completed on other
  382. * CPUs.
  383. *
  384. * Returns 0 on success, else a negative status code. Does not return until
  385. * remote CPUs are nearly ready to execute func or are or have executed.
  386. *
  387. * You must not call this function with disabled interrupts or from a
  388. * hardware interrupt handler or from a bottom half handler.
  389. * Actually there are a few legal cases, like panic.
  390. */
  391. int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
  392. int wait)
  393. {
  394. return smp_call_function_mask(cpu_online_map, func, info, wait);
  395. }
  396. EXPORT_SYMBOL(smp_call_function);
  397. static void stop_this_cpu(void *dummy)
  398. {
  399. local_irq_disable();
  400. /*
  401. * Remove this CPU:
  402. */
  403. cpu_clear(smp_processor_id(), cpu_online_map);
  404. disable_local_APIC();
  405. for (;;)
  406. halt();
  407. }
  408. void smp_send_stop(void)
  409. {
  410. int nolock;
  411. unsigned long flags;
  412. if (reboot_force)
  413. return;
  414. /* Don't deadlock on the call lock in panic */
  415. nolock = !spin_trylock(&call_lock);
  416. local_irq_save(flags);
  417. __smp_call_function_mask(cpu_online_map, stop_this_cpu, NULL, 0);
  418. if (!nolock)
  419. spin_unlock(&call_lock);
  420. disable_local_APIC();
  421. local_irq_restore(flags);
  422. }
  423. /*
  424. * Reschedule call back. Nothing to do,
  425. * all the work is done automatically when
  426. * we return from the interrupt.
  427. */
  428. asmlinkage void smp_reschedule_interrupt(void)
  429. {
  430. ack_APIC_irq();
  431. add_pda(irq_resched_count, 1);
  432. }
  433. asmlinkage void smp_call_function_interrupt(void)
  434. {
  435. void (*func) (void *info) = call_data->func;
  436. void *info = call_data->info;
  437. int wait = call_data->wait;
  438. ack_APIC_irq();
  439. /*
  440. * Notify initiating CPU that I've grabbed the data and am
  441. * about to execute the function
  442. */
  443. mb();
  444. atomic_inc(&call_data->started);
  445. /*
  446. * At this point the info structure may be out of scope unless wait==1
  447. */
  448. exit_idle();
  449. irq_enter();
  450. (*func)(info);
  451. add_pda(irq_call_count, 1);
  452. irq_exit();
  453. if (wait) {
  454. mb();
  455. atomic_inc(&call_data->finished);
  456. }
  457. }