RE: IRP_MN_QUERY_POWER for a System Power State

A quick update on this. There is a subtle race condition in this
callback between its invocation and the actual Sx irp coming to your
driver. Since the callback is called before the PnP device tree lock is
held, you have a window where your driver can load after the callback
has been called and before the tree is locked. If you selectively
suspend in this window, you will not receive a callback to indicate that
you should cancel selective suspend and move into D0. What this means
to me is either

  1. you ignore the problem and say that the device will never be plugged
    in right when the machine is going into Sx

Or

  1. you use the actual Sx irp as the indication to cancel the selective
    suspend request, bring your device into D0, and then into the
    appropriate Dx state based on the indicated Sx state.

For WDF, I can’t choose 1) since I can’t generically make that statement
for all drivers. I have chosen 2) since I can handle its drawbacks (it
can come at dispatch level, it can come after the paging device is off
so you can’t cause paging i/o, etc).

Thx
d

-----Original Message-----
From: Doron Holan
Sent: Friday, September 03, 2004 2:22 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

I wouldn’t consider having to move into D0 and then back into Dx a
drawback. This allows you to distinguish between arming for selective
suspend and arming for wake from Sx. The 2 reasons for arming could be
different. Or, if you don’t wake form Sx, you have to move into D3 and
you are not allowed to have a Dx -> Dx transition, so you have to move
D2 -> D0 -> D3 anyways.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Michal Vodicka
Sent: Friday, September 03, 2004 1:52 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

Thank you. I already implemented it and it seems as optimal solution.
Callback is called before Sx query IRP is sent and is called again to
reaffirm S0 even if some driver vetoes query. I hope number of
registered callbacks isn’t limited as I register one per device; it is
easier.

The only minor drawback when used for selective suspend cancellation is
that if device is already suspended, it has to be awakened there (D2 ->
D0) even if following S0 -> Sx transition puts it back to D2.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]


From:
xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com
] on behalf of Jake Oshins[SMTP:xxxxx@windows.microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Friday, September 03, 2004 7:30 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] IRP_MN_QUERY_POWER for a System Power State

Let me amplify what Doron is saying here.

The power state callback is called *before* the Power manager calls
into the
PnP manager and locks the PnP tree, which is *before* the Power
manager
starts sending any S-state IRPs.

The power state callback was created specifically so that you could be

guaranteed that you could do pageable, blockable operations as part of
a
system power state transition, even if your driver has to clear
DO_POWER_PAGABLE.


Jake Oshins
Windows Kernel Group

This posting is provided “AS IS” with no warranties, and confers no
rights.

“Doron Holan” wrote in message
> news:xxxxx@ntdev…
> The Po subsystem won’t block it. It allows d irps at any time (of
> course, if there is a D irp already in the stack, the D0 irp will get
> queued until the in stack D irp completes). What you described is
> pretty much how WDF works and I have tested this on xp sp2 and it
worked
> just fine.
>
> d
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of Michal Vodicka
> Sent: Thursday, September 02, 2004 1:39 PM
> To: Windows System Software Devs Interest List
> Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State
>
> > ----------
> > From:
>
xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com
>] on behalf of Doron Holan[SMTP:xxxxx@windows.microsoft.com]
> > Reply To: Windows System Software Devs Interest List
> > Sent: Thursday, September 02, 2004 10:18 PM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State
> >
> > The Po subsystem calls the callback. It gets called before you move
> > into Sx and after you have returned into S0. You will not see a S
> power
> > irp while this callback is being made.
> >
> Thanks, you convinced me to try it. The scenario I’m affraid of is
> following:
>
> - power state callback is called because of S0 -> Sx
> - called code tries to cancel selective suspend and waits until it
> finishes
> - cancel attempt is too late and idle callback is called before. It
wins
> races (handled correctly) and sends D2 IRP
> - idle callback waits until D2 request is finished and then signals an
> event for which is SS cancel code waiting
>
> If D2 is blocked for some reason, there is a deadlock. Is there any
> possibility it is blocked when power state callback is called? In Po
> subsystem or USB stack with XP SP2 changes. It seems as there should
be
> no reason to block but I want to be sure.
>
> Best regards,
>
> Michal Vodicka
> UPEK, Inc.
> [xxxxx@upek.com, http://www.upek.com]
>
>
>
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: unknown lmsubst tag
argument:
> ‘’
> To unsubscribe send a blank email to xxxxx@lists.osr.com>
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@upek.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: unknown lmsubst tag argument:
‘’
To unsubscribe send a blank email to xxxxx@lists.osr.com

Yep, yet another race conditions I hadn’t considered. Thanks for the info. Fortunately, I left selective suspend cancellation in Sx query path so even if it really occurs, it should be handled. Code is simply skipped if called at dispatch level and can’t wait. Not quite correct but the probability that both callback is missed and query is called at dispatch level is hopefully low enough. In fact, I have both #1 and #2 combined :slight_smile:

Do you have any info about races between selective suspend and surpsise removal I described here about two weeks before? How do you handle it in WDF?

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]


From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of Doron Holan[SMTP:xxxxx@windows.microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Friday, October 22, 2004 8:48 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

A quick update on this. There is a subtle race condition in this
callback between its invocation and the actual Sx irp coming to your
driver. Since the callback is called before the PnP device tree lock is
held, you have a window where your driver can load after the callback
has been called and before the tree is locked. If you selectively
suspend in this window, you will not receive a callback to indicate that
you should cancel selective suspend and move into D0. What this means
to me is either

  1. you ignore the problem and say that the device will never be plugged
    in right when the machine is going into Sx

Or

  1. you use the actual Sx irp as the indication to cancel the selective
    suspend request, bring your device into D0, and then into the
    appropriate Dx state based on the indicated Sx state.

For WDF, I can’t choose 1) since I can’t generically make that statement
for all drivers. I have chosen 2) since I can handle its drawbacks (it
can come at dispatch level, it can come after the paging device is off
so you can’t cause paging i/o, etc).

Thx
d

-----Original Message-----
From: Doron Holan
Sent: Friday, September 03, 2004 2:22 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

I wouldn’t consider having to move into D0 and then back into Dx a
drawback. This allows you to distinguish between arming for selective
suspend and arming for wake from Sx. The 2 reasons for arming could be
different. Or, if you don’t wake form Sx, you have to move into D3 and
you are not allowed to have a Dx -> Dx transition, so you have to move
D2 -> D0 -> D3 anyways.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Michal Vodicka
Sent: Friday, September 03, 2004 1:52 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

Thank you. I already implemented it and it seems as optimal solution.
Callback is called before Sx query IRP is sent and is called again to
reaffirm S0 even if some driver vetoes query. I hope number of
registered callbacks isn’t limited as I register one per device; it is
easier.

The only minor drawback when used for selective suspend cancellation is
that if device is already suspended, it has to be awakened there (D2 ->
D0) even if following S0 -> Sx transition puts it back to D2.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

> ----------
> From:
xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com
] on behalf of Jake Oshins[SMTP:xxxxx@windows.microsoft.com]
> Reply To: Windows System Software Devs Interest List>
> Sent: Friday, September 03, 2004 7:30 PM
> To: Windows System Software Devs Interest List
> Subject: Re:[ntdev] IRP_MN_QUERY_POWER for a System Power State
>
> Let me amplify what Doron is saying here.
>
> The power state callback is called *before* the Power manager calls
into the
> PnP manager and locks the PnP tree, which is *before* the Power
manager
> starts sending any S-state IRPs.
>
> The power state callback was created specifically so that you could be

> guaranteed that you could do pageable, blockable operations as part of
a
> system power state transition, even if your driver has to clear
> DO_POWER_PAGABLE.
>
> –
> Jake Oshins
> Windows Kernel Group
>
> This posting is provided “AS IS” with no warranties, and confers no
rights.
>
>
> “Doron Holan” wrote in message
> > news:xxxxx@ntdev…
> > The Po subsystem won’t block it. It allows d irps at any time (of
> > course, if there is a D irp already in the stack, the D0 irp will get
> > queued until the in stack D irp completes). What you described is
> > pretty much how WDF works and I have tested this on xp sp2 and it
> worked
> > just fine.
> >
> > d
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com] On Behalf Of Michal Vodicka
> > Sent: Thursday, September 02, 2004 1:39 PM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State
> >
> > > ----------
> > > From:
> >
> xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com
> >] on behalf of Doron Holan[SMTP:xxxxx@windows.microsoft.com]
> > > Reply To: Windows System Software Devs Interest List
> > > Sent: Thursday, September 02, 2004 10:18 PM
> > > To: Windows System Software Devs Interest List
> > > Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State
> > >
> > > The Po subsystem calls the callback. It gets called before you move
> > > into Sx and after you have returned into S0. You will not see a S
> > power
> > > irp while this callback is being made.
> > >
> > Thanks, you convinced me to try it. The scenario I’m affraid of is
> > following:
> >
> > - power state callback is called because of S0 -> Sx
> > - called code tries to cancel selective suspend and waits until it
> > finishes
> > - cancel attempt is too late and idle callback is called before. It
> wins
> > races (handled correctly) and sends D2 IRP
> > - idle callback waits until D2 request is finished and then signals an
> > event for which is SS cancel code waiting
> >
> > If D2 is blocked for some reason, there is a deadlock. Is there any
> > possibility it is blocked when power state callback is called? In Po
> > subsystem or USB stack with XP SP2 changes. It seems as there should
> be
> > no reason to block but I want to be sure.
> >
> > Best regards,
> >
> > Michal Vodicka
> > UPEK, Inc.
> > [xxxxx@upek.com, http://www.upek.com]
> >
> >
> >
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: unknown lmsubst tag
> argument:
> > ‘’
> > To unsubscribe send a blank email to xxxxx@lists.osr.com>
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@upek.com
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: unknown lmsubst tag argument:
> ‘’
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256>
>
> You are currently subscribed to ntdev as: unknown lmsubst tag argument: ‘’
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>

Remember that the query Sx irp is not guaranteed to come, the os can
send an Sx irp w/out the query.

As for the SS race with surprise remove, did Jason not contact you? I
fwd’ed your problem to the usb core team and he said he would email you.
As for WDF, if a surprise remove comes in, I represent it as an event
into the power policy engine. If the power policy engine is in the
middle of a locked transaction like selective suspend, it will complete
the locked transaction first, pending the surprise remove. Once the
transaction is complete and I move into an unlocked state, will process
the surprise remove. I will have to look at your deadlock again to see
if I can get into the same state.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Michal Vodicka
Sent: Friday, October 22, 2004 1:10 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

Yep, yet another race conditions I hadn’t considered. Thanks for the
info. Fortunately, I left selective suspend cancellation in Sx query
path so even if it really occurs, it should be handled. Code is simply
skipped if called at dispatch level and can’t wait. Not quite correct
but the probability that both callback is missed and query is called at
dispatch level is hopefully low enough. In fact, I have both #1 and #2
combined :slight_smile:

Do you have any info about races between selective suspend and surpsise
removal I described here about two weeks before? How do you handle it in
WDF?

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]


From:
xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com
] on behalf of Doron Holan[SMTP:xxxxx@windows.microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Friday, October 22, 2004 8:48 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

A quick update on this. There is a subtle race condition in this
callback between its invocation and the actual Sx irp coming to your
driver. Since the callback is called before the PnP device tree lock
is
held, you have a window where your driver can load after the callback
has been called and before the tree is locked. If you selectively
suspend in this window, you will not receive a callback to indicate
that
you should cancel selective suspend and move into D0. What this means
to me is either

  1. you ignore the problem and say that the device will never be
    plugged
    in right when the machine is going into Sx

Or

  1. you use the actual Sx irp as the indication to cancel the
    selective
    suspend request, bring your device into D0, and then into the
    appropriate Dx state based on the indicated Sx state.

For WDF, I can’t choose 1) since I can’t generically make that
statement
for all drivers. I have chosen 2) since I can handle its drawbacks
(it
can come at dispatch level, it can come after the paging device is off
so you can’t cause paging i/o, etc).

Thx
d

-----Original Message-----
From: Doron Holan
Sent: Friday, September 03, 2004 2:22 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

I wouldn’t consider having to move into D0 and then back into Dx a
drawback. This allows you to distinguish between arming for selective
suspend and arming for wake from Sx. The 2 reasons for arming could
be
different. Or, if you don’t wake form Sx, you have to move into D3
and
you are not allowed to have a Dx -> Dx transition, so you have to move
D2 -> D0 -> D3 anyways.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Michal Vodicka
Sent: Friday, September 03, 2004 1:52 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

Thank you. I already implemented it and it seems as optimal solution.
Callback is called before Sx query IRP is sent and is called again to
reaffirm S0 even if some driver vetoes query. I hope number of
registered callbacks isn’t limited as I register one per device; it is
easier.

The only minor drawback when used for selective suspend cancellation
is
that if device is already suspended, it has to be awakened there (D2
->
D0) even if following S0 -> Sx transition puts it back to D2.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]

> ----------
> From:

xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com

] on behalf of Jake Oshins[SMTP:xxxxx@windows.microsoft.com]
> Reply To: Windows System Software Devs Interest List>
> Sent: Friday, September 03, 2004 7:30 PM
> To: Windows System Software Devs Interest List
> Subject: Re:[ntdev] IRP_MN_QUERY_POWER for a System Power State
>
> Let me amplify what Doron is saying here.
>
> The power state callback is called *before* the Power manager calls
into the
> PnP manager and locks the PnP tree, which is *before* the Power
manager
> starts sending any S-state IRPs.
>
> The power state callback was created specifically so that you could
be

> guaranteed that you could do pageable, blockable operations as part
of
a
> system power state transition, even if your driver has to clear
> DO_POWER_PAGABLE.
>
> –
> Jake Oshins
> Windows Kernel Group
>
> This posting is provided “AS IS” with no warranties, and confers no
rights.
>
>
> “Doron Holan” wrote in message
> > news:xxxxx@ntdev…
> > The Po subsystem won’t block it. It allows d irps at any time (of
> > course, if there is a D irp already in the stack, the D0 irp will
get
> > queued until the in stack D irp completes). What you described is
> > pretty much how WDF works and I have tested this on xp sp2 and it
> worked
> > just fine.
> >
> > d
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com] On Behalf Of Michal
Vodicka
> > Sent: Thursday, September 02, 2004 1:39 PM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State
> >
> > > ----------
> > > From:
> >
>
xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com
> >] on behalf of Doron Holan[SMTP:xxxxx@windows.microsoft.com]
> > > Reply To: Windows System Software Devs Interest List
> > > Sent: Thursday, September 02, 2004 10:18 PM
> > > To: Windows System Software Devs Interest List
> > > Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State
> > >
> > > The Po subsystem calls the callback. It gets called before you
move
> > > into Sx and after you have returned into S0. You will not see a S
> > power
> > > irp while this callback is being made.
> > >
> > Thanks, you convinced me to try it. The scenario I’m affraid of is
> > following:
> >
> > - power state callback is called because of S0 -> Sx
> > - called code tries to cancel selective suspend and waits until it
> > finishes
> > - cancel attempt is too late and idle callback is called before. It
> wins
> > races (handled correctly) and sends D2 IRP
> > - idle callback waits until D2 request is finished and then signals
an
> > event for which is SS cancel code waiting
> >
> > If D2 is blocked for some reason, there is a deadlock. Is there any
> > possibility it is blocked when power state callback is called? In Po
> > subsystem or USB stack with XP SP2 changes. It seems as there should
> be
> > no reason to block but I want to be sure.
> >
> > Best regards,
> >
> > Michal Vodicka
> > UPEK, Inc.
> > [xxxxx@upek.com, http://www.upek.com]
> >
> >
> >
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: unknown lmsubst tag
> argument:
> > ‘’
> > To unsubscribe send a blank email to
xxxxx@lists.osr.com>
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@upek.com
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: unknown lmsubst tag
argument:
> ‘’
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256>
>
> You are currently subscribed to ntdev as: unknown lmsubst tag
argument: ‘’
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: unknown lmsubst tag argument:
‘’
To unsubscribe send a blank email to xxxxx@lists.osr.com

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of Doron Holan[SMTP:xxxxx@windows.microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Friday, October 22, 2004 10:40 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] IRP_MN_QUERY_POWER for a System Power State

Remember that the query Sx irp is not guaranteed to come, the os can
send an Sx irp w/out the query.

Yes, I know. Still I believe the probability of both Sx set without previous query and driver load within this window is very low and can be ignored. I will probably consider it when driver is updated next time but current version is already waiting 3 weeks for new signature and customers keep asking about it so I can’t afford to cancel signature just because of this possibility.

As for the SS race with surprise remove, did Jason not contact you? I
fwd’ed your problem to the usb core team and he said he would email you.

Thanks for forwarding. He didn’t email me about it, yet.

As for WDF, if a surprise remove comes in, I represent it as an event
into the power policy engine. If the power policy engine is in the
middle of a locked transaction like selective suspend, it will complete
the locked transaction first, pending the surprise remove. Once the
transaction is complete and I move into an unlocked state, will process
the surprise remove. I will have to look at your deadlock again to see
if I can get into the same state.

I guess it will deadlock, too, because selective suspend never completes in this state. It should be rather easy to simulate as the window is wide enough (probably up to 500 ms). At least I was able to reproduce it in one of three or four attempts when disconnected device manually in the “right” moment. Next week I should have time to measure window exactly; I’ll modify our firmware to disconnect after configurable timeout.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http://www.upek.com]