[{"id":33419,"web_url":"https://patchwork.libcamera.org/comment/33419/","msgid":"<20250223232246.GA17974@pendragon.ideasonboard.com>","date":"2025-02-23T23:22:46","subject":"Re: [PATCH v3 7/8] libcamera: base: log: Protect log categories with\n\tlock","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Barnabás,\n\nThank you for the patch.\n\nOn Mon, Feb 17, 2025 at 06:55:05PM +0000, Barnabás Pőcze wrote:\n> Log categories may be added from any thread, so it is important to\n> synchronize access to the `Logger::categories_` list between its two\n> users: category creation (by LogCategory::create(), which calls\n> Logger::findCategory() and Logger::registerCategory()); and log level\n> setting (by Logger::logSetLevel()).\n> \n> The LogCategory::create() function uses a mutex to serialize category\n> creation, but Logger::logSetLevel() can access `Logger::categories_`\n> concurrently without any protection. To fix the issue, move the mutex to\n> the Logger class, and use it to protect all accesses to the categories\n> list. This requires moving all the logic of LogCategory::create() to a\n> new Logger::findOrCreateCategory() function that combines both\n> Logger::findCategory() and Logger::registerCategory() in order to make\n> the two operations exacute atomically.\n> \n> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n> Reviewed-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(-)\n> \n> diff --git a/include/libcamera/base/log.h b/include/libcamera/base/log.h\n> index 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_;\n> diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\n> index 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\ncamelCase please. You could name is categoryName.\n\nAny reason to not keep\n\n\tconst std::string &categoryName = category->name();\n\nhere ? Both should be fine, but if Category::name() were ever changef to\nreturn a std::string by value, the code above would lead to a\nuse-after-free. Up to you.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\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>  /**","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 6A6E7C324E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 23 Feb 2025 23:23:05 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8364C686BA;\n\tMon, 24 Feb 2025 00:23:04 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BB7DF6185A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 24 Feb 2025 00:23:03 +0100 (CET)","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 88B62353;\n\tMon, 24 Feb 2025 00:21:37 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"Tu2o3sJY\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1740352897;\n\tbh=PVRI22ke8O9Qzj+Uzyo7cJI/Ne2+cGxAdNIe+DWVtKM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Tu2o3sJYnAyJ0g07rcUpbIt2m2Ld/6Qb51zq3DxXwskKHyVmuAmnSiqY4md5Ed/UA\n\tOVcG3Ddeh4rMxE9C8Y9p735bbJtahESvPy8UnHG29J02XFQtdYwUclXDXeUk4jh1MZ\n\tJfD2hdb8yFpJI6QhJPdl7rKsjffzkS2UNQGRlEwA=","Date":"Mon, 24 Feb 2025 01:22:46 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tJacopo Mondi <jacopo.mondi@ideasonboard.com>","Subject":"Re: [PATCH v3 7/8] libcamera: base: log: Protect log categories with\n\tlock","Message-ID":"<20250223232246.GA17974@pendragon.ideasonboard.com>","References":"<20250217185433.306833-1-pobrn@protonmail.com>\n\t<20250217185433.306833-8-pobrn@protonmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250217185433.306833-8-pobrn@protonmail.com>","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>"}}]