Skip to content

Commit b664db8

Browse files
LeoBrasmpe
authored andcommitted
powerpc/rtas: Implement reentrant rtas call
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 raspberrypi#1 lock_rtas () raspberrypi#2 rtas_call (token=8204, nargs=1, nret=1, outputs=0x0) raspberrypi#3 ics_rtas_mask_real_irq (hw_irq=4100) raspberrypi#4 machine_kexec_mask_interrupts raspberrypi#5 default_machine_crash_shutdown raspberrypi#6 machine_crash_shutdown raspberrypi#7 __crash_kexec raspberrypi#8 crash_kexec raspberrypi#9 oops_end Signed-off-by: Leonardo Bras <leobras.c@gmail.com> [mpe: Move under #ifdef PSERIES to avoid build breakage] Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/20200518234245.200672-3-leobras.c@gmail.com
1 parent 783a015 commit b664db8

5 files changed

Lines changed: 98 additions & 11 deletions

File tree

‎arch/powerpc/include/asm/paca.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <asm/hmi.h>
3030
#include <asm/cpuidle.h>
3131
#include <asm/atomic.h>
32+
#include <asm/rtas-types.h>
3233

3334
#include <asm-generic/mmiowb_types.h>
3435

@@ -256,6 +257,7 @@ struct paca_struct {
256257
u64 l1d_flush_size;
257258
#endif
258259
#ifdef CONFIG_PPC_PSERIES
260+
struct rtas_args *rtas_args_reentrant;
259261
u8 *mce_data_buf; /* buffer to hold per cpu rtas errlog */
260262
#endif /* CONFIG_PPC_PSERIES */
261263

‎arch/powerpc/include/asm/rtas.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ extern struct rtas_t rtas;
236236
extern int rtas_token(const char *service);
237237
extern int rtas_service_present(const char *service);
238238
extern int rtas_call(int token, int, int, int *, ...);
239+
int rtas_call_reentrant(int token, int nargs, int nret, int *outputs, ...);
239240
void rtas_call_unlocked(struct rtas_args *args, int token, int nargs,
240241
int nret, ...);
241242
extern void __noreturn rtas_restart(char *cmd);

‎arch/powerpc/kernel/paca.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <asm/kexec.h>
1717
#include <asm/svm.h>
1818
#include <asm/ultravisor.h>
19+
#include <asm/rtas.h>
1920

2021
#include "setup.h"
2122

@@ -164,6 +165,30 @@ static struct slb_shadow * __init new_slb_shadow(int cpu, unsigned long limit)
164165

165166
#endif /* CONFIG_PPC_BOOK3S_64 */
166167

168+
#ifdef CONFIG_PPC_PSERIES
169+
/**
170+
* new_rtas_args() - Allocates rtas args
171+
* @cpu: CPU number
172+
* @limit: Memory limit for this allocation
173+
*
174+
* Allocates a struct rtas_args and return it's pointer,
175+
* if not in Hypervisor mode
176+
*
177+
* Return: Pointer to allocated rtas_args
178+
* NULL if CPU in Hypervisor Mode
179+
*/
180+
static struct rtas_args * __init new_rtas_args(int cpu, unsigned long limit)
181+
{
182+
limit = min_t(unsigned long, limit, RTAS_INSTANTIATE_MAX);
183+
184+
if (early_cpu_has_feature(CPU_FTR_HVMODE))
185+
return NULL;
186+
187+
return alloc_paca_data(sizeof(struct rtas_args), L1_CACHE_BYTES,
188+
limit, cpu);
189+
}
190+
#endif /* CONFIG_PPC_PSERIES */
191+
167192
/* The Paca is an array with one entry per processor. Each contains an
168193
* lppaca, which contains the information shared between the
169194
* hypervisor and Linux.
@@ -202,6 +227,10 @@ void __init __nostackprotector initialise_paca(struct paca_struct *new_paca, int
202227
/* For now -- if we have threads this will be adjusted later */
203228
new_paca->tcd_ptr = &new_paca->tcd;
204229
#endif
230+
231+
#ifdef CONFIG_PPC_PSERIES
232+
new_paca->rtas_args_reentrant = NULL;
233+
#endif
205234
}
206235

207236
/* Put the paca pointer into r13 and SPRG_PACA */
@@ -273,6 +302,9 @@ void __init allocate_paca(int cpu)
273302
#endif
274303
#ifdef CONFIG_PPC_BOOK3S_64
275304
paca->slb_shadow_ptr = new_slb_shadow(cpu, limit);
305+
#endif
306+
#ifdef CONFIG_PPC_PSERIES
307+
paca->rtas_args_reentrant = new_rtas_args(cpu, limit);
276308
#endif
277309
paca_struct_size += sizeof(struct paca_struct);
278310
}

‎arch/powerpc/kernel/rtas.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <asm/time.h>
4242
#include <asm/mmu.h>
4343
#include <asm/topology.h>
44+
#include <asm/paca.h>
4445

4546
/* This is here deliberately so it's only used in this file */
4647
void enter_rtas(unsigned long);
@@ -1014,6 +1015,57 @@ int rtas_ibm_suspend_me(u64 handle)
10141015
free_cpumask_var(offline_mask);
10151016
return atomic_read(&data.error);
10161017
}
1018+
1019+
/**
1020+
* rtas_call_reentrant() - Used for reentrant rtas calls
1021+
* @token: Token for desired reentrant RTAS call
1022+
* @nargs: Number of Input Parameters
1023+
* @nret: Number of Output Parameters
1024+
* @outputs: Array of outputs
1025+
* @...: Inputs for desired RTAS call
1026+
*
1027+
* According to LoPAR documentation, only "ibm,int-on", "ibm,int-off",
1028+
* "ibm,get-xive" and "ibm,set-xive" are currently reentrant.
1029+
* Reentrant calls need their own rtas_args buffer, so not using rtas.args, but
1030+
* PACA one instead.
1031+
*
1032+
* Return: -1 on error,
1033+
* First output value of RTAS call if (nret > 0),
1034+
* 0 otherwise,
1035+
*/
1036+
int rtas_call_reentrant(int token, int nargs, int nret, int *outputs, ...)
1037+
{
1038+
va_list list;
1039+
struct rtas_args *args;
1040+
unsigned long flags;
1041+
int i, ret = 0;
1042+
1043+
if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
1044+
return -1;
1045+
1046+
local_irq_save(flags);
1047+
preempt_disable();
1048+
1049+
/* We use the per-cpu (PACA) rtas args buffer */
1050+
args = local_paca->rtas_args_reentrant;
1051+
1052+
va_start(list, outputs);
1053+
va_rtas_call_unlocked(args, token, nargs, nret, list);
1054+
va_end(list);
1055+
1056+
if (nret > 1 && outputs)
1057+
for (i = 0; i < nret - 1; ++i)
1058+
outputs[i] = be32_to_cpu(args->rets[i + 1]);
1059+
1060+
if (nret > 0)
1061+
ret = be32_to_cpu(args->rets[0]);
1062+
1063+
local_irq_restore(flags);
1064+
preempt_enable();
1065+
1066+
return ret;
1067+
}
1068+
10171069
#else /* CONFIG_PPC_PSERIES */
10181070
int rtas_ibm_suspend_me(u64 handle)
10191071
{

‎arch/powerpc/sysdev/xics/ics-rtas.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ static void ics_rtas_unmask_irq(struct irq_data *d)
5050

5151
server = xics_get_irq_server(d->irq, irq_data_get_affinity_mask(d), 0);
5252

53-
call_status = rtas_call(ibm_set_xive, 3, 1, NULL, hw_irq, server,
54-
DEFAULT_PRIORITY);
53+
call_status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, hw_irq,
54+
server, DEFAULT_PRIORITY);
5555
if (call_status != 0) {
5656
printk(KERN_ERR
5757
"%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)
6060
}
6161

6262
/* Now unmask the interrupt (often a no-op) */
63-
call_status = rtas_call(ibm_int_on, 1, 1, NULL, hw_irq);
63+
call_status = rtas_call_reentrant(ibm_int_on, 1, 1, NULL, hw_irq);
6464
if (call_status != 0) {
6565
printk(KERN_ERR "%s: ibm_int_on irq=%u returned %d\n",
6666
__func__, hw_irq, call_status);
@@ -91,16 +91,16 @@ static void ics_rtas_mask_real_irq(unsigned int hw_irq)
9191
if (hw_irq == XICS_IPI)
9292
return;
9393

94-
call_status = rtas_call(ibm_int_off, 1, 1, NULL, hw_irq);
94+
call_status = rtas_call_reentrant(ibm_int_off, 1, 1, NULL, hw_irq);
9595
if (call_status != 0) {
9696
printk(KERN_ERR "%s: ibm_int_off irq=%u returned %d\n",
9797
__func__, hw_irq, call_status);
9898
return;
9999
}
100100

101101
/* Have to set XIVE to 0xff to be able to remove a slot */
102-
call_status = rtas_call(ibm_set_xive, 3, 1, NULL, hw_irq,
103-
xics_default_server, 0xff);
102+
call_status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, hw_irq,
103+
xics_default_server, 0xff);
104104
if (call_status != 0) {
105105
printk(KERN_ERR "%s: ibm_set_xive(0xff) irq=%u returned %d\n",
106106
__func__, hw_irq, call_status);
@@ -131,7 +131,7 @@ static int ics_rtas_set_affinity(struct irq_data *d,
131131
if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
132132
return -1;
133133

134-
status = rtas_call(ibm_get_xive, 1, 3, xics_status, hw_irq);
134+
status = rtas_call_reentrant(ibm_get_xive, 1, 3, xics_status, hw_irq);
135135

136136
if (status) {
137137
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,
146146
return -1;
147147
}
148148

149-
status = rtas_call(ibm_set_xive, 3, 1, NULL,
150-
hw_irq, irq_server, xics_status[1]);
149+
status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL,
150+
hw_irq, irq_server, xics_status[1]);
151151

152152
if (status) {
153153
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)
179179
return -EINVAL;
180180

181181
/* Check if RTAS knows about this interrupt */
182-
rc = rtas_call(ibm_get_xive, 1, 3, status, hw_irq);
182+
rc = rtas_call_reentrant(ibm_get_xive, 1, 3, status, hw_irq);
183183
if (rc)
184184
return -ENXIO;
185185

@@ -198,7 +198,7 @@ static long ics_rtas_get_server(struct ics *ics, unsigned long vec)
198198
{
199199
int rc, status[2];
200200

201-
rc = rtas_call(ibm_get_xive, 1, 3, status, vec);
201+
rc = rtas_call_reentrant(ibm_get_xive, 1, 3, status, vec);
202202
if (rc)
203203
return -1;
204204
return status[0];

0 commit comments

Comments
 (0)