[{"id":2234,"web_url":"https://patchwork.libcamera.org/comment/2234/","msgid":"<20190712115832.GA31080@localhost.localdomain>","date":"2019-07-12T11:58:32","subject":"Re: [libcamera-devel] [PATCH] libcamera: event_notifier_poll: Fix\n\tnotifier unregistration during event processing","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"content":"Hi Laurent,\n\nThanks for the patch.\n\nOn Fri, Jul 12, 2019 at 02:04:28PM +0300, Laurent Pinchart wrote:\n> An event notifier may be unregistered from its activated signal. This\n> can cause the notifiers set entry in notifiers_ to be deleted while\n> processNotifiers() is looping over the notifiers_ map, leading to\n> problems.\n> \n> To fix this, add a flag to the EventNotifierPoll class to indicate that\n> event processing is in progress. If the flag is set, the notifiers_\n> entry is not deleted during notifier unregistration, but will be deleted\n> by the event processing loop.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nLooks good to me.\n\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n\n> ---\n>  src/libcamera/event_dispatcher_poll.cpp       | 18 ++++++++++++++++++\n>  src/libcamera/include/event_dispatcher_poll.h |  2 ++\n>  2 files changed, 20 insertions(+)\n> \n> diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp\n> index df9dffb2326c..4f15f3e3269b 100644\n> --- a/src/libcamera/event_dispatcher_poll.cpp\n> +++ b/src/libcamera/event_dispatcher_poll.cpp\n> @@ -46,6 +46,7 @@ static const char *notifierType(EventNotifier::Type type)\n>   */\n>  \n>  EventDispatcherPoll::EventDispatcherPoll()\n> +\t: processingEvents_(false)\n>  {\n>  \t/*\n>  \t * Create the event fd. Failures are fatal as we can't implement an\n> @@ -96,6 +97,15 @@ void EventDispatcherPoll::unregisterEventNotifier(EventNotifier *notifier)\n>  \t}\n>  \n>  \tset.notifiers[type] = nullptr;\n> +\n> +\t/*\n> +\t * Don't race with event processing if this method is called from an\n> +\t * event notifier. The notifiers_ entry will be erased by\n> +\t * processEvents().\n> +\t */\n> +\tif (processingEvents_)\n> +\t\treturn;\n> +\n>  \tif (!set.notifiers[0] && !set.notifiers[1] && !set.notifiers[2])\n>  \t\tnotifiers_.erase(iter);\n>  }\n> @@ -241,6 +251,8 @@ void EventDispatcherPoll::processNotifiers(const std::vector<struct pollfd> &pol\n>  \t\t{ EventNotifier::Exception, POLLPRI },\n>  \t};\n>  \n> +\tprocessingEvents_ = true;\n> +\n>  \tfor (const pollfd &pfd : pollfds) {\n>  \t\tauto iter = notifiers_.find(pfd.fd);\n>  \t\tASSERT(iter != notifiers_.end());\n> @@ -269,7 +281,13 @@ void EventDispatcherPoll::processNotifiers(const std::vector<struct pollfd> &pol\n>  \t\t\tif (pfd.revents & event.events)\n>  \t\t\t\tnotifier->activated.emit(notifier);\n>  \t\t}\n> +\n> +\t\t/* Erase the notifiers_ entry if it is now empty. */\n> +\t\tif (!set.notifiers[0] && !set.notifiers[1] && !set.notifiers[2])\n> +\t\t\tnotifiers_.erase(iter);\n>  \t}\n> +\n> +\tprocessingEvents_ = false;\n>  }\n>  \n>  void EventDispatcherPoll::processTimers()\n> diff --git a/src/libcamera/include/event_dispatcher_poll.h b/src/libcamera/include/event_dispatcher_poll.h\n> index 14c3eea13b5e..d82b302c4aea 100644\n> --- a/src/libcamera/include/event_dispatcher_poll.h\n> +++ b/src/libcamera/include/event_dispatcher_poll.h\n> @@ -45,6 +45,8 @@ private:\n>  \tstd::list<Timer *> timers_;\n>  \tint eventfd_;\n>  \n> +\tbool processingEvents_;\n> +\n>  \tint poll(std::vector<struct pollfd> *pollfds);\n>  \tvoid processInterrupt(const struct pollfd &pfd);\n>  \tvoid processNotifiers(const std::vector<struct pollfd> &pollfds);\n> -- \n> Regards,\n> \n> Laurent Pinchart\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<paul.elder@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 909C26156F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 12 Jul 2019 13:58:42 +0200 (CEST)","from localhost.localdomain (softbank126209254147.bbtec.net\n\t[126.209.254.147])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D72252B2;\n\tFri, 12 Jul 2019 13:58:40 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1562932722;\n\tbh=bbIyC1gLYJEtEkbocvdoRecgr+v4ZRckiBSkfbqvWXc=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=SW7Pb/vhNs1yiwoxTdb9UdsFmnue4g1a8Vp58Xp9TqHAjrJmjORMyC5048gFxfB+4\n\tQZ83O91UKTa4SSe/o/1yqYX/pbM8DeY5t9vW3MBgQu7zwSb8uP1rd1lX6EWzhqVYSZ\n\tyTFk4HZi++Di/uYHJ9+3zchcBxlVHabKVrS6DOc8=","Date":"Fri, 12 Jul 2019 20:58:32 +0900","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190712115832.GA31080@localhost.localdomain>","References":"<20190712110428.20433-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20190712110428.20433-1-laurent.pinchart@ideasonboard.com>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH] libcamera: event_notifier_poll: Fix\n\tnotifier unregistration during event processing","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Fri, 12 Jul 2019 11:58:42 -0000"}},{"id":2239,"web_url":"https://patchwork.libcamera.org/comment/2239/","msgid":"<20190713044420.GA15070@wyvern>","date":"2019-07-13T04:44:20","subject":"Re: [libcamera-devel] [PATCH] libcamera: event_notifier_poll: Fix\n\tnotifier unregistration during event processing","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Laurent,\n\nThanks for your work.\n\nOn 2019-07-12 14:04:28 +0300, Laurent Pinchart wrote:\n> An event notifier may be unregistered from its activated signal. This\n> can cause the notifiers set entry in notifiers_ to be deleted while\n> processNotifiers() is looping over the notifiers_ map, leading to\n> problems.\n> \n> To fix this, add a flag to the EventNotifierPoll class to indicate that\n> event processing is in progress. If the flag is set, the notifiers_\n> entry is not deleted during notifier unregistration, but will be deleted\n> by the event processing loop.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n\n> ---\n>  src/libcamera/event_dispatcher_poll.cpp       | 18 ++++++++++++++++++\n>  src/libcamera/include/event_dispatcher_poll.h |  2 ++\n>  2 files changed, 20 insertions(+)\n> \n> diff --git a/src/libcamera/event_dispatcher_poll.cpp b/src/libcamera/event_dispatcher_poll.cpp\n> index df9dffb2326c..4f15f3e3269b 100644\n> --- a/src/libcamera/event_dispatcher_poll.cpp\n> +++ b/src/libcamera/event_dispatcher_poll.cpp\n> @@ -46,6 +46,7 @@ static const char *notifierType(EventNotifier::Type type)\n>   */\n>  \n>  EventDispatcherPoll::EventDispatcherPoll()\n> +\t: processingEvents_(false)\n>  {\n>  \t/*\n>  \t * Create the event fd. Failures are fatal as we can't implement an\n> @@ -96,6 +97,15 @@ void EventDispatcherPoll::unregisterEventNotifier(EventNotifier *notifier)\n>  \t}\n>  \n>  \tset.notifiers[type] = nullptr;\n> +\n> +\t/*\n> +\t * Don't race with event processing if this method is called from an\n> +\t * event notifier. The notifiers_ entry will be erased by\n> +\t * processEvents().\n> +\t */\n> +\tif (processingEvents_)\n> +\t\treturn;\n> +\n>  \tif (!set.notifiers[0] && !set.notifiers[1] && !set.notifiers[2])\n>  \t\tnotifiers_.erase(iter);\n>  }\n> @@ -241,6 +251,8 @@ void EventDispatcherPoll::processNotifiers(const std::vector<struct pollfd> &pol\n>  \t\t{ EventNotifier::Exception, POLLPRI },\n>  \t};\n>  \n> +\tprocessingEvents_ = true;\n> +\n>  \tfor (const pollfd &pfd : pollfds) {\n>  \t\tauto iter = notifiers_.find(pfd.fd);\n>  \t\tASSERT(iter != notifiers_.end());\n> @@ -269,7 +281,13 @@ void EventDispatcherPoll::processNotifiers(const std::vector<struct pollfd> &pol\n>  \t\t\tif (pfd.revents & event.events)\n>  \t\t\t\tnotifier->activated.emit(notifier);\n>  \t\t}\n> +\n> +\t\t/* Erase the notifiers_ entry if it is now empty. */\n> +\t\tif (!set.notifiers[0] && !set.notifiers[1] && !set.notifiers[2])\n> +\t\t\tnotifiers_.erase(iter);\n>  \t}\n> +\n> +\tprocessingEvents_ = false;\n>  }\n>  \n>  void EventDispatcherPoll::processTimers()\n> diff --git a/src/libcamera/include/event_dispatcher_poll.h b/src/libcamera/include/event_dispatcher_poll.h\n> index 14c3eea13b5e..d82b302c4aea 100644\n> --- a/src/libcamera/include/event_dispatcher_poll.h\n> +++ b/src/libcamera/include/event_dispatcher_poll.h\n> @@ -45,6 +45,8 @@ private:\n>  \tstd::list<Timer *> timers_;\n>  \tint eventfd_;\n>  \n> +\tbool processingEvents_;\n> +\n>  \tint poll(std::vector<struct pollfd> *pollfds);\n>  \tvoid processInterrupt(const struct pollfd &pfd);\n>  \tvoid processNotifiers(const std::vector<struct pollfd> &pollfds);\n> -- \n> Regards,\n> \n> Laurent Pinchart\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-pl1-x644.google.com (mail-pl1-x644.google.com\n\t[IPv6:2607:f8b0:4864:20::644])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 707B26156A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 13 Jul 2019 06:44:25 +0200 (CEST)","by mail-pl1-x644.google.com with SMTP id k8so5752398plt.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 12 Jul 2019 21:44:25 -0700 (PDT)","from localhost (softbank126209254147.bbtec.net. [126.209.254.147])\n\tby smtp.gmail.com with ESMTPSA id\n\tp67sm13209820pfg.124.2019.07.12.21.44.22\n\t(version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256);\n\tFri, 12 Jul 2019 21:44:22 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to\n\t:user-agent; bh=F3ZgVrVlClByqkloEdvctZOtMUKjMSOZ3td6tltgF54=;\n\tb=CcARSeRj+lNYScaPYddVDO+RzPCkd18ForyXQQVOlSqWOvhCB23MD7mCQdEtDeL2Wl\n\tQeJqC5C9uQqn4LX9OxSfT+yO5Mlstc57LhQRlXT1VpWt6kqsydGxS5OSQxCtAtsVSk3y\n\tPIJjztBLcqlrtrlhFP+1LPyMDovLfsQT3stay4a4hD2lqo0hUi37IZDRYt0SAnkXkC88\n\thl+xnphV4m1CO1qscOFDRVBF4iIchX4w+NRHBXjcmNwZzDFTZvZixA4Ypqyug/MDM43+\n\tlTGuu2aSPlOpRrBarDv04YVKREV1HGzdC4alr1ET6MRSQ0hgPp4wLO8Ye+PUO1yRzsgU\n\tXaTg==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to:user-agent;\n\tbh=F3ZgVrVlClByqkloEdvctZOtMUKjMSOZ3td6tltgF54=;\n\tb=Wy4LDAHofHJzaUFgQPeZX0sw+46KQMoOG1Y2ubx0N40/NpPBD0zl+WYBkPSUYLO6Lj\n\tiI/ZlXxTZpTKSLkGXlc5gpCo8RKF7d1ypZLXy8ikdRci6xxjIeNoqaW3QwRCxhCcLm8A\n\tUsSW0lNs3oLeiWlt26RPTkVBrhObQ2Yf0ksNLt5zFjVFOKB/nHbK4sm9feebg3W9tu/o\n\tvFBAUIf7pzezMde9yEVS2QqP/E5bvSY/KkNB0KbSVhYlwzBKzHZ1lxBrh3aPKUp9lULe\n\t37YNvUXSUFblupGfqfHWuHag0lxSqD7MLsvOe9U8gVdoZqSzOEuj/4gVSib01PRljLVV\n\tWU3A==","X-Gm-Message-State":"APjAAAXgOBAJRjow8zdXDos87DJhj237fTldupHTdbUimcowYgwi3Cng\n\tXe56mVnwA9KkOL9FuTgYoMk=","X-Google-Smtp-Source":"APXvYqzMIPABEeckkHZRB4CD9Wm2uEfb8AZg0XKFTfEJXc1PihCAN8Bf4dy6c1+wv9tBVF6s+d/Zaw==","X-Received":"by 2002:a17:902:bd94:: with SMTP id\n\tq20mr4766390pls.307.1562993063638; \n\tFri, 12 Jul 2019 21:44:23 -0700 (PDT)","Date":"Sat, 13 Jul 2019 13:44:20 +0900","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190713044420.GA15070@wyvern>","References":"<20190712110428.20433-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190712110428.20433-1-laurent.pinchart@ideasonboard.com>","User-Agent":"Mutt/1.12.1 (2019-06-15)","Subject":"Re: [libcamera-devel] [PATCH] libcamera: event_notifier_poll: Fix\n\tnotifier unregistration during event processing","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Sat, 13 Jul 2019 04:44:25 -0000"}}]