| @@ -647,6 +647,7 @@ static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, · 646 unmodified lines |
| 647 | 647 | unsigned long stolen; |
| 648 | 648 | unsigned long core_stolen; |
| 649 | 649 | u64 now; |
| 650 | + unsigned long flags; |
| 650 | 651 | |
| 651 | 652 | dt = vcpu->arch.dtl_ptr; |
| 652 | 653 | vpa = vcpu->arch.vpa.pinned_addr; |
| @@ -654,10 +655,10 @@ static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, · 1 unmodified line |
| 654 | 655 | core_stolen = vcore_stolen_time(vc, now); |
| 655 | 656 | stolen = core_stolen - vcpu->arch.stolen_logged; |
| 656 | 657 | vcpu->arch.stolen_logged = core_stolen; |
| 657 | | − spin_lock_irq(&vcpu->arch.tbacct_lock); |
| 658 | + spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
| 658 | 659 | stolen += vcpu->arch.busy_stolen; |
| 659 | 660 | vcpu->arch.busy_stolen = 0; |
| 660 | | − spin_unlock_irq(&vcpu->arch.tbacct_lock); |
| 661 | + spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
| 661 | 662 | if (!dt || !vpa) |
| 662 | 663 | return; |
| 663 | 664 | memset(dt, 0, sizeof(struct dtl_entry)); |
| @@ -2085,7 +2086,7 @@ static void kvmppc_end_cede(struct kvm_vcpu *vcpu) · 1421 unmodified lines |
| 2085 | 2086 | } |
| 2086 | 2087 | } |
| 2087 | 2088 | |
| 2088 | | −extern void __kvmppc_vcore_entry(void); |
| 2089 | +extern int __kvmppc_vcore_entry(void); |
| 2089 | 2090 | |
| 2090 | 2091 | static void kvmppc_remove_runnable(struct kvmppc_vcore *vc, |
| 2091 | 2092 | struct kvm_vcpu *vcpu) |
| @@ -2167,6 +2168,31 @@ static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu) · 75 unmodified lines |
| 2167 | 2168 | smp_call_function_single(cpu + i, do_nothing, NULL, 1); |
| 2168 | 2169 | } |
| 2169 | 2170 | |
| 2171 | +static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu) |
| 2172 | +{ |
| 2173 | + struct kvm *kvm = vcpu->kvm; |
| 2174 | + |
| 2175 | + /* |
| 2176 | + * With radix, the guest can do TLB invalidations itself, |
| 2177 | + * and it could choose to use the local form (tlbiel) if |
| 2178 | + * it is invalidating a translation that has only ever been |
| 2179 | + * used on one vcpu. However, that doesn't mean it has |
| 2180 | + * only ever been used on one physical cpu, since vcpus |
| 2181 | + * can move around between pcpus. To cope with this, when |
| 2182 | + * a vcpu moves from one pcpu to another, we need to tell |
| 2183 | + * any vcpus running on the same core as this vcpu previously |
| 2184 | + * ran to flush the TLB. The TLB is shared between threads, |
| 2185 | + * so we use a single bit in .need_tlb_flush for all 4 threads. |
| 2186 | + */ |
| 2187 | + if (vcpu->arch.prev_cpu != pcpu) { |
| 2188 | + if (vcpu->arch.prev_cpu >= 0 && |
| 2189 | + cpu_first_thread_sibling(vcpu->arch.prev_cpu) != |
| 2190 | + cpu_first_thread_sibling(pcpu)) |
| 2191 | + radix_flush_cpu(kvm, vcpu->arch.prev_cpu, vcpu); |
| 2192 | + vcpu->arch.prev_cpu = pcpu; |
| 2193 | + } |
| 2194 | +} |
| 2195 | + |
| 2170 | 2196 | static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc) |
| 2171 | 2197 | { |
| 2172 | 2198 | int cpu; |
| @@ -2182,26 +2208,6 @@ static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc) · 9 unmodified lines |
| 2182 | 2208 | cpu += vcpu->arch.ptid; |
| 2183 | 2209 | vcpu->cpu = vc->pcpu; |
| 2184 | 2210 | vcpu->arch.thread_cpu = cpu; |
| 2185 | | − |
| 2186 | | − /* |
| 2187 | | − * With radix, the guest can do TLB invalidations itself, |
| 2188 | | − * and it could choose to use the local form (tlbiel) if |
| 2189 | | − * it is invalidating a translation that has only ever been |
| 2190 | | − * used on one vcpu. However, that doesn't mean it has |
| 2191 | | − * only ever been used on one physical cpu, since vcpus |
| 2192 | | − * can move around between pcpus. To cope with this, when |
| 2193 | | − * a vcpu moves from one pcpu to another, we need to tell |
| 2194 | | − * any vcpus running on the same core as this vcpu previously |
| 2195 | | − * ran to flush the TLB. The TLB is shared between threads, |
| 2196 | | − * so we use a single bit in .need_tlb_flush for all 4 threads. |
| 2197 | | − */ |
| 2198 | | − if (kvm_is_radix(kvm) && vcpu->arch.prev_cpu != cpu) { |
| 2199 | | − if (vcpu->arch.prev_cpu >= 0 && |
| 2200 | | − cpu_first_thread_sibling(vcpu->arch.prev_cpu) != |
| 2201 | | − cpu_first_thread_sibling(cpu)) |
| 2202 | | − radix_flush_cpu(kvm, vcpu->arch.prev_cpu, vcpu); |
| 2203 | | − vcpu->arch.prev_cpu = cpu; |
| 2204 | | − } |
| 2205 | 2211 | cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest); |
| 2206 | 2212 | } |
| 2207 | 2213 | tpaca = &paca[cpu]; |
| @@ -2470,6 +2476,18 @@ static void collect_piggybacks(struct core_info *cip, int target_threads) · 262 unmodified lines |
| 2470 | 2476 | spin_unlock(&lp->lock); |
| 2471 | 2477 | } |
| 2472 | 2478 | |
| 2479 | +static bool recheck_signals(struct core_info *cip) |
| 2480 | +{ |
| 2481 | + int sub, i; |
| 2482 | + struct kvm_vcpu *vcpu; |
| 2483 | + |
| 2484 | + for (sub = 0; sub < cip->n_subcores; ++sub) |
| 2485 | + for_each_runnable_thread(i, vcpu, cip->vc[sub]) |
| 2486 | + if (signal_pending(vcpu->arch.run_task)) |
| 2487 | + return true; |
| 2488 | + return false; |
| 2489 | +} |
| 2490 | + |
| 2473 | 2491 | static void post_guest_process(struct kvmppc_vcore *vc, bool is_master) |
| 2474 | 2492 | { |
| 2475 | 2493 | int still_running = 0, i; |
| @@ -2568,6 +2586,21 @@ static inline int kvmppc_set_host_core(unsigned int cpu) · 92 unmodified lines |
| 2568 | 2586 | return 0; |
| 2569 | 2587 | } |
| 2570 | 2588 | |
| 2589 | +static void set_irq_happened(int trap) |
| 2590 | +{ |
| 2591 | + switch (trap) { |
| 2592 | + case BOOK3S_INTERRUPT_EXTERNAL: |
| 2593 | + local_paca->irq_happened |= PACA_IRQ_EE; |
| 2594 | + break; |
| 2595 | + case BOOK3S_INTERRUPT_H_DOORBELL: |
| 2596 | + local_paca->irq_happened |= PACA_IRQ_DBELL; |
| 2597 | + break; |
| 2598 | + case BOOK3S_INTERRUPT_HMI: |
| 2599 | + local_paca->irq_happened |= PACA_IRQ_HMI; |
| 2600 | + break; |
| 2601 | + } |
| 2602 | +} |
| 2603 | + |
| 2571 | 2604 | /* |
| 2572 | 2605 | * Run a set of guest threads on a physical core. |
| 2573 | 2606 | * Called with vc->lock held. |
| @@ -2587,6 +2620,7 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) · 13 unmodified lines |
| 2587 | 2620 | int pcpu, thr; |
| 2588 | 2621 | int target_threads; |
| 2589 | 2622 | int controlled_threads; |
| 2623 | + int trap; |
| 2590 | 2624 | |
| 2591 | 2625 | /* |
| 2592 | 2626 | * Remove from the list any threads that have a signal pending |
| @@ -2638,6 +2672,43 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) · 45 unmodified lines |
| 2638 | 2672 | if (vc->num_threads < target_threads) |
| 2639 | 2673 | collect_piggybacks(&core_info, target_threads); |
| 2640 | 2674 | |
| 2675 | + /* |
| 2676 | + * On radix, arrange for TLB flushing if necessary. |
| 2677 | + * This has to be done before disabling interrupts since |
| 2678 | + * it uses smp_call_function(). |
| 2679 | + */ |
| 2680 | + pcpu = smp_processor_id(); |
| 2681 | + if (kvm_is_radix(vc->kvm)) { |
| 2682 | + for (sub = 0; sub < core_info.n_subcores; ++sub) |
| 2683 | + for_each_runnable_thread(i, vcpu, core_info.vc[sub]) |
| 2684 | + kvmppc_prepare_radix_vcpu(vcpu, pcpu); |
| 2685 | + } |
| 2686 | + |
| 2687 | + /* |
| 2688 | + * Hard-disable interrupts, and check resched flag and signals. |
| 2689 | + * If we need to reschedule or deliver a signal, clean up |
| 2690 | + * and return without going into the guest(s). |
| 2691 | + */ |
| 2692 | + local_irq_disable(); |
| 2693 | + hard_irq_disable(); |
| 2694 | + if (lazy_irq_pending() || need_resched() || |
| 2695 | + recheck_signals(&core_info)) { |
| 2696 | + local_irq_enable(); |
| 2697 | + vc->vcore_state = VCORE_INACTIVE; |
| 2698 | + /* Unlock all except the primary vcore */ |
| 2699 | + for (sub = 1; sub < core_info.n_subcores; ++sub) { |
| 2700 | + pvc = core_info.vc[sub]; |
| 2701 | + /* Put back on to the preempted vcores list */ |
| 2702 | + kvmppc_vcore_preempt(pvc); |
| 2703 | + spin_unlock(&pvc->lock); |
| 2704 | + } |
| 2705 | + for (i = 0; i < controlled_threads; ++i) |
| 2706 | + kvmppc_release_hwthread(pcpu + i); |
| 2707 | + return; |
| 2708 | + } |
| 2709 | + |
| 2710 | + kvmppc_clear_host_core(pcpu); |
| 2711 | + |
| 2641 | 2712 | /* Decide on micro-threading (split-core) mode */ |
| 2642 | 2713 | subcore_size = threads_per_subcore; |
| 2643 | 2714 | cmd_bit = stat_bit = 0; |
| @@ -2665,7 +2736,6 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) · 21 unmodified lines |
| 2665 | 2736 | /* order writes to split_info before kvm_split_mode pointer */ |
| 2666 | 2737 | smp_wmb(); |
| 2667 | 2738 | } |
| 2668 | | − pcpu = smp_processor_id(); |
| 2669 | 2739 | for (thr = 0; thr < controlled_threads; ++thr) |
| 2670 | 2740 | paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip; |
| 2671 | 2741 | |
| @@ -2685,8 +2755,6 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) · 13 unmodified lines |
| 2685 | 2755 | } |
| 2686 | 2756 | } |
| 2687 | 2757 | |
| 2688 | | − kvmppc_clear_host_core(pcpu); |
| 2689 | | − |
| 2690 | 2758 | /* Start all the threads */ |
| 2691 | 2759 | active = 0; |
| 2692 | 2760 | for (sub = 0; sub < core_info.n_subcores; ++sub) { |
| @@ -2738,14 +2806,25 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) · 45 unmodified lines |
| 2738 | 2806 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
| 2739 | 2807 | spin_unlock(&core_info.vc[sub]->lock); |
| 2740 | 2808 | |
| 2809 | + /* |
| 2810 | + * Interrupts will be enabled once we get into the guest, |
| 2811 | + * so tell lockdep that we're about to enable interrupts. |
| 2812 | + */ |
| 2813 | + trace_hardirqs_on(); |
| 2814 | + |
| 2741 | 2815 | guest_enter(); |
| 2742 | 2816 | |
| 2743 | 2817 | srcu_idx = srcu_read_lock(&vc->kvm->srcu); |
| 2744 | 2818 | |
| 2745 | | − __kvmppc_vcore_entry(); |
| 2819 | + trap = __kvmppc_vcore_entry(); |
| 2746 | 2820 | |
| 2747 | 2821 | srcu_read_unlock(&vc->kvm->srcu, srcu_idx); |
| 2748 | 2822 | |
| 2823 | + guest_exit(); |
| 2824 | + |
| 2825 | + trace_hardirqs_off(); |
| 2826 | + set_irq_happened(trap); |
| 2827 | + |
| 2749 | 2828 | spin_lock(&vc->lock); |
| 2750 | 2829 | /* prevent other vcpu threads from doing kvmppc_start_thread() now */ |
| 2751 | 2830 | vc->vcore_state = VCORE_EXITING; |
| @@ -2773,6 +2852,10 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) · 21 unmodified lines |
| 2773 | 2852 | split_info.do_nap = 0; |
| 2774 | 2853 | } |
| 2775 | 2854 | |
| 2855 | + kvmppc_set_host_core(pcpu); |
| 2856 | + |
| 2857 | + local_irq_enable(); |
| 2858 | + |
| 2776 | 2859 | /* Let secondaries go back to the offline loop */ |
| 2777 | 2860 | for (i = 0; i < controlled_threads; ++i) { |
| 2778 | 2861 | kvmppc_release_hwthread(pcpu + i); |
| @@ -2781,13 +2864,10 @@ static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) · 2 unmodified lines |
| 2781 | 2864 | cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest); |
| 2782 | 2865 | } |
| 2783 | 2866 | |
| 2784 | | − kvmppc_set_host_core(pcpu); |
| 2785 | | − |
| 2786 | 2867 | spin_unlock(&vc->lock); |
| 2787 | 2868 | |
| 2788 | 2869 | /* make sure updates to secondary vcpu structs are visible now */ |
| 2789 | 2870 | smp_mb(); |
| 2790 | | − guest_exit(); |
| 2791 | 2871 | |
| 2792 | 2872 | for (sub = 0; sub < core_info.n_subcores; ++sub) { |
| 2793 | 2873 | pvc = core_info.vc[sub]; |