Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp3512663ioo; Wed, 25 May 2022 02:14:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwBz1VUfP7Ph4r/0vwDZv3+pUxEvHCRMiR7CXdRpiisXV7Dvib5pnfPAPTgio8UbIfp7lMF X-Received: by 2002:a17:90a:c682:b0:1df:c4a8:5db6 with SMTP id n2-20020a17090ac68200b001dfc4a85db6mr9153251pjt.43.1653470043859; Wed, 25 May 2022 02:14:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653470043; cv=none; d=google.com; s=arc-20160816; b=Yn8wS0x5dHTHVsi2iLfc59UbAdFnaNq5fRbRDOZhkTu186cAYkm6EYSBayJNIKNDE6 /woYRsaSdYThaP90k1PgI8HB62qXVaTSRgGWd9AGV3lgRr3zi1bEK6yEV27eKL9Yo+DR qccGYA3bX6QsxALDDrIW0HvbYyUX7whHJO3aCHGzeR4Bm1c8cp0iUb6Ic346/JzNwte/ l1jPqGG4slYq396VMfKWHsGyo7JDY7iMQQQOJ3quf3uXUFJqegWO8DpQNWB7BJ2V9UGF D7B4m5jKoxGyesnqxsVa8GjhcdCuY99rBFjd9lHzBuRY/lYXLZ4bEs3sntpd2n7XGwJK zx2w== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=bTOdDL+5gJkxOynkTEC20187DY7Wiq4VqO4EHMe1dV4=; b=Ulac5aRnxHjxwC+btBGFfRPOynfp8pat3imfxpZa83wCecP0f/wFQwbOB8WC8s9Mir U33ASf+tx0o1Vx3u5i/FlW9TsRE68eJaqekcEfzLGDgN2vL2vCptvpEtKXsUU21dDbcn CmewZXd3Q+2veUuv5ZR15ReZWGaA7z+2gPUaa4gyxfcITlU4tU2eWRf0gSX322lloO3r r6lnoG/j4vAxR9Hjm406FwqpqfqFtQ32+VW8m32sYQoVTgk95UGBXpZfAaYkCtjVOuza SuNTyAaQ+Pq8/BtQS7DMEYtx2W+fTbOwtir8WOb3wBj1FD3cyyresClUK+S1/WYoCTBN /gXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=AkmOMbIf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e12-20020a63db0c000000b003f5d8756675si16477069pgg.371.2022.05.25.02.13.50; Wed, 25 May 2022 02:14:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=AkmOMbIf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239065AbiEXPsN (ORCPT + 99 others); Tue, 24 May 2022 11:48:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237597AbiEXPsJ (ORCPT ); Tue, 24 May 2022 11:48:09 -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 DA0256D4F3 for ; Tue, 24 May 2022 08:48:08 -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 7711C61710 for ; Tue, 24 May 2022 15:48:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A901CC34113; Tue, 24 May 2022 15:48:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1653407287; bh=BA+6CwXsTUOz/2MTwxnWu1ecx/28wj3mScYI844rIM4=; h=From:To:Cc:Subject:Date:From; b=AkmOMbIfumvCekpkT5mJRQ4izpUicDyQz1NlZSlJ/oEtpMYt+J7YvyFlvrCkGFmen saYRDsr5jkTHq8cgtrJpHRjBxfvnjgpmIIlP4PdBlvnaNS9kzeRxaTEz57pxI7TBst MAXjdRIwzR2o1lLryWL6D6WRUGD146J0C8KwEdlrefnnqU+fOptWBkytb+HnSMJ/dB 6AOyy+TRZGKdkYfw7VGoQ36rzP2l+rQpIQVGCI9n8hCr7lmCzpdbQD9fM5p0VpJHZu 30S7xfRSMOQjq6kMmV3mNzYZ7QwGnBBoqGO5tujUpk0B5ivaC+jpmH+KwwczGMF9+9 yvsIZyP04BFJQ== From: Jisheng Zhang To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Arnd Bergmann Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v4] riscv: add irq stack support Date: Tue, 24 May 2022 23:39:28 +0800 Message-Id: <20220524153928.2488-1-jszhang@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Currently, IRQs are still handled on the kernel stack of the current task on riscv platforms. If the task has a deep call stack at the time of interrupt, and handling the interrupt also requires a deep stack, it's possible to see stack overflow. Before this patch, the stack_max_size of a v5.17-rc1 kernel running on a lichee RV board gave: ~ # cat /sys/kernel/debug/tracing/stack_max_size 3736 After this patch, ~ # cat /sys/kernel/debug/tracing/stack_max_size 3176 We reduce the max kernel stack usage by 560 bytes! Signed-off-by: Jisheng Zhang --- since v3: - Always use vmalloc to allocate irq stack. Thank Arnd. since v2: - rebase on v5.18-rcN - update commit msg, I.E remove the "it's possible to reduce the THREAD_SIZE to 8KB for RV64 platforms..." since v1: - add __ro_after_init to the irq_stack[] array. arch/riscv/include/asm/thread_info.h | 1 + arch/riscv/kernel/asm-offsets.c | 2 ++ arch/riscv/kernel/entry.S | 33 +++++++++++++++++++++++++--- arch/riscv/kernel/irq.c | 12 ++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index 74d888c8d631..98ea73721a0b 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -25,6 +25,7 @@ #endif #define THREAD_SIZE (PAGE_SIZE << THREAD_SIZE_ORDER) +#define IRQ_STACK_SIZE THREAD_SIZE /* * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by * checking sp & (1 << THREAD_SHIFT), which we can do cheaply in the entry diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index df9444397908..9e32748af0e8 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -37,6 +37,8 @@ void asm_offsets(void) OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_count); OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp); OFFSET(TASK_TI_USER_SP, task_struct, thread_info.user_sp); + OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu); + OFFSET(TASK_STACK, task_struct, stack); OFFSET(TASK_THREAD_F0, task_struct, thread.fstate.f[0]); OFFSET(TASK_THREAD_F1, task_struct, thread.fstate.f[1]); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index c8b9ce274b9a..e91cae183ef4 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -126,12 +126,39 @@ skip_context_tracking: */ bge s4, zero, 1f - la ra, ret_from_exception + /* preserve the sp */ + move s0, sp - /* Handle interrupts */ move a0, sp /* pt_regs */ + + /* + * Compare sp with the base of the task stack. + * If the top ~(THREAD_SIZE - 1) bits match, we are on a task stack, + * and should switch to the irq stack. + */ + REG_L t0, TASK_STACK(tp) + xor t0, t0, s0 + li t1, ~(THREAD_SIZE - 1) + and t0, t0, t1 + bnez t0, 2f + + la t1, irq_stack + REG_L t2, TASK_TI_CPU(tp) + slli t2, t2, RISCV_LGPTR + add t1, t1, t2 + REG_L t2, 0(t1) + li t1, IRQ_STACK_SIZE + /* switch to the irq stack */ + add sp, t2, t1 + +2: + /* Handle interrupts */ la a1, generic_handle_arch_irq - jr a1 + jalr a1 + + /* Restore sp */ + move sp, s0 + j ret_from_exception 1: /* * Exceptions run with interrupts enabled or disabled depending on the diff --git a/arch/riscv/kernel/irq.c b/arch/riscv/kernel/irq.c index 7207fa08d78f..6b26960e2e17 100644 --- a/arch/riscv/kernel/irq.c +++ b/arch/riscv/kernel/irq.c @@ -10,6 +10,8 @@ #include #include +void *irq_stack[NR_CPUS] __ro_after_init; + int arch_show_interrupts(struct seq_file *p, int prec) { show_ipi_stats(p, prec); @@ -18,7 +20,17 @@ int arch_show_interrupts(struct seq_file *p, int prec) void __init init_IRQ(void) { + int cpu; + irqchip_init(); if (!handle_arch_irq) panic("No interrupt controller found."); + + for_each_possible_cpu(cpu) { + void *s = __vmalloc_node(IRQ_STACK_SIZE, THREAD_ALIGN, + THREADINFO_GFP, cpu_to_node(cpu), + __builtin_return_address(0)); + + irq_stack[cpu] = s; + } } -- 2.34.1