linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Aaron Lu <aaron.lu@intel.com>
To: Jeff Garzik <jgarzik@pobox.com>,
	"Rafael J. Wysocki" <rjw@sisk.pl>,
	James Bottomley <James.Bottomley@hansenpartnership.com>,
	Alan Stern <stern@rowland.harvard.edu>, Tejun Heo <tj@kernel.org>,
	Oliver Neukum <oliver@neukum.org>
Cc: Jeff Wu <jeff.wu@amd.com>, Aaron Lu <aaron.lwe@gmail.com>,
	Shane Huang <shane.huang@amd.com>,
	linux-ide@vger.kernel.org, linux-pm@vger.kernel.org,
	linux-scsi@vger.kernel.org, linux-acpi@vger.kernel.org
Subject: [PATCH v8 09/11] block: add a new interface to block events
Date: Mon, 29 Oct 2012 17:01:36 +0800	[thread overview]
Message-ID: <1351501298-3716-10-git-send-email-aaron.lu@intel.com> (raw)
In-Reply-To: <1351501298-3716-1-git-send-email-aaron.lu@intel.com>

A new interface to block disk events is added, this interface is
meant to eliminate a race between PM runtime callback and disk events
checking.

Suppose the following device tree:
device_sata_port  (the parent)
  device_ODD      (the child)

When ODD is runtime suspended, sata port will have a chance to enter
runtime suspended state. And in sata port's runtime suspend callback,
it will check if it is OK to omit the power of the ODD. And if yes, the
periodically running events checking work will be stopped, as the ODD
will be waken up by that check and cancel it can make the ODD stay in
zero power state much longer(no worry about how the ODD gets media
change event in ZPODD's case).

I used disk_block_events to do the events blocking, but there is a race
and can lead to a deadlock: when I call disk_block_events in sata port's
runtime suspend callback, the events checking work may already be running
and it will resume the ODD synchronously, and PM core will try to resume
its parent, the sata port first. The PM core makes sure that runtime
resume callback does not run concurrently with runtime suspend callback,
and if the runtime suspend callback is running, the runtime resume
callback will wait for it done. So the events checking work will wait
for sata port's runtime suspend callback, while the sata port's runtime
suspend callback is waiting for the disk events work to finish. Deadlock.

ODD_suspend                        disk_events_workfn
  ata_port_suspend                   check_events
    disk_block_events                  resume ODD
      cancel_delayed_work_sync           resume parent
      (waiting for disk_events_workfn)   (waiting for suspend callback)

So a new function disk_try_block_events is added, it will try to
cancel the delayed work if the work is currently pending, which means
it has been queued but the timer is not expired yet. If the work is not
pending, we return false, as we do not know if the task is already
running or not queued at all. And if the queued work is successfully
cancelled, disk_block_events will be called and we are done. But if we
failed to cancel the work, false will be returned, and the calling
thread knows that it's not safe to block events at the moment and should
act accordingly. In ZPODD's case, poweroff will not be attempted.

Note:
ODD means: Optical Disc Drive.
ZPODD means: Zero Power ODD. It's a mechnism to place the ODD into zero
power state without user's awareness when the system is at S0 system
state.

Signed-off-by: Aaron Lu <aaron.lu@intel.com>
---
 block/genhd.c         | 26 ++++++++++++++++++++++++++
 include/linux/genhd.h |  1 +
 2 files changed, 27 insertions(+)

diff --git a/block/genhd.c b/block/genhd.c
index cac7366..c6af755 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -1469,6 +1469,32 @@ void disk_block_events(struct gendisk *disk)
 	mutex_unlock(&ev->block_mutex);
 }
 
+/*
+ * Under some circumstances, there is a race between the calling thread
+ * of disk_block_events and the events checking function. To avoid such a race,
+ * this function will check if the delayed work is pending. If not, it means
+ * the work is either not queued or is already running, false is returned.
+ * And if yes, try to cancel the delayed work. If succedded, disk_block_events
+ * will be called and there is no worry that cancel_delayed_work_sync will
+ * deadlock the events checking function. And if failed, false is returned.
+ */
+bool disk_try_block_events(struct gendisk *disk)
+{
+	struct disk_events *ev = disk->ev;
+
+	if (!ev)
+		return false;
+
+	if (delayed_work_pending(&ev->dwork)) {
+		if (cancel_delayed_work(&disk->ev->dwork)) {
+			disk_block_events(disk);
+			return true;
+		}
+	}
+
+	return false;
+}
+
 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
 {
 	struct disk_events *ev = disk->ev;
diff --git a/include/linux/genhd.h b/include/linux/genhd.h
index 4f440b3..b67247f 100644
--- a/include/linux/genhd.h
+++ b/include/linux/genhd.h
@@ -420,6 +420,7 @@ static inline int get_disk_ro(struct gendisk *disk)
 }
 
 extern void disk_block_events(struct gendisk *disk);
+extern bool disk_try_block_events(struct gendisk *disk);
 extern void disk_unblock_events(struct gendisk *disk);
 extern void disk_flush_events(struct gendisk *disk, unsigned int mask);
 extern unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask);
-- 
1.7.12.4


  parent reply	other threads:[~2012-10-29  9:01 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-29  9:01 [PATCH v8 00/11] ZPODD Patches Aaron Lu
2012-10-29  9:01 ` [PATCH v8 01/11] scsi: sr: support runtime pm Aaron Lu
2012-10-29  9:01 ` [PATCH v8 02/11] ata: zpodd: Add CONFIG_SATA_ZPODD Aaron Lu
2012-10-29 18:11   ` James Bottomley
2012-10-30  3:19     ` Aaron Lu
2012-10-29  9:01 ` [PATCH v8 03/11] ata: zpodd: identify and init ZPODD devices Aaron Lu
2012-10-29  9:01 ` [PATCH v8 04/11] libata: acpi: move acpi notification code to sata_zpodd Aaron Lu
2012-10-29  9:01 ` [PATCH v8 05/11] libata-eh: allow defer in ata_exec_internal Aaron Lu
2012-10-29 15:20   ` Tejun Heo
2012-10-30  3:00     ` Aaron Lu
2012-10-30  3:01       ` Tejun Heo
2012-10-30  3:09         ` Aaron Lu
2012-10-31 21:52           ` Tejun Heo
2012-11-01  2:35             ` Aaron Lu
2012-11-01 16:03               ` Tejun Heo
2012-11-02  0:43                 ` Aaron Lu
2012-10-29  9:01 ` [PATCH v8 06/11] ata: zpodd: check loading mechanism for ODD Aaron Lu
2012-10-29  9:01 ` [PATCH v8 07/11] libata: separate ATAPI code Aaron Lu
2012-10-29  9:01 ` [PATCH v8 08/11] ata: zpodd: check zero power ready status Aaron Lu
2012-10-29  9:01 ` Aaron Lu [this message]
2012-10-29 15:35   ` [PATCH v8 09/11] block: add a new interface to block events Tejun Heo
2012-10-30  7:04     ` Aaron Lu
2012-10-31 21:51       ` Tejun Heo
2012-11-01  6:30         ` Aaron Lu
2012-10-29  9:01 ` [PATCH v8 10/11] scsi: sr: support (un)block events Aaron Lu
2012-10-29 18:11   ` James Bottomley
2012-10-29 22:22     ` Alan Stern
2012-10-30  4:34       ` James Bottomley
2012-10-30  5:02     ` Aaron Lu
2012-10-29  9:01 ` [PATCH v8 11/11] ata: zpodd: handle power transition of ODD Aaron Lu

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=1351501298-3716-10-git-send-email-aaron.lu@intel.com \
    --to=aaron.lu@intel.com \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=aaron.lwe@gmail.com \
    --cc=jeff.wu@amd.com \
    --cc=jgarzik@pobox.com \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-ide@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=oliver@neukum.org \
    --cc=rjw@sisk.pl \
    --cc=shane.huang@amd.com \
    --cc=stern@rowland.harvard.edu \
    --cc=tj@kernel.org \
    /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).