kvmppc_init_hostwide() builds the hostwide L0-stats guest-state message and
buffer with kvmppc_gsm_new() and kvmppc_gsb_new() while holding
lock_l0_stats, which is taken with spin_lock_irqsave(). Both helpers
allocate with GFP_KERNEL (and kvmppc_gsb_new() does a further GFP_KERNEL
allocation internally), so they can sleep in atomic context, triggering
"BUG: sleeping function called from invalid context" under
CONFIG_DEBUG_ATOMIC_SLEEP.

lock_l0_stats is a spinlock because it also serializes the PMU ->read path
(kvmppc_pmu_event_update()), so it cannot be turned into a mutex. Build the
message and buffer in local variables outside the lock and only take the
lock to publish them, rechecking for a concurrent registration.

Fixes: f0c9c49c5067 ("powerpc/kvm-hv-pmu: Implement GSB message-ops for 
hostwide counters")
Cc: [email protected]
Signed-off-by: Hyunjung Ko <[email protected]>
---
 arch/powerpc/perf/kvm-hv-pmu.c | 70 ++++++++++++++++++++++++++----------------
 1 file changed, 42 insertions(+), 28 deletions(-)

diff --git a/arch/powerpc/perf/kvm-hv-pmu.c b/arch/powerpc/perf/kvm-hv-pmu.c
index aa72b96b5a8c..deba59cbe234 100644
--- a/arch/powerpc/perf/kvm-hv-pmu.c
+++ b/arch/powerpc/perf/kvm-hv-pmu.c
@@ -314,53 +314,67 @@ static struct kvmppc_gs_msg_ops gsb_ops_l0_stats = {
 
 static int kvmppc_init_hostwide(void)
 {
-       int rc = 0;
+       struct kvmppc_gs_msg *gsm;
+       struct kvmppc_gs_buff *gsb;
        unsigned long flags;
+       int rc;
 
        spin_lock_irqsave(&lock_l0_stats, flags);
-
        /* already registered ? */
        if (gsm_l0_stats) {
-               rc = 0;
-               goto out;
+               spin_unlock_irqrestore(&lock_l0_stats, flags);
+               return 0;
        }
+       spin_unlock_irqrestore(&lock_l0_stats, flags);
+
+       /*
+        * kvmppc_gsm_new() and kvmppc_gsb_new() allocate with GFP_KERNEL and
+        * may sleep, so build the message and buffer outside lock_l0_stats,
+        * which is a spinlock (it also serializes the PMU ->read path), and
+        * only take the lock to publish them.
+        */
 
        /* setup the Guest state message/buffer to talk to L0 */
-       gsm_l0_stats = kvmppc_gsm_new(&gsb_ops_l0_stats, &l0_stats,
-                                     GSM_SEND, GFP_KERNEL);
-       if (!gsm_l0_stats) {
-               rc = -ENOMEM;
-               goto out;
-       }
+       gsm = kvmppc_gsm_new(&gsb_ops_l0_stats, &l0_stats, GSM_SEND, 
GFP_KERNEL);
+       if (!gsm)
+               return -ENOMEM;
 
        /* Populate the Idents */
-       kvmppc_gsm_include(gsm_l0_stats, KVMPPC_GSID_L0_GUEST_HEAP);
-       kvmppc_gsm_include(gsm_l0_stats, KVMPPC_GSID_L0_GUEST_HEAP_MAX);
-       kvmppc_gsm_include(gsm_l0_stats, KVMPPC_GSID_L0_GUEST_PGTABLE_SIZE);
-       kvmppc_gsm_include(gsm_l0_stats, KVMPPC_GSID_L0_GUEST_PGTABLE_SIZE_MAX);
-       kvmppc_gsm_include(gsm_l0_stats, KVMPPC_GSID_L0_GUEST_PGTABLE_RECLAIM);
+       kvmppc_gsm_include(gsm, KVMPPC_GSID_L0_GUEST_HEAP);
+       kvmppc_gsm_include(gsm, KVMPPC_GSID_L0_GUEST_HEAP_MAX);
+       kvmppc_gsm_include(gsm, KVMPPC_GSID_L0_GUEST_PGTABLE_SIZE);
+       kvmppc_gsm_include(gsm, KVMPPC_GSID_L0_GUEST_PGTABLE_SIZE_MAX);
+       kvmppc_gsm_include(gsm, KVMPPC_GSID_L0_GUEST_PGTABLE_RECLAIM);
 
        /* allocate GSB. Guest/Vcpu Id is ignored */
-       gsb_l0_stats = kvmppc_gsb_new(kvmppc_gsm_size(gsm_l0_stats), 0, 0,
-                                     GFP_KERNEL);
-       if (!gsb_l0_stats) {
+       gsb = kvmppc_gsb_new(kvmppc_gsm_size(gsm), 0, 0, GFP_KERNEL);
+       if (!gsb) {
                rc = -ENOMEM;
-               goto out;
+               goto free_gsm;
        }
 
        /* ask the ops to fill in the info */
-       rc = kvmppc_gsm_fill_info(gsm_l0_stats, gsb_l0_stats);
+       rc = kvmppc_gsm_fill_info(gsm, gsb);
+       if (rc)
+               goto free_gsb;
 
-out:
-       if (rc) {
-               if (gsm_l0_stats)
-                       kvmppc_gsm_free(gsm_l0_stats);
-               if (gsb_l0_stats)
-                       kvmppc_gsb_free(gsb_l0_stats);
-               gsm_l0_stats = NULL;
-               gsb_l0_stats = NULL;
+       spin_lock_irqsave(&lock_l0_stats, flags);
+       if (gsm_l0_stats) {
+               /* raced with another CPU; use the existing registration */
+               spin_unlock_irqrestore(&lock_l0_stats, flags);
+               rc = 0;
+               goto free_gsb;
        }
+       gsm_l0_stats = gsm;
+       gsb_l0_stats = gsb;
        spin_unlock_irqrestore(&lock_l0_stats, flags);
+
+       return 0;
+
+free_gsb:
+       kvmppc_gsb_free(gsb);
+free_gsm:
+       kvmppc_gsm_free(gsm);
        return rc;
 }
 
-- 
2.43.0

Reply via email to