SYSFS: need a noncaching read
neilb at suse.de
Thu Sep 13 03:57:13 EST 2007
On Wednesday September 12, nickpiggin at yahoo.com.au wrote:
> On Wednesday 12 September 2007 20:01, Greg KH wrote:
> > On Wed, Sep 12, 2007 at 07:32:07AM +0200, Robert Schwebel wrote:
> > > On Tue, Sep 11, 2007 at 11:43:17AM +0200, Heiko Schocher wrote:
> > > > I have developed a device driver and use the sysFS to export some
> > > > registers to userspace.
> > >
> > > Uuuh, uggly. Don't do that. Device drivers are there to abstract things,
> > > not to play around with registers from userspace.
> > >
> > > > I opened the sysFS File for one register and did some reads from this
> > > > File, but I alwas becoming the same value from the register, whats not
> > > > OK, because they are changing. So I found out that the sysFS caches
> > > > the reads ... :-(
> > >
> > > Yes, it does. What you can do is close()ing the file handle between
> > > accesses, which makes it work but is slow.
> > Do an lseek back to 0 and then re-read, you will get called in your
> > driver again.
> Can you do a pread with offset 0 to avoid the two syscalls? (which some
> people seem to be concerned about)
Looking in fs/sysfs/file.c, we notice the field "needs_read_fill" in
sysfs_read_file will only call fill_read_buffer (which calls the
->show routine) if this is 1;
It is cleared by fill_read_buffer and set to 1:
- at open
- by fill_write_buffer (i.e. if you write to the file descriptor)
- by sysfs_poll when an event was detected.
So currently you cannot simply open a sysfs file an read multiple
One option would be to call fill_read_buffer if *ppos == 0.
I cannot see that being a problem in practice, but maybe there is a
reason why it wasn't done that way.
Another option might be to call fill_read_buffer also if
buffer->event != atomic_read(&attr_sd->s_event)
and require drivers to call sysfs_notify when they make a change that
should be noticed. But I doubt that is really important.
More information about the Linuxppc-dev