Skip to contentSkip to conversation

pm_qos: make update_request callable from interrupt context

PATCH Superseded [email protected] 4 participants 14 messages Jun 7, 2010 – Jun 9, 2010 Power management core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pm_qos: make update_request callable from interrupt context

We introduce new atomic_notifiers and only call the (legacy) blocking notifiers if there are any registered. A might_sleep() tries to fence off all misuse.

This should make it possible to call update_request from interrupt-context as long as only atomic notifiers are registered.

---
 include/linux/pm_qos_params.h |    3 +
 kernel/pm_qos_params.c        |  114 ++++++++++++++++++++++++++++++++--------
 2 files changed, 94 insertions(+), 23 deletions(-)
include/linux/pm_qos_params.h +3 -0
index 8ba440e..60ed542 100644
@@ -22,6 +22,9 @@ void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
 void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req);
 
 int pm_qos_request(int pm_qos_class);
+
 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier);
 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier);
 
+int pm_qos_add_notifier_nonblocking(int pm_qos_class, struct notifier_block *notifier);
+int pm_qos_remove_notifier_nonblocking(int pm_qos_class, struct notifier_block *notifier);
kernel/pm_qos_params.c +91 -23

218 lines not on this page · raw message

Signature · 1 line
1.7.1
#1 lore raw
Quoted 42 lines · On Mon, 2010-06-07 at 14:31 +0200, [email protected] wrote:

We introduce new atomic_notifiers and only call the (legacy) blocking notifiers if there are any registered. A might_sleep() tries to fence off all misuse.

This should make it possible to call update_request from interrupt-context as long as only atomic notifiers are registered.

---
 include/linux/pm_qos_params.h |    3 +
 kernel/pm_qos_params.c        |  114 ++++++++++++++++++++++++++++++++--------
 2 files changed, 94 insertions(+), 23 deletions(-)
include/linux/pm_qos_params.h +3 -0
index 8ba440e..60ed542 100644
@@ -22,6 +22,9 @@ void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
 void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req);
 
 int pm_qos_request(int pm_qos_class);
+
 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier);
 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier);
 
+int pm_qos_add_notifier_nonblocking(int pm_qos_class, struct notifier_block *notifier);
+int pm_qos_remove_notifier_nonblocking(int pm_qos_class, struct notifier_block *notifier);
kernel/pm_qos_params.c +2 -1
index f42d3f7..0a67997 100644
@@ -63,7 +63,8 @@ static s32 min_compare(s32 v1, s32 v2);
 
 struct pm_qos_object {
 	struct pm_qos_request_list requests;
-	struct blocking_notifier_head *notifiers;
+	struct atomic_notifier_head *notifiers;
+	struct blocking_notifier_head *blocking_notifiers;
 	struct miscdevice pm_qos_power_miscdev;
 	char *name;
 	s32 default_value;

kernel/pm_qos_params.c struct pm_qos_object { · line 77

Quoted 6 lines
kernel/pm_qos_params.c +2 -1
@@ -72,20 +73,24 @@ struct pm_qos_object {
 };
 
 static struct pm_qos_object null_pm_qos;
-static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
+static ATOMIC_NOTIFIER_HEAD(cpu_dma_lat_notifier);
+static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_blocking_notifier);

So I think it might be better implemented by having only a single active notifier head: either blocking or atomic because all this depends on where the callsites for the notifiers are, and the person adding the notifier should know this.. We can add atomic notifiers to the blocking chain, just not vice versa. The idea is that if all the add and update call sites are blocking, you just register the blocking chain and forget the atomic one. The only difference between atomic and blocking notifiers is whether we use a spinlock or a mutex to guard the integrity of the call chain ... if you know you always have user context at the callsites, then you can always use the mutex.

Then, for blocking notifiers, I think in init, we can register a single notifier which just calls __might_sleep() ... that will pick up at runtime any atomic callsite.

For atomics, you just set up an atomic call chain and leave the blocking one null. Then we get a BUG if anyone tries to register a blocking notifier to an atomic only pm_qos_object.

The implementation looks fine, except:

[...]

kernel/pm_qos_params.c void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req) · line 327

Quoted 12 lines
kernel/pm_qos_params.c +11 -0
 /**
+ * pm_qos_add_notifier_nonblocking - sets notification entry for changes to target value
+ *
+ * Code executed by the notifier block may not sleep!
+ *
+ * @pm_qos_class: identifies which qos target changes should be notified.
+ * @notifier: notifier block managed by caller.
+ *
+ * Will register the notifier into a notification chain that gets called
+ * upon changes to the pm_qos_class target value.
+ */
+int pm_qos_add_notifier_nonblocking(int pm_qos_class, struct notifier_block *notifier)

Rightly or wrongly, the notifier people use atomic not nonblocking, so we should really stick with it to avoid confusion.

James

#2 lore raw
Quoted 4 lines · James Bottomley
Quoted 1 line

+int pm_qos_add_notifier_nonblocking(int pm_qos_class, struct notifier_block *notifier)

Rightly or wrongly, the notifier people use atomic not nonblocking, so we should really stick with it to avoid confusion.

I'm afraid that's my fault. I used "atomic" because it's shorter and punchier than "nonblocking". It refers to the context in which the notifier chain may be called.

Alan Stern

#3 lore raw
In reply to James Bottomley
Quoted 32 lines · On Mon, 07 Jun 2010 09:10:40 -0400 James Bottomley <[email protected]>…
Quoted 15 lines
kernel/pm_qos_params.c +2 -1
index f42d3f7..0a67997 100644
@@ -63,7 +63,8 @@ static s32 min_compare(s32 v1, s32 v2);
 
 struct pm_qos_object {
 	struct pm_qos_request_list requests;
-	struct blocking_notifier_head *notifiers;
+	struct atomic_notifier_head *notifiers;
+	struct blocking_notifier_head *blocking_notifiers;
 	struct miscdevice pm_qos_power_miscdev;
 	char *name;
 	s32 default_value;

kernel/pm_qos_params.c struct pm_qos_object { · line 77

Quoted 6 lines
kernel/pm_qos_params.c +2 -1
@@ -72,20 +73,24 @@ struct pm_qos_object {
 };
 
 static struct pm_qos_object null_pm_qos;
-static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
+static ATOMIC_NOTIFIER_HEAD(cpu_dma_lat_notifier);
+static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_blocking_notifier);

So I think it might be better implemented by having only a single active notifier head: either blocking or atomic because all this depends on where the callsites for the notifiers are, and the person adding the notifier should know this..

We can add atomic notifiers to the blocking
chain, just not vice versa. The idea is that if all the add and update call sites are blocking, you just register the blocking chain and forget the atomic one.

Quoted 8 lines

The only difference between atomic and blocking
notifiers is whether we use a spinlock or a mutex to guard the integrity of the call chain ... if you know you always have user context at the callsites, then you can always use the mutex.

Then, for blocking notifiers, I think in init, we can register a single notifier which just calls __might_sleep() ... that will pick up at runtime any atomic callsite.

I like that part. Simple and elegant :)

Quoted 5 lines

For atomics, you just set up an atomic call chain and leave the blocking one null. Then we get a BUG if anyone tries to register a blocking notifier to an atomic only pm_qos_object.

(Well, we can also just ignore and print a WARN() ... but I got your point)

But I don't think I understand how you want to set up the call chains. (I.e. How to decide if all call-sites are from process-context (mutex allowed)? )

As far as I see, the locking for the notifier-chains is in the head. So I have to decide before the first AddNotifier what locking I want (blocking_ or atomic_notifier_head).

Are you thinking about having it hardcoded alongside the pm_qos_object instantiation? (I think that would be ok)

Or are you thinking about some other scheme I don't see?

Quoted 16 lines

The implementation looks fine, except:

[...]

kernel/pm_qos_params.c void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req) · line 327

Quoted 12 lines
kernel/pm_qos_params.c +11 -0
 /**
+ * pm_qos_add_notifier_nonblocking - sets notification entry for changes to target value
+ *
+ * Code executed by the notifier block may not sleep!
+ *
+ * @pm_qos_class: identifies which qos target changes should be notified.
+ * @notifier: notifier block managed by caller.
+ *
+ * Will register the notifier into a notification chain that gets called
+ * upon changes to the pm_qos_class target value.
+ */
+int pm_qos_add_notifier_nonblocking(int pm_qos_class, struct notifier_block *notifier)
Quoted 2 lines

Rightly or wrongly, the notifier people use atomic not nonblocking, so we should really stick with it to avoid confusion.

Yes. I think that's better.

Quoted 2 lines

James

... mulling it over, I think everything you say add's up if I move the decision to the pm_qos_object instatiation. So I will send out a new patch with that implemented.

Cheers,
Flo

#4 lore raw
Quoted 51 lines · Florian Mickler
Quoted 32 lines · On Mon, 07 Jun 2010 09:10:40 -0400 James Bottomley <[email protected]>…
Quoted 15 lines
kernel/pm_qos_params.c +2 -1
index f42d3f7..0a67997 100644
@@ -63,7 +63,8 @@ static s32 min_compare(s32 v1, s32 v2);
 
 struct pm_qos_object {
 	struct pm_qos_request_list requests;
-	struct blocking_notifier_head *notifiers;
+	struct atomic_notifier_head *notifiers;
+	struct blocking_notifier_head *blocking_notifiers;
 	struct miscdevice pm_qos_power_miscdev;
 	char *name;
 	s32 default_value;

kernel/pm_qos_params.c struct pm_qos_object { · line 77

Quoted 6 lines
kernel/pm_qos_params.c +2 -1
@@ -72,20 +73,24 @@ struct pm_qos_object {
 };
 
 static struct pm_qos_object null_pm_qos;
-static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
+static ATOMIC_NOTIFIER_HEAD(cpu_dma_lat_notifier);
+static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_blocking_notifier);

So I think it might be better implemented by having only a single active notifier head: either blocking or atomic because all this depends on where the callsites for the notifiers are, and the person adding the notifier should know this..

We can add atomic notifiers to the blocking
chain, just not vice versa. The idea is that if all the add and update call sites are blocking, you just register the blocking chain and forget the atomic one.

Quoted 8 lines

The only difference between atomic and blocking
notifiers is whether we use a spinlock or a mutex to guard the integrity of the call chain ... if you know you always have user context at the callsites, then you can always use the mutex.

Then, for blocking notifiers, I think in init, we can register a single notifier which just calls __might_sleep() ... that will pick up at runtime any atomic callsite.

I like that part. Simple and elegant :)

Quoted 5 lines

For atomics, you just set up an atomic call chain and leave the blocking one null. Then we get a BUG if anyone tries to register a blocking notifier to an atomic only pm_qos_object.

Quoted 6 lines

(Well, we can also just ignore and print a WARN() ... but I got your point)

But I don't think I understand how you want to set up the call chains. (I.e. How to decide if all call-sites are from process-context (mutex allowed)? )

We don't. Whoever creates the new pm_qos constratint decides this ... based on where they're putting the add and update calls. They do this by either declaring a blocking or an atomic notifier chain for the pm_qos_object.

Quoted 3 lines

As far as I see, the locking for the notifier-chains is in the head. So I have to decide before the first AddNotifier what locking I want (blocking_ or atomic_notifier_head).

Right. We still have the atomic and blocking pm_qos notifier registers ... but if you try to register an blocking notifier to a pm_qos_object that only supports atomic, we bug. Conversely, if you try to register an atomic notifier to a pm_qos_object that supports blocking, we just add it to the blocking call chain ... there's no harm in that since an atomic notifier is only guaranteeing that it won't sleep, so it's irrelevant to it whether we have user context or not.

Quoted 4 lines

Are you thinking about having it hardcoded alongside the pm_qos_object instantiation? (I think that would be ok)

Or are you thinking about some other scheme I don't see?

James

#5 lore raw

PATCH v2 1/1 pm_qos: make update_request callable from interrupt context

We use the spinlocked notifier chain variant (struct atomic_notifier_head) and add an __might_sleep() to the chain for constraints which have non-atomic notifiers. This way we catch all interrupt-context-update-sites at runtime.

---
 include/linux/pm_qos_params.h |    2 +-
 kernel/pm_qos_params.c        |   54 +++++++++++++++++++++-------------------
 2 files changed, 29 insertions(+), 27 deletions(-)
include/linux/pm_qos_params.h +1 -1
index 8ba440e..2959aac 100644
@@ -22,6 +22,6 @@ void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
 void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req);
 
 int pm_qos_request(int pm_qos_class);
+
 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier);
 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier);
-
kernel/pm_qos_params.c +28 -26

146 lines not on this page · raw message

Signature · 1 line
1.7.1
#6 lore raw

PATCH v3 1/1 pm_qos: make update_request callable from interrupt context

In reply to James Bottomley

We use the spinlocked notifier chain variant (struct atomic_notifier_head) and add an __might_sleep() to the chain for constraints which have non-atomic notifiers. This way we catch all interrupt-context-update-sites at runtime.

---

sorry, forgot to commit the changes of my last pass.

btw, that = __might_sleep() gives an "incompatible pointer" warning.

should we care? it boots ok.

include/linux/pm_qos_params.h | 3 +-

 kernel/pm_qos_params.c        |   61 +++++++++++++++++++++++-----------------
 2 files changed, 36 insertions(+), 28 deletions(-)
include/linux/pm_qos_params.h +1 -2
index 8ba440e..c21ab20 100644
@@ -20,8 +20,7 @@ struct pm_qos_request_list *pm_qos_add_request(int pm_qos_class, s32 value);
 void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
 		s32 new_value);
 void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req);
-
 int pm_qos_request(int pm_qos_class);
+
 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier);
 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier);
-
kernel/pm_qos_params.c +35 -26

174 lines not on this page · raw message

Signature · 1 line
1.7.1
#7 lore raw
Quoted 4 lines · On Mon, 2010-06-07 at 17:34 +0200, [email protected] wrote:

We use the spinlocked notifier chain variant (struct atomic_notifier_head) and add an __might_sleep() to the chain for constraints which have non-atomic notifiers. This way we catch all interrupt-context-update-sites at runtime.

Actually, I'm afraid we can't really call blocking notifiers through the atomic chain because we might end up with a contested chain call and a huge busy wait in the spinlock (especially if one of the notifiers is sleeping).

I think the pm_qos_object still needs the two notifier chains ... it's just that when set up, one must either fill an atomic or a blocking chain (leaving the other NULL). We use the NULL to check to decide what chain to add notifiers to, and if the blocking chain is null, we refuse to add blocking notifiers (with a BUG). If the blocking chain is non-null, we register the might_sleep() notifier (actually, given the argument mismatch, you'll have to wrapper that).

James

#8 lore raw
Quoted 19 lines · James Bottomley
Quoted 4 lines · On Mon, 2010-06-07 at 17:34 +0200, [email protected] wrote:

We use the spinlocked notifier chain variant (struct atomic_notifier_head) and add an __might_sleep() to the chain for constraints which have non-atomic notifiers. This way we catch all interrupt-context-update-sites at runtime.

Actually, I'm afraid we can't really call blocking notifiers through the atomic chain because we might end up with a contested chain call and a huge busy wait in the spinlock (especially if one of the notifiers is sleeping).

I think the pm_qos_object still needs the two notifier chains ... it's just that when set up, one must either fill an atomic or a blocking chain (leaving the other NULL). We use the NULL to check to decide what chain to add notifiers to, and if the blocking chain is null, we refuse to add blocking notifiers (with a BUG). If the blocking chain is non-null, we register the might_sleep() notifier (actually, given the argument mismatch, you'll have to wrapper that).

Quoted 1 line

James

Can't we just requiere that all notifier callbacks be atomic context safe and not fart around with 2 classes of notifiers?

--mgross

#9 lore raw
Quoted 11 lines · On Mon, 7 Jun 2010 21:13:41 -0700 mark gross <[email protected]> wrote:
Quoted 10 lines · James Bottomley
Quoted 4 lines · On Mon, 2010-06-07 at 17:34 +0200, [email protected] wrote:

We use the spinlocked notifier chain variant (struct atomic_notifier_head) and add an __might_sleep() to the chain for constraints which have non-atomic notifiers. This way we catch all interrupt-context-update-sites at runtime.

Actually, I'm afraid we can't really call blocking notifiers through the atomic chain because we might end up with a contested chain call and a huge busy wait in the spinlock (especially if one of the notifiers is sleeping).

Argh. True. The spinlock is held while calling the notifiers.

Quoted 9 lines
Quoted 9 lines

I think the pm_qos_object still needs the two notifier chains ... it's just that when set up, one must either fill an atomic or a blocking chain (leaving the other NULL). We use the NULL to check to decide what chain to add notifiers to, and if the blocking chain is null, we refuse to add blocking notifiers (with a BUG). If the blocking chain is non-null, we register the might_sleep() notifier (actually, given the argument mismatch, you'll have to wrapper that).

Quoted 5 lines
Quoted 1 line

James

Can't we just requiere that all notifier callbacks be atomic context safe and not fart around with 2 classes of notifiers?

--mgross

I think that would be conceptual right.

Under the assumption, that the qos-infrastructure is used by the drivers to guarantee their functioning, we can not allow a race to occur.

( Individual listeners of course are free to spawn or schedule work if they think it is safe to do so and have all races possible considered. )

I'll audit the current listeners this evening and respin this patch without the might_sleep()'s and adapted comments.
What do you think?

Cheers,
Flo

#10 lore raw
In reply to mark gross
Quoted 20 lines · mark gross
Quoted 19 lines · James Bottomley
Quoted 4 lines · On Mon, 2010-06-07 at 17:34 +0200, [email protected] wrote:

We use the spinlocked notifier chain variant (struct atomic_notifier_head) and add an __might_sleep() to the chain for constraints which have non-atomic notifiers. This way we catch all interrupt-context-update-sites at runtime.

Actually, I'm afraid we can't really call blocking notifiers through the atomic chain because we might end up with a contested chain call and a huge busy wait in the spinlock (especially if one of the notifiers is sleeping).

I think the pm_qos_object still needs the two notifier chains ... it's just that when set up, one must either fill an atomic or a blocking chain (leaving the other NULL). We use the NULL to check to decide what chain to add notifiers to, and if the blocking chain is null, we refuse to add blocking notifiers (with a BUG). If the blocking chain is non-null, we register the might_sleep() notifier (actually, given the argument mismatch, you'll have to wrapper that).

Quoted 4 lines
Quoted 1 line

James

Can't we just requiere that all notifier callbacks be atomic context safe and not fart around with 2 classes of notifiers?

Not unless someone rewrites the network notifier: it uses mutexes and is clearly assuming user context. Perhaps they could simply be replaced with spinlocks but someone who understands the net code would would have to advise on this.

James

#11 lore raw
Quoted 26 lines · On Tue, 08 Jun 2010 08:06:04 -0400 James Bottomley <[email protected]>…
Quoted 20 lines · mark gross
Quoted 19 lines · James Bottomley
Quoted 4 lines · On Mon, 2010-06-07 at 17:34 +0200, [email protected] wrote:

We use the spinlocked notifier chain variant (struct atomic_notifier_head) and add an __might_sleep() to the chain for constraints which have non-atomic notifiers. This way we catch all interrupt-context-update-sites at runtime.

Actually, I'm afraid we can't really call blocking notifiers through the atomic chain because we might end up with a contested chain call and a huge busy wait in the spinlock (especially if one of the notifiers is sleeping).

I think the pm_qos_object still needs the two notifier chains ... it's just that when set up, one must either fill an atomic or a blocking chain (leaving the other NULL). We use the NULL to check to decide what chain to add notifiers to, and if the blocking chain is null, we refuse to add blocking notifiers (with a BUG). If the blocking chain is non-null, we register the might_sleep() notifier (actually, given the argument mismatch, you'll have to wrapper that).

Quoted 4 lines
Quoted 1 line

James

Can't we just requiere that all notifier callbacks be atomic context safe and not fart around with 2 classes of notifiers?

Quoted 7 lines

Not unless someone rewrites the network notifier: it uses mutexes and is clearly assuming user context. Perhaps they could simply be replaced with spinlocks but someone who understands the net code would would have to advise on this.

James

What I thought about is to introduce a "scheduled_notifier_block" for using sleeping notifiers on atomic_notifier_chain. That notifier block would not execute the given function but use schedule_work() to schedule it. I will try to shake up an implementation for that.

Either way, my preferred approach would be to just use schedule_work in the network notifier.
I think that would be safe: Currently there is no synchronization between pm_qos_update_request caller and the ieee listener. So an schedule_work would change the timing, but not introduce an race that wasn't there before. execute_in_process_context might not be a good idea, because the spinlock will be held in case it's called from user context.

I will do a patch for scheduling the network notifier and present it to the relevant people, methinks.

Cheers,
Flo

p.s.: if my assumptions don't hold and it get's shot down by the network people, I think James' two-queues-one-used solution is sensible.

#12 lore raw
Quoted 11 lines · On Wed, 9 Jun 2010 08:54:27 +0200 Florian Mickler <[email protected]> wrote:
Quoted 11 lines · James Bottomley
Quoted 10 lines
Quoted 10 lines · On Mon, 2010-06-07 at 17:34 +0200, [email protected] wrote:
Quoted 4 lines

We use the spinlocked notifier chain variant (struct atomic_notifier_head) and add an __might_sleep() to the chain for constraints which have non-atomic notifiers. This way we catch all interrupt-context-update-sites at runtime.

Actually, I'm afraid we can't really call blocking notifiers through the atomic chain because we might end up with a contested chain call and a huge busy wait in the spinlock (especially if one of the notifiers is sleeping).

Actually, I just checked and __atomic_notifier_call_chain() uses an rcu_read_lock() to protect the chain from removal of notifiers while going through it.

So I (now again) think this patch (with queuing the might_sleep() for the network notifier) might be enough.

Cheers,
Flo

#13 lore raw
Quoted 19 lines · On Wed, 9 Jun 2010 09:13:30 +0200 Florian Mickler <[email protected]> wrote:
Quoted 11 lines · On Wed, 9 Jun 2010 08:54:27 +0200 Florian Mickler <[email protected]> wrote:
Quoted 11 lines · James Bottomley
Quoted 10 lines
Quoted 10 lines · On Mon, 2010-06-07 at 17:34 +0200, [email protected] wrote:
Quoted 4 lines

We use the spinlocked notifier chain variant (struct atomic_notifier_head) and add an __might_sleep() to the chain for constraints which have non-atomic notifiers. This way we catch all interrupt-context-update-sites at runtime.

Actually, I'm afraid we can't really call blocking notifiers through the atomic chain because we might end up with a contested chain call and a huge busy wait in the spinlock (especially if one of the notifiers is sleeping).

Actually, I just checked and __atomic_notifier_call_chain() uses an rcu_read_lock() to protect the chain from removal of notifiers while going through it.

Quoted 5 lines

So I (now again) think this patch (with queuing the might_sleep() for the network notifier) might be enough.

Cheers,
Flo

Oh, wow. I changed my mind again after reading up on RCU :|.

* It is illegal to block while in an RCU read-side critical section.

(from include/linux/rcupdate.h)

Cheers,
Flo

p.s.: so, back to my original plan.