Received: by 2002:a05:7412:bc1a:b0:d7:7d3a:4fe2 with SMTP id ki26csp310816rdb; Sat, 19 Aug 2023 02:46:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHpu4x4xvMa29sq2t5s4zyco/4EyUSYqy3/jbkTANvi7EtquZvB/LtHde5NUDe81TuRwhda X-Received: by 2002:a05:6a21:6da5:b0:137:3eba:b81f with SMTP id wl37-20020a056a216da500b001373ebab81fmr2339362pzb.3.1692438375696; Sat, 19 Aug 2023 02:46:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692438375; cv=none; d=google.com; s=arc-20160816; b=RiubnNY1zdEQw1lyL+6W3pSEYvAHgglQzV/exyTVAfj5ZF74EV3GBKAITPZIk77HWq ZFsmtAx7AHrVwA0yn4FeSd+0SrhQFrDBEuJQmTbQKJzEbsDT2N82pwl3AVETXuhRKp9U g8qH2wvFDbYKx442Q0eLV19xnC3e27ve6PaGU7h+s/A+CVxwUAsnRs8ufVGtfK+jYRks TXRYxr0Eaq+FnmlRKbdmFGAt6QAVYZNGbhb9AYIbPEgEFHq2q/NkTCD2AmvQ+BtgnPem 3DXBBcJocbJY5YQePpV641kKIdq/220BcZS1h+AIgIQy7fM1TsaesT+0T9Ui7r01DT3E 0JHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=WrfRqvXS/YQAQD3rvUURRhi4O4V3buFLgz+cT4E6CXY=; fh=EU0TIckDJHUB8oxmYASKGRuQqUEMHHFj6AqpMEFrOc4=; b=K7GiGC5B5cH24XnEwfpZxkYBxVgVQKaRGOfEC4IpEjLXKilR4yeQGEebJ7HVOSfTDQ LFicBFEESOPcYjYp9Tfxwe/Y730xPS9r7LevNWrwL2XX2h4sN11ZwUW0yclzGcWa6eyX jgqcMMGo27V9Qckki6wkraiAquAaC/oCuSf29wRwlVAsVKK9RcWvDOyBYjMmCay20Ewd AA1nReVW3Be/shtBMiugFyEuw8MEDkjaZkU31YemYB+fJ58NgrwfW78kk78Bzn1FpbL/ qxtHi6HrUCyzU+FtWc5KAnJ9jcuipSdKL3Kq75M+CxBK4hRtG1CB1hOquZS9P94+UiPh +V2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=0b0AfFag; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id cm21-20020a056a00339500b00688bd8e5335si3121369pfb.285.2023.08.19.02.46.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 02:46:15 -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=@google.com header.s=20221208 header.b=0b0AfFag; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 825F4301DE; Sat, 19 Aug 2023 01:28:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238103AbjHOUfJ (ORCPT + 99 others); Tue, 15 Aug 2023 16:35:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238051AbjHOUew (ORCPT ); Tue, 15 Aug 2023 16:34:52 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8657D1BF7 for ; Tue, 15 Aug 2023 13:34:51 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-26b67f0a773so2086631a91.1 for ; Tue, 15 Aug 2023 13:34:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692131691; x=1692736491; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WrfRqvXS/YQAQD3rvUURRhi4O4V3buFLgz+cT4E6CXY=; b=0b0AfFag2OqtlzKfyBXLWRGSF8gW1eMeleZQdhIEnHmK7L+mnBqsuJuqNSwV6rtOEU u0s8fw9OKaQxUqiJRpnU0Hj7p2gKX/LzNZrGl2doSZ/b3K7fqQBZZ0gtHwr3NNrZmsmB bElC4ZVwGPAE2nH9h5njd1+8dir9Iv7EJaT4Lvaplu8can7LpTu0q410aYDm4oygCKG6 CfmWyM/5WXnsrFI9sYk8nJqgbKJWDH+e0Ftx6cUw7Ad5/bw0dCu4KZjUmFeQGgW27LQW b1+64f0E00cHsDKGcITMxivXieyFjcRQHcbigwhvU5bczQjxjY4/w6pRvIEHpu+0KRhz IQEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692131691; x=1692736491; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WrfRqvXS/YQAQD3rvUURRhi4O4V3buFLgz+cT4E6CXY=; b=Yv/Qg0/SIqCTPe7isSDEtHWY2kdA8JGqpdc5LSKL8vda6AN55j12yft/vHr7HuNROq SHIb0KW1oYlDa5CZjne/wQPMzoH8pbn3TuWcFv7HDE8L+yHFIFwy7RcwF9V6cg4FCLy3 o5s/Otzw0jLW3UcsDAyWQrA18hDgW0RUF7XCIifRV6t7VYOyIxIORce/0kPQ7lKcqosJ ciOID6MncZibfqGBtkFhZ4qkKZb8MrkSDFq71dYg4a5boMIrK0pL1W1anuFn8F7AblQq NhZJPhqb/IzxIqA0GyRMTpaErbdbUvXcpMTs5vE43JK9PdhY3SKn0WLILX59lzY87B52 YI6Q== X-Gm-Message-State: AOJu0YyBGyGY6anBY3rNGETt6idVIyMV++VARHC55EgjiDGfCLtdJExk nTohIGa/yeJLyVdw1lpXV6VMbGt6yvPNKbej0H4= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:90a:d502:b0:26b:5fe2:5892 with SMTP id t2-20020a17090ad50200b0026b5fe25892mr1185559pju.6.1692131690966; Tue, 15 Aug 2023 13:34:50 -0700 (PDT) Date: Tue, 15 Aug 2023 20:34:45 +0000 In-Reply-To: <20230815203442.1608773-8-samitolvanen@google.com> Mime-Version: 1.0 References: <20230815203442.1608773-8-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5593; i=samitolvanen@google.com; h=from:subject; bh=fOkgWkASfAqgpBfAvGFiQ8Ap5BTE+sMjcVhFVqoevU4=; b=owEB7QES/pANAwAKAUy19oSLvFbuAcsmYgBk2+FiD9deMgIJ3fhkaSc5TjCFw9aOJgO3lojDl IJKQL6WcO2JAbMEAAEKAB0WIQQ1zPtjsoPW0663g5RMtfaEi7xW7gUCZNvhYgAKCRBMtfaEi7xW 7uETC/oDWTmoCmprkVOzJT880FOolG/86ApcG+2Vp5pFm9jTSXw0uhsv0QJYCKBTupTIjofrNuk yID9Hm9vbntt8SHaS+jwKoheRWZDOAdh0tJuWru56EB0QyTPLLuI6f2NTAFPPDF7/N0MEfFY9Q2 73sbckQRqQxt7tkF7oxFDkS6SMsogOzT8IZ2NuTszPpFCpFOybsEIX+sds97QqG22w0LjKn120J ABr9wCeGmcRNk7+5igryjV3nOw6/uMjWyZwGKIyzWrC3FFDqbHm5AuDcIgkhUd0DK5YK13HFgls qtnla0SK6MtOkM/Zg37Tt4SMgUlX9a75WKGDlVjX1j6KQSvU3TGRjp1Lyas587+RFyuvHXVjdX1 qYlEOUKf8qQjIcqiNnCqllRAsCjzds/YrdVFgoHxrkzeXt+aq89oaE2tLG8gh3OC9y2pQ04Bpz8 vLLi1qj7Wm/3TWsrEydTvIulNO8xWEYLDag4+LI+L8CYYpBAwjrgoVvB0cCcF1VGQ0U5Y= X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815203442.1608773-10-samitolvanen@google.com> Subject: [PATCH v2 2/6] riscv: Deduplicate IRQ stack switching From: Sami Tolvanen To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Kees Cook Cc: Guo Ren , Deepak Gupta , Nathan Chancellor , Nick Desaulniers , Fangrui Song , linux-riscv@lists.infradead.org, llvm@lists.linux.dev, linux-kernel@vger.kernel.org, Sami Tolvanen Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL 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 With CONFIG_IRQ_STACKS, we switch to a separate per-CPU IRQ stack before calling handle_riscv_irq or __do_softirq. We currently have duplicate inline assembly snippets for stack switching in both code paths. Now that we can access per-CPU variables in assembly, implement call_on_irq_stack in assembly, and use that instead of redudant inline assembly. Signed-off-by: Sami Tolvanen --- arch/riscv/include/asm/asm.h | 5 +++++ arch/riscv/include/asm/irq_stack.h | 3 +++ arch/riscv/kernel/entry.S | 32 ++++++++++++++++++++++++++++++ arch/riscv/kernel/irq.c | 32 ++++++++---------------------- arch/riscv/kernel/traps.c | 29 ++++----------------------- 5 files changed, 52 insertions(+), 49 deletions(-) diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h index bfb4c26f113c..8e446be2d57c 100644 --- a/arch/riscv/include/asm/asm.h +++ b/arch/riscv/include/asm/asm.h @@ -104,6 +104,11 @@ .endm #endif /* CONFIG_SMP */ +.macro load_per_cpu dst ptr tmp + asm_per_cpu \dst \ptr \tmp + REG_L \dst, 0(\dst) +.endm + /* save all GPs except x1 ~ x5 */ .macro save_from_x6_to_x31 REG_S x6, PT_T1(sp) diff --git a/arch/riscv/include/asm/irq_stack.h b/arch/riscv/include/asm/irq_stack.h index e4042d297580..6441ded3b0cf 100644 --- a/arch/riscv/include/asm/irq_stack.h +++ b/arch/riscv/include/asm/irq_stack.h @@ -12,6 +12,9 @@ DECLARE_PER_CPU(ulong *, irq_stack_ptr); +asmlinkage void call_on_irq_stack(struct pt_regs *regs, + void (*func)(struct pt_regs *)); + #ifdef CONFIG_VMAP_STACK /* * To ensure that VMAP'd stack overflow detection works correctly, all VMAP'd diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 3d11aa3af105..39875f5e08a6 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -218,6 +218,38 @@ SYM_CODE_START(ret_from_fork) tail syscall_exit_to_user_mode SYM_CODE_END(ret_from_fork) +#ifdef CONFIG_IRQ_STACKS +/* + * void call_on_irq_stack(struct pt_regs *regs, + * void (*func)(struct pt_regs *)); + * + * Calls func(regs) using the per-CPU IRQ stack. + */ +SYM_FUNC_START(call_on_irq_stack) + /* Create a frame record to save ra and s0 (fp) */ + addi sp, sp, -RISCV_SZPTR + REG_S ra, (sp) + addi sp, sp, -RISCV_SZPTR + REG_S s0, (sp) + addi s0, sp, 2*RISCV_SZPTR + + /* Switch to the per-CPU IRQ stack and call the handler */ + load_per_cpu t0, irq_stack_ptr, t1 + li t1, IRQ_STACK_SIZE + add sp, t0, t1 + jalr a1 + + /* Switch back to the thread stack and restore ra and s0 */ + addi sp, s0, -2*RISCV_SZPTR + REG_L s0, (sp) + addi sp, sp, RISCV_SZPTR + REG_L ra, (sp) + addi sp, sp, RISCV_SZPTR + + ret +SYM_FUNC_END(call_on_irq_stack) +#endif /* CONFIG_IRQ_STACKS */ + /* * Integer register context switch * The callee-saved registers must be saved and restored. diff --git a/arch/riscv/kernel/irq.c b/arch/riscv/kernel/irq.c index d0577cc6a081..95dafdcbd135 100644 --- a/arch/riscv/kernel/irq.c +++ b/arch/riscv/kernel/irq.c @@ -61,32 +61,16 @@ static void init_irq_stacks(void) #endif /* CONFIG_VMAP_STACK */ #ifdef CONFIG_HAVE_SOFTIRQ_ON_OWN_STACK +static void ___do_softirq(struct pt_regs *regs) +{ + __do_softirq(); +} + void do_softirq_own_stack(void) { -#ifdef CONFIG_IRQ_STACKS - if (on_thread_stack()) { - ulong *sp = per_cpu(irq_stack_ptr, smp_processor_id()) - + IRQ_STACK_SIZE/sizeof(ulong); - __asm__ __volatile( - "addi sp, sp, -"RISCV_SZPTR "\n" - REG_S" ra, (sp) \n" - "addi sp, sp, -"RISCV_SZPTR "\n" - REG_S" s0, (sp) \n" - "addi s0, sp, 2*"RISCV_SZPTR "\n" - "move sp, %[sp] \n" - "call __do_softirq \n" - "addi sp, s0, -2*"RISCV_SZPTR"\n" - REG_L" s0, (sp) \n" - "addi sp, sp, "RISCV_SZPTR "\n" - REG_L" ra, (sp) \n" - "addi sp, sp, "RISCV_SZPTR "\n" - : - : [sp] "r" (sp) - : "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", - "t0", "t1", "t2", "t3", "t4", "t5", "t6", - "memory"); - } else -#endif + if (on_thread_stack()) + call_on_irq_stack(NULL, ___do_softirq); + else __do_softirq(); } #endif /* CONFIG_HAVE_SOFTIRQ_ON_OWN_STACK */ diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index deb2144d9143..83319b6816da 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -350,31 +350,10 @@ static void noinstr handle_riscv_irq(struct pt_regs *regs) asmlinkage void noinstr do_irq(struct pt_regs *regs) { irqentry_state_t state = irqentry_enter(regs); -#ifdef CONFIG_IRQ_STACKS - if (on_thread_stack()) { - ulong *sp = per_cpu(irq_stack_ptr, smp_processor_id()) - + IRQ_STACK_SIZE/sizeof(ulong); - __asm__ __volatile( - "addi sp, sp, -"RISCV_SZPTR "\n" - REG_S" ra, (sp) \n" - "addi sp, sp, -"RISCV_SZPTR "\n" - REG_S" s0, (sp) \n" - "addi s0, sp, 2*"RISCV_SZPTR "\n" - "move sp, %[sp] \n" - "move a0, %[regs] \n" - "call handle_riscv_irq \n" - "addi sp, s0, -2*"RISCV_SZPTR"\n" - REG_L" s0, (sp) \n" - "addi sp, sp, "RISCV_SZPTR "\n" - REG_L" ra, (sp) \n" - "addi sp, sp, "RISCV_SZPTR "\n" - : - : [sp] "r" (sp), [regs] "r" (regs) - : "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", - "t0", "t1", "t2", "t3", "t4", "t5", "t6", - "memory"); - } else -#endif + + if (IS_ENABLED(CONFIG_IRQ_STACKS) && on_thread_stack()) + call_on_irq_stack(regs, handle_riscv_irq); + else handle_riscv_irq(regs); irqentry_exit(regs, state); -- 2.41.0.694.ge786442a9b-goog