{"id":19948,"url":"https://patchwork.libcamera.org/api/patches/19948/?format=json","web_url":"https://patchwork.libcamera.org/patch/19948/","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":"<20240424234224.9658-4-laurent.pinchart@ideasonboard.com>","date":"2024-04-24T23:42:21","name":"[v1,3/6] test: fence: Fix race condition","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"ce0a53c1b31844c7d3bde2d75c1d5fc9735e7b88","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/19948/mbox/","series":[{"id":4271,"url":"https://patchwork.libcamera.org/api/series/4271/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4271","date":"2024-04-24T23:42:18","name":"vimc scaling improvements","version":1,"mbox":"https://patchwork.libcamera.org/series/4271/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19948/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19948/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 4DF80C328D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 24 Apr 2024 23:42:46 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 598D46340D;\n\tThu, 25 Apr 2024 01:42:41 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BC1F261A94\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 25 Apr 2024 01:42:33 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(117.145-247-81.adsl-dyn.isp.belgacom.be [81.247.145.117])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8F707B1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 25 Apr 2024 01:41:41 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"hGiWKRL7\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1714002101;\n\tbh=R5qiJqECiD7VBLdiDw+N7e4/3NwEWQ2P/sQZAkMqGwM=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=hGiWKRL7Cq0Q58TNUnGxuE3kO2fp/OYUEX4OQhJr4I3yG3L48/gyK32+M+jmiZi1J\n\tYmLy35etQ4oVQ9cO9d7Ij06mqI1Z57N7vX/VtTaioQqBZXrpu6DXv5swAPBhaDN0Kn\n\tJTyuah74GLND8hNwlRQm/o1LUtwOAXQs5YfZ+K2E=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Subject":"[PATCH v1 3/6] test: fence: Fix race condition","Date":"Thu, 25 Apr 2024 02:42:21 +0300","Message-ID":"<20240424234224.9658-4-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.43.2","In-Reply-To":"<20240424234224.9658-1-laurent.pinchart@ideasonboard.com>","References":"<20240424234224.9658-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The fence test is racy, as it relies on the main loop being executed\nbetween completion of request signalledRequestId_ and\nsignalledRequestId_ + 1. This usually happens, but is not guaranteed.\n\nTo fix the race condition, change the request identification logic by\nreplacing usage of the cookie value, is zero-based and wraps around at\nnbuffers_ - 1, with a completed request counter that is one-based and\ndoesn't wrap. The completedRequestId_, expiredRequestId_ and\nsignalledRequestId_ variables now track the identifier of the last\nrequest that has completed, the request whose fence will time out, and\nthe request whose fence will be signalled.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n test/fence.cpp | 29 ++++++++++++++---------------\n 1 file changed, 14 insertions(+), 15 deletions(-)","diff":"diff --git a/test/fence.cpp b/test/fence.cpp\nindex e6f79d2faa21..7949bfbb176b 100644\n--- a/test/fence.cpp\n+++ b/test/fence.cpp\n@@ -57,8 +57,7 @@ private:\n \tbool expectedCompletionResult_ = true;\n \tbool setFence_ = true;\n \n-\tunsigned int completedRequest_;\n-\n+\tunsigned int completedRequestId_;\n \tunsigned int signalledRequestId_;\n \tunsigned int expiredRequestId_;\n \tunsigned int nbuffers_;\n@@ -126,8 +125,9 @@ int FenceTest::init()\n \t\treturn TestFail;\n \t}\n \n-\tsignalledRequestId_ = nbuffers_ - 2;\n-\texpiredRequestId_ = nbuffers_ - 1;\n+\tcompletedRequestId_ = 0;\n+\texpiredRequestId_ = nbuffers_;\n+\tsignalledRequestId_ = nbuffers_ * 2;\n \n \treturn TestPass;\n }\n@@ -189,16 +189,16 @@ void FenceTest::requestRequeue(Request *request)\n \tconst Request::BufferMap &buffers = request->buffers();\n \tconst Stream *stream = buffers.begin()->first;\n \tFrameBuffer *buffer = buffers.begin()->second;\n-\tuint64_t cookie = request->cookie();\n \n \trequest->reuse();\n \n-\tif (cookie == signalledRequestId_ && setFence_) {\n+\tif (completedRequestId_ == signalledRequestId_ - nbuffers_ && setFence_) {\n \t\t/*\n-\t\t * The second time this request is queued add a fence to it.\n-\t\t *\n-\t\t * The main loop signals it by using a timer to write to the\n-\t\t * efd2_ file descriptor before the fence expires.\n+\t\t * This is the request that will be used to test fence\n+\t\t * signalling when it completes next time. Add a fence to it,\n+\t\t * using efd2_. The main loop will signal the fence by using a\n+\t\t * timer to write to the efd2_ file descriptor before the fence\n+\t\t * expires.\n \t\t */\n \t\tstd::unique_ptr<Fence> fence =\n \t\t\tstd::make_unique<Fence>(std::move(eventFd2_));\n@@ -213,8 +213,7 @@ void FenceTest::requestRequeue(Request *request)\n \n void FenceTest::requestComplete(Request *request)\n {\n-\tuint64_t cookie = request->cookie();\n-\tcompletedRequest_ = cookie;\n+\tcompletedRequestId_++;\n \n \t/*\n \t * The last request is expected to fail as its fence has not been\n@@ -222,7 +221,7 @@ void FenceTest::requestComplete(Request *request)\n \t *\n \t * Validate the fence status but do not re-queue it.\n \t */\n-\tif (cookie == expiredRequestId_) {\n+\tif (completedRequestId_ == expiredRequestId_) {\n \t\tif (validateExpiredRequest(request) != TestPass)\n \t\t\texpectedCompletionResult_ = false;\n \n@@ -271,7 +270,7 @@ int FenceTest::run()\n \t\t}\n \n \t\tint ret;\n-\t\tif (i == expiredRequestId_) {\n+\t\tif (i == expiredRequestId_ - 1) {\n \t\t\t/* This request will have a fence, and it will expire. */\n \t\t\tstd::unique_ptr<Fence> fence =\n \t\t\t\tstd::make_unique<Fence>(std::move(eventFd_));\n@@ -318,7 +317,7 @@ int FenceTest::run()\n \tTimer timer;\n \ttimer.start(1000ms);\n \twhile (timer.isRunning() && expectedCompletionResult_) {\n-\t\tif (completedRequest_ == signalledRequestId_ && setFence_)\n+\t\tif (completedRequestId_ == signalledRequestId_ - 1 && setFence_)\n \t\t\t/*\n \t\t\t * signalledRequestId_ has just completed and it has\n \t\t\t * been re-queued with a fence. Start the timer to\n","prefixes":["v1","3/6"]}