{"id":22807,"url":"https://patchwork.libcamera.org/api/1.1/patches/22807/?format=json","web_url":"https://patchwork.libcamera.org/patch/22807/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20250217185433.306833-8-pobrn@protonmail.com>","date":"2025-02-17T18:55:05","name":"[v3,7/8] libcamera: base: log: Protect log categories with lock","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"ccabda191cefc1ce4bd9132f9de279891c2629e7","submitter":{"id":133,"url":"https://patchwork.libcamera.org/api/1.1/people/133/?format=json","name":"Pőcze Barnabás","email":"pobrn@protonmail.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22807/mbox/","series":[{"id":5007,"url":"https://patchwork.libcamera.org/api/1.1/series/5007/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5007","date":"2025-02-17T18:54:35","name":"libcamera: base: log: Misc. changes","version":3,"mbox":"https://patchwork.libcamera.org/series/5007/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22807/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22807/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 C61EDC3200\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 17 Feb 2025 18:55:11 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1E38A68695;\n\tMon, 17 Feb 2025 19:55:11 +0100 (CET)","from mail-4322.protonmail.ch (mail-4322.protonmail.ch\n\t[185.70.43.22])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 66A386868F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 17 Feb 2025 19:55:09 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"YfcyLUDG\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1739818508; x=1740077708;\n\tbh=pqomrIDtjkGyEkLgEq9/KVdSaTtPwV82mzxoAhFP/us=;\n\th=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References:\n\tFeedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID:\n\tMessage-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post;\n\tb=YfcyLUDGSYwkL560bSoRx6XVGJD2uxSaVZWu8eD4IKX/VQ7Gn5FxUeF/q4MdNklfv\n\tEUthgkAFrzDRZRBMuxSYeQWsyabXMJ5aSzXN3xXOQqQVxCvtC4onaK+NqRpKK3Jgm5\n\tAoX9KbUZwTSlnoiMuP2oRQhxZ0mBzuM/ixFK9gjqUk1ERlK+gZN2dM2QeDGWGLsf9z\n\t3lmn0fuVBxhMOwrY2b1fdPBYXkCTQft/S6ZJGm7vQvhcdL7gzhqvsAN57lGJiZyobL\n\troaQzivh03Cs6VZsO9eScwV1AufJxd1vqZPdvtCaeuYlxj+LejNoulAQ/W6m64y9G1\n\t5f/xSpBjTMBgg==","Date":"Mon, 17 Feb 2025 18:55:05 +0000","To":"libcamera-devel@lists.libcamera.org","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","Subject":"[PATCH v3 7/8] libcamera: base: log: Protect log categories with\n\tlock","Message-ID":"<20250217185433.306833-8-pobrn@protonmail.com>","In-Reply-To":"<20250217185433.306833-1-pobrn@protonmail.com>","References":"<20250217185433.306833-1-pobrn@protonmail.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"2856231cfd9a8cdce6b6b60b560d4693ebd08e90","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","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":"Log categories may be added from any thread, so it is important to\nsynchronize access to the `Logger::categories_` list between its two\nusers: category creation (by LogCategory::create(), which calls\nLogger::findCategory() and Logger::registerCategory()); and log level\nsetting (by Logger::logSetLevel()).\n\nThe LogCategory::create() function uses a mutex to serialize category\ncreation, but Logger::logSetLevel() can access `Logger::categories_`\nconcurrently without any protection. To fix the issue, move the mutex to\nthe Logger class, and use it to protect all accesses to the categories\nlist. This requires moving all the logic of LogCategory::create() to a\nnew Logger::findOrCreateCategory() function that combines both\nLogger::findCategory() and Logger::registerCategory() in order to make\nthe two operations exacute atomically.\n\nSigned-off-by: Barnabás Pőcze <pobrn@protonmail.com>\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n include/libcamera/base/log.h |  1 +\n src/libcamera/base/log.cpp   | 58 +++++++++++++-----------------------\n 2 files changed, 22 insertions(+), 37 deletions(-)","diff":"diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\nindex 195426c41..8ae1b51d0 100644\n--- a/include/libcamera/base/log.h\n+++ b/include/libcamera/base/log.h\n@@ -39,6 +39,7 @@ public:\n \tstatic const LogCategory &defaultCategory();\n \n private:\n+\tfriend class Logger;\n \texplicit LogCategory(std::string_view name);\n \n \tconst std::string name_;\ndiff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\nindex 2c233be36..fd6c11716 100644\n--- a/src/libcamera/base/log.cpp\n+++ b/src/libcamera/base/log.cpp\n@@ -317,12 +317,12 @@ private:\n \tstatic LogSeverity parseLogLevel(std::string_view level);\n \n \tfriend LogCategory;\n-\tvoid registerCategory(LogCategory *category);\n-\tLogCategory *findCategory(std::string_view name) const;\n+\tLogCategory *findOrCreateCategory(std::string_view name);\n \n \tstatic bool destroyed_;\n \n-\tstd::vector<LogCategory *> categories_;\n+\tMutex mutex_;\n+\tstd::vector<LogCategory *> categories_ LIBCAMERA_TSA_GUARDED_BY(mutex_);\n \tstd::list<std::pair<std::string, LogSeverity>> levels_;\n \n \tstd::atomic<std::shared_ptr<LogOutput>> output_;\n@@ -572,6 +572,8 @@ void Logger::logSetLevel(const char *category, const char *level)\n \tif (severity == LogInvalid)\n \t\treturn;\n \n+\tMutexLocker locker(mutex_);\n+\n \tfor (LogCategory *c : categories_) {\n \t\tif (c->name() == category) {\n \t\t\tc->setSeverity(severity);\n@@ -708,37 +710,28 @@ LogSeverity Logger::parseLogLevel(std::string_view level)\n }\n \n /**\n- * \\brief Register a log category with the logger\n- * \\param[in] category The log category\n- *\n- * Log categories must have unique names. It is invalid to call this function\n- * if a log category with the same name already exists.\n+ * \\brief Find an existing log category with the given name or create one\n+ * \\param[in] name Name of the log category\n+ * \\return The pointer to the log category found or created\n  */\n-void Logger::registerCategory(LogCategory *category)\n+LogCategory *Logger::findOrCreateCategory(std::string_view name)\n {\n-\tcategories_.push_back(category);\n+\tMutexLocker locker(mutex_);\n \n-\tconst std::string &name = category->name();\n-\tfor (const auto &[pattern, severity] : levels_) {\n-\t\tif (fnmatch(pattern.c_str(), name.c_str(), FNM_NOESCAPE) == 0)\n-\t\t\tcategory->setSeverity(severity);\n+\tfor (LogCategory *category : categories_) {\n+\t\tif (category->name() == name)\n+\t\t\treturn category;\n \t}\n-}\n \n-/**\n- * \\brief Find an existing log category with the given name\n- * \\param[in] name Name of the log category\n- * \\return The pointer to the found log category or nullptr if not found\n- */\n-LogCategory *Logger::findCategory(std::string_view name) const\n-{\n-\tif (auto it = std::find_if(categories_.begin(), categories_.end(),\n-\t\t\t\t   [name](auto c) { return c->name() == name; });\n-\t    it != categories_.end()) {\n-\t\treturn *it;\n+\tLogCategory *category = categories_.emplace_back(new LogCategory(name));\n+\tconst char *name_cstr = category->name().c_str();\n+\n+\tfor (const auto &[pattern, severity] : levels_) {\n+\t\tif (fnmatch(pattern.c_str(), name_cstr, FNM_NOESCAPE) == 0)\n+\t\t\tcategory->setSeverity(severity);\n \t}\n \n-\treturn nullptr;\n+\treturn category;\n }\n \n /**\n@@ -776,16 +769,7 @@ LogCategory *Logger::findCategory(std::string_view name) const\n  */\n LogCategory *LogCategory::create(std::string_view name)\n {\n-\tstatic Mutex mutex_;\n-\tMutexLocker locker(mutex_);\n-\tLogCategory *category = Logger::instance()->findCategory(name);\n-\n-\tif (!category) {\n-\t\tcategory = new LogCategory(name);\n-\t\tLogger::instance()->registerCategory(category);\n-\t}\n-\n-\treturn category;\n+\treturn Logger::instance()->findOrCreateCategory(name);\n }\n \n /**\n","prefixes":["v3","7/8"]}