Received: by 2002:a05:7412:7c14:b0:fa:6e18:a558 with SMTP id ii20csp387057rdb; Mon, 22 Jan 2024 07:17:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFIyB048NzDGGC6TDZEVrOmcRnDwfXORAq37llS/QZKzoJyxhGffK2cEdSUXV1M2kwlduQj X-Received: by 2002:a05:620a:2484:b0:783:a6fa:b882 with SMTP id i4-20020a05620a248400b00783a6fab882mr30204qkn.102.1705936665813; Mon, 22 Jan 2024 07:17:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705936665; cv=pass; d=google.com; s=arc-20160816; b=zJYW86+KHMVDFkn0kNcgmOeU6/m2ZBexHTyVlGwZQq+hT2KkaT9k613ORqHPg/gDXC 3lk7e4sgA8WrM7zkbOSxtjEZK0Vtv1GyrMF/3Rkds7zCkMHmkTOqAXfe7WrmUOnggwmj Ntd4iL6VSEKK6U4YhWhYcUVuxweJl6yZv4n5tNgBd2iiuqZ4jdUMdBvq4A9O74Dur5Lz fCicYadlBhmCp7a32dT34dB6NixTk/LJ91Y50yLY0SBYAuk0gTZwXPbXv10t7NblUmYv egBa3xISzHT8oM/TigEpbE0PW8C9U3+Ex8/TIUaamR4DqOpOg4Fyse/24jZ/Yze/IHS3 Uulg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=VTx55Nt/YgJPWbUu0BAQNnqqX8IoCZXDwwfrDxMQAN0=; fh=I1GCDW4ijOKglrqOIZB/d2WqiEd22OT+sd/O7OkS3y4=; b=l9Djqq0Op9meJJAsxc5Ssa6LlN62ORw5DtgF4yHwQhHgSSITxScksNfqS+i9vV78Rl OX+MINQ6zjrEclgjhflqG4SkhsOIxKPD6GSnK/+Jus8xvMaagWftLajn82kIlto2e5Sg xTxrn/MOMsaS77+HocshVJ18yYkpcSWRsWCmNHZqPwHBgMxVy1xG1vA9Rp8uZv6EUD38 LsOeLd8T0gBzoBHWpWl2xFFYvw21tZRG0E2KNXx7g/2iGXrwfOiva552RqVMqEVCC/3I PAAc/7XgTbKd53oNm4N2CL8zb2fBMzB1dwaXr3P1MHHFuDWbesAToSi1ltWVGzR72dn7 GYOw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RM4GZIWv; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-33266-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33266-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id vv11-20020a05620a562b00b007832e7649c8si5746697qkn.301.2024.01.22.07.17.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 07:17:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-33266-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RM4GZIWv; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-33266-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33266-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 7F76D1C21F2B for ; Mon, 22 Jan 2024 15:17:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED2A457326; Mon, 22 Jan 2024 14:58:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RM4GZIWv" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F04A557303; Mon, 22 Jan 2024 14:58:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705935522; cv=none; b=FtF6esDPkpwrhW/2+CfhSBtNu7zMB7GL//NefKVNpbRfxJjufoUueWG8AyG/LpB+CI6DQaJ8xfbmKTLYExIyIXZ4xxID0JVqvvug5TbNcCd0TTavJanxUUi0LpA3VSaBQLs9mIZi9I7xXuJsB+gjNE3PQX4JKVqAsU7pU5uK0y0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705935522; c=relaxed/simple; bh=LKTSZOGSaxq2ddTyAl0YeKbC+oAsCIrtnkAvoJDLljQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aTqiiQsCxy/7xpWVLS+k4kznHawH+sIzvnJATsJ5EpbeB6p9OxMW8w2Lsusl8tN4fmc+34uRqeylxnzC+vu/oI06Shx25K6Jn5xV9FjXudd9YQa6x1cyPWcXlE42SL7FO6gWlbIPI+Nl4pPkEwldzXljX9wqLJ6oBW4a4jao2FE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RM4GZIWv; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AD4F8C433F1; Mon, 22 Jan 2024 14:58:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705935521; bh=LKTSZOGSaxq2ddTyAl0YeKbC+oAsCIrtnkAvoJDLljQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RM4GZIWvUUsur0DQAF4ALwqVO8mb2D7rlzXLnU0d9f/3zmJ2WoZQrLkiwIa6tpL0i 5YFjc7oOnRfQFoU8tla1fNOysf1V+i1ZFAwtY/a70WZp/y4BwCZ7F5hGpumkosZz0K NJ9+nIRjf7v4aAEF8CUdI/0XksYU08+AfS/s5mfxIwKNUsCyxBJEOfAxu/RcpU/xEt VsVYqp+4RaVgMg7RWWi/9jrOaDRTc9IlaBvv7eoB2wJiUysmfiTVyxEos/ztVb3wYB 2hksVTL6/QPB+iqOntMW6+tvST76lWI8h8EtfGgXskNGZLbYDn6XYPGKEWGUoxWCC3 dtceqmG+Hhqcg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ming Qian , Xiaolei Wang , Hans Verkuil , Sasha Levin , eagle.zhou@nxp.com, mchehab@kernel.org, linux-media@vger.kernel.org Subject: [PATCH AUTOSEL 6.7 46/88] media: amphion: remove mutext lock in condition of wait_event Date: Mon, 22 Jan 2024 09:51:19 -0500 Message-ID: <20240122145608.990137-46-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240122145608.990137-1-sashal@kernel.org> References: <20240122145608.990137-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.7.1 Content-Transfer-Encoding: 8bit From: Ming Qian [ Upstream commit f9c8ddce2fe3b767582f299d03fc8fb85943568c ] 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 CC: Xiaolei Wang Signed-off-by: Hans Verkuil Signed-off-by: Sasha Levin --- drivers/media/platform/amphion/vpu.h | 3 ++- drivers/media/platform/amphion/vpu_cmds.c | 28 ++++++++--------------- drivers/media/platform/amphion/vpu_v4l2.c | 1 + 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/drivers/media/platform/amphion/vpu.h b/drivers/media/platform/amphion/vpu.h index 5a701f64289e..0246cf0ac3a8 100644 --- a/drivers/media/platform/amphion/vpu.h +++ b/drivers/media/platform/amphion/vpu.h @@ -154,7 +154,6 @@ struct vpu_core { struct vpu_mbox tx_type; struct vpu_mbox tx_data; struct vpu_mbox rx; - unsigned long cmd_seq; wait_queue_head_t ack_wq; struct completion cmp; @@ -253,6 +252,8 @@ struct vpu_inst { struct list_head cmd_q; void *pending; + unsigned long cmd_seq; + atomic_long_t last_response_cmd; struct vpu_inst_ops *ops; const struct vpu_format *formats; diff --git a/drivers/media/platform/amphion/vpu_cmds.c b/drivers/media/platform/amphion/vpu_cmds.c index c2337812573e..5695f5c1cb3e 100644 --- a/drivers/media/platform/amphion/vpu_cmds.c +++ b/drivers/media/platform/amphion/vpu_cmds.c @@ -32,6 +32,7 @@ struct vpu_cmd_t { struct vpu_cmd_request *request; struct vpu_rpc_event *pkt; unsigned long key; + atomic_long_t *last_response_cmd; }; static struct vpu_cmd_request vpu_cmd_requests[] = { @@ -115,6 +116,8 @@ static void vpu_free_cmd(struct vpu_cmd_t *cmd) { if (!cmd) return; + if (cmd->last_response_cmd) + atomic_long_set(cmd->last_response_cmd, cmd->key); vfree(cmd->pkt); vfree(cmd); } @@ -172,7 +175,8 @@ static int vpu_request_cmd(struct vpu_inst *inst, u32 id, void *data, return -ENOMEM; mutex_lock(&core->cmd_lock); - cmd->key = core->cmd_seq++; + cmd->key = ++inst->cmd_seq; + cmd->last_response_cmd = &inst->last_response_cmd; if (key) *key = cmd->key; if (sync) @@ -246,26 +250,12 @@ void vpu_clear_request(struct vpu_inst *inst) static bool check_is_responsed(struct vpu_inst *inst, unsigned long key) { - struct vpu_core *core = inst->core; - struct vpu_cmd_t *cmd; - bool flag = true; + unsigned long last_response = atomic_long_read(&inst->last_response_cmd); - mutex_lock(&core->cmd_lock); - cmd = inst->pending; - if (cmd && key == cmd->key) { - flag = false; - goto exit; - } - list_for_each_entry(cmd, &inst->cmd_q, list) { - if (key == cmd->key) { - flag = false; - break; - } - } -exit: - mutex_unlock(&core->cmd_lock); + if (key <= last_response && (last_response - key) < (ULONG_MAX >> 1)) + return true; - return flag; + return false; } static int sync_session_response(struct vpu_inst *inst, unsigned long key, long timeout, int try) diff --git a/drivers/media/platform/amphion/vpu_v4l2.c b/drivers/media/platform/amphion/vpu_v4l2.c index 0f6e4c666440..d7e0de49b3dc 100644 --- a/drivers/media/platform/amphion/vpu_v4l2.c +++ b/drivers/media/platform/amphion/vpu_v4l2.c @@ -716,6 +716,7 @@ int vpu_v4l2_open(struct file *file, struct vpu_inst *inst) func = &vpu->decoder; atomic_set(&inst->ref_count, 0); + atomic_long_set(&inst->last_response_cmd, 0); vpu_inst_get(inst); inst->vpu = vpu; inst->core = vpu_request_core(vpu, inst->type); -- 2.43.0