{"id":16491,"url":"https://patchwork.libcamera.org/api/patches/16491/?format=json","web_url":"https://patchwork.libcamera.org/patch/16491/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220630203221.12440-1-laurent.pinchart@ideasonboard.com>","date":"2022-06-30T20:32:21","name":"[libcamera-devel,v2.1,12/12] gstreamer: Fix race conditions in task pause/resume","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"5d54020b8202fe549c95d0e7b0e8e4335709fe53","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16491/mbox/","series":[{"id":3240,"url":"https://patchwork.libcamera.org/api/series/3240/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3240","date":"2022-06-30T20:32:21","name":null,"version":1,"mbox":"https://patchwork.libcamera.org/series/3240/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16491/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16491/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 1CEEEBD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 30 Jun 2022 20:32:51 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4ACA26564F;\n\tThu, 30 Jun 2022 22:32:50 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 82FAA65633\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 30 Jun 2022 22:32:48 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E2DAC45F;\n\tThu, 30 Jun 2022 22:32:47 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1656621170;\n\tbh=4jsfxW2utTClHjYT8mD+Cva3IEExR9uqwHWdIYD/iRA=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=n5vVJWBhpk0zvLB+8eK0YTHR5eU9o2Ilv/6a7wN1Q3LDy3pe7aZpWctypG376nqod\n\ti0HI9AtIKxEFDYKKFGAEkVyVF4VZaik6Xvl7RZxL8g/4+XQopXS0AwoPvFAOVRtF67\n\tWpJO3AOqj8q2UGmhrc/yCHqi/UwrIpOocnR0zb18VH2LuVSUiHfgT2Y0zZJxdhtza2\n\tNCRWWZ7A8HA0L2DXW6NmDuvULqJqlf5RRmYgJuVPeHlTkhJKCCkMoLKcgdk9GUt0Zt\n\tP7R2RGz1xQ8BrrzRythzkLSpjdyzIU7I7Oy/lzvhlhmK/G1lbElqbrzKTEUEFJYRVw\n\tulPlLKwKxaR+w==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1656621168;\n\tbh=4jsfxW2utTClHjYT8mD+Cva3IEExR9uqwHWdIYD/iRA=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=g/cdltVe3v96hhRtItPhT2s+91wadEKd5gZ9lvVUATHSxEpNXxao8bB7AZwuxZl0k\n\tjmjTELGxAei95qTfAt72RKkwmO0obPCPeD1IR7TmuwWFUM6E/bvvwEVIXyElDfxb1V\n\tig9vpPEsRfoB4ZZPjJpRjONt9MTcIEwBsvyk4oSA="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"g/cdltVe\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 30 Jun 2022 23:32:21 +0300","Message-Id":"<20220630203221.12440-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220630000251.31295-13-laurent.pinchart@ideasonboard.com>","References":"<20220630000251.31295-13-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2.1 12/12] gstreamer: Fix race conditions\n\tin task pause/resume","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"Nicolas Dufresne <nicolas.dufresne@collabora.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The task run function races with two other threads that want to resume\nthe task: the requestCompleted() handler and the buffer-notify signal\nhandler. If the former queues completed requests or the latter queues\nback buffers to the pool, and then resume the task, after the task run\nhandler checks the queues but before it attemps to pause the task, then\nthe task may be paused without noticing that more work is available.\n\nThe most immediate way to fix this is to take the stream_lock in the\nrequestCompleted() and buffer-notify signal handlers, or cover the whole\ntask run handler with the GstLibcameraSrcState lock. This could cause\nlong delays in the requestCompleted() handler, so that's not a good\noption.\n\nInstead, add a wakeup flag, preotected by the GstLibcameraSrcState lock,\nthat allows detection of a lost race, and retry the task run.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nChanges since v2:\n\n- Invert the pause/resume logic\n\nChanges since v1:\n\n- Fix incorrect wakeup and pause logic\n---\n src/gstreamer/gstlibcamerasrc.cpp | 71 ++++++++++++++++++++++---------\n 1 file changed, 50 insertions(+), 21 deletions(-)","diff":"diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp\nindex 9ea59631a9f2..a7a0c4403e28 100644\n--- a/src/gstreamer/gstlibcamerasrc.cpp\n+++ b/src/gstreamer/gstlibcamerasrc.cpp\n@@ -259,6 +259,7 @@ GstLibcameraSrcState::requestCompleted(Request *request)\n int GstLibcameraSrcState::processRequest()\n {\n \tstd::unique_ptr<RequestWrap> wrap;\n+\tint err = 0;\n \n \t{\n \t\tMutexLocker locker(lock_);\n@@ -267,10 +268,13 @@ int GstLibcameraSrcState::processRequest()\n \t\t\twrap = std::move(completedRequests_.front());\n \t\t\tcompletedRequests_.pop();\n \t\t}\n+\n+\t\tif (completedRequests_.empty())\n+\t\t\terr = -ENOBUFS;\n \t}\n \n \tif (!wrap)\n-\t\treturn -ENODATA;\n+\t\treturn -ENOBUFS;\n \n \tGstFlowReturn ret = GST_FLOW_OK;\n \tgst_flow_combiner_reset(src_->flow_combiner);\n@@ -310,7 +314,7 @@ int GstLibcameraSrcState::processRequest()\n \t\treturn -EPIPE;\n \t}\n \n-\treturn 0;\n+\treturn err;\n }\n \n static bool\n@@ -380,47 +384,72 @@ gst_libcamera_src_task_run(gpointer user_data)\n \tGstLibcameraSrc *self = GST_LIBCAMERA_SRC(user_data);\n \tGstLibcameraSrcState *state = self->state;\n \n+\t/*\n+\t * Start by pausing the task. The task may also get resumed by the\n+\t * buffer-notify signal when new buffers are queued back to the pool,\n+\t * or by the request completion handler when a new request has\n+\t * completed.  Both will resume the task after adding the buffers or\n+\t * request to their respective lists, which are checked below to decide\n+\t * if the task needs to be resumed for another iteration. This is thus\n+\t * guaranteed to be race-free, the lock taken by gst_task_pause() and\n+\t * gst_task_resume() serves as a memory barrier.\n+\t */\n+\tgst_task_pause(self->task);\n+\n+\tbool doResume = false;\n+\n \t/*\n \t * Create and queue one request. If no buffers are available the\n \t * function returns -ENOBUFS, which we ignore here as that's not a\n \t * fatal error.\n \t */\n \tint ret = state->queueRequest();\n-\tif (ret == -ENOMEM) {\n+\tswitch (ret) {\n+\tcase 0:\n+\t\t/*\n+\t\t * The request was successfully queued, there may be enough\n+\t\t * buffers to create a new one. Don't pause the task to give it\n+\t\t * another try.\n+\t\t */\n+\t\tdoResume = true;\n+\t\tbreak;\n+\n+\tcase -ENOMEM:\n \t\tGST_ELEMENT_ERROR(self, RESOURCE, NO_SPACE_LEFT,\n \t\t\t\t  (\"Failed to allocate request for camera '%s'.\",\n \t\t\t\t   state->cam_->id().c_str()),\n \t\t\t\t  (\"libcamera::Camera::createRequest() failed\"));\n \t\tgst_task_stop(self->task);\n \t\treturn;\n+\n+\tcase -ENOBUFS:\n+\tdefault:\n+\t\tbreak;\n \t}\n \n-\t/* Process one completed request, if available. */\n+\t/*\n+\t * Process one completed request, if available, and record if further\n+\t * requests are ready for processing.\n+\t */\n \tret = state->processRequest();\n \tswitch (ret) {\n+\tcase 0:\n+\t\t/* Another completed request is available, resume the task. */\n+\t\tdoResume = true;\n+\t\tbreak;\n+\n \tcase -EPIPE:\n \t\tgst_task_stop(self->task);\n \t\treturn;\n \n-\tcase -ENODATA:\n-\t\tgst_task_pause(self->task);\n-\t\treturn;\n+\tcase -ENOBUFS:\n+\tdefault:\n+\t\tbreak;\n \t}\n \n-\t/*\n-\t * Here we need to decide if we want to pause. This needs to\n-\t * happen in lock step with the callback thread which may want\n-\t * to resume the task and might push pending buffers.\n-\t */\n-\tbool do_pause;\n-\n-\t{\n-\t\tMutexLocker locker(state->lock_);\n-\t\tdo_pause = state->completedRequests_.empty();\n-\t}\n-\n-\tif (do_pause)\n-\t\tgst_task_pause(self->task);\n+\t/* Resume the task for another iteration if needed. */\n+\tif (doResume)\n+\t\tgst_task_resume(self->task);\n }\n \n static void\n","prefixes":["libcamera-devel","v2.1","12/12"]}