{"id":16446,"url":"https://patchwork.libcamera.org/api/patches/16446/?format=json","web_url":"https://patchwork.libcamera.org/patch/16446/","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":"<20220630000251.31295-10-laurent.pinchart@ideasonboard.com>","date":"2022-06-30T00:02:48","name":"[libcamera-devel,v2,09/12] gstreamer: Fix pads locking","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"e53a8c0e782ccab4c31f5854a8efca039fd0ad3f","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/16446/mbox/","series":[{"id":3234,"url":"https://patchwork.libcamera.org/api/series/3234/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3234","date":"2022-06-30T00:02:39","name":"gstreamer: Queue multiple requests","version":2,"mbox":"https://patchwork.libcamera.org/series/3234/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16446/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16446/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 E8163BD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 30 Jun 2022 00:03:33 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 89A1965657;\n\tThu, 30 Jun 2022 02:03:33 +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 4B2F765645\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 30 Jun 2022 02:03:19 +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 CC7E274A;\n\tThu, 30 Jun 2022 02:03:18 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1656547413;\n\tbh=0pZ9z6Uqpww/XQgU0bUnhvSJRvTe8SpNHy0Id5Hj9oo=;\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=IFq+Uny6Lb2lFyKBzRPwa1RG9MLldTSsXb18W+z+xPuttXQJOuJJ4FWqirQSL0KMq\n\taWzhSnspmmkC6qfxw7qDKmYYziXQJmR+f9nWpoh8fMB4OX63UpWmEb8x+EBuv25m/A\n\tKQsBYK0rF7sAeXo8WW4Rq1DeHdY7k1mXUoijuHI7u2fQ3XyzStpBmJjIjHBf2nupTF\n\t1oeJJsURnPEE503ZtkEet3DKBWKwRHJkpv0jx+AxkS0G/PdIGom+xtW6UVe/2xcwJ6\n\tnHf6I5+56gUo5LZ7fIvhF3rnnoSMQ9hWjJdb8anCti6mzt9OuIfdoMctEHo83HVhZa\n\t4ZJNDIQ3eSorA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1656547399;\n\tbh=0pZ9z6Uqpww/XQgU0bUnhvSJRvTe8SpNHy0Id5Hj9oo=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=ABZ9FhkLQYgK/409O/VPf59JmZe774Ac2XwhW2uk7O9Hs9cTZpoMo2q8r7a3/mhiq\n\tZ7N6LmSfmWUtTXR6jMQtPaclvKGepEVg4kV++qmfrzE98bfEjmn5od83wmIHNVYmg/\n\tNGGAYwQWX85jI8umiNLqRon+I+5z3Ea40U3ly8Og="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"ABZ9FhkL\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 30 Jun 2022 03:02:48 +0300","Message-Id":"<20220630000251.31295-10-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220630000251.31295-1-laurent.pinchart@ideasonboard.com>","References":"<20220630000251.31295-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 09/12] gstreamer: Fix pads locking","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 srcpads_ vector is protected by two different locks, the GstObject\nlock of the libcamerasrc element, and the stream_lock that covers the\nrun function of the thread. This isn't correct. Use the stream_lock\nconsistently to protect the pads.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n---\nChanges since v1:\n\n- Add a comment about lock ordering\n---\n src/gstreamer/gstlibcamerasrc.cpp | 73 +++++++++++++++++--------------\n 1 file changed, 40 insertions(+), 33 deletions(-)","diff":"diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp\nindex 6f9a03c515d2..c92ca7d29fe6 100644\n--- a/src/gstreamer/gstlibcamerasrc.cpp\n+++ b/src/gstreamer/gstlibcamerasrc.cpp\n@@ -112,12 +112,18 @@ struct GstLibcameraSrcState {\n \tstd::shared_ptr<CameraManager> cm_;\n \tstd::shared_ptr<Camera> cam_;\n \tstd::unique_ptr<CameraConfiguration> config_;\n-\tstd::vector<GstPad *> srcpads_;\n+\n+\tstd::vector<GstPad *> srcpads_; /* Protected by stream_lock */\n \n \t/*\n \t * Contention on this lock_ must be minimized, as it has to be taken in\n \t * the realtime-sensitive requestCompleted() handler to protect\n \t * queuedRequests_ and completedRequests_.\n+\t *\n+\t * stream_lock must be taken before lock_ in contexts where both locks\n+\t * need to be taken. In particular, this means that the lock_ must not\n+\t * be held while calling into other graph elements (e.g. when calling\n+\t * gst_pad_query()).\n \t */\n \tMutex lock_;\n \tstd::queue<std::unique_ptr<RequestWrap>> queuedRequests_\n@@ -354,36 +360,34 @@ gst_libcamera_src_task_run(gpointer user_data)\n \t\t\t\t\t\t\tsrcpad, ret);\n \t}\n \n+\tif (ret != GST_FLOW_OK) {\n+\t\tif (ret == GST_FLOW_EOS) {\n+\t\t\tg_autoptr(GstEvent) eos = gst_event_new_eos();\n+\t\t\tguint32 seqnum = gst_util_seqnum_next();\n+\t\t\tgst_event_set_seqnum(eos, seqnum);\n+\t\t\tfor (GstPad *srcpad : state->srcpads_)\n+\t\t\t\tgst_pad_push_event(srcpad, gst_event_ref(eos));\n+\t\t} else if (ret != GST_FLOW_FLUSHING) {\n+\t\t\tGST_ELEMENT_FLOW_ERROR(self, ret);\n+\t\t}\n+\t\tgst_task_stop(self->task);\n+\t\treturn;\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\tif (ret != GST_FLOW_OK) {\n-\t\t\tif (ret == GST_FLOW_EOS) {\n-\t\t\t\tg_autoptr(GstEvent) eos = gst_event_new_eos();\n-\t\t\t\tguint32 seqnum = gst_util_seqnum_next();\n-\t\t\t\tgst_event_set_seqnum(eos, seqnum);\n-\t\t\t\tfor (GstPad *srcpad : state->srcpads_)\n-\t\t\t\t\tgst_pad_push_event(srcpad, gst_event_ref(eos));\n-\t\t\t} else if (ret != GST_FLOW_FLUSHING) {\n-\t\t\t\tGST_ELEMENT_FLOW_ERROR(self, ret);\n-\t\t\t}\n-\t\t\tgst_task_stop(self->task);\n-\t\t\treturn;\n-\t\t}\n-\n-\t\t/*\n-\t\t * Here we need to decide if we want to pause. This needs to\n-\t\t * happen in lock step with the callback thread which may want\n-\t\t * to resume the task and might push pending buffers.\n-\t\t */\n-\t\tbool do_pause;\n-\n-\t\t{\n-\t\t\tMutexLocker locker(state->lock_);\n-\t\t\tdo_pause = state->completedRequests_.empty();\n-\t\t}\n-\n-\t\tif (do_pause)\n-\t\t\tgst_task_pause(self->task);\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 }\n \n static void\n@@ -537,8 +541,11 @@ gst_libcamera_src_task_leave([[maybe_unused]] GstTask *task,\n \t\tstate->completedRequests_ = {};\n \t}\n \n-\tfor (GstPad *srcpad : state->srcpads_)\n-\t\tgst_libcamera_pad_set_pool(srcpad, nullptr);\n+\t{\n+\t\tGLibRecLocker locker(&self->stream_lock);\n+\t\tfor (GstPad *srcpad : state->srcpads_)\n+\t\t\tgst_libcamera_pad_set_pool(srcpad, nullptr);\n+\t}\n \n \tg_clear_object(&self->allocator);\n \tg_clear_pointer(&self->flow_combiner,\n@@ -697,7 +704,7 @@ gst_libcamera_src_request_new_pad(GstElement *element, GstPadTemplate *templ,\n \tg_object_ref_sink(pad);\n \n \tif (gst_element_add_pad(element, pad)) {\n-\t\tGLibLocker lock(GST_OBJECT(self));\n+\t\tGLibRecLocker lock(&self->stream_lock);\n \t\tself->state->srcpads_.push_back(reinterpret_cast<GstPad *>(g_object_ref(pad)));\n \t} else {\n \t\tGST_ELEMENT_ERROR(element, STREAM, FAILED,\n@@ -717,7 +724,7 @@ gst_libcamera_src_release_pad(GstElement *element, GstPad *pad)\n \tGST_DEBUG_OBJECT(self, \"Pad %\" GST_PTR_FORMAT \" being released\", pad);\n \n \t{\n-\t\tGLibLocker lock(GST_OBJECT(self));\n+\t\tGLibRecLocker lock(&self->stream_lock);\n \t\tstd::vector<GstPad *> &pads = self->state->srcpads_;\n \t\tauto begin_iterator = pads.begin();\n \t\tauto end_iterator = pads.end();\n","prefixes":["libcamera-devel","v2","09/12"]}