Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4589797pxu; Tue, 20 Oct 2020 23:32:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcmgYwVLPvFHLx7DgiVh0dCop3zN5I8M66H1sfoBiQFY+HZjS8YeysVnJMIL3TJieDQDoN X-Received: by 2002:aa7:dc12:: with SMTP id b18mr1542667edu.295.1603261971471; Tue, 20 Oct 2020 23:32:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603261971; cv=none; d=google.com; s=arc-20160816; b=fu/QUXsOHPnrnTPGh2Syp6q9u1uTJnhZTOh+5VydwFPnBAPwMgpUTWQJyIRoX5pAhQ DcsgCexBndESdzNBFFuxsWMT4uT8eBaXPUKT4QIyNmgSs8o3CvTn39EyeZPu9sc3e70+ 9PHxj6hWcjCYKBO502imezUpKbcGBDwNf02M1L5z/ewDNzm+JFCXumrS6+un5dVce4fq Ii8r050fliIxlyXvO4IIaReJBGvnLj4QADmbsiuaWuYviqba3ula4SCBNRjEWXoNLFki eHnHG0E7iVNDr82f26ZcRvMWaPEojAHQ2HtQOWtY/k8WdOd0pVw6E5L6nqnaRMBV/xa/ bk7Q== 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=974Pru8SQsTsWRztSFlkqkBRxzWVu5U32q4PL8EdyE0=; b=QGCmtRzyD5AZp3RDZ98XN9DhIkn9LGlIG1tE/fDd8y2KnGodxhcX0wKhiAfCl+gu0O CeVr8nSA/WwyLyDjdKEngmJHh+zIWNUU26/BMJ+3XG16kQiKKNXxv2xulNijGmF2bYFQ 9prgERme7kiI4vOJMpMtnVrctglQcFJ2TUfRvsGMVvrWDmbshkyu3D6/HuR+UYqdfmxm 4r5+4/8+F22YayRc7a0ub48UCj14PKjBphUOFkapw8inLyaQG5fdSnp176q31ZjcVS0o O+T8oy+wVp0jDvtskAseaFDyuAQlwOo0y3t9pey2jdCXUGX9LBUkqjYDiVWiFChLK8wn u/Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@lechnology.com header.s=default header.b=CFdn00D2; 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 qq26si711683ejb.103.2020.10.20.23.32.29; Tue, 20 Oct 2020 23:32:51 -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=CFdn00D2; 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 S2437127AbgJTQGs (ORCPT + 99 others); Tue, 20 Oct 2020 12:06:48 -0400 Received: from vern.gendns.com ([98.142.107.122]:39302 "EHLO vern.gendns.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729540AbgJTQGs (ORCPT ); Tue, 20 Oct 2020 12:06:48 -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=974Pru8SQsTsWRztSFlkqkBRxzWVu5U32q4PL8EdyE0=; b=CFdn00D2yLVnQnBZSvE++F6RB2 hwgF2+hqz/F0vUmiYOqU0jaKg+NbntGRoJ8NITvlS2fLmSPmeVAdNLSU0FbE4o1eKZ/8N55HGaQwL MipSGLiP92IMF/VwWfBSYkI5FD7RC3P/Zw5MoOHUGmhUnywHTnXKtvRNMYj5O9WM+fERe5dZUyoVG U3WhL09Ez004/nBbSvA6nx14dJwQTz8yS1n+eEN6+7S/CMsxhAUZVLXh6FXBX8PSZkDbDXxAIaEvN oEeStdGS5IfKzsKbh0LQzAnVgq5EM4wItLfeBOEEH3+4YYYd1IaGgKvb1iHXDLOl9iF2B2VGmFxN1 MI2z9Gqw==; Received: from [2600:1700:4830:165f::19e] (port=58074) by vern.gendns.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1kUu9s-0005nK-CI; Tue, 20 Oct 2020 12:06:44 -0400 Subject: Re: [PATCH v5 3/5] counter: Add character device interface To: William Breathitt Gray 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 References: <00be1fccc672c5207f3b04fe4cc09c29e22641f4.1601170670.git.vilhelm.gray@gmail.com> <20201018165822.GE231549@shinobu> From: David Lechner Message-ID: Date: Tue, 20 Oct 2020 11:06:42 -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: <20201018165822.GE231549@shinobu> 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 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 = 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. > > 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.