Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1039160ybk; Fri, 15 May 2020 22:26:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBFC5jBRpr2x1sWWhXfBnzUKMXJOqYvITqtu+AN5m4SzNDepW4SZZlU+SkEe3XdM+eHU3v X-Received: by 2002:aa7:d455:: with SMTP id q21mr5557389edr.235.1589606775834; Fri, 15 May 2020 22:26:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589606775; cv=none; d=google.com; s=arc-20160816; b=y+jYI39wuY6TDic/P6qWlDjpAVjPBalf2bzUbjyVrx2y1wY14HhJXI8aiJRU15hvQ9 qMtde2xhdljOeXnnk0IZbuWbyK+96XetwExWjafIodesPf/zo2h+9MnGJtQwKRfV6bnU K4UgBZqHroEaIofvDd+/IF5WmJRavl7cRqBidOa43JFeUbr9sd+t08zfRbWJ0Vy0X3pp ZcYTUOMpI6XlOQ0v6O07ssvTmBfDK44H802nM6EoCJe6LUpQIvdS93ySMOe3M/aoyoY+ qWu3GcG4GV4+NGVP3sk99N6DlBB7rvUob8aCyU4hBIIE/KMo/BAyTx6hesGyY/YLrv7H 89tA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4pZo3j1bhay29n6OFcWuLjhRCgSzwajlMBMjTCKDNGo=; b=riUm1pW5Ca81rRjmhc7NMJrtX3f9WC/NghDmUwrhD+c2NOnXSw8RXkbtQx5nxpv7nn al3JhtO82DWvwPZ5C9bJHnoNGZx0P9Pb0HVrPw092LK4+vJ2oQEndYgndAawsaAJdlDW 8d2x5udvk67JkQZkQ0lkXjcP0Wq03bV7FeIgN9fRhi8JADAW7VlSbFdkjh9fHpJ+PruW 8cmFGkf+ZydGXqQce2cfEfPcaGWOZHPe9VCBkduWNuNiQNMAS3T+1q8JCNdyU2unIqWi Z/nVUfEA1hQuXOna7l65WMrAPw4OxyttKdyHv2Y5ca/2hydxIWopAh04SqvS6z2doMPR p+ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="sDj/IEGS"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j2si2389944edk.17.2020.05.15.22.25.52; Fri, 15 May 2020 22:26:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="sDj/IEGS"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726422AbgEPFWr (ORCPT + 99 others); Sat, 16 May 2020 01:22:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725275AbgEPFWr (ORCPT ); Sat, 16 May 2020 01:22:47 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3406C061A0C for ; Fri, 15 May 2020 22:22:46 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id b1so3924757qtt.1 for ; Fri, 15 May 2020 22:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4pZo3j1bhay29n6OFcWuLjhRCgSzwajlMBMjTCKDNGo=; b=sDj/IEGSSLvmv1wI10+xju/KDYTvQ9Ur9/Rw2JTyrvc1ZC6KyDTdANZDPdAfnvhkNH /1POVn6iNEi0k5S+OejA3SeL8TSDjI2a+ghVecjFJ6Sy78XEPiFNl2HgtzBDFayLFmIl WIpgb+KzDri12bXTXdA6e+rM6aTRZvOVKCOVjn3UM8o8nXurozIiTE3XRnbur5ID3qMP 0izO8S3YP+MYsCE5iyFeHVfWSvUPbVFSv+JfLMqYeXYgISuUOxJ3fNOkBthWBrOwINeY tLKjifu4EKvn6GIOF88f2XsEUBNkmjH4a64X8SO3/WeCEdRRET5K5TMHIrRUWaXu0JFH GMJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4pZo3j1bhay29n6OFcWuLjhRCgSzwajlMBMjTCKDNGo=; b=sYs/lh6VtIHve5Wzt7PYlvBeOtM5FO06S0hKDimfGoHn+1D8lh6MZ1JzC4GSjZ8vUv T6zGwO0rod4xt3U05PF3TEvYmX3TBi2ng+ZRGYBZeKFyRyOfua8P+1Ik8epRfirU8i1e SKMPlTzC7y1gsfJ+XefOPY1B7djtIg2/P62mj+fuXSNDHWatQ+NlXLN6f5+NKCbnOkBm eStY+VQUVsGqNlD4WS/SRtzy4Z+U9XdTuYJv289X3pJ7qgCNiM1xT+XeaMxrHnI1hBnz hLh23wyaP70mEVw3QnARxLbl+uLeolILWYTTqxU7Rmsp/phWEJQNIzgmCo0PvkE64xpa X3nw== X-Gm-Message-State: AOAM53011rxwKUcsnkplcY04g3U5+gvGTB655Q7pzmbNJtX+Aben88m6 HBACwNFkRWXMYpoACfGleNdaH9YW X-Received: by 2002:ac8:6055:: with SMTP id k21mr6806625qtm.294.1589606566028; Fri, 15 May 2020 22:22:46 -0700 (PDT) Received: from LeoBras.aus.stglabs.ibm.com (179-125-143-209.dynamic.desktop.com.br. [179.125.143.209]) by smtp.gmail.com with ESMTPSA id u20sm3866721qtj.97.2020.05.15.22.22.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 May 2020 22:22:45 -0700 (PDT) From: Leonardo Bras To: Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Nicholas Piggin , Thomas Gleixner , Leonardo Bras , Allison Randal , Greg Kroah-Hartman , Thiago Jung Bauermann , Anshuman Khandual , Daniel Axtens , Nathan Lynch , "Gautham R. Shenoy" , Nadav Amit Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 2/2] powerpc/rtas: Implement reentrant rtas call Date: Sat, 16 May 2020 02:21:37 -0300 Message-Id: <20200516052137.175881-3-leobras.c@gmail.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20200516052137.175881-1-leobras.c@gmail.com> References: <20200516052137.175881-1-leobras.c@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implement rtas_call_reentrant() for reentrant rtas-calls: "ibm,int-on", "ibm,int-off",ibm,get-xive" and "ibm,set-xive". On LoPAPR Version 1.1 (March 24, 2016), from 7.3.10.1 to 7.3.10.4, items 2 and 3 say: 2 - For the PowerPC External Interrupt option: The * call must be reentrant to the number of processors on the platform. 3 - For the PowerPC External Interrupt option: The * argument call buffer for each simultaneous call must be physically unique. So, these rtas-calls can be called in a lockless way, if using a different buffer for each cpu doing such rtas call. For this, it was suggested to add the buffer (struct rtas_args) in the PACA struct, so each cpu can have it's own buffer. The PACA struct received a pointer to rtas buffer, which is allocated in the memory range available to rtas 32-bit. Reentrant rtas calls are useful to avoid deadlocks in crashing, where rtas-calls are needed, but some other thread crashed holding the rtas.lock. This is a backtrace of a deadlock from a kdump testing environment: #0 arch_spin_lock #1 lock_rtas () #2 rtas_call (token=8204, nargs=1, nret=1, outputs=0x0) #3 ics_rtas_mask_real_irq (hw_irq=4100) #4 machine_kexec_mask_interrupts #5 default_machine_crash_shutdown #6 machine_crash_shutdown #7 __crash_kexec #8 crash_kexec #9 oops_end Signed-off-by: Leonardo Bras --- arch/powerpc/include/asm/paca.h | 2 ++ arch/powerpc/include/asm/rtas.h | 1 + arch/powerpc/kernel/paca.c | 20 +++++++++++ arch/powerpc/kernel/rtas.c | 52 +++++++++++++++++++++++++++++ arch/powerpc/sysdev/xics/ics-rtas.c | 22 ++++++------ 5 files changed, 86 insertions(+), 11 deletions(-) diff --git a/arch/powerpc/include/asm/paca.h b/arch/powerpc/include/asm/paca.h index e3cc9eb9204d..87cd9c2220cc 100644 --- a/arch/powerpc/include/asm/paca.h +++ b/arch/powerpc/include/asm/paca.h @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -270,6 +271,7 @@ struct paca_struct { #ifdef CONFIG_MMIOWB struct mmiowb_state mmiowb_state; #endif + struct rtas_args *reentrant_args; } ____cacheline_aligned; extern void copy_mm_to_paca(struct mm_struct *mm); diff --git a/arch/powerpc/include/asm/rtas.h b/arch/powerpc/include/asm/rtas.h index c35c5350b7e4..fa7509c85881 100644 --- a/arch/powerpc/include/asm/rtas.h +++ b/arch/powerpc/include/asm/rtas.h @@ -236,6 +236,7 @@ extern struct rtas_t rtas; extern int rtas_token(const char *service); extern int rtas_service_present(const char *service); extern int rtas_call(int token, int, int, int *, ...); +int rtas_call_reentrant(int token, int nargs, int nret, int *outputs, ...); void rtas_call_unlocked(struct rtas_args *args, int token, int nargs, int nret, ...); extern void __noreturn rtas_restart(char *cmd); diff --git a/arch/powerpc/kernel/paca.c b/arch/powerpc/kernel/paca.c index 3f91ccaa9c74..88c9b61489fc 100644 --- a/arch/powerpc/kernel/paca.c +++ b/arch/powerpc/kernel/paca.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "setup.h" @@ -164,6 +165,23 @@ static struct slb_shadow * __init new_slb_shadow(int cpu, unsigned long limit) #endif /* CONFIG_PPC_BOOK3S_64 */ +/** + * new_rtas_args() - Allocates rtas args + * @cpu: CPU number + * @limit: Memory limit for this allocation + * + * Allocates a struct rtas_args and return it's pointer. + * + * Return: Pointer to allocated rtas_args + */ +static struct rtas_args * __init new_rtas_args(int cpu, unsigned long limit) +{ + limit = min_t(unsigned long, limit, RTAS_INSTANTIATE_MAX); + + return alloc_paca_data(sizeof(struct rtas_args), L1_CACHE_BYTES, + limit, cpu); +} + /* The Paca is an array with one entry per processor. Each contains an * lppaca, which contains the information shared between the * hypervisor and Linux. @@ -202,6 +220,7 @@ void __init __nostackprotector initialise_paca(struct paca_struct *new_paca, int /* For now -- if we have threads this will be adjusted later */ new_paca->tcd_ptr = &new_paca->tcd; #endif + new_paca->reentrant_args = NULL; } /* Put the paca pointer into r13 and SPRG_PACA */ @@ -274,6 +293,7 @@ void __init allocate_paca(int cpu) #ifdef CONFIG_PPC_BOOK3S_64 paca->slb_shadow_ptr = new_slb_shadow(cpu, limit); #endif + paca->reentrant_args = new_rtas_args(cpu, limit); paca_struct_size += sizeof(struct paca_struct); } diff --git a/arch/powerpc/kernel/rtas.c b/arch/powerpc/kernel/rtas.c index c5fa251b8950..6e22eb4fc0e7 100644 --- a/arch/powerpc/kernel/rtas.c +++ b/arch/powerpc/kernel/rtas.c @@ -41,6 +41,7 @@ #include #include #include +#include /* This is here deliberately so it's only used in this file */ void enter_rtas(unsigned long); @@ -483,6 +484,57 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...) } EXPORT_SYMBOL(rtas_call); +/** + * rtas_call_reentrant() - Used for reentrant rtas calls + * @token: Token for desired reentrant RTAS call + * @nargs: Number of Input Parameters + * @nret: Number of Output Parameters + * @outputs: Array of outputs + * @...: Inputs for desired RTAS call + * + * According to LoPAR documentation, only "ibm,int-on", "ibm,int-off", + * "ibm,get-xive" and "ibm,set-xive" are currently reentrant. + * Reentrant calls need their own rtas_args buffer, so not using rtas.args, but + * PACA one instead. + * + * Return: -1 on error, + * First output value of RTAS call if (nret > 0), + * 0 otherwise, + */ + +int rtas_call_reentrant(int token, int nargs, int nret, int *outputs, ...) +{ + va_list list; + struct rtas_args *args; + unsigned long flags; + int i, ret = 0; + + if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE) + return -1; + + local_irq_save(flags); + preempt_disable(); + + /* We use the per-cpu (PACA) rtas args buffer */ + args = local_paca->reentrant_args; + + va_start(list, outputs); + va_rtas_call_unlocked(args, token, nargs, nret, list); + va_end(list); + + if (nret > 1 && outputs) + for (i = 0; i < nret - 1; ++i) + outputs[i] = be32_to_cpu(args->rets[i + 1]); + + if (nret > 0) + ret = be32_to_cpu(args->rets[0]); + + local_irq_restore(flags); + preempt_enable(); + + return ret; +} + /* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds. */ diff --git a/arch/powerpc/sysdev/xics/ics-rtas.c b/arch/powerpc/sysdev/xics/ics-rtas.c index 6aabc74688a6..4cf18000f07c 100644 --- a/arch/powerpc/sysdev/xics/ics-rtas.c +++ b/arch/powerpc/sysdev/xics/ics-rtas.c @@ -50,8 +50,8 @@ static void ics_rtas_unmask_irq(struct irq_data *d) server = xics_get_irq_server(d->irq, irq_data_get_affinity_mask(d), 0); - call_status = rtas_call(ibm_set_xive, 3, 1, NULL, hw_irq, server, - DEFAULT_PRIORITY); + call_status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, hw_irq, + server, DEFAULT_PRIORITY); if (call_status != 0) { printk(KERN_ERR "%s: ibm_set_xive irq %u server %x returned %d\n", @@ -60,7 +60,7 @@ static void ics_rtas_unmask_irq(struct irq_data *d) } /* Now unmask the interrupt (often a no-op) */ - call_status = rtas_call(ibm_int_on, 1, 1, NULL, hw_irq); + call_status = rtas_call_reentrant(ibm_int_on, 1, 1, NULL, hw_irq); if (call_status != 0) { printk(KERN_ERR "%s: ibm_int_on irq=%u returned %d\n", __func__, hw_irq, call_status); @@ -91,7 +91,7 @@ static void ics_rtas_mask_real_irq(unsigned int hw_irq) if (hw_irq == XICS_IPI) return; - call_status = rtas_call(ibm_int_off, 1, 1, NULL, hw_irq); + call_status = rtas_call_reentrant(ibm_int_off, 1, 1, NULL, hw_irq); if (call_status != 0) { printk(KERN_ERR "%s: ibm_int_off irq=%u returned %d\n", __func__, hw_irq, call_status); @@ -99,8 +99,8 @@ static void ics_rtas_mask_real_irq(unsigned int hw_irq) } /* Have to set XIVE to 0xff to be able to remove a slot */ - call_status = rtas_call(ibm_set_xive, 3, 1, NULL, hw_irq, - xics_default_server, 0xff); + call_status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, hw_irq, + xics_default_server, 0xff); if (call_status != 0) { printk(KERN_ERR "%s: ibm_set_xive(0xff) irq=%u returned %d\n", __func__, hw_irq, call_status); @@ -131,7 +131,7 @@ static int ics_rtas_set_affinity(struct irq_data *d, if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS) return -1; - status = rtas_call(ibm_get_xive, 1, 3, xics_status, hw_irq); + status = rtas_call_reentrant(ibm_get_xive, 1, 3, xics_status, hw_irq); if (status) { printk(KERN_ERR "%s: ibm,get-xive irq=%u returns %d\n", @@ -146,8 +146,8 @@ static int ics_rtas_set_affinity(struct irq_data *d, return -1; } - status = rtas_call(ibm_set_xive, 3, 1, NULL, - hw_irq, irq_server, xics_status[1]); + status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, + hw_irq, irq_server, xics_status[1]); if (status) { printk(KERN_ERR "%s: ibm,set-xive irq=%u returns %d\n", @@ -179,7 +179,7 @@ static int ics_rtas_map(struct ics *ics, unsigned int virq) return -EINVAL; /* Check if RTAS knows about this interrupt */ - rc = rtas_call(ibm_get_xive, 1, 3, status, hw_irq); + rc = rtas_call_reentrant(ibm_get_xive, 1, 3, status, hw_irq); if (rc) return -ENXIO; @@ -198,7 +198,7 @@ static long ics_rtas_get_server(struct ics *ics, unsigned long vec) { int rc, status[2]; - rc = rtas_call(ibm_get_xive, 1, 3, status, vec); + rc = rtas_call_reentrant(ibm_get_xive, 1, 3, status, vec); if (rc) return -1; return status[0]; -- 2.25.4