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=-7.3 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING, SPF_PASS,USER_AGENT_MUTT autolearn=ham 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 432D5C43219 for ; Wed, 1 May 2019 14:35:56 +0000 (UTC) Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (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 E55F220651 for ; Wed, 1 May 2019 14:35:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=kroah.com header.i=@kroah.com header.b="QcYw3WF9"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="SsHwsEHv" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E55F220651 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kroah.com Authentication-Results: mail.kernel.org; spf=fail smtp.mailfrom=kernelnewbies-bounces@kernelnewbies.org Received: from localhost ([::1] helo=shelob.surriel.com) by shelob.surriel.com with esmtp (Exim 4.91) (envelope-from ) id 1hLqKn-0007u6-I3; Wed, 01 May 2019 10:35:45 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by shelob.surriel.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1hLqKl-0007tz-8N for kernelnewbies@kernelnewbies.org; Wed, 01 May 2019 10:35:43 -0400 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 3B6282221E; Wed, 1 May 2019 10:35:42 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 01 May 2019 10:35:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kroah.com; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=3azYTJ9J96qTMGG9Nqs0E+XyjGZ g2rrW7H8TchknMzc=; b=QcYw3WF9g63GzqOQVb4Kp5Hn8wUS0dd/mgUo9+Ow6GV LlwLHcdt3fvbukwbpkl6YRGp6QMlfGIbN8LV29Diu5yNtek2np2q+qIRxuzKaTFS H8Q9ZMLDd2gP75uDzujs9Jz05mNQAZyKKyepYLqGliMh/RD65462y9TJI1IOBu5k YIf8psGryc/NGCcxRCJWvu9EC6Vt2OBARRDgxGPdGmwJUQS/bqKwZcPqLJJ009nE /onb9ouQgOCubfjJlzcc0+D87rUiXy9QSPjC76R8Mv4vI2GnPKby239JYbC1x5Ef kyOHMKkTw2dNPLN4ryN28Gua2p0c4SYu3haZc63bZCw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=3azYTJ 9J96qTMGG9Nqs0E+XyjGZg2rrW7H8TchknMzc=; b=SsHwsEHvkiMr16tr9yeREl vOzYaNRd/xy87Ip5ZUyEMm4HKbO3BaCo5UfIQZmGO//dt/YgGPWC8pF+zMSWkZOu ytoJA0MGt1UYYveFaTN3oaxSSDBwcqUCHXWEDKZufh6rW/MZMjFJbOOC+5RlXqvE ++h5FSEFg6vCWU8Xxa86H0wZe6IyX3I3kJ9gYP/6AbMPXz952HmI5i3eza9VhdSS +V3rFLj4zTfM0ve2aSzui7VXbl/wwKH6i3JnrDKZaEwLe6J4HePy9kG6317wDAnh wZpJ+cPrQN1F3fesGVQ/28O69jASWztJGQD6RyHv4XhuTRJ11TA0a4GmZI47+hRw == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrieejgdektdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecuogfuuhhsphgvtghtffhomhgrihhnucdlgeelmdenuc fjughrpeffhffvuffkfhggtggujggfsehttdertddtredvnecuhfhrohhmpedkifhrvghg ucfmjfdkuceoghhrvghgsehkrhhorghhrdgtohhmqeenucffohhmrghinhepghhoohhglh gvrdgtohhmpdhgihhthhhusgdrtghomhenucfkphepkeefrdekiedrkeelrddutdejnecu rfgrrhgrmhepmhgrihhlfhhrohhmpehgrhgvgheskhhrohgrhhdrtghomhenucevlhhush htvghrufhiiigvpedt X-ME-Proxy: Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) by mail.messagingengine.com (Postfix) with ESMTPA id 60753103CB; Wed, 1 May 2019 10:35:40 -0400 (EDT) Date: Wed, 1 May 2019 16:35:38 +0200 From: 'Greg KH' To: rdq@metamail.co Subject: Re: debugfs question ... Message-ID: <20190501143538.GA30079@kroah.com> References: <009001d4ff4a$2ac6a030$8053e090$@metamail.co> <20190430120720.GA22892@kroah.com> <014a01d50028$ce098580$6a1c9080$@metamail.co> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <014a01d50028$ce098580$6a1c9080$@metamail.co> User-Agent: Mutt/1.11.4 (2019-03-13) Cc: kernelnewbies@kernelnewbies.org X-BeenThere: kernelnewbies@kernelnewbies.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Learn about the Linux kernel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kernelnewbies-bounces@kernelnewbies.org On Wed, May 01, 2019 at 03:18:51PM +0100, rdq@metamail.co wrote: > Hello and thanks, > > > > > > > The pattern for the implementation is (AFAICT) right out of the book > > > > > > > You are returning a "short" read, and then disallowing ppos to be set to a > > non-zero value? That's a recipie for disaster :( > > > > Also, you allow userspace to allocate as much memory as it asks for? > > Not good :( > > Well I started here: > https://github.com/torvalds/linux/blob/v4.14/sound/soc/soc-core.c#L251. > > > Why not just use the simple_read_from_buffer() call? That handles all of > the > > "housekeeping" for you, and your function can be _much_ simpler. > > Possibly but it does not explain why returning 0 means cat displays nothing You returned nothing, so what is cat supposed to do? It worked properly. > and returning the string length causes it to loop indefinitely. You have to give userspace what it asks for, don't ignore the ppos variable. Have you tried writing a userspace program that does a correct read() call on a file or device node like this? You have to keep reading until you get all of the data you asked for, or the kernel returns end-of-file, or an error happens. You don't just do a single read and hope for the best :) > My example now reduced to: > > int ret; > char buf[64]; > int n = snprintf(buf,64,"Hello World\n"); > if (n < 0) > return -EINVAL; > // zero on success > ret = copy_to_user(user_buf,buf,n); > *ppos += n; > return ret; > > Is this the canonical form? Not at all, that's a recipe for disaster. > Using a 4.14 kernel, cat displays nothing. Or has something changed? > If so mighty odd as there are ancient example as well as the new: > > https://sites.google.com/site/linuxkernel88/sample-code/writing-a-character-driver in char_device_read() A char device is MUCH different from a file interface that you are using here. It kind of looks the same, but it has some subtle ways in which things can go sideways very easily. Also, that code is really really wrong, if that char_device_read() function in the above link was ever submitted to me, I would have a field day with it. Nothing like bad random internet examples to steer people in the wrong direction :( > https://github.com/torvalds/linux/blob/master/drivers/base/regmap/regmap-debugfs.c#L254 That one is not "quite" like your example here either. You have the option to handle all of the fun corner cases and error values by writing it yourself, or just use the helper function I pointed you at which does all of what you need to do, correctly. To do something in the middle is to guarantee that something will not work properly with some sort of userspace program, or be totally insecure. thanks, greg k-h _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies