Show a patch.

GET /api/patches/20121/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 20121,
    "url": "https://patchwork.libcamera.org/api/patches/20121/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/20121/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20240529154341.10426-4-laurent.pinchart@ideasonboard.com>",
    "date": "2024-05-29T15:43:38",
    "name": "[v2,3/6] test: fence: Fix race condition",
    "commit_ref": "13dcc7fc5f662a8c00eff5dce689c81231b5216e",
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "78514a126a849b22eb1d09a96d72ef056e496236",
    "submitter": {
        "id": 2,
        "url": "https://patchwork.libcamera.org/api/people/2/?format=api",
        "name": "Laurent Pinchart",
        "email": "laurent.pinchart@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/20121/mbox/",
    "series": [
        {
            "id": 4333,
            "url": "https://patchwork.libcamera.org/api/series/4333/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4333",
            "date": "2024-05-29T15:43:35",
            "name": "vimc scaling improvements",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/4333/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/20121/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/20121/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 184E8C32C8\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 29 May 2024 15:44:04 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7B8B2634C2;\n\tWed, 29 May 2024 17:44:03 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2FA0E634B0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 29 May 2024 17:43:57 +0200 (CEST)",
            "from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 840604AB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 29 May 2024 17:43:53 +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=\"F9VWOvOk\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1716997433;\n\tbh=cmQsA0itWFeK2xyRSzqjXhxzOpEW750LXkk58QfuZKU=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=F9VWOvOkShQnaGVwXh9YUXdb6/DmDm99SAO+4R0Fib94ShqNYT6r8wguH2dyuvB+p\n\tIiCu9IfOqaU+wPVPLURcMjJBGG6mAofSyKOp+5a1dpBpIUOd/BpWHA3VS7ktOV4fvy\n\tanoehYkGx3YR4cYh7D80/w08nhSj+wU5bJWBz+b0=",
        "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Subject": "[PATCH v2 3/6] test: fence: Fix race condition",
        "Date": "Wed, 29 May 2024 18:43:38 +0300",
        "Message-ID": "<20240529154341.10426-4-laurent.pinchart@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.44.1",
        "In-Reply-To": "<20240529154341.10426-1-laurent.pinchart@ideasonboard.com>",
        "References": "<20240529154341.10426-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, which is zero-based and wraps\naround at nbuffers_ - 1, with a completed request counter that is\none-based and doesn't wrap. The completedRequestId_, expiredRequestId_\nand signalledRequestId_ 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---\nChanges since v1:\n\n- Add and update comments\n- Fix typo in commit message\n---\n test/fence.cpp | 57 +++++++++++++++++++++++++++++++-------------------\n 1 file changed, 36 insertions(+), 21 deletions(-)",
    "diff": "diff --git a/test/fence.cpp b/test/fence.cpp\nindex c282886287aa..a8fba7284d82 100644\n--- a/test/fence.cpp\n+++ b/test/fence.cpp\n@@ -57,8 +57,11 @@ private:\n \tbool expectedCompletionResult_ = true;\n \tbool setFence_ = true;\n \n-\tunsigned int completedRequest_;\n-\n+\t/*\n+\t * Request IDs track the number of requests that have completed. They\n+\t * are one-based, and don't wrap.\n+\t */\n+\tunsigned int completedRequestId_;\n \tunsigned int signalledRequestId_;\n \tunsigned int expiredRequestId_;\n \tunsigned int nbuffers_;\n@@ -126,8 +129,19 @@ int FenceTest::init()\n \t\treturn TestFail;\n \t}\n \n-\tsignalledRequestId_ = nbuffers_ - 2;\n-\texpiredRequestId_ = nbuffers_ - 1;\n+\tcompletedRequestId_ = 0;\n+\n+\t/*\n+\t * All but two requests are queued without a fence. Request\n+\t * expiredRequestId_ will be queued with a fence that we won't signal\n+\t * (which is then expected to expire), and request signalledRequestId_\n+\t * will be queued with a fence that gets signalled. Select nbuffers_\n+\t * and nbuffers_ * 2 for those two requests, to space them by a few\n+\t * frames while still not requiring a long time for the test to\n+\t * complete.\n+\t */\n+\texpiredRequestId_ = nbuffers_;\n+\tsignalledRequestId_ = nbuffers_ * 2;\n \n \treturn TestPass;\n }\n@@ -189,16 +203,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,16 +227,15 @@ 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-\t * signaled.\n+\t * Request expiredRequestId_ is expected to fail as its fence has not\n+\t * been signalled.\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@@ -230,7 +243,7 @@ void FenceTest::requestComplete(Request *request)\n \t\treturn;\n \t}\n \n-\t/* Validate all requests but the last. */\n+\t/* Validate all other requests. */\n \tif (validateRequest(request) != TestPass) {\n \t\texpectedCompletionResult_ = false;\n \n@@ -271,7 +284,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,11 +331,13 @@ 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-\t\t\t * signal the fence in 10 msec.\n+\t\t\t * The request just before signalledRequestId_ has just\n+\t\t\t * completed. Request signalledRequestId_ has been\n+\t\t\t * queued with a fence, and libcamera is likely already\n+\t\t\t * waiting on the fence, or will soon. Start the timer\n+\t\t\t * to signal the fence in 10 msec.\n \t\t\t */\n \t\t\tfenceTimer.start(10ms);\n \n",
    "prefixes": [
        "v2",
        "3/6"
    ]
}