Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp171060pxu; Thu, 15 Oct 2020 00:24:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwhF87SOHa1aUTpFvB8UYEFZNAHH/Kewgx1vrvmg6o89OJNLIlyl+YRgWnSi/VYQNEYhV1U X-Received: by 2002:a17:906:590d:: with SMTP id h13mr2964958ejq.226.1602746663348; Thu, 15 Oct 2020 00:24:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602746663; cv=none; d=google.com; s=arc-20160816; b=DldetCqHpfGApOp3D12tJH2RpR3q7p7EsrB9PGfdsTuHCC+k5LFzonlz9OFBivI0+O oYewC/Ttv7oml94ZEzaIIefCn79mW7rcEV+5H0/VyO5GTnmyGcouWGtfG9kCGa7wBsnp Wssw4hjG+Ghw28ypT6lrZrCpAw6P7lXlWVubZDyAqiF3nx0rHaw0u5pvat6yCNak7A/W Ue1HM+qpoFwvY3KjWje5WJaOIZEi2rhtFQbuZHgJQqwp+yttjwo/sDO/vjytun1DDbli uTE9cFW/ZnRcTaskDpIhhj+w5shArS5EOzgphe15hTE+LBpo7LwOOXR07Kq40W+QSIeR 9EvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=dID71IWmSuJrKqwrxHNZaTKnyffa+NbYhcw7le2zj3k=; b=ddoxEWCMEGjLHScn97V2dKwfPwKnLgKxJ5vmbAmZWg5kOSgRmvjfgod3x+tIv9Mb5z 8e7eI2YZlOwQ0/tEfYYrtqRz1B4blM4iUZdKDwoAjc4glm90NJH5gW6aqW04jUif/i/j 7bHVpnOxgibEGy9QREYkPbPG3IahPi5rO0d8fnUWcxyArH/HivXqBsoXkrQ6CFGw3MMG fmIAY1sLH9ur50skSRDK5zB9zcUXGCjnYNHp1ocJSNXCtZcyRiFK1VoecABAfAEdmCXZ RJ5fFtL60botj+OX/gXuzqvhgmGjPhjUq892OhO2BF+dFIH/N8W/SnDEAB2Apa6/0DlZ 11DQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@lechnology.com header.s=default header.b=k3ui0MR7; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l14si1545380ejk.427.2020.10.15.00.24.00; Thu, 15 Oct 2020 00:24:23 -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=fail header.i=@lechnology.com header.s=default header.b=k3ui0MR7; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729696AbgJNWkt (ORCPT + 99 others); Wed, 14 Oct 2020 18:40:49 -0400 Received: from vern.gendns.com ([98.142.107.122]:46422 "EHLO vern.gendns.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727387AbgJNWkt (ORCPT ); Wed, 14 Oct 2020 18:40:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lechnology.com; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=dID71IWmSuJrKqwrxHNZaTKnyffa+NbYhcw7le2zj3k=; b=k3ui0MR7qAGP6ZgXBSIje/KtYf Zf0cM1bxI9UsYCKky2OBNQq+FVj6SWOj7hUQ1+MLgry20PefqhYwPVrQL9t3cgdhlU7xgdFc6WASW Y2UHc8cenJUS3n065aCJTQ1sZ5ZyVz06O0ubRbhRpvCVbRC1DASoeVVBXBBOIwLkcdlNfgA60bGYr r+y7ppFGb4Fa3C7omuxtp754zYs+rQUtm9UbJ2aimE0R3Y3QroSHXyksaJ5X56Ehn6LBEEKKuEbYc z/5QAnFC/rutDO1KU5cCrsvbcZpOKKsC8z/uHF2AQtmpWkOOYl7ogHaGgvvnQHpZyRKIxSSKkmjzj l7kypjnA==; Received: from [2600:1700:4830:165f::19e] (port=50656) by vern.gendns.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1kSpRt-0001fv-W9; Wed, 14 Oct 2020 18:40:46 -0400 Subject: Re: [PATCH v5 3/5] counter: Add character device interface To: William Breathitt Gray , jic23@kernel.org Cc: 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 References: <00be1fccc672c5207f3b04fe4cc09c29e22641f4.1601170670.git.vilhelm.gray@gmail.com> From: David Lechner Message-ID: Date: Wed, 14 Oct 2020 17:40:44 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <00be1fccc672c5207f3b04fe4cc09c29e22641f4.1601170670.git.vilhelm.gray@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - vern.gendns.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - lechnology.com X-Get-Message-Sender-Via: vern.gendns.com: authenticated_id: davidmain+lechnology.com/only user confirmed/virtual account not confirmed X-Authenticated-Sender: vern.gendns.com: davidmain@lechnology.com X-Source: X-Source-Args: X-Source-Dir: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 = 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 = wait_event_interruptible(counter->events_wait, > + !kfifo_is_empty(&counter->events)); > + if (err) > + return err; > + } > + > + raw_spin_lock_irqsave(&counter->events_lock, flags); > + err = 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. Example: static ssize_t iio_event_chrdev_read(struct file *filep, char __user *buf, size_t count, loff_t *f_ps) { struct iio_dev *indio_dev = filep->private_data; struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); struct iio_event_interface *ev_int = iio_dev_opaque->event_interface; unsigned int copied; int ret; if (!indio_dev->info) return -ENODEV; if (count < sizeof(struct iio_event_data)) return -EINVAL; do { if (kfifo_is_empty(&ev_int->det_events)) { if (filep->f_flags & O_NONBLOCK) return -EAGAIN; ret = wait_event_interruptible(ev_int->wait, !kfifo_is_empty(&ev_int->det_events) || indio_dev->info == NULL); if (ret) return ret; if (indio_dev->info == NULL) return -ENODEV; } if (mutex_lock_interruptible(&ev_int->read_lock)) return -ERESTARTSYS; ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied); mutex_unlock(&ev_int->read_lock); if (ret) return ret; /* * If we couldn't read anything from the fifo (a different * thread might have been faster) we either return -EAGAIN if * the file descriptor is non-blocking, otherwise we go back to * sleep and wait for more data to arrive. */ if (copied == 0 && (filep->f_flags & O_NONBLOCK)) return -EAGAIN; } while (copied == 0); return copied; }