From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=3.0 tests=BAYES_00,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_2 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BD9BC433DB for ; Sat, 27 Feb 2021 15:19:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 17ACD64E7A for ; Sat, 27 Feb 2021 15:19:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230102AbhB0PTi (ORCPT ); Sat, 27 Feb 2021 10:19:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:46530 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229953AbhB0PTf (ORCPT ); Sat, 27 Feb 2021 10:19:35 -0500 Received: from archlinux (cpc108967-cmbg20-2-0-cust86.5-4.cable.virginm.net [81.101.6.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 97A6C64E46; Sat, 27 Feb 2021 15:18:51 +0000 (UTC) Date: Sat, 27 Feb 2021 15:18:47 +0000 From: Jonathan Cameron To: William Breathitt Gray Cc: kernel@pengutronix.de, linux-stm32@st-md-mailman.stormreply.com, a.fatoum@pengutronix.de, kamel.bouhara@bootlin.com, gwendal@chromium.org, alexandre.belloni@bootlin.com, david@lechnology.com, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, syednwaris@gmail.com, patrick.havelange@essensium.com, fabrice.gasnier@st.com, mcoquelin.stm32@gmail.com, alexandre.torgue@st.com, o.rempel@pengutronix.de Subject: Re: [PATCH v8 20/22] counter: Implement events_queue_size sysfs attribute Message-ID: <20210227151847.074864df@archlinux> In-Reply-To: References: <013b2b8682ddc3c85038083e6d5567696b6254b3.1613131238.git.vilhelm.gray@gmail.com> <20210214181146.66d43da7@archlinux> <20210221155140.3e1ef13c@archlinux> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 26 Feb 2021 09:03:48 +0900 William Breathitt Gray wrote: > On Sun, Feb 21, 2021 at 03:51:40PM +0000, Jonathan Cameron wrote: > > On Thu, 18 Feb 2021 19:32:16 +0900 > > William Breathitt Gray wrote: > > > > > On Sun, Feb 14, 2021 at 06:11:46PM +0000, Jonathan Cameron wrote: > > > > On Fri, 12 Feb 2021 21:13:44 +0900 > > > > William Breathitt Gray wrote: > > > > > > > > > The events_queue_size sysfs attribute provides a way for users to > > > > > dynamically configure the Counter events queue size for the Counter > > > > > character device interface. The size is in number of struct > > > > > counter_event data structures. The number of elements will be rounded-up > > > > > to a power of 2 due to a requirement of the kfifo_alloc function called > > > > > during reallocation of the queue. > > > > > > > > > > Cc: Oleksij Rempel > > > > > Signed-off-by: William Breathitt Gray > > > > > --- > > > > > Documentation/ABI/testing/sysfs-bus-counter | 8 +++++++ > > > > > drivers/counter/counter-chrdev.c | 23 +++++++++++++++++++ > > > > > drivers/counter/counter-chrdev.h | 2 ++ > > > > > drivers/counter/counter-sysfs.c | 25 +++++++++++++++++++++ > > > > > 4 files changed, 58 insertions(+) > > > > > > > > > > diff --git a/Documentation/ABI/testing/sysfs-bus-counter b/Documentation/ABI/testing/sysfs-bus-counter > > > > > index 847e96f19d19..f6cb2a8b08a7 100644 > > > > > --- a/Documentation/ABI/testing/sysfs-bus-counter > > > > > +++ b/Documentation/ABI/testing/sysfs-bus-counter > > > > > @@ -212,6 +212,14 @@ Description: > > > > > both edges: > > > > > Any state transition. > > > > > > > > > > +What: /sys/bus/counter/devices/counterX/events_queue_size > > > > > +KernelVersion: 5.13 > > > > > +Contact: linux-iio@vger.kernel.org > > > > > +Description: > > > > > + Size of the Counter events queue in number of struct > > > > > + counter_event data structures. The number of elements will be > > > > > + rounded-up to a power of 2. > > > > > + > > > > > What: /sys/bus/counter/devices/counterX/name > > > > > KernelVersion: 5.2 > > > > > Contact: linux-iio@vger.kernel.org > > > > > diff --git a/drivers/counter/counter-chrdev.c b/drivers/counter/counter-chrdev.c > > > > > index 16f02df7f73d..53eea894e13f 100644 > > > > > --- a/drivers/counter/counter-chrdev.c > > > > > +++ b/drivers/counter/counter-chrdev.c > > > > > @@ -375,6 +375,29 @@ void counter_chrdev_remove(struct counter_device *const counter) > > > > > cdev_del(&counter->chrdev); > > > > > } > > > > > > > > > > +int counter_chrdev_realloc_queue(struct counter_device *const counter, > > > > > + size_t queue_size) > > > > > +{ > > > > > + int err; > > > > > + DECLARE_KFIFO_PTR(events, struct counter_event); > > > > > + unsigned long flags; > > > > > + > > > > > + /* Allocate new events queue */ > > > > > + err = kfifo_alloc(&events, queue_size, GFP_ATOMIC); > > > > > > > > Is there any potential for losing events? > > > > > > We take the events_list_lock down below so we're safe against missing an > > > event, but past events currently unread in the queue will be lost. > > > > > > Shortening the size of the queue is inherently a destructive process if > > > we have more events in the current queue than can fit in the new queue. > > > Because we a liable to lose some events in such a case, I think it's > > > best to keep the behavior of this reallocation consistent and have it > > > provide a fresh empty queue every time, as opposed to sometimes dropping > > > events and sometimes not. > > > > > > I also suspect an actual user would be setting the size of their queue > > > to the required amount before they begin watching events, rather than > > > adjusting it sporadically during a live operation. > > > > > > > Absolutely agree. As such I wonder if you are better off enforcing this > > behaviour? If the cdev is open for reading, don't allow the fifo to be > > resized. > > > > Jonathan > > I can't really think of a good reason not to, so let's enforce it: if > the cdev is open, then we'll return an EINVAL if the user attempts to > resize the queue. > > What is a good way to check for this condition? Should I just call > kref_read() and see if it's greater than 1? For example, in > counter_chrdev_realloc_queue(): > > if (kref_read(&counter->dev.kobj.kref) > 1) > return -EINVAL; In theory at least you might want the kobj.kref to be incremented for other reasons than just open. So to keep different concepts separate, perhaps it's worth a separate variable somewhere to track whether the file is open currently. However, it's reasonable (I think) to assume the kref will have a minimum value if open, so perhaps what you suggest works fine. Jonathan > > William Breathitt Gray From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_2 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 567D4C433E0 for ; Sat, 27 Feb 2021 15:20:10 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0CDDC64E09 for ; Sat, 27 Feb 2021 15:20:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0CDDC64E09 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Bj7+xe8viTFvqbMelp4Vh75EEKsPb+wXg3xnC94XSEk=; b=pp76gpdTx/ObdI8ymk/DjtPpO RBaykBTWBRphUKUsYeUmvjckiFYSj3m0f6pvVSBx78kWniDVoYTxpWk8m27mjDMvVYvteapgVQ+KA Yzk5DXlLDxuWob3L1g/UFwQ+k7QVf+tn/B659lTvFrALwG1n5qDhN8ZvrwRg7xZp/IgD0WDHcdzrP Zu93FSnfXo5jtdTlB4SXAvOE1XJ/1OiurSSeRb5I0woz/gqe0GaEw38ze8H7frarmJ2rQS2WcgmrF MLQl1o2dmMV60sJRmUWzWPchqHgAS4r5AXx0456ZJsS+a/jj56mjwy60ENLqbEtXkpVfLbLiPNfyx W2Wj/l+/g==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1lG1Mv-00015I-SV; Sat, 27 Feb 2021 15:18:57 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1lG1Mt-00014s-2n for linux-arm-kernel@lists.infradead.org; Sat, 27 Feb 2021 15:18:56 +0000 Received: from archlinux (cpc108967-cmbg20-2-0-cust86.5-4.cable.virginm.net [81.101.6.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 97A6C64E46; Sat, 27 Feb 2021 15:18:51 +0000 (UTC) Date: Sat, 27 Feb 2021 15:18:47 +0000 From: Jonathan Cameron To: William Breathitt Gray Subject: Re: [PATCH v8 20/22] counter: Implement events_queue_size sysfs attribute Message-ID: <20210227151847.074864df@archlinux> In-Reply-To: References: <013b2b8682ddc3c85038083e6d5567696b6254b3.1613131238.git.vilhelm.gray@gmail.com> <20210214181146.66d43da7@archlinux> <20210221155140.3e1ef13c@archlinux> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210227_101855_277549_BFD05644 X-CRM114-Status: GOOD ( 36.66 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kamel.bouhara@bootlin.com, gwendal@chromium.org, a.fatoum@pengutronix.de, david@lechnology.com, linux-iio@vger.kernel.org, patrick.havelange@essensium.com, alexandre.belloni@bootlin.com, mcoquelin.stm32@gmail.com, linux-kernel@vger.kernel.org, o.rempel@pengutronix.de, kernel@pengutronix.de, fabrice.gasnier@st.com, syednwaris@gmail.com, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, alexandre.torgue@st.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, 26 Feb 2021 09:03:48 +0900 William Breathitt Gray wrote: > On Sun, Feb 21, 2021 at 03:51:40PM +0000, Jonathan Cameron wrote: > > On Thu, 18 Feb 2021 19:32:16 +0900 > > William Breathitt Gray wrote: > > > > > On Sun, Feb 14, 2021 at 06:11:46PM +0000, Jonathan Cameron wrote: > > > > On Fri, 12 Feb 2021 21:13:44 +0900 > > > > William Breathitt Gray wrote: > > > > > > > > > The events_queue_size sysfs attribute provides a way for users to > > > > > dynamically configure the Counter events queue size for the Counter > > > > > character device interface. The size is in number of struct > > > > > counter_event data structures. The number of elements will be rounded-up > > > > > to a power of 2 due to a requirement of the kfifo_alloc function called > > > > > during reallocation of the queue. > > > > > > > > > > Cc: Oleksij Rempel > > > > > Signed-off-by: William Breathitt Gray > > > > > --- > > > > > Documentation/ABI/testing/sysfs-bus-counter | 8 +++++++ > > > > > drivers/counter/counter-chrdev.c | 23 +++++++++++++++++++ > > > > > drivers/counter/counter-chrdev.h | 2 ++ > > > > > drivers/counter/counter-sysfs.c | 25 +++++++++++++++++++++ > > > > > 4 files changed, 58 insertions(+) > > > > > > > > > > diff --git a/Documentation/ABI/testing/sysfs-bus-counter b/Documentation/ABI/testing/sysfs-bus-counter > > > > > index 847e96f19d19..f6cb2a8b08a7 100644 > > > > > --- a/Documentation/ABI/testing/sysfs-bus-counter > > > > > +++ b/Documentation/ABI/testing/sysfs-bus-counter > > > > > @@ -212,6 +212,14 @@ Description: > > > > > both edges: > > > > > Any state transition. > > > > > > > > > > +What: /sys/bus/counter/devices/counterX/events_queue_size > > > > > +KernelVersion: 5.13 > > > > > +Contact: linux-iio@vger.kernel.org > > > > > +Description: > > > > > + Size of the Counter events queue in number of struct > > > > > + counter_event data structures. The number of elements will be > > > > > + rounded-up to a power of 2. > > > > > + > > > > > What: /sys/bus/counter/devices/counterX/name > > > > > KernelVersion: 5.2 > > > > > Contact: linux-iio@vger.kernel.org > > > > > diff --git a/drivers/counter/counter-chrdev.c b/drivers/counter/counter-chrdev.c > > > > > index 16f02df7f73d..53eea894e13f 100644 > > > > > --- a/drivers/counter/counter-chrdev.c > > > > > +++ b/drivers/counter/counter-chrdev.c > > > > > @@ -375,6 +375,29 @@ void counter_chrdev_remove(struct counter_device *const counter) > > > > > cdev_del(&counter->chrdev); > > > > > } > > > > > > > > > > +int counter_chrdev_realloc_queue(struct counter_device *const counter, > > > > > + size_t queue_size) > > > > > +{ > > > > > + int err; > > > > > + DECLARE_KFIFO_PTR(events, struct counter_event); > > > > > + unsigned long flags; > > > > > + > > > > > + /* Allocate new events queue */ > > > > > + err = kfifo_alloc(&events, queue_size, GFP_ATOMIC); > > > > > > > > Is there any potential for losing events? > > > > > > We take the events_list_lock down below so we're safe against missing an > > > event, but past events currently unread in the queue will be lost. > > > > > > Shortening the size of the queue is inherently a destructive process if > > > we have more events in the current queue than can fit in the new queue. > > > Because we a liable to lose some events in such a case, I think it's > > > best to keep the behavior of this reallocation consistent and have it > > > provide a fresh empty queue every time, as opposed to sometimes dropping > > > events and sometimes not. > > > > > > I also suspect an actual user would be setting the size of their queue > > > to the required amount before they begin watching events, rather than > > > adjusting it sporadically during a live operation. > > > > > > > Absolutely agree. As such I wonder if you are better off enforcing this > > behaviour? If the cdev is open for reading, don't allow the fifo to be > > resized. > > > > Jonathan > > I can't really think of a good reason not to, so let's enforce it: if > the cdev is open, then we'll return an EINVAL if the user attempts to > resize the queue. > > What is a good way to check for this condition? Should I just call > kref_read() and see if it's greater than 1? For example, in > counter_chrdev_realloc_queue(): > > if (kref_read(&counter->dev.kobj.kref) > 1) > return -EINVAL; In theory at least you might want the kobj.kref to be incremented for other reasons than just open. So to keep different concepts separate, perhaps it's worth a separate variable somewhere to track whether the file is open currently. However, it's reasonable (I think) to assume the kref will have a minimum value if open, so perhaps what you suggest works fine. Jonathan > > William Breathitt Gray _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel