Re: [PATCHv3 3/4] powerpc/kvm-hv-pmu: Don't allocate gsm/gsb GFP_KERNEL allocations under spinlock

From: Mukesh Kumar Chaurasiya

Date: Wed Nov 25 2026 - 00:53:53 EST


On Mon, Nov 23, 2026 at 07:21:52PM +0530, Ritesh Harjani (IBM) wrote:
> kvmppc_init_hostwide() takes lock_l0_stats with spin_lock_irqsave() and
> then allocates the guest state message and buffer with GFP_KERNEL. On
> a nested API v2 host with DEBUG_ATOMIC_SLEEP this warns at boot:
>
> BUG: sleeping function called from invalid context at include/linux/sched/mm.h:322
> in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 1, name: swapper/0
> preempt_count: 1, expected: 0
> RCU nest depth: 0, expected: 0
> locks held by swapper/0/1: 1, on CPU#0:
> #0: c000000004ad95b8 (lock_l0_stats){....}-{3:3}, at: kvmppc_register_pmu+0x50/0x270
> CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 7.4.0-rc2-00028-ge5a8bf3a3ee9-dirty #124 PREEMPT
> Call Trace:
> dump_stack_lvl+0xe8/0x150 (unreliable)
> __might_resched+0x190/0x30c
> __kmalloc_cache_noprof+0x4dc/0x760
> kvmppc_gsm_new+0x68/0xcc
> kvmppc_register_pmu+0xd4/0x270
> do_one_initcall+0xac/0x48c
> kernel_init_freeable+0x3cc/0x7f0
> kernel_init+0x3c/0x274
> ret_from_kernel_user_thread+0x14/0x1c
>
> The lock is not needed for the allocation of gsm and gsb buffers.
> Allocate and fill the message and buffer without the lock and only take
> it to publish the two global pointers.
>
> Fixes: f0c9c49c5067 ("powerpc/kvm-hv-pmu: Implement GSB message-ops for hostwide counters")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@xxxxxxxxx>
> ---
> arch/powerpc/perf/kvm-hv-pmu.c | 65 ++++++++++++++++------------------
> 1 file changed, 30 insertions(+), 35 deletions(-)
>
> diff --git a/arch/powerpc/perf/kvm-hv-pmu.c b/arch/powerpc/perf/kvm-hv-pmu.c
> index aa72b96b5a8c..a58634f656e9 100644
> --- a/arch/powerpc/perf/kvm-hv-pmu.c
> +++ b/arch/powerpc/perf/kvm-hv-pmu.c
> @@ -314,53 +314,48 @@ 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;
> - }
> -
> - /* 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;
> - }
> + /* allocate the Guest state message/buffer to talk to L0 */
> + 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;
> - }
> + /* setup the Guest state message/buffer to talk to L0 */
> + spin_lock_irqsave(&lock_l0_stats, flags);
> + 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.39.5
>

LGTM

Reviewed-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@xxxxxxxxx>