Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: amphion: remove mutext lock in condition of wait_event

mutext_lock should not be called in condition of wait_event, otherwise,
when CONFIG_DEBUG_ATOMIC_SLEEP is enabled, we may meet the following
warning:
do not call blocking ops when !TASK_RUNNING; state=2
WARNING: CPU: 5 PID: 741 at kernel/sched/core.c:9859
__might_sleep+0x80/0xa4
Hardware name: Freescale i.MX8QM MEK (DT)
pstate: 60000005 (nZCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--) pc :
__might_sleep+0x80/0xa4 lr : __might_sleep+0x80/0xa4 sp : ffffffc0123738a0
x29: ffffffc0123738a0 x28: ffffffc009194c48 x27: ffffffc00bbc1050
x26: ffffff8814b282f0 x25: ffffff8814b280d0 x24: ffffff8814b28080
x23: 0000000000000001 x22: 0000000000000032 x21: ffffffc00bbc1000
x20: 000000000000011b x19: ffffffc009324670 x18: 00000000fffffffd
x17: 30303c5b20746120 x16: 74657320323d6574 x15: 617473203b474e49
x14: 00058b5b8b9aa1f1 x13: ffffffc00903cda0 x12: 00000000d744fcc9
x11: 000000000000001c x10: 00000000000009a0 x9 : ffffffc0090201f4
x8 : ffffff8828245000 x7 : 0000000000000001 x6 : 0000000000000001
x5 : 00000000410fd080 x4 : 0000000000000002 x3 : ffffff8815aab4c8
x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffffff8828244600
Call trace:
__might_sleep+0x80/0xa4
mutex_lock+0x2c/0x80
sync_session_response+0x110/0x310
vpu_session_send_cmd+0x18c/0x244
vpu_session_start+0x38/0x70
vdec_start_session+0x1b4/0x3e0
vpu_vb2_start_streaming+0xa0/0x1c4
vb2_start_streaming+0x74/0x160
vb2_core_qbuf+0x488/0x650
vb2_qbuf+0x9c/0x100
v4l2_m2m_qbuf+0x7c/0x224
v4l2_m2m_ioctl_qbuf+0x20/0x2c
v4l_qbuf+0x50/0x6c
__video_do_ioctl+0x174/0x3f0
video_usercopy+0x210/0x7cc
video_ioctl2+0x20/0x30
v4l2_ioctl+0x48/0x6c

we need to refine check_is_responsed() to remove the mutext_lock, each
cmd has a monotonically increasing id, and cmds are executed
sequentially, so we can check the id of the last reponsed cmd, then
determine whether a command has been responded or not.

Signed-off-by: Ming Qian <ming.qian@nxp.com>
CC: Xiaolei Wang <xiaolei.wang@windriver.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>

authored by

Ming Qian and committed by
Hans Verkuil
f9c8ddce 1ddeeac2

+12 -20
+2 -1
drivers/media/platform/amphion/vpu.h
··· 154 154 struct vpu_mbox tx_type; 155 155 struct vpu_mbox tx_data; 156 156 struct vpu_mbox rx; 157 - unsigned long cmd_seq; 158 157 159 158 wait_queue_head_t ack_wq; 160 159 struct completion cmp; ··· 252 253 253 254 struct list_head cmd_q; 254 255 void *pending; 256 + unsigned long cmd_seq; 257 + atomic_long_t last_response_cmd; 255 258 256 259 struct vpu_inst_ops *ops; 257 260 const struct vpu_format *formats;
+9 -19
drivers/media/platform/amphion/vpu_cmds.c
··· 32 32 struct vpu_cmd_request *request; 33 33 struct vpu_rpc_event *pkt; 34 34 unsigned long key; 35 + atomic_long_t *last_response_cmd; 35 36 }; 36 37 37 38 static struct vpu_cmd_request vpu_cmd_requests[] = { ··· 116 115 { 117 116 if (!cmd) 118 117 return; 118 + if (cmd->last_response_cmd) 119 + atomic_long_set(cmd->last_response_cmd, cmd->key); 119 120 vfree(cmd->pkt); 120 121 vfree(cmd); 121 122 } ··· 175 172 return -ENOMEM; 176 173 177 174 mutex_lock(&core->cmd_lock); 178 - cmd->key = core->cmd_seq++; 175 + cmd->key = ++inst->cmd_seq; 176 + cmd->last_response_cmd = &inst->last_response_cmd; 179 177 if (key) 180 178 *key = cmd->key; 181 179 if (sync) ··· 250 246 251 247 static bool check_is_responsed(struct vpu_inst *inst, unsigned long key) 252 248 { 253 - struct vpu_core *core = inst->core; 254 - struct vpu_cmd_t *cmd; 255 - bool flag = true; 249 + unsigned long last_response = atomic_long_read(&inst->last_response_cmd); 256 250 257 - mutex_lock(&core->cmd_lock); 258 - cmd = inst->pending; 259 - if (cmd && key == cmd->key) { 260 - flag = false; 261 - goto exit; 262 - } 263 - list_for_each_entry(cmd, &inst->cmd_q, list) { 264 - if (key == cmd->key) { 265 - flag = false; 266 - break; 267 - } 268 - } 269 - exit: 270 - mutex_unlock(&core->cmd_lock); 251 + if (key <= last_response && (last_response - key) < (ULONG_MAX >> 1)) 252 + return true; 271 253 272 - return flag; 254 + return false; 273 255 } 274 256 275 257 static int sync_session_response(struct vpu_inst *inst, unsigned long key, long timeout, int try)
+1
drivers/media/platform/amphion/vpu_v4l2.c
··· 716 716 func = &vpu->decoder; 717 717 718 718 atomic_set(&inst->ref_count, 0); 719 + atomic_long_set(&inst->last_response_cmd, 0); 719 720 vpu_inst_get(inst); 720 721 inst->vpu = vpu; 721 722 inst->core = vpu_request_core(vpu, inst->type);