Show a patch.

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

{
    "id": 13935,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/13935/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/13935/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/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": "<20210924172525.160482-6-jacopo@jmondi.org>",
    "date": "2021-09-24T17:25:25",
    "name": "[libcamera-devel,v3,5/5] libcamera: control_serializer: Separate the handles space",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "442638cbffdf54cab5f41d72ee3bfb6106507768",
    "submitter": {
        "id": 3,
        "url": "https://patchwork.libcamera.org/api/1.1/people/3/?format=api",
        "name": "Jacopo Mondi",
        "email": "jacopo@jmondi.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/13935/mbox/",
    "series": [
        {
            "id": 2567,
            "url": "https://patchwork.libcamera.org/api/1.1/series/2567/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2567",
            "date": "2021-09-24T17:25:20",
            "name": "libcamera: control serializer fixes",
            "version": 3,
            "mbox": "https://patchwork.libcamera.org/series/2567/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/13935/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/13935/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 B7002BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 24 Sep 2021 17:24:51 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A406269190;\n\tFri, 24 Sep 2021 19:24:50 +0200 (CEST)",
            "from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net\n\t[217.70.183.201])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6829369195\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 24 Sep 2021 19:24:46 +0200 (CEST)",
            "(Authenticated sender: jacopo@jmondi.org)\n\tby relay8-d.mail.gandi.net (Postfix) with ESMTPSA id BC8AC1BF20A;\n\tFri, 24 Sep 2021 17:24:45 +0000 (UTC)"
        ],
        "From": "Jacopo Mondi <jacopo@jmondi.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Fri, 24 Sep 2021 19:25:25 +0200",
        "Message-Id": "<20210924172525.160482-6-jacopo@jmondi.org>",
        "X-Mailer": "git-send-email 2.32.0",
        "In-Reply-To": "<20210924172525.160482-1-jacopo@jmondi.org>",
        "References": "<20210924172525.160482-1-jacopo@jmondi.org>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v3 5/5] libcamera: control_serializer:\n\tSeparate the handles space",
        "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": "Two indipendnt instancess of the ControlSerializer class are in use at\nthe IPC boundaries, one in the Proxy class that serializes data from the\npipeline handler to the IPA, and one in the ProxyWorker which serializes\ndata in the opposite direction.\n\nEach instance operates autonomously, without any centralized point of\ncontrol, and each one assigns a numerical handle to each ControlInfoMap\nit serializes. This creates a risk of potential collision on the handle\nvalues, as both instances will use the same numerical space and\nare not aware of what handles has been already used by the instance \"on\nthe other side\".\n\nTo fix that, partition the handles numerical space by initializing the\ncontrol serializer with a seed according to the role of the component\nthat creates the serializer and increment the handle number by 2, to\navoid any collision risk.\n\nWhile this is temporary and rather hacky solution, it solves an issue\nwith isolated IPA modules without too much complexity added.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n---\n .../libcamera/internal/control_serializer.h   |  8 ++-\n src/libcamera/control_serializer.cpp          | 63 +++++++++++++++++--\n test/serialization/control_serialization.cpp  |  4 +-\n .../ipa_data_serializer_test.cpp              |  6 +-\n .../module_ipa_proxy.cpp.tmpl                 |  3 +-\n .../module_ipa_proxy_worker.cpp.tmpl          |  4 +-\n 6 files changed, 75 insertions(+), 13 deletions(-)",
    "diff": "diff --git a/include/libcamera/internal/control_serializer.h b/include/libcamera/internal/control_serializer.h\nindex 8a66be324138..caeafa11bc54 100644\n--- a/include/libcamera/internal/control_serializer.h\n+++ b/include/libcamera/internal/control_serializer.h\n@@ -20,7 +20,12 @@ class ByteStreamBuffer;\n class ControlSerializer\n {\n public:\n-\tControlSerializer();\n+\tenum class Role {\n+\t\tProxy,\n+\t\tWorker\n+\t};\n+\n+\tControlSerializer(Role role);\n \n \tvoid reset();\n \n@@ -47,6 +52,7 @@ private:\n \tControlInfo loadControlInfo(ControlType type, ByteStreamBuffer &buffer);\n \n \tunsigned int serial_;\n+\tunsigned int serialSeed_;\n \tstd::vector<std::unique_ptr<ControlId>> controlIds_;\n \tstd::vector<std::unique_ptr<ControlIdMap>> controlIdMaps_;\n \tstd::map<unsigned int, ControlInfoMap> infoMaps_;\ndiff --git a/src/libcamera/control_serializer.cpp b/src/libcamera/control_serializer.cpp\nindex f1245ea620ab..025222d77e4d 100644\n--- a/src/libcamera/control_serializer.cpp\n+++ b/src/libcamera/control_serializer.cpp\n@@ -62,6 +62,14 @@ LOG_DEFINE_CATEGORY(Serializer)\n  * corresponding ControlInfoMap handle in the binary data, and when\n  * deserializing to retrieve the corresponding ControlInfoMap.\n  *\n+ * As independent ControlSerializer instances are used on both sides of the IPC\n+ * boundary, and the two instances operate without a shared point of control,\n+ * there is a potential risk of collision of the numerical handles assigned to\n+ * each serialized ControlInfoMap. For this reason the control serializer is\n+ * initialized with a seed and the handle is incremented by 2, so that instances\n+ * initialized with a different seed operate on a separate numerical space,\n+ * avoiding any collision risk.\n+ *\n  * In order to perform those tasks, the serializer keeps an internal state that\n  * needs to be properly populated. This mechanism requires the ControlInfoMap\n  * corresponding to a ControlList to have been serialized or deserialized\n@@ -77,9 +85,45 @@ LOG_DEFINE_CATEGORY(Serializer)\n  * proceed with care to avoid stale references.\n  */\n \n-ControlSerializer::ControlSerializer()\n-\t: serial_(0)\n+/**\n+ * \\enum ControlSerializer::Role\n+ * \\brief Define the role of the IPC component using the control serializer.\n+ *\n+ * The role of the component that creates the serializer is used to initialize\n+ * the handles numerical space.\n+ *\n+ * \\var ControlSerializer::Role::Proxy\n+ * \\brief The control serializer is used by the IPC Proxy classes\n+ *\n+ * \\var ControlSerializer::Role::Worker\n+ * \\brief The control serializer is used by the IPC ProxyWorker classes\n+ */\n+\n+/**\n+ * \\brief Construct a new ControlSerializer\n+ * \\param[in] role The role of the IPC component using the serializer\n+ */\n+ControlSerializer::ControlSerializer(Role role)\n {\n+\t/*\n+\t * Initialize the handle numerical space using the role of the\n+\t * component that created the instance.\n+\t *\n+\t * Instances initialized for a different role will use a different\n+\t * numerical handle space, avoiding any collision risk when, in example,\n+\t * two instances of the ControlSerializer class are used at the IPC\n+\t * boundaries.\n+\t *\n+\t * Start counting handles from '1' as '0' is a special value used as\n+\t * place holder when serializing lists that do not have a ControlInfoMap\n+\t * associated (in example list of libcamera controls::controls).\n+\t *\n+\t * \\todo This is a temporary hack and should probably be better\n+\t * engineered, but for the time being it avoid collisions on the handle\n+\t * value when using IPC.\n+\t */\n+\tserialSeed_ = role == Role::Proxy ? 1 : 2;\n+\tserial_ = serialSeed_;\n }\n \n /**\n@@ -90,7 +134,7 @@ ControlSerializer::ControlSerializer()\n  */\n void ControlSerializer::reset()\n {\n-\tserial_ = 0;\n+\tserial_ = serialSeed_;\n \n \tinfoMapHandles_.clear();\n \tinfoMaps_.clear();\n@@ -200,10 +244,10 @@ int ControlSerializer::serialize(const ControlInfoMap &infoMap,\n \telse\n \t\tidMapType = IPA_CONTROL_ID_MAP_V4L2;\n \n-\t/* Prepare the packet header, assign a handle to the ControlInfoMap. */\n+\t/* Prepare the packet header. */\n \tstruct ipa_controls_header hdr;\n \thdr.version = IPA_CONTROLS_FORMAT_VERSION;\n-\thdr.handle = ++serial_;\n+\thdr.handle = serial_;\n \thdr.entries = infoMap.size();\n \thdr.size = sizeof(hdr) + entriesSize + valuesSize;\n \thdr.data_offset = sizeof(hdr) + entriesSize;\n@@ -211,6 +255,15 @@ int ControlSerializer::serialize(const ControlInfoMap &infoMap,\n \n \tbuffer.write(&hdr);\n \n+\t/*\n+\t * Increment the handle for the ControlInfoMap by 2 to keep the handles\n+\t * numerical space partitioned between instances initialized for a\n+\t * different role.\n+\t *\n+\t * \\sa ControlSerializer::Role\n+\t */\n+\tserial_ += 2;\n+\n \t/*\n \t * Serialize all entries.\n \t * \\todo Serialize the control name too\ndiff --git a/test/serialization/control_serialization.cpp b/test/serialization/control_serialization.cpp\nindex 5ac9c4ede5f9..a507d98a152d 100644\n--- a/test/serialization/control_serialization.cpp\n+++ b/test/serialization/control_serialization.cpp\n@@ -30,8 +30,8 @@ protected:\n \n \tint run() override\n \t{\n-\t\tControlSerializer serializer;\n-\t\tControlSerializer deserializer;\n+\t\tControlSerializer serializer(ControlSerializer::Role::Proxy);\n+\t\tControlSerializer deserializer(ControlSerializer::Role::Worker);\n \n \t\tstd::vector<uint8_t> infoData;\n \t\tstd::vector<uint8_t> listData;\ndiff --git a/test/serialization/ipa_data_serializer_test.cpp b/test/serialization/ipa_data_serializer_test.cpp\nindex eca19a6642e1..5fcdcb8eae92 100644\n--- a/test/serialization/ipa_data_serializer_test.cpp\n+++ b/test/serialization/ipa_data_serializer_test.cpp\n@@ -162,7 +162,7 @@ private:\n \n \tint testControls()\n \t{\n-\t\tControlSerializer cs;\n+\t\tControlSerializer cs(ControlSerializer::Role::Proxy);\n \n \t\tconst ControlInfoMap &infoMap = camera_->controls();\n \t\tControlList list = generateControlList(infoMap);\n@@ -195,7 +195,7 @@ private:\n \n \tint testVector()\n \t{\n-\t\tControlSerializer cs;\n+\t\tControlSerializer cs(ControlSerializer::Role::Proxy);\n \n \t\t/*\n \t\t * We don't test FileDescriptor serdes because it dup()s, so we\n@@ -265,7 +265,7 @@ private:\n \n \tint testMap()\n \t{\n-\t\tControlSerializer cs;\n+\t\tControlSerializer cs(ControlSerializer::Role::Proxy);\n \n \t\t/*\n \t\t * Realistically, only string and integral keys.\ndiff --git a/utils/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl b/utils/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl\nindex aab1fffbcaf0..d856339aa9ee 100644\n--- a/utils/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl\n+++ b/utils/ipc/generators/libcamera_templates/module_ipa_proxy.cpp.tmpl\n@@ -46,7 +46,8 @@ namespace {{ns}} {\n {%- endif %}\n \n {{proxy_name}}::{{proxy_name}}(IPAModule *ipam, bool isolate)\n-\t: IPAProxy(ipam), isolate_(isolate), seq_(0)\n+\t: IPAProxy(ipam), isolate_(isolate),\n+\t  controlSerializer_(ControlSerializer::Role::Proxy), seq_(0)\n {\n \tLOG(IPAProxy, Debug)\n \t\t<< \"initializing {{module_name}} proxy: loading IPA from \"\ndiff --git a/utils/ipc/generators/libcamera_templates/module_ipa_proxy_worker.cpp.tmpl b/utils/ipc/generators/libcamera_templates/module_ipa_proxy_worker.cpp.tmpl\nindex 890246a8849b..764e7a3af63a 100644\n--- a/utils/ipc/generators/libcamera_templates/module_ipa_proxy_worker.cpp.tmpl\n+++ b/utils/ipc/generators/libcamera_templates/module_ipa_proxy_worker.cpp.tmpl\n@@ -53,7 +53,9 @@ class {{proxy_worker_name}}\n {\n public:\n \t{{proxy_worker_name}}()\n-\t\t: ipa_(nullptr), exit_(false) {}\n+\t\t: ipa_(nullptr),\n+\t\t  controlSerializer_(ControlSerializer::Role::Worker),\n+\t\t  exit_(false) {}\n \n \t~{{proxy_worker_name}}() {}\n \n",
    "prefixes": [
        "libcamera-devel",
        "v3",
        "5/5"
    ]
}