Skip to content

Commit b841128

Browse files
Junhao HeSuzuki K Poulose
authored andcommitted
coresight: ultrasoc-smb: Fix sleep while close preempt in enable_smb
When we to enable the SMB by perf, the perf sched will call perf_ctx_lock() to close system preempt in event_function_call(). But SMB::enable_smb() use mutex to lock the critical section, which may sleep. BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580 in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 153023, name: perf preempt_count: 2, expected: 0 RCU nest depth: 0, expected: 0 INFO: lockdep is turned off. irq event stamp: 0 hardirqs last enabled at (0): [<0000000000000000>] 0x0 hardirqs last disabled at (0): [<ffffa2983f5c5f40>] copy_process+0xae8/0x2b48 softirqs last enabled at (0): [<ffffa2983f5c5f40>] copy_process+0xae8/0x2b48 softirqs last disabled at (0): [<0000000000000000>] 0x0 CPU: 2 PID: 153023 Comm: perf Kdump: loaded Tainted: G W O 6.5.0-rc4+ #1 Call trace: ... __mutex_lock+0xbc/0xa70 mutex_lock_nested+0x34/0x48 smb_update_buffer+0x58/0x360 [ultrasoc_smb] etm_event_stop+0x204/0x2d8 [coresight] etm_event_del+0x1c/0x30 [coresight] event_sched_out+0x17c/0x3b8 group_sched_out.part.0+0x5c/0x208 __perf_event_disable+0x15c/0x210 event_function+0xe0/0x230 remote_function+0xb4/0xe8 generic_exec_single+0x160/0x268 smp_call_function_single+0x20c/0x2a0 event_function_call+0x20c/0x220 _perf_event_disable+0x5c/0x90 perf_event_for_each_child+0x58/0xc0 _perf_ioctl+0x34c/0x1250 perf_ioctl+0x64/0x98 ... Use spinlock to replace mutex to control driver data access to one at a time. The function copy_to_user() may sleep, it cannot be in a spinlock context, so we can't simply replace it in smb_read(). But we can ensure that only one user gets the SMB device fd by smb_open(), so remove the locks from smb_read() and buffer synchronization is guaranteed by the user. Fixes: 06f5c29 ("drivers/coresight: Add UltraSoc System Memory Buffer driver") Signed-off-by: Junhao He <[email protected]> Reviewed-by: James Clark <[email protected]> Signed-off-by: Suzuki K Poulose <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent e49c0b1 commit b841128

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

drivers/hwtracing/coresight/ultrasoc-smb.c

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static int smb_open(struct inode *inode, struct file *file)
9999
struct smb_drv_data, miscdev);
100100
int ret = 0;
101101

102-
mutex_lock(&drvdata->mutex);
102+
spin_lock(&drvdata->spinlock);
103103

104104
if (drvdata->reading) {
105105
ret = -EBUSY;
@@ -115,7 +115,7 @@ static int smb_open(struct inode *inode, struct file *file)
115115

116116
drvdata->reading = true;
117117
out:
118-
mutex_unlock(&drvdata->mutex);
118+
spin_unlock(&drvdata->spinlock);
119119

120120
return ret;
121121
}
@@ -132,10 +132,8 @@ static ssize_t smb_read(struct file *file, char __user *data, size_t len,
132132
if (!len)
133133
return 0;
134134

135-
mutex_lock(&drvdata->mutex);
136-
137135
if (!sdb->data_size)
138-
goto out;
136+
return 0;
139137

140138
to_copy = min(sdb->data_size, len);
141139

@@ -145,20 +143,15 @@ static ssize_t smb_read(struct file *file, char __user *data, size_t len,
145143

146144
if (copy_to_user(data, sdb->buf_base + sdb->buf_rdptr, to_copy)) {
147145
dev_dbg(dev, "Failed to copy data to user\n");
148-
to_copy = -EFAULT;
149-
goto out;
146+
return -EFAULT;
150147
}
151148

152149
*ppos += to_copy;
153-
154150
smb_update_read_ptr(drvdata, to_copy);
155-
156-
dev_dbg(dev, "%zu bytes copied\n", to_copy);
157-
out:
158151
if (!sdb->data_size)
159152
smb_reset_buffer(drvdata);
160-
mutex_unlock(&drvdata->mutex);
161153

154+
dev_dbg(dev, "%zu bytes copied\n", to_copy);
162155
return to_copy;
163156
}
164157

@@ -167,9 +160,9 @@ static int smb_release(struct inode *inode, struct file *file)
167160
struct smb_drv_data *drvdata = container_of(file->private_data,
168161
struct smb_drv_data, miscdev);
169162

170-
mutex_lock(&drvdata->mutex);
163+
spin_lock(&drvdata->spinlock);
171164
drvdata->reading = false;
172-
mutex_unlock(&drvdata->mutex);
165+
spin_unlock(&drvdata->spinlock);
173166

174167
return 0;
175168
}
@@ -262,7 +255,7 @@ static int smb_enable(struct coresight_device *csdev, enum cs_mode mode,
262255
struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
263256
int ret = 0;
264257

265-
mutex_lock(&drvdata->mutex);
258+
spin_lock(&drvdata->spinlock);
266259

267260
/* Do nothing, the trace data is reading by other interface now */
268261
if (drvdata->reading) {
@@ -294,7 +287,7 @@ static int smb_enable(struct coresight_device *csdev, enum cs_mode mode,
294287

295288
dev_dbg(&csdev->dev, "Ultrasoc SMB enabled\n");
296289
out:
297-
mutex_unlock(&drvdata->mutex);
290+
spin_unlock(&drvdata->spinlock);
298291

299292
return ret;
300293
}
@@ -304,7 +297,7 @@ static int smb_disable(struct coresight_device *csdev)
304297
struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
305298
int ret = 0;
306299

307-
mutex_lock(&drvdata->mutex);
300+
spin_lock(&drvdata->spinlock);
308301

309302
if (drvdata->reading) {
310303
ret = -EBUSY;
@@ -327,7 +320,7 @@ static int smb_disable(struct coresight_device *csdev)
327320

328321
dev_dbg(&csdev->dev, "Ultrasoc SMB disabled\n");
329322
out:
330-
mutex_unlock(&drvdata->mutex);
323+
spin_unlock(&drvdata->spinlock);
331324

332325
return ret;
333326
}
@@ -408,7 +401,7 @@ static unsigned long smb_update_buffer(struct coresight_device *csdev,
408401
if (!buf)
409402
return 0;
410403

411-
mutex_lock(&drvdata->mutex);
404+
spin_lock(&drvdata->spinlock);
412405

413406
/* Don't do anything if another tracer is using this sink. */
414407
if (atomic_read(&csdev->refcnt) != 1)
@@ -432,7 +425,7 @@ static unsigned long smb_update_buffer(struct coresight_device *csdev,
432425
if (!buf->snapshot && lost)
433426
perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
434427
out:
435-
mutex_unlock(&drvdata->mutex);
428+
spin_unlock(&drvdata->spinlock);
436429

437430
return data_size;
438431
}
@@ -590,7 +583,7 @@ static int smb_probe(struct platform_device *pdev)
590583
return ret;
591584
}
592585

593-
mutex_init(&drvdata->mutex);
586+
spin_lock_init(&drvdata->spinlock);
594587
drvdata->pid = -1;
595588

596589
ret = smb_register_sink(pdev, drvdata);

drivers/hwtracing/coresight/ultrasoc-smb.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#define _ULTRASOC_SMB_H
99

1010
#include <linux/miscdevice.h>
11-
#include <linux/mutex.h>
11+
#include <linux/spinlock.h>
1212

1313
/* Offset of SMB global registers */
1414
#define SMB_GLB_CFG_REG 0x00
@@ -105,7 +105,7 @@ struct smb_data_buffer {
105105
* @csdev: Component vitals needed by the framework.
106106
* @sdb: Data buffer for SMB.
107107
* @miscdev: Specifics to handle "/dev/xyz.smb" entry.
108-
* @mutex: Control data access to one at a time.
108+
* @spinlock: Control data access to one at a time.
109109
* @reading: Synchronise user space access to SMB buffer.
110110
* @pid: Process ID of the process being monitored by the
111111
* session that is using this component.
@@ -116,7 +116,7 @@ struct smb_drv_data {
116116
struct coresight_device *csdev;
117117
struct smb_data_buffer sdb;
118118
struct miscdevice miscdev;
119-
struct mutex mutex;
119+
spinlock_t spinlock;
120120
bool reading;
121121
pid_t pid;
122122
enum cs_mode mode;

0 commit comments

Comments
 (0)