[{"id":21400,"web_url":"https://patchwork.libcamera.org/comment/21400/","msgid":"<YaWoeISfrZohPKh6@pendragon.ideasonboard.com>","date":"2021-11-30T04:28:40","subject":"Re: [libcamera-devel] [PATCH v2 09/11] android: camera_device: Fix\n\tvariables access without protection","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hiro,\n\nThank you for the patch.\n\nOn Mon, Nov 29, 2021 at 08:44:51PM +0900, Hirokazu Honda wrote:\n> This fixes the code accessing descriptors and\n> Camera3RequestDescriptor::pendingStreamsToProcess_ without holding\n> descriptorsMutex_ and Camera3RequestDescriptor::streamProcessMutex_\n> in CameraDevice.\n> \n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> ---\n>  src/android/camera_device.cpp | 8 +++++++-\n>  1 file changed, 7 insertions(+), 1 deletion(-)\n> \n> diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\n> index f2e0bdbd..59185e7f 100644\n> --- a/src/android/camera_device.cpp\n> +++ b/src/android/camera_device.cpp\n> @@ -422,7 +422,11 @@ void CameraDevice::stop()\n>  \tworker_.stop();\n>  \tcamera_->stop();\n>  \n> -\tdescriptors_ = {};\n> +\t{\n> +\t\tMutexLocker descriptorsLock(descriptorsMutex_);\n> +\t\tdescriptors_ = {};\n> +\t}\n> +\n>  \tstreams_.clear();\n>  \n>  \tstate_ = State::Stopped;\n> @@ -919,6 +923,8 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n>  \t\t */\n>  \t\tFrameBuffer *frameBuffer = nullptr;\n>  \t\tint acquireFence = -1;\n> +\n> +\t\tMutexLocker lock(descriptor->streamsProcessMutex_);\n\nI'd add a blank line here.\n\nIt's annoying to take a lock here, when we know that there can be no\nconcurrent access. Is there any way to tell the thread analyzer that we\nknow what we're doing ? If not, I really hope std::mutex is backed by\nfutexes, and very cheap to lock and unlock when there's no contention.\n\n>  \t\tswitch (cameraStream->type()) {\n>  \t\tcase CameraStream::Type::Mapped:\n>  \t\t\t/*","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 28717BDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Nov 2021 04:29:08 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6356F605B4;\n\tTue, 30 Nov 2021 05:29:07 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5E1C7604FC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 05:29:05 +0100 (CET)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D4CBA8F0;\n\tTue, 30 Nov 2021 05:29:04 +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=\"rRNzwFpg\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638246545;\n\tbh=i8hEW9v1NoLtgRkeE9tUlQ8Mswq9O6ZEKoreQT9MDJ8=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=rRNzwFpg0Q9ec+NB1WKq4ludOyj8Osc4XRCZB+yUG5ufNVbI+DCGO1lHj3vO27VXa\n\t0gTscNe9F+Z3svH12xqycwf0exFcUooPGgRXPGzCshgpNFLEKAvKKNNL5QfE+RRV9w\n\tTmxET4EcQSRs0vnSwEUcHcI+PUCEEZOutjxI4hxw=","Date":"Tue, 30 Nov 2021 06:28:40 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YaWoeISfrZohPKh6@pendragon.ideasonboard.com>","References":"<20211129114453.3186042-1-hiroh@chromium.org>\n\t<20211129114453.3186042-10-hiroh@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211129114453.3186042-10-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [PATCH v2 09/11] android: camera_device: Fix\n\tvariables access without protection","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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":21457,"web_url":"https://patchwork.libcamera.org/comment/21457/","msgid":"<CAO5uPHPYrK_P1HG-5M2Uvksr6ASOOi+CZDQ9pgXk=pmK-h3bEg@mail.gmail.com>","date":"2021-11-30T13:12:26","subject":"Re: [libcamera-devel] [PATCH v2 09/11] android: camera_device: Fix\n\tvariables access without protection","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/people/63/","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"content":"Hi Laurent,\n\nOn Tue, Nov 30, 2021 at 1:29 PM Laurent Pinchart\n<laurent.pinchart@ideasonboard.com> wrote:\n>\n> Hi Hiro,\n>\n> Thank you for the patch.\n>\n> On Mon, Nov 29, 2021 at 08:44:51PM +0900, Hirokazu Honda wrote:\n> > This fixes the code accessing descriptors and\n> > Camera3RequestDescriptor::pendingStreamsToProcess_ without holding\n> > descriptorsMutex_ and Camera3RequestDescriptor::streamProcessMutex_\n> > in CameraDevice.\n> >\n> > Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> > ---\n> >  src/android/camera_device.cpp | 8 +++++++-\n> >  1 file changed, 7 insertions(+), 1 deletion(-)\n> >\n> > diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\n> > index f2e0bdbd..59185e7f 100644\n> > --- a/src/android/camera_device.cpp\n> > +++ b/src/android/camera_device.cpp\n> > @@ -422,7 +422,11 @@ void CameraDevice::stop()\n> >       worker_.stop();\n> >       camera_->stop();\n> >\n> > -     descriptors_ = {};\n> > +     {\n> > +             MutexLocker descriptorsLock(descriptorsMutex_);\n> > +             descriptors_ = {};\n> > +     }\n> > +\n> >       streams_.clear();\n> >\n> >       state_ = State::Stopped;\n> > @@ -919,6 +923,8 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques\n> >                */\n> >               FrameBuffer *frameBuffer = nullptr;\n> >               int acquireFence = -1;\n> > +\n> > +             MutexLocker lock(descriptor->streamsProcessMutex_);\n>\n> I'd add a blank line here.\n>\n> It's annoying to take a lock here, when we know that there can be no\n> concurrent access. Is there any way to tell the thread analyzer that we\n> know what we're doing ? If not, I really hope std::mutex is backed by\n> futexes, and very cheap to lock and unlock when there's no contention.\n>\n\nI don't think there is no annotation for that.\nThe workaround is to factorize this part and annotate with\nNO_THREAD_SAFETY_ANALYSIS [1], or do this in Descriptor\nconstructor[2].\n[1] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#no-thread-safety-analysis\n[2] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#no-checking-inside-constructors-and-destructors\n\n-Hiro\n> >               switch (cameraStream->type()) {\n> >               case CameraStream::Type::Mapped:\n> >                       /*\n>\n> --\n> Regards,\n>\n> Laurent Pinchart","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 E53FCBDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Nov 2021 13:12:39 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E90A46071A;\n\tTue, 30 Nov 2021 14:12:38 +0100 (CET)","from mail-ed1-x529.google.com (mail-ed1-x529.google.com\n\t[IPv6:2a00:1450:4864:20::529])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B0698605C4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 14:12:37 +0100 (CET)","by mail-ed1-x529.google.com with SMTP id o20so86108443eds.10\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 05:12:37 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"j2Ex6TJH\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=mime-version:references:in-reply-to:from:date:message-id:subject:to\n\t:cc; bh=BPonhtPOqDJ1pB92F4xqovDuYC/I3V26aHfOxtUoDdQ=;\n\tb=j2Ex6TJHceEhTNir8FbgcSlrkO9m3p1yWWuwb9T+FJQ3B3F0z/IoibQJbgGtk9i2N6\n\tPXtY4Y0z1Uj8zA+BH5Y3gY5G0oaRSfUzJBUuebH6j/EE3iDRVKQmJ7nv446lofrWZUra\n\tzmm525Hkg32qAa7ITlqzSxpIe26dZFgCCvc3g=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:mime-version:references:in-reply-to:from:date\n\t:message-id:subject:to:cc;\n\tbh=BPonhtPOqDJ1pB92F4xqovDuYC/I3V26aHfOxtUoDdQ=;\n\tb=4EhAIq8hJmJkmQlIUV23WP6I13aIz4o/iQZFjUFOgomqA0vwii7J87Xbsn+4kAbC0w\n\tekw3C0hi2ARC28DBiMcvfNLMyu6dC2LQfms7KZqcW8HPtW8Lp73HvNqhUCKSV+jnxmj7\n\tlYDk6cOodzpMLnxgE80t9MgT6V5q8SiNA9sY8DEOsj6VhAuropMT1nc69anGZmnAX9Zi\n\tVTlAGO3SLMhCuzPulZ37HPl39NCPL1yh5aiMD2Zf0Iso4yecbkGel1/Gw53Mn8cEQj2V\n\tCOWKV37FoKVq0luS463qb6qql+tspvPh+7pLG+opZwTQQi1v09LbqF5WFwkQ+1Cg4zIn\n\tr2xQ==","X-Gm-Message-State":"AOAM533xCbz6tM1XcOO0LCyU92VamxXNe6n/mlOYuiiLAVP/CPIV9Zbs\n\tFZaA6SJjFR28H5tO5OhQkZVHquQTRa0SgJSMsPLsuU21LUR3tw==","X-Google-Smtp-Source":"ABdhPJw6baWqqR9tgVEG1jcxSiqiZlgxnrgM2MdvIUdwlEEy6NxuwqKO30mBMxHCIMLzm4WCfZ4c+HMcbWsLuy/wkUA=","X-Received":"by 2002:a05:6402:1395:: with SMTP id\n\tb21mr84709247edv.299.1638277957230; \n\tTue, 30 Nov 2021 05:12:37 -0800 (PST)","MIME-Version":"1.0","References":"<20211129114453.3186042-1-hiroh@chromium.org>\n\t<20211129114453.3186042-10-hiroh@chromium.org>\n\t<YaWoeISfrZohPKh6@pendragon.ideasonboard.com>","In-Reply-To":"<YaWoeISfrZohPKh6@pendragon.ideasonboard.com>","From":"Hirokazu Honda <hiroh@chromium.org>","Date":"Tue, 30 Nov 2021 22:12:26 +0900","Message-ID":"<CAO5uPHPYrK_P1HG-5M2Uvksr6ASOOi+CZDQ9pgXk=pmK-h3bEg@mail.gmail.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Content-Type":"text/plain; charset=\"UTF-8\"","Subject":"Re: [libcamera-devel] [PATCH v2 09/11] android: camera_device: Fix\n\tvariables access without protection","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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]