Skip to contentSkip to conversation

sysfs and power management

DISCUSSION Onkalo Samu 5 participants 15 messages Oct 27, 2010 – Nov 3, 2010

lore
Share & tools
lore
#0 lore raw

Hi

I have recently sent couple of sensor drivers to kernel. Interface is purely sysfs based. The hard part is to get power management to work reasonable way.

With /dev/* interface power management is quite easy to do since driver gets clear open and release calls. With sysfs, there is only read / write methods available.
My solution has been separate sysfs entry which controls operating state. However, there are some problems with that.
In case of boolean type enable / disable control, there must be only one userspace component which decides sensor state. In case of counting type of control, there is a change that sensor is permanently left on if the userspace component crashes. This would lead to unbalanced enable / disable count.

If the sensor functionality is based on interrupts (like threshold interrupt), it may take long time before there is any activity. In this kind of case timeout from the previous sysfs access doesn't make much sense.
I started to wonder if it makes sense to enhance sysfs so that it optionally prodives open / close call backs. Internally sysfs has some bookkeeping about the refcount but this is not visible to the driver. Of course majority of the sysfs users doesn't need that at all and for them this is just overhead.

Is it out of the question to provide call back to driver when some sysfs entry is opened (first time) and when refcnt goes back to 0?

Regards,
Samu

#1 lore raw
Quoted 5 lines

I started to wonder if it makes sense to enhance sysfs so that it optionally prodives open / close call backs. Internally sysfs has some bookkeeping about the refcount but this is not visible to the driver. Of course majority of the sysfs users doesn't need that at all and for them this is just overhead.

I think we need it. There doesn't need to be much overhead however as there is no need (or sense) in providing per sysfs node open/close hooks.

The pm layer also lacks a clean race-free way to actually ascertain when the device was last kicked out of pm saving.

#2 lore raw
Quoted 11 lines

-----Original Message-----
From: ext Alan Cox [mailto:[email protected]]

Quoted 5 lines

I started to wonder if it makes sense to enhance sysfs so that it optionally prodives open / close call backs. Internally sysfs has some bookkeeping about the refcount but this is not visible to the driver. Of course majority of the sysfs users doesn't need that at all and for them this is just overhead.

Quoted 3 lines

I think we need it. There doesn't need to be much overhead however as there is no need (or sense) in providing per sysfs node open/close hooks.

Do you mean per device hook and each sysfs open / close uses per device ref-counting.

So the driver would just know that some sysfs entry is open or all the entries are closed. I think that this kind of behavior is ok for this purpose.

Quoted 3 lines

The pm layer also lacks a clean race-free way to actually ascertain when the device was last kicked out of pm saving.

#3 lore raw
Quoted 2 lines

Do you mean per device hook and each sysfs open / close uses per device ref-counting.

Or you can do it by having an optional per device hook which is passed the sysfs node for each open/close and leave the rest up to the driver. If it needs to do clever stuff it can.

Alan

#4 lore raw
Quoted 6 lines · Alan Cox
Quoted 2 lines

Do you mean per device hook and each sysfs open / close uses per device ref-counting.

Or you can do it by having an optional per device hook which is passed the sysfs node for each open/close and leave the rest up to the driver. If it needs to do clever stuff it can.

I really don't want to add open/close to the sysfs file model for the driver core.

What is the specific problem with not doing any sensor work until userspace asks for the data? Then do the read from the hardware and go back to sleep. That's probably the best way to do this, as userspace isn't going to open the sysfs file and not close it instantly anyway after it has read the data (seeking on a sysfs file isn't really recommended, even if it sometimes seems to work.)

thanks,

greg k-h

#5 lore raw
Quoted 4 lines · Greg KH

back to sleep. That's probably the best way to do this, as userspace isn't going to open the sysfs file and not close it instantly anyway after it has read the data (seeking on a sysfs file isn't really recommended, even if it sometimes seems to work.)

Well, it is documented that seek(start of file) on sysfs works, and it is ABI already (some userspace uses it on poll/select-capable attributes). So, maybe seek(somewhere that is not the start of the file) doesn't work -- and it should return an error in that case if it doesn't already... but it is a lot more deterministic than "sometimes" ;-)

So yes, userspace will open() and not close() a sysfs attribute immediately afterwards. It is not only shell crap that interfaces to the kernel over sysfs :-)

It would make a lot of sense to support the poll/select model on any sensor for which you have event-driven notifications of change from the hardware or firmware.

I don't know about open/close notifications, however. Usually you need those when you're going to stream something to userspace, and there are FAR better interfaces to use in that case, such as the ring buffers used by the data acquisition devices, netlink (which userspace programmers seems not to like much :p), input devices, and generic character devices...

Signature · 4 lines
  "One disk to rule them all, One disk to find them. One disk to bring
  them all and in the darkness grind them. In the Land of Redmond
  where the shadows lie." -- The Silicon Valley Tarot
  Henrique Holschuh
#6 lore raw
Quoted 13 lines · ext Henrique de Moraes Holschuh
Quoted 4 lines · Greg KH

back to sleep. That's probably the best way to do this, as userspace isn't going to open the sysfs file and not close it instantly anyway after it has read the data (seeking on a sysfs file isn't really recommended, even if it sometimes seems to work.)

Well, it is documented that seek(start of file) on sysfs works, and it is ABI already (some userspace uses it on poll/select-capable attributes). So, maybe seek(somewhere that is not the start of the file) doesn't work -- and it should return an error in that case if it doesn't already... but it is a lot more deterministic than "sometimes" ;-)

Quoted 4 lines

So yes, userspace will open() and not close() a sysfs attribute immediately afterwards. It is not only shell crap that interfaces to the kernel over sysfs :-)

What I would like to do is:

Control sensors operating mode and regulators based on the userspace activity. If no-one is interested about the sensor, it can be turned totally off including its operating power via regulator framework.

So far the only accepted interface for the small sensor seems to be sysfs. I tried use misc device but it was not accepted.

For example ambient light sensor or proximity sensor may operate fully under interrupts. There is no need to poll status which is fine with sysfs. However, problem is that kernel driver have no idea if someone keeps some sysfs entry open and waits for the data.

What I would like to have is just an indication that at least one of the sysfs entries is opened by some application. When that condition is true, the sensor would be powered on and kept running.
And similarly, then all the users has gone, turn the device off.

I can prepare a patch to show what I mean.

-Samu

Quoted 10 lines

It would make a lot of sense to support the poll/select model on any sensor for which you have event-driven notifications of change from the hardware or firmware.

I don't know about open/close notifications, however. Usually you need those when you're going to stream something to userspace, and there are FAR better interfaces to use in that case, such as the ring buffers used by the data acquisition devices, netlink (which userspace programmers seems not to like much :p), input devices, and generic character devices...

#7 lore raw
Quoted 20 lines · Onkalo Samu
Quoted 13 lines · ext Henrique de Moraes Holschuh
Quoted 4 lines · Greg KH

back to sleep. That's probably the best way to do this, as userspace isn't going to open the sysfs file and not close it instantly anyway after it has read the data (seeking on a sysfs file isn't really recommended, even if it sometimes seems to work.)

Well, it is documented that seek(start of file) on sysfs works, and it is ABI already (some userspace uses it on poll/select-capable attributes). So, maybe seek(somewhere that is not the start of the file) doesn't work -- and it should return an error in that case if it doesn't already... but it is a lot more deterministic than "sometimes" ;-)

Quoted 4 lines

So yes, userspace will open() and not close() a sysfs attribute immediately afterwards. It is not only shell crap that interfaces to the kernel over sysfs :-)

Quoted 8 lines

What I would like to do is:

Control sensors operating mode and regulators based on the userspace activity. If no-one is interested about the sensor, it can be turned totally off including its operating power via regulator framework.

So far the only accepted interface for the small sensor seems to be sysfs. I tried use misc device but it was not accepted.

Look at the drivers/staging/iio/ subsystem. It is working on a framework that you can use through a character device (I think) to properly manage your drivers in this manner.

Try working with those developers as I think it is what you are looking for here.

thanks,

greg k-h

#8 lore raw
Quoted 10 lines · ext Greg KH
Quoted 9 lines

What I would like to do is:

Control sensors operating mode and regulators based on the userspace activity. If no-one is interested about the sensor, it can be turned totally off including its operating power via regulator framework.

So far the only accepted interface for the small sensor seems to be sysfs. I tried use misc device but it was not accepted.

Quoted 7 lines

Look at the drivers/staging/iio/ subsystem. It is working on a framework that you can use through a character device (I think) to properly manage your drivers in this manner.

Try working with those developers as I think it is what you are looking for here.

I took a look to that. It seems that iio is more or less sysfs based. There are ring buffers and event device which are chardev based but still the data outside ring buffer and the control is sysfs based.

By getting open and close from sysfs would be nice from the driver point of view. However, I understand that this is just overhead for majority of the cases.

Regards,
Samu

#9 lore raw
Quoted 3 lines

I took a look to that. It seems that iio is more or less sysfs based. There are ring buffers and event device which are chardev based but still the data outside ring buffer and the control is sysfs based.

IIO is sysfs dependant, heavyweight and makes no sense for some of the sysfs based drivers. IIO is also staging based and Linus already threw out the last attempt to unify these drivers sanely with an ALS layer - which was smaller, cleaner and better !

Quoted 3 lines

By getting open and close from sysfs would be nice from the driver point of view. However, I understand that this is just overhead for majority of the cases.

The alternative really is to end up with a parallel 'not quite sysfs' which is sysfs + open/close. My feeling is its cleaner to have a hook at the per device level (so we don't bloat the sysfs nodes at all) than have two copies of sysfs.

Alan

#10 lore raw
Quoted 8 lines · Alan Cox
Quoted 3 lines

I took a look to that. It seems that iio is more or less sysfs based. There are ring buffers and event device which are chardev based but still the data outside ring buffer and the control is sysfs based.

IIO is sysfs dependant, heavyweight and makes no sense for some of the sysfs based drivers. IIO is also staging based and Linus already threw out the last attempt to unify these drivers sanely with an ALS layer - which was smaller, cleaner and better !

I think we need to revisit this issue again, before iio is merged to the main kernel tree. I've been totally ignoring the iio user/kernel api at the moment, waiting for things to settle down there.

thanks,

greg k-h

#11 lore raw
Quoted 6 lines · On Mon, 1 Nov 2010 11:07:40 -0700 Greg KH <[email protected]> wrote:
Quoted 5 lines · Alan Cox
Quoted 4 lines

I took a look to that. It seems that iio is more or less sysfs based. There are ring buffers and event device which are chardev based but still the data outside ring buffer and the control is sysfs based.

Quoted 8 lines
Quoted 4 lines

IIO is sysfs dependant, heavyweight and makes no sense for some of the sysfs based drivers. IIO is also staging based and Linus already threw out the last attempt to unify these drivers sanely with an ALS layer - which was smaller, cleaner and better !

I think we need to revisit this issue again, before iio is merged to the main kernel tree. I've been totally ignoring the iio user/kernel api at the moment, waiting for things to settle down there

Actually I think there is another way to do it cleanly

Keep a flag per device (or per runtime pm struct of device)

And on the open/close do

	if (runtime_pm on device && device has SYSFS_PM set)
		pm_runtime_foo

so that devices that need to be powered up to handle sysfs requests can set a single flag and just work.

#12 lore raw
Quoted 32 lines

-----Original Message-----
From: ext Alan Cox [mailto:[email protected]]
Sent: 03 November, 2010 11:45
To: Greg KH
Cc: Onkalo Samu.P (Nokia-MS/Tampere); ext Henrique de Moraes Holschuh; Alan Cox; [email protected]; [email protected]
Subject: Re: sysfs and power management

Quoted 6 lines · On Mon, 1 Nov 2010 11:07:40 -0700 Greg KH <[email protected]> wrote:
Quoted 5 lines · Alan Cox
Quoted 4 lines

I took a look to that. It seems that iio is more or less sysfs based. There are ring buffers and event device which are chardev based but still the data outside ring buffer and the control is sysfs based.

Quoted 8 lines
Quoted 4 lines

IIO is sysfs dependant, heavyweight and makes no sense for some of the sysfs based drivers. IIO is also staging based and Linus already threw out the last attempt to unify these drivers sanely with an ALS layer - which was smaller, cleaner and better !

I think we need to revisit this issue again, before iio is merged to the main kernel tree. I've been totally ignoring the iio user/kernel api at the moment, waiting for things to settle down there

Actually I think there is another way to do it cleanly

Keep a flag per device (or per runtime pm struct of device)

Quoted 7 lines

And on the open/close do

	if (runtime_pm on device && device has SYSFS_PM set)
		pm_runtime_foo

so that devices that need to be powered up to handle sysfs requests can set a single flag and just work.

That is one quite clean way.
sysfs_ops still needs function pointer to device core function which does pm_runtime calls.
And there is one drawback. Driver doesn't know about new users after the first one. It may want to refresh results whenever a new user appears. But that is probably not a big issue.

-Samu

#13 lore raw
In reply to Alan Cox
Quoted 22 lines · Alan Cox
Quoted 6 lines · On Mon, 1 Nov 2010 11:07:40 -0700 Greg KH <[email protected]> wrote:
Quoted 5 lines · Alan Cox
Quoted 4 lines

I took a look to that. It seems that iio is more or less sysfs based. There are ring buffers and event device which are chardev based but still the data outside ring buffer and the control is sysfs based.

Quoted 8 lines
Quoted 4 lines

IIO is sysfs dependant, heavyweight and makes no sense for some of the sysfs based drivers. IIO is also staging based and Linus already threw out the last attempt to unify these drivers sanely with an ALS layer - which was smaller, cleaner and better !

I think we need to revisit this issue again, before iio is merged to the main kernel tree. I've been totally ignoring the iio user/kernel api at the moment, waiting for things to settle down there

Actually I think there is another way to do it cleanly

Keep a flag per device (or per runtime pm struct of device)

Quoted 7 lines

And on the open/close do

	if (runtime_pm on device && device has SYSFS_PM set)
		pm_runtime_foo

so that devices that need to be powered up to handle sysfs requests can set a single flag and just work.

That sounds like a reasonable idea.

thanks,

greg k-h

#14 lore raw
Quoted 36 lines

-----Original Message-----
From: ext Greg KH [mailto:[email protected]]
Sent: 03 November, 2010 15:09
To: Alan Cox
Cc: Onkalo Samu.P (Nokia-MS/Tampere); ext Henrique de Moraes Holschuh; Alan Cox; [email protected]; [email protected]
Subject: Re: sysfs and power management

Quoted 9 lines · Alan Cox
Quoted 6 lines · On Mon, 1 Nov 2010 11:07:40 -0700 Greg KH <[email protected]> wrote:
Quoted 5 lines · Alan Cox
Quoted 4 lines

I took a look to that. It seems that iio is more or less sysfs based. There are ring buffers and event device which are chardev based but still the data outside ring buffer and the control is sysfs based.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

IIO is sysfs dependant, heavyweight and makes no sense for some of the sysfs based drivers. IIO is also staging based and Linus already threw out the last attempt to unify these drivers sanely with an ALS layer - which was smaller, cleaner and better !

I think we need to revisit this issue again, before iio is merged to the main kernel tree. I've been totally ignoring the iio

user/kernel

Quoted 8 lines
Quoted 1 line

api at the moment, waiting for things to settle down there

Actually I think there is another way to do it cleanly

Keep a flag per device (or per runtime pm struct of device)

And on the open/close do

Quoted 9 lines
Quoted 4 lines
	if (runtime_pm on device && device has SYSFS_PM set)
		pm_runtime_foo

so that devices that need to be powered up to handle sysfs requests

can

Quoted 1 line

set a single flag and just work.

That sounds like a reasonable idea.

I'm working with the implementation which adds possibility to add per attribute open_close_notification method with minimal overhead to current system.

-Samu