kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: Greg KH <greg@kroah.com>
To: rdq@metamail.co
Cc: kernelnewbies@kernelnewbies.org
Subject: Re: debugfs question ...
Date: Tue, 30 Apr 2019 14:07:20 +0200	[thread overview]
Message-ID: <20190430120720.GA22892@kroah.com> (raw)
In-Reply-To: <009001d4ff4a$2ac6a030$8053e090$@metamail.co>

On Tue, Apr 30, 2019 at 12:45:08PM +0100, rdq@metamail.co wrote:
> Greetings,
> 
> My I2C sensor driver has a debugfs entry for development purposes.
> Everything works fine with the exception of the read operation. When 'cat'
> is used, the read operation is called repeatedly and indefinitely. If the
> read() is changed to return 0 then, as expected, nothing is displayed. 
> 
> The pattern for the implementation is (AFAICT) right out of  the book (shown
> below). 
> 
> What am I missing? Any thoughts much appreciated.
> 
> TAIA.
> 
> RDQ
> 
> static ssize_t sc031gs_reg_read_file(struct file *file, char __user
> *user_buf,
> 				   size_t count, loff_t *ppos)
> {
> 	char *buf = 0;
> 	ssize_t total = 0;
> 	struct sc031gs_dev *sensor = file->private_data;
> 	if (!sensor)
> 		return -EINVAL;
> 	if (*ppos < 0 || !count)
> 		return -EINVAL;
> 	buf = kmalloc(count, GFP_KERNEL);
> 	if (!buf)
> 		return -ENOMEM;
> 	total = snprintf(buf,count,"Hello world\n");
> 	if (total >= 0) {
> 		if (copy_to_user(user_buf, buf, total)) {
> 			kfree(buf);
> 			return -EFAULT;
> 		}
> 		*ppos += total;
> 	}
> 	kfree(buf);
> 	return total;
> }

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 :(

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.

good luck!

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

  reply	other threads:[~2019-04-30 12:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-30 11:45 debugfs question rdq
2019-04-30 12:07 ` Greg KH [this message]
     [not found]   ` <014a01d50028$ce098580$6a1c9080$@metamail.co>
2019-05-01 14:35     ` 'Greg KH'
2019-04-30 12:11 ` Martin Christian
2019-05-01 12:06   ` rdq

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190430120720.GA22892@kroah.com \
    --to=greg@kroah.com \
    --cc=kernelnewbies@kernelnewbies.org \
    --cc=rdq@metamail.co \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).