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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E0F2C433F5 for ; Sun, 17 Oct 2021 15:35:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 585C4610E5 for ; Sun, 17 Oct 2021 15:35:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237841AbhJQPhc (ORCPT ); Sun, 17 Oct 2021 11:37:32 -0400 Received: from relay10.mail.gandi.net ([217.70.178.230]:45209 "EHLO relay10.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234748AbhJQPha (ORCPT ); Sun, 17 Oct 2021 11:37:30 -0400 Received: (Authenticated sender: alexandre.belloni@bootlin.com) by relay10.mail.gandi.net (Postfix) with ESMTPSA id 719CE240003; Sun, 17 Oct 2021 15:35:16 +0000 (UTC) Date: Sun, 17 Oct 2021 17:35:16 +0200 From: Alexandre Belloni To: Greg KH Cc: William Breathitt Gray , jic23@kernel.org, linux-stm32@st-md-mailman.stormreply.com, kernel@pengutronix.de, a.fatoum@pengutronix.de, kamel.bouhara@bootlin.com, gwendal@chromium.org, 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, jarkko.nikula@linux.intel.com, Dan Carpenter Subject: Re: [PATCH v17 2/9] counter: Add character device interface Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 17/10/2021 16:40:14+0200, Greg KH wrote: > On Sun, Oct 17, 2021 at 04:02:42PM +0200, Alexandre Belloni wrote: > > On 17/10/2021 15:50:11+0200, Greg KH wrote: > > > Note, review of this now that it has been submitted in a pull request to > > > me, sorry I missed this previously... > > > > > > On Wed, Sep 29, 2021 at 12:15:59PM +0900, William Breathitt Gray wrote: > > > > +static int counter_chrdev_open(struct inode *inode, struct file *filp) > > > > +{ > > > > + struct counter_device *const counter = container_of(inode->i_cdev, > > > > + typeof(*counter), > > > > + chrdev); > > > > + > > > > + /* Ensure chrdev is not opened more than 1 at a time */ > > > > + if (!atomic_add_unless(&counter->chrdev_lock, 1, 1)) > > > > + return -EBUSY; > > > > > > I understand the feeling that you wish to stop userspace from doing > > > this, but really, it does not work. Eventhough you are doing this > > > correctly (you should see all the other attempts at doing this), you are > > > not preventing userspace from having multiple processes access this > > > device node at the same time, so please, don't even attempt to stop this > > > from happening. > > > > > > So you can drop the atomic "lock" you have here, it's not needed at all. > > > > > > > Could you elaborate a bit here because we've had a similar thing in the > > RTC subsystem: > > > > https://elixir.bootlin.com/linux/latest/source/drivers/rtc/dev.c#L28 > > Yeah, that too will not work :( Note, it does stop open from being > called from different processes, but think of the following sequence of > userspace calls: > open() > fork/exec() > both processes access the file descriptor > > or passing a fd across a socket? > > Or duplicating the file descriptor and sending it to a different task > (like across a socket or many other IPC ways)? > > Once userspace has a file descriptor, all bets are off as to where it > goes and what it does with it. There's no need to try to save userspace > from itself by preventing multiple opens when really, it doesn't stop > anyone who really wants to do this. > > If userspace does do multiple read/writes from different threads / > processes / whatever on the same file descriptor, it gets to keep the > pieces of the mess it causes. It's not the kernel's job to try to > "protect" userspace from itself here. > > Look at serial/tty connections as one example of this always being the > case. > > Does that help? > Thanks for the explanation, this is now clear to me. > > And it would mean I can remove rtc->flags completely. > > I think you can do that. > > thanks, > > greg k-h -- Alexandre Belloni, co-owner and COO, Bootlin Embedded Linux and Kernel engineering https://bootlin.com 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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A13E9C433F5 for ; Sun, 17 Oct 2021 15:36:37 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 6971C60E74 for ; Sun, 17 Oct 2021 15:36:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6971C60E74 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=bootlin.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc: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=I4/1ziSjl0cwlJ+qwW8vbjU8jUO7fpe+3emaJWYGzN4=; b=0ap5bFuz71bgXk /5hDzFcrwSplW5OpmqJPNx63UljCSBzPfqDQ5kaMbwJir9RvgbFQuS0EYiJuzvossqzLsYHlrjFfe rsSCyM5p4kecrx5tN3J91Jo4bB3t5eTQ12CNVs8Vh3jniED2dmIfrZx2VjTU1kWQSB13Yrp07Da3N 59eG3hj9u3aFjm1ECwEXFqI3dGT5nMVqIjAo3IxSFMSqzuo/CzCME1/7ewnApHZc7GTp/mhxdYnZt ZnuegIBcvjyWjF/N5nFZAbQu7b4LzjeR8UDrE0UtLhZkILTcDrvYWKiWCuO4JIzxp1fWeKQ8nh/oH gLaz6V0DVCKGHSUDebpQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mc8C5-00CtV7-FA; Sun, 17 Oct 2021 15:35:25 +0000 Received: from relay10.mail.gandi.net ([217.70.178.230]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mc8C1-00CtU2-1q for linux-arm-kernel@lists.infradead.org; Sun, 17 Oct 2021 15:35:23 +0000 Received: (Authenticated sender: alexandre.belloni@bootlin.com) by relay10.mail.gandi.net (Postfix) with ESMTPSA id 719CE240003; Sun, 17 Oct 2021 15:35:16 +0000 (UTC) Date: Sun, 17 Oct 2021 17:35:16 +0200 From: Alexandre Belloni To: Greg KH Cc: William Breathitt Gray , jic23@kernel.org, linux-stm32@st-md-mailman.stormreply.com, kernel@pengutronix.de, a.fatoum@pengutronix.de, kamel.bouhara@bootlin.com, gwendal@chromium.org, 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, jarkko.nikula@linux.intel.com, Dan Carpenter Subject: Re: [PATCH v17 2/9] counter: Add character device interface Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211017_083521_403196_2C7DF12B X-CRM114-Status: GOOD ( 31.23 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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 17/10/2021 16:40:14+0200, Greg KH wrote: > On Sun, Oct 17, 2021 at 04:02:42PM +0200, Alexandre Belloni wrote: > > On 17/10/2021 15:50:11+0200, Greg KH wrote: > > > Note, review of this now that it has been submitted in a pull request to > > > me, sorry I missed this previously... > > > > > > On Wed, Sep 29, 2021 at 12:15:59PM +0900, William Breathitt Gray wrote: > > > > +static int counter_chrdev_open(struct inode *inode, struct file *filp) > > > > +{ > > > > + struct counter_device *const counter = container_of(inode->i_cdev, > > > > + typeof(*counter), > > > > + chrdev); > > > > + > > > > + /* Ensure chrdev is not opened more than 1 at a time */ > > > > + if (!atomic_add_unless(&counter->chrdev_lock, 1, 1)) > > > > + return -EBUSY; > > > > > > I understand the feeling that you wish to stop userspace from doing > > > this, but really, it does not work. Eventhough you are doing this > > > correctly (you should see all the other attempts at doing this), you are > > > not preventing userspace from having multiple processes access this > > > device node at the same time, so please, don't even attempt to stop this > > > from happening. > > > > > > So you can drop the atomic "lock" you have here, it's not needed at all. > > > > > > > Could you elaborate a bit here because we've had a similar thing in the > > RTC subsystem: > > > > https://elixir.bootlin.com/linux/latest/source/drivers/rtc/dev.c#L28 > > Yeah, that too will not work :( Note, it does stop open from being > called from different processes, but think of the following sequence of > userspace calls: > open() > fork/exec() > both processes access the file descriptor > > or passing a fd across a socket? > > Or duplicating the file descriptor and sending it to a different task > (like across a socket or many other IPC ways)? > > Once userspace has a file descriptor, all bets are off as to where it > goes and what it does with it. There's no need to try to save userspace > from itself by preventing multiple opens when really, it doesn't stop > anyone who really wants to do this. > > If userspace does do multiple read/writes from different threads / > processes / whatever on the same file descriptor, it gets to keep the > pieces of the mess it causes. It's not the kernel's job to try to > "protect" userspace from itself here. > > Look at serial/tty connections as one example of this always being the > case. > > Does that help? > Thanks for the explanation, this is now clear to me. > > And it would mean I can remove rtc->flags completely. > > I think you can do that. > > thanks, > > greg k-h -- Alexandre Belloni, co-owner and COO, Bootlin Embedded Linux and Kernel engineering https://bootlin.com _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel