Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3523043pxb; Mon, 4 Apr 2022 19:49:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcctdgdzqmq6ztEntpMjnOcVo6v9JKOp84Pdl5c3g4OojDOdoweVJAcGvMWbnuRNIIVasO X-Received: by 2002:a05:6a00:1907:b0:4f7:945:14cf with SMTP id y7-20020a056a00190700b004f7094514cfmr1186741pfi.47.1649126981696; Mon, 04 Apr 2022 19:49:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649126981; cv=none; d=google.com; s=arc-20160816; b=vla1cqKEKhQ+bkyx3bXX21+PaO/a5EXGLfjR+MY5Uo25d+Z8uzmZCfi/Yh0todalTU 5SSx6f8SCX+81aQKowMSxHXo6P+6fhkyYBuXCUmq5Z1N9sk55d01ZGClvAbMRRGoMoGH gcon6I8w0KOhNw8fCpe8R8HlzMACV/AYa4aEe31sDY1kl+pmSER3BeEwmCBB3H/ungfk 0toW8Q4KQvF6sWmt1D546oBKFSHJo5kCt/JH+jHymWTI4eZJbmjoQv+4V1or0TL+QJfo MG6E4Yu488GYdJVEwS7+/P69JRDXsEddnvepttHaU2l6YXF6rtJeSw3NiFED1cbeRrc6 /79w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DstlAJNQjPGe7pSmqgqia1Q/dV1t6umgqLw2g1oz3Pg=; b=Ee+KrHSn/uQ8Kh8Mu2l99AX6jwHlHxVKZSIEDkY5odnRJDvYlnDwRekB8TXjDeG/TM PazWCJm+Js6DKDXS4asHMpOotpAMeiA0dwYDjbVgGEqNC00MN/FRk9cSdiMqM40NpoYK yupeiDPsit6sJWXibECTd0nK3nN2eHGHWG5xoq6fCLKwkYt1XmR3613O65r0wlMqrJun fQuHFy/j5PiHJbDseNlvMTUDixFeNsWuP9gNecuxgH/T0gC6Cv1sQLvVUqV0lw+0Vzfg UJR35aJt8Wc06obEJ6IDJ0f9tNJlFVGZ8dmI5BJE3N+BpdFeY2vwRsx4ztFFuE0eAoIS NhuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bs6jOih6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id m15-20020a6562cf000000b003822887ecc2si10562529pgv.161.2022.04.04.19.49.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 19:49:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bs6jOih6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CC285435375; Mon, 4 Apr 2022 18:13:25 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347389AbiDAOjb (ORCPT + 99 others); Fri, 1 Apr 2022 10:39:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348301AbiDAOdy (ORCPT ); Fri, 1 Apr 2022 10:33:54 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76577FEB; Fri, 1 Apr 2022 07:31:49 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 12E0961CC9; Fri, 1 Apr 2022 14:31:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 79D23C2BBE4; Fri, 1 Apr 2022 14:31:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1648823508; bh=hU3Q0C6ciYXg84ZqJPR6S0ruqfbCwlbRuBLw65YdvU0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bs6jOih6TcmUhl4qmZ8/LGinIdpmyZiyapbQL/pnNvKVOPbgl5MsK5kkNObn4h0zG IgXs4cvtbZPY2+ar1SfMwNE4O2IpZ6j7dUllHvK58bhJMNlfMCJrRvXstWB+2zd/fw IH6zp9tx9n4YVmdkChGqcS3fAo8t+TzFJfEHpWl5nYp6F368A/JK2xhGHW8nLlQYqe CFjfHnrVctwgWLMAVe89xKXS+n9wktGijKopHadq4msubbANdBM6f3F8+GiMMRfK+0 DxuH/2hZnotaS/mF4W9AFvLNz6g3YLcOIPVnmEV7edQRiTJecRMKYQEEwyVhqqca18 mizET6q0lhOig== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Michael Ellerman , Paul Menzel , Peter Zijlstra , Christophe Leroy , Sasha Levin , linuxppc-dev@lists.ozlabs.org Subject: [PATCH AUTOSEL 5.17 122/149] powerpc/code-patching: Pre-map patch area Date: Fri, 1 Apr 2022 10:25:09 -0400 Message-Id: <20220401142536.1948161-122-sashal@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220401142536.1948161-1-sashal@kernel.org> References: <20220401142536.1948161-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Michael Ellerman [ Upstream commit 591b4b268435f00d2f0b81f786c2c7bd5ef66416 ] Paul reported a warning with DEBUG_ATOMIC_SLEEP=y: BUG: sleeping function called from invalid context at include/linux/sched/mm.h:256 in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 1, name: swapper/0 preempt_count: 0, expected: 0 ... Call Trace: dump_stack_lvl+0xa0/0xec (unreliable) __might_resched+0x2f4/0x310 kmem_cache_alloc+0x220/0x4b0 __pud_alloc+0x74/0x1d0 hash__map_kernel_page+0x2cc/0x390 do_patch_instruction+0x134/0x4a0 arch_jump_label_transform+0x64/0x78 __jump_label_update+0x148/0x180 static_key_enable_cpuslocked+0xd0/0x120 static_key_enable+0x30/0x50 check_kvm_guest+0x60/0x88 pSeries_smp_probe+0x54/0xb0 smp_prepare_cpus+0x3e0/0x430 kernel_init_freeable+0x20c/0x43c kernel_init+0x30/0x1a0 ret_from_kernel_thread+0x5c/0x64 Peter pointed out that this is because do_patch_instruction() has disabled interrupts, but then map_patch_area() calls map_kernel_page() then hash__map_kernel_page() which does a sleeping memory allocation. We only see the warning in KVM guests with SMT enabled, which is not particularly common, or on other platforms if CONFIG_KPROBES is disabled, also not common. The reason we don't see it in most configurations is that another path that happens to have interrupts enabled has allocated the required page tables for us, eg. there's a path in kprobes init that does that. That's just pure luck though. As Christophe suggested, the simplest solution is to do a dummy map/unmap when we initialise the patching, so that any required page table levels are pre-allocated before the first call to do_patch_instruction(). This works because the unmap doesn't free any page tables that were allocated by the map, it just clears the PTE, leaving the page table levels there for the next map. Reported-by: Paul Menzel Debugged-by: Peter Zijlstra Suggested-by: Christophe Leroy Signed-off-by: Michael Ellerman Link: https://lore.kernel.org/r/20220223015821.473097-1-mpe@ellerman.id.au Signed-off-by: Sasha Levin --- arch/powerpc/lib/code-patching.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/powerpc/lib/code-patching.c b/arch/powerpc/lib/code-patching.c index 906d43463366..00c68e7fb11e 100644 --- a/arch/powerpc/lib/code-patching.c +++ b/arch/powerpc/lib/code-patching.c @@ -43,9 +43,14 @@ int raw_patch_instruction(u32 *addr, ppc_inst_t instr) #ifdef CONFIG_STRICT_KERNEL_RWX static DEFINE_PER_CPU(struct vm_struct *, text_poke_area); +static int map_patch_area(void *addr, unsigned long text_poke_addr); +static void unmap_patch_area(unsigned long addr); + static int text_area_cpu_up(unsigned int cpu) { struct vm_struct *area; + unsigned long addr; + int err; area = get_vm_area(PAGE_SIZE, VM_ALLOC); if (!area) { @@ -53,6 +58,15 @@ static int text_area_cpu_up(unsigned int cpu) cpu); return -1; } + + // Map/unmap the area to ensure all page tables are pre-allocated + addr = (unsigned long)area->addr; + err = map_patch_area(empty_zero_page, addr); + if (err) + return err; + + unmap_patch_area(addr); + this_cpu_write(text_poke_area, area); return 0; -- 2.34.1