Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp3664977rwe; Mon, 17 Apr 2023 01:22:50 -0700 (PDT) X-Google-Smtp-Source: AKy350Yzn9V14RmuCi0vDQTq5xSU0YBATGPAaJYu5zJTopqM4JQv7DUsu349WGTCdqCQdaKCYBs5 X-Received: by 2002:a17:902:ecc6:b0:1a6:bb05:d86b with SMTP id a6-20020a170902ecc600b001a6bb05d86bmr7995739plh.38.1681719770572; Mon, 17 Apr 2023 01:22:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681719770; cv=none; d=google.com; s=arc-20160816; b=TMuwWb5C+sy6BJ3llAfS0hxRdfZjMampfFdU+bM0hou/L+TCMy80YASb+S21Jljctz Gmse6djk8IQIIvL4Ijw1vmeB/4zhhk1f/6j6HYqpMpDdhCCpPEjAiFqGbqjobNDBn/KW y8S8FJDi7XXpHTeJ4RbJ4zouI2VJb2yQL6ghfZpIZynjozw4y+CMqwY460b0qhKVmhT2 Ubbd2F6YrYap+DO5E93Oj9ZKb/pQz+tBTzMBMTbtXvSDbbW6udyFHFo/JnFnSlO5MxaI DgY/Wu9zLBj8+4wBLkGEfB+Hn4mTrI7DOGbXeN9uXTLy8XMLdslSAo5KLnIRKxMKXnco 6PDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=vtIb9tYbe6yjfReUyHamMXfyrKWMabuVK2QPQKV6Ies=; b=f6coFlAGComEm6wXNMM0RmZsZWxdQiXWv0wlUtD9s340TyP+7lsAXXdckSYR8RA8DY 5m1+57HwPoXlMBkCzUcVNCG1GKJXGsGi1rjf6RuWm4pOZpTUgj1XKCR0p+EEyhkH7GyP nc68D6G9fKwBZwjcXO7e4KpCpoPxcFzzmQYaJOjOhE1Jb1T8P1FYj5ygGVBURXCKeD0B 4GrKZbEwbg0/6OuA4eqnYfvuuqeXbyzQ/VIDh6NhEiYB7YJQ0/2zZqglqz/BHJXLq1iG wMbdukQpCey9mobjK8/fe5c5mA15rqwmFuspT7Fs6TCzi6BWMhj9B5BggyTHrPmPdHRj aV7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=a9ZtKBFJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gx19-20020a17090b125300b002473e16b67esi8127310pjb.48.2023.04.17.01.22.38; Mon, 17 Apr 2023 01:22:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=a9ZtKBFJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230203AbjDQIRx (ORCPT + 99 others); Mon, 17 Apr 2023 04:17:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229741AbjDQIRu (ORCPT ); Mon, 17 Apr 2023 04:17:50 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0EB026B2; Mon, 17 Apr 2023 01:17:48 -0700 (PDT) X-UUID: 537a69fedcf811eda9a90f0bb45854f4-20230417 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=vtIb9tYbe6yjfReUyHamMXfyrKWMabuVK2QPQKV6Ies=; b=a9ZtKBFJTlDSMSeg+qF7t/jFa3YKH9vDkYaWS5e6uDR3UKzuMCKqt7lDOcGNpCUWcZJhuLzD6jifn1uoNHGKOuVa3yADsHLgDYuTDRR88gX+KZdhXuJHhI1aS3EZPbEWEDRMRuh0kAdk1egxVnz5syxJcKwuEkobkH27k+fzCbs=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22,REQID:34a49bda-3911-4913-8900-ccc034b47e1b,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:95 X-CID-INFO: VERSION:1.1.22,REQID:34a49bda-3911-4913-8900-ccc034b47e1b,IP:0,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTION :quarantine,TS:95 X-CID-META: VersionHash:120426c,CLOUDID:dc294e84-cd9c-45f5-8134-710979e3df0e,B ulkID:230417161743L1ENFHNP,BulkQuantity:0,Recheck:0,SF:38|29|28|17|19|48,T C:nil,Content:0,EDM:-3,IP:nil,URL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0 ,OSI:0,OSA:0,AV:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-UUID: 537a69fedcf811eda9a90f0bb45854f4-20230417 Received: from mtkmbs10n1.mediatek.inc [(172.21.101.34)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1581083758; Mon, 17 Apr 2023 16:17:42 +0800 Received: from mtkmbs13n2.mediatek.inc (172.21.101.108) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Mon, 17 Apr 2023 16:17:41 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Mon, 17 Apr 2023 16:17:41 +0800 From: Yunfei Dong To: Hans Verkuil , Mauro Carvalho Chehab CC: Pina Chen , Yunfei Dong , , , , Subject: [PATCH] media: v4l2-mem2mem: add lock to protect parameter num_rdy Date: Mon, 17 Apr 2023 16:17:40 +0800 Message-ID: <20230417081740.26075-1-yunfei.dong@mediatek.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-MTK: N X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Getting below error when using KCSAN to check the driver. Adding lock to protect parameter num_rdy when getting the value with function: v4l2_m2m_num_src_bufs_ready/v4l2_m2m_num_dst_bufs_ready. kworker/u16:3: [name:report&]BUG: KCSAN: data-race in v4l2_m2m_buf_queue kworker/u16:3: [name:report&] kworker/u16:3: [name:report&]read-write to 0xffffff8105f35b94 of 1 bytes by task 20865 on cpu 7: kworker/u16:3:  v4l2_m2m_buf_queue+0xd8/0x10c Signed-off-by: Pina Chen Signed-off-by: Yunfei Dong --- include/media/v4l2-mem2mem.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/include/media/v4l2-mem2mem.h b/include/media/v4l2-mem2mem.h index bb9de6a899e0..d6c8eb2b5201 100644 --- a/include/media/v4l2-mem2mem.h +++ b/include/media/v4l2-mem2mem.h @@ -593,7 +593,14 @@ void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, static inline unsigned int v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx) { - return m2m_ctx->out_q_ctx.num_rdy; + unsigned int num_buf_rdy; + unsigned long flags; + + spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags); + num_buf_rdy = m2m_ctx->out_q_ctx.num_rdy; + spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags); + + return num_buf_rdy; } /** @@ -605,7 +612,14 @@ unsigned int v4l2_m2m_num_src_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx) static inline unsigned int v4l2_m2m_num_dst_bufs_ready(struct v4l2_m2m_ctx *m2m_ctx) { - return m2m_ctx->cap_q_ctx.num_rdy; + unsigned int num_buf_rdy; + unsigned long flags; + + spin_lock_irqsave(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags); + num_buf_rdy = m2m_ctx->cap_q_ctx.num_rdy; + spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags); + + return num_buf_rdy; } /** -- 2.18.0