Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp611836pxu; Sun, 25 Oct 2020 11:07:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQoW7Nf3TX8ThczCwH24C8k7So7zJtCcVlF8QMeZqLRrayDGc/lB+efgSA7GS8ttoM9LXg X-Received: by 2002:aa7:cc11:: with SMTP id q17mr11946652edt.183.1603649268086; Sun, 25 Oct 2020 11:07:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603649268; cv=none; d=google.com; s=arc-20160816; b=jN2oH5Wqlb5vnxlvBO0pBprNpKSMspQO/sxzPMlyH4LpdePcTLRqLlWuk7D33ZK6xN Aq7OEtj8LfZzVnOxkb5izdDL4UPptuS25+HW/YRpi/h7ZYcxAwUeLlTM2345ZJkjtQQt uOZ3nq6aU8ShM0GOIReIJPVbldUx9/xgcM+GzAsX4fwpKFAj4jN5k4i89r2/3Gqjgtpt jXtW4j4OQ550lQV2NrxfrJnu6L/rF8QFP1OM9dNraMa359uABJiTEwE1YcixI2IxoaTF cZnGhydwfd0Qef7dQcfcRJ3HXmjglDCQxvfGU3rahUqZz2tqz8lmI3V4TL+bgXri2OPc 345g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=WnvTU6VDuLEf+Rt2Zn1Gdt5ZWfNTNLLVWS72IF0h6Yk=; b=E251qzQvtwO2VjMlcuz8w4+hymOcn5Q8++hjeskkD4V3jl0mKfen2Lksm+18p27+Na v1GIEcS5UsZQosuMLlpabBUrKZz+cEModZbil6SIF6Oe5pLFVhF4Gs4youCNEftbSRaL GvxGVVSzbDLjA254KwlLgPLfHxh73CZJbRguxcDxj+XRuBIZdl1icQie43bIGYajZUcH tF75jMYb1dvhKhSrpTgYj04tZN4p+c/dFyxTfBE+mNbXh3IJ2ZlzXj17gXb2mSGGvwC0 3huCZEkfEHP7Jipc/erTJxfDVkTvDN/zNZN1givGcYMRsJK39N6yppm0SHt6SIucHxoN s83w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PqE+NIkC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dc11si4998263ejb.375.2020.10.25.11.07.25; Sun, 25 Oct 2020 11:07:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PqE+NIkC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1416413AbgJYRxj (ORCPT + 99 others); Sun, 25 Oct 2020 13:53:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1415571AbgJYRxj (ORCPT ); Sun, 25 Oct 2020 13:53:39 -0400 Received: from mail-qt1-x842.google.com (mail-qt1-x842.google.com [IPv6:2607:f8b0:4864:20::842]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1157C061755; Sun, 25 Oct 2020 10:53:38 -0700 (PDT) Received: by mail-qt1-x842.google.com with SMTP id j62so5147317qtd.0; Sun, 25 Oct 2020 10:53:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=WnvTU6VDuLEf+Rt2Zn1Gdt5ZWfNTNLLVWS72IF0h6Yk=; b=PqE+NIkCpidtvg9ccnzTZ8R6ZKaslUFa6jUfX78jzuqP4FeCXm1ehopZIeOpEIGvfV zUwuE0941wfby2vncTggj2xmv+C0VoORDdNOy4uXuo6t84MB+9Jla4/6B2D3nBcAmUlC aGa8wseNCWKTJIvsysxLs50d+eJ1g55WHEfTD7j9cwtlAf88IIkv6OPx1cr9xuFcHk+b 5mSZhYaceFpLEH7rkUNZgE5+zS/aHWq0j4XV3a8tEOAtltuqSLK1g4N62B+dBd1H+EAK Mi0g35eg6z2LdugxurSJut//sA73JpywiRfozrHFzOXvzEr3jekwqHflohiZowC6/zua ofvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=WnvTU6VDuLEf+Rt2Zn1Gdt5ZWfNTNLLVWS72IF0h6Yk=; b=W9etE+1hoTC9JgGR4mTquglR4B+6K4IApFE4tG+jW4DbKrI2uijDxkAXwDlQmaT6a1 3mupNEe1F75umrs87VOvOI2nSsAJXn5oD33rfBpKtwLRugDJte3USTBQWKzVIjzVx2Us Obm1l6cl25Mnx4nGWzrm4etOfA+gsUSNNLuxItKYhoFmXdjgJP+8vnTyGvbZOYgnLrAP ik5sC+BmjOzZMGZPwqEHaUh29GDi8If0DilvJ/KuLbCIA31Ely+OsVi0oLBKn9Kvp5Yj 6CYAF0L5xOTUW7+44R+ZrE6N4t8CuyLiYduV7Easx1HrqxZVTS2ts0bu0Fqui6O57H27 vwlA== X-Gm-Message-State: AOAM531x6nVg2YUadVnzvBDQOEDEmU6uKxnK5VygJwSj+E5dTZrpYM5V zb2Qcl4CDnBWkrzfv2Vnx28= X-Received: by 2002:ac8:7c97:: with SMTP id y23mr13455309qtv.48.1603648418004; Sun, 25 Oct 2020 10:53:38 -0700 (PDT) Received: from shinobu (072-189-064-225.res.spectrum.com. [72.189.64.225]) by smtp.gmail.com with ESMTPSA id k64sm4949218qkc.97.2020.10.25.10.53.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Oct 2020 10:53:36 -0700 (PDT) Date: Sun, 25 Oct 2020 13:53:22 -0400 From: William Breathitt Gray To: David Lechner Cc: jic23@kernel.org, kamel.bouhara@bootlin.com, gwendal@chromium.org, alexandre.belloni@bootlin.com, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, 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 Subject: Re: [PATCH v5 3/5] counter: Add character device interface Message-ID: <20201025175322.GA14219@shinobu> References: <00be1fccc672c5207f3b04fe4cc09c29e22641f4.1601170670.git.vilhelm.gray@gmail.com> <20201018165822.GE231549@shinobu> <20201025131809.GB3458@shinobu> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="NzB8fVQJ5HfG6fxh" Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --NzB8fVQJ5HfG6fxh Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Oct 25, 2020 at 11:34:43AM -0500, David Lechner wrote: > On 10/25/20 8:18 AM, William Breathitt Gray wrote: > > On Tue, Oct 20, 2020 at 11:06:42AM -0500, David Lechner wrote: > >> On 10/18/20 11:58 AM, William Breathitt Gray wrote: > >>> On Wed, Oct 14, 2020 at 05:40:44PM -0500, David Lechner wrote: > >>>> On 9/26/20 9:18 PM, William Breathitt Gray wrote: > >>>>> +static ssize_t counter_chrdev_read(struct file *filp, char __user = *buf, > >>>>> + size_t len, loff_t *f_ps) > >>>>> +{ > >>>>> + struct counter_device *const counter =3D filp->private_data; > >>>>> + int err; > >>>>> + unsigned long flags; > >>>>> + unsigned int copied; > >>>>> + > >>>>> + if (len < sizeof(struct counter_event)) > >>>>> + return -EINVAL; > >>>>> + > >>>>> + do { > >>>>> + if (kfifo_is_empty(&counter->events)) { > >>>>> + if (filp->f_flags & O_NONBLOCK) > >>>>> + return -EAGAIN; > >>>>> + > >>>>> + err =3D wait_event_interruptible(counter->events_wait, > >>>>> + !kfifo_is_empty(&counter->events)); > >>>>> + if (err) > >>>>> + return err; > >>>>> + } > >>>>> + > >>>>> + raw_spin_lock_irqsave(&counter->events_lock, flags); > >>>>> + err =3D kfifo_to_user(&counter->events, buf, len, &copied); > >>>>> + raw_spin_unlock_irqrestore(&counter->events_lock, flags); > >>>>> + if (err) > >>>>> + return err; > >>>>> + } while (!copied); > >>>>> + > >>>>> + return copied; > >>>>> +} > >>>> > >>>> All other uses of kfifo_to_user() I saw use a mutex instead of spin > >>>> lock. I don't see a reason for disabling interrupts here. > >>> > >>> The Counter character device interface is special in this case because > >>> counter->events could be accessed from an interrupt context. This is > >>> possible if counter_push_event() is called for an interrupt (as is the > >>> case for the 104_quad_8 driver). In this case, we can't use mutex > >>> because we can't sleep in an interrupt context, so our only option is= to > >>> use spin lock. > >>> > >> > >> > >> The way I understand it, locking is only needed for concurrent readers > >> and locking between reader and writer is not needed. > >=20 > > You're right, it does say in the kfifo.h comments that with only one > > concurrent reader and one current write, we don't need extra locking to > > use these macros. Because we only have one kfifo_to_user() operating on > > counter->events, does that mean we don't need locking at all here for > > the counter_chrdev_read() function? > >=20 > > William Breathitt Gray > >=20 >=20 > Even if we have the policy that only one file handle to the chrdev > can be open at a time, it is still possible that the it could be > read from multiple threads. So it I think it makes sense to keep > it just to be safe. All right, I'll keep the locks in the code for now to keep it safe in case we have multiple threads reading. William Breathitt Gray --NzB8fVQJ5HfG6fxh Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEk5I4PDJ2w1cDf/bghvpINdm7VJIFAl+Vu3IACgkQhvpINdm7 VJL6Gg/+Mlx2N3lg0kRzevbHYdReGI0UFjEyLqM3u6BmeS5zhbkHY+/QYF9Ak2Th AEe1tI0KJ3KrTiPRH8oEkbZ5f1eY58AovHPYByxzOBMDHx9Z2PUtRmdDF9jqU1mA kw0x/SMHc/Rznkzu44VHl1vLTRxpPDJ3a91PmuKEe5prF0BufeCh+4WdMpwQq1yH RmeaXWg5aKjQtIpjVFivkLjp5sBlP2dh51r34bEd1LA9wQ1FcSdoDe3Jxwwt8Wnx wySRy7nh/RIDp0CkgxdtJfNgSskJ+m/5/bVWCIqKcRkVJCApndX5N02c7C0573so biLq7YKWbF27KjMIyl22XwPxdhBW0CfN+nXGDM0rv8WtzE2qqtEbRe1t3oYGfMZK 6CtXjDL03JyGVIaZCT8XyeAkHhUJM5gxGKygfCRq+O1ftVdzsnDtlhxnmm4Wtn44 YKJvjN2Na66BN1jObZZvfaCSU+iaBqNapkcImB1BHGo6uiAjo/u63ZZNYDABvbbK corwSbj3kgu3kHXgnX9u6sR2pSMZeqcGT+uBNXt3F8tPkDd7UwSNjKswADQrY5GN bNIeee2fI5kmxdqitRPqCYSM4OgB4P/WsI7KgLcLsbH3uFRpb2rIIHGRgdfnWCc5 t/T9hQH0gczN1Beuyj+8MPM657zoQojz9l39rY1XA9dJ5iXhZuM= =gnpW -----END PGP SIGNATURE----- --NzB8fVQJ5HfG6fxh--