Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp522961iob; Tue, 3 May 2022 04:05:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfBS7JLsAbAxZshHl+0ArJ8oFk8rbxTe8Uo37wmit1KSoekaWAMP8ZXCfHBXLxgafQ1VbP X-Received: by 2002:a2e:b014:0:b0:23c:9593:f7 with SMTP id y20-20020a2eb014000000b0023c959300f7mr9588522ljk.209.1651575940261; Tue, 03 May 2022 04:05:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651575940; cv=none; d=google.com; s=arc-20160816; b=CnULSIZ0K4OSpVmYmTjvDL3vEt9nVadYATccV2JKSxnh0fDeB9AVsjlC4GxnzUF/2C mm6uC5jHiJDlcKIDU46I73vzN6slfIs/KlL+qHFS1jGbCM7Kwqc3SJeMIDjHOKayPg2y vBqQrZfcwMFoWRtG7aDCBZAViAMk+1tojmxoYi0FMc+Rx0iIoR/3Q2ncox4i7W+GIIib 0b4cwzsHsyGlh/CdHJ0APFPKgaNZvk5Dg4XaI5sDomvvSdXPm7pth8HzKRw5raOVZb9Q IXiMdIki9ZYGo29x2yaw11UFosc+ZyxcBOqV7yadqTN6oZwO/SeJGKnJY9R+4g/MtUKb nbHQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=F8m5FG4AHBX8BWYCof8Xwc4vOKWhD2PwpJQI2/SkO9Q=; b=MtdWNOx3V2VhXUBXVADoXgg1LHxft2JKB9V1ZnRHmAh9PJYudq0MyFkHhPJjw0q78J 6RmWJK6UmpoTpWzK/mrl2tmZUTNLP/njYV6ctSxHr4LuCsUkgNPUtKRBomEfG9ohXomy IbnCOB2nNWZk8OaT67S7IwjQ2cCbzUaUV8/pFWczCKz6dUSnnCU8P80kPGSiM84dSTyU KspNTke8rVmcX0YJbldseyWgRo+zi4h4/9IJtsdCXjR4RxNdYAXPXkPRh1yh7Y6J1xHT 0x3/WoS6zcO4wKkyI59nWHg0ceJPDo1cQc3kUvdE/5gymdIa9h0bez72KdHBsfJ2xbCt DsRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@microchip.com header.s=mchp header.b=L0ytM2e6; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u8-20020a056512128800b004724d70bbe1si476069lfs.188.2022.05.03.04.05.09; Tue, 03 May 2022 04:05:40 -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=fail header.i=@microchip.com header.s=mchp header.b=L0ytM2e6; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233003AbiECIt3 (ORCPT + 99 others); Tue, 3 May 2022 04:49:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233068AbiECItH (ORCPT ); Tue, 3 May 2022 04:49:07 -0400 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.154.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31417646A; Tue, 3 May 2022 01:45:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1651567531; x=1683103531; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=n2uIJaRZUL/S6HvMX63PPRJ3j4+MNQSEBbMnkivvTPU=; b=L0ytM2e6vCRpbwIqp7iE8XWUNvkS5n8lmMpH4GdNIh3SmaA+g1pnB86y ol9vR2GQK2t4BZZx3t/QteZAwAE07SS9KvmPPtIgbFnxfj03Gdbt9aUh/ t5ged1dku0i4vzgUqojrp1Klv8E7GZ78rwmSuKafXaoyK9Kx2Kagzdvwt jcnW6hY0UTqNrsqlM/ks5I0zDuVXbR4BTIJwQM+yui904goXf9d8skIqn cw6isz696ssbOzwOc670TfgGnlpt2rRszmq/+oawDFWExR7ked2ztONrz bAEEeIKf+NlraAsTosnLeH9KFQ1m5iJq17bwuiBCDwv6exrhM3SAXpO44 w==; X-IronPort-AV: E=Sophos;i="5.91,194,1647327600"; d="scan'208";a="154621927" Received: from smtpout.microchip.com (HELO email.microchip.com) ([198.175.253.82]) by esa4.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 03 May 2022 01:45:30 -0700 Received: from chn-vm-ex02.mchp-main.com (10.10.85.144) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Tue, 3 May 2022 01:45:28 -0700 Received: from ROB-ULT-M18282.microchip.com (10.10.115.15) by chn-vm-ex02.mchp-main.com (10.10.85.144) with Microsoft SMTP Server id 15.1.2375.17 via Frontend Transport; Tue, 3 May 2022 01:45:23 -0700 From: Eugen Hristev To: , CC: , , , , Eugen Hristev Subject: [PATCH v10 5/8] media: atmel: atmel-isc-base: use mutex to lock awb workq from streaming Date: Tue, 3 May 2022 11:44:18 +0300 Message-ID: <20220503084421.40682-6-eugen.hristev@microchip.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220503084421.40682-1-eugen.hristev@microchip.com> References: <20220503084421.40682-1-eugen.hristev@microchip.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 The AWB workqueue runs in a kernel thread and needs to be synchronized w.r.t. the streaming status. It is possible that streaming is stopped while the AWB workq is running. In this case it is likely that the check for vb2_start_streaming_called is done at one point in time, but the AWB computations are done later, including a call to isc_update_profile, which requires streaming to be started. Thus , isc_update_profile will fail if during this operation sequence the streaming was stopped. To solve this issue, a mutex is added, that will serialize the awb work and streaming stopping, with the mention that either streaming is stopped completely including termination of the last frame is done, and after that the AWB work can check stream status and stop; either first AWB work is completed and after that the streaming can stop correctly. The awb spin lock cannot be used since this spinlock is taken in the same context and using it in the stop streaming will result in a recursion BUG. Signed-off-by: Eugen Hristev Reviewed-by: Jacopo Mondi --- Changes in v10: -> add missing mutex_lock in s_awb_ctrl -> edited commit message drivers/media/platform/atmel/atmel-isc-base.c | 30 ++++++++++++++++--- drivers/media/platform/atmel/atmel-isc.h | 2 ++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/drivers/media/platform/atmel/atmel-isc-base.c b/drivers/media/platform/atmel/atmel-isc-base.c index 67b4a2323fed..2f07a50035c8 100644 --- a/drivers/media/platform/atmel/atmel-isc-base.c +++ b/drivers/media/platform/atmel/atmel-isc-base.c @@ -401,6 +401,7 @@ static void isc_stop_streaming(struct vb2_queue *vq) struct isc_buffer *buf; int ret; + mutex_lock(&isc->awb_mutex); v4l2_ctrl_activate(isc->do_wb_ctrl, false); isc->stop = true; @@ -410,6 +411,8 @@ static void isc_stop_streaming(struct vb2_queue *vq) v4l2_err(&isc->v4l2_dev, "Timeout waiting for end of the capture\n"); + mutex_unlock(&isc->awb_mutex); + /* Disable DMA interrupt */ regmap_write(isc->regmap, ISC_INTDIS, ISC_INT_DDONE); @@ -1397,10 +1400,6 @@ static void isc_awb_work(struct work_struct *w) u32 min, max; int ret; - /* streaming is not active anymore */ - if (isc->stop) - return; - if (ctrls->hist_stat != HIST_ENABLED) return; @@ -1455,7 +1454,24 @@ static void isc_awb_work(struct work_struct *w) } regmap_write(regmap, ISC_HIS_CFG + isc->offsets.his, hist_id | baysel | ISC_HIS_CFG_RAR); + + /* + * We have to make sure the streaming has not stopped meanwhile. + * ISC requires a frame to clock the internal profile update. + * To avoid issues, lock the sequence with a mutex + */ + mutex_lock(&isc->awb_mutex); + + /* streaming is not active anymore */ + if (isc->stop) { + mutex_unlock(&isc->awb_mutex); + return; + }; + isc_update_profile(isc); + + mutex_unlock(&isc->awb_mutex); + /* if awb has been disabled, we don't need to start another histogram */ if (ctrls->awb) regmap_write(regmap, ISC_CTRLEN, ISC_CTRL_HISREQ); @@ -1534,6 +1550,7 @@ static int isc_s_awb_ctrl(struct v4l2_ctrl *ctrl) isc_update_awb_ctrls(isc); + mutex_lock(&isc->awb_mutex); if (vb2_is_streaming(&isc->vb2_vidq)) { /* * If we are streaming, we can update profile to @@ -1548,6 +1565,7 @@ static int isc_s_awb_ctrl(struct v4l2_ctrl *ctrl) */ v4l2_ctrl_activate(isc->do_wb_ctrl, false); } + mutex_unlock(&isc->awb_mutex); /* if we have autowhitebalance on, start histogram procedure */ if (ctrls->awb == ISC_WB_AUTO && @@ -1729,6 +1747,7 @@ static void isc_async_unbind(struct v4l2_async_notifier *notifier, { struct isc_device *isc = container_of(notifier->v4l2_dev, struct isc_device, v4l2_dev); + mutex_destroy(&isc->awb_mutex); cancel_work_sync(&isc->awb_work); video_unregister_device(&isc->video_dev); v4l2_ctrl_handler_free(&isc->ctrls.handler); @@ -1838,6 +1857,8 @@ static int isc_async_complete(struct v4l2_async_notifier *notifier) isc->current_subdev = container_of(notifier, struct isc_subdev_entity, notifier); mutex_init(&isc->lock); + mutex_init(&isc->awb_mutex); + init_completion(&isc->comp); /* Initialize videobuf2 queue */ @@ -1906,6 +1927,7 @@ static int isc_async_complete(struct v4l2_async_notifier *notifier) return 0; isc_async_complete_err: + mutex_destroy(&isc->awb_mutex); mutex_destroy(&isc->lock); return ret; } diff --git a/drivers/media/platform/atmel/atmel-isc.h b/drivers/media/platform/atmel/atmel-isc.h index f9ad7ec6bd13..ff60ba020cb9 100644 --- a/drivers/media/platform/atmel/atmel-isc.h +++ b/drivers/media/platform/atmel/atmel-isc.h @@ -218,6 +218,7 @@ struct isc_reg_offsets { * * @lock: lock for serializing userspace file operations * with ISC operations + * @awb_mutex: serialize access to streaming status from awb work queue * @awb_lock: lock for serializing awb work queue operations * with DMA/buffer operations * @@ -290,6 +291,7 @@ struct isc_device { struct work_struct awb_work; struct mutex lock; + struct mutex awb_mutex; spinlock_t awb_lock; struct regmap_field *pipeline[ISC_PIPE_LINE_NODE_NUM]; -- 2.25.1