public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: "Arve Hjønnevåg" <arve@android.com>
To: linux-pm@lists.linux-foundation.org, linux-kernel@vger.kernel.org
Cc: Len Brown <len.brown@intel.com>,
	Jim Collar <jim.collar@eqware.net>,
	linux-doc@vger.kernel.org, Greg Kroah-Hartman <gregkh@suse.de>,
	Avi Kivity <avi@redhat.com>,
	Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>,
	Magnus Damm <damm@igel.co.jp>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH 2/8] PM: suspend_block: Add driver to access suspend blockers from user-space
Date: Thu, 13 May 2010 21:11:07 -0700	[thread overview]
Message-ID: <1273810273-3039-3-git-send-email-arve@android.com> (raw)
In-Reply-To: <1273810273-3039-2-git-send-email-arve@android.com>

Add a misc device, "suspend_blocker", that allows user-space processes
to block auto suspend. The device has ioctls to create a suspend_blocker,
and to block and unblock suspend. To delete the suspend_blocker, close
the device.

Signed-off-by: Arve Hjønnevåg <arve@android.com>
---
 Documentation/ioctl/ioctl-number.txt          |    3 +-
 Documentation/power/opportunistic-suspend.txt |   27 +++++
 include/linux/suspend_ioctls.h                |    4 +
 kernel/power/Kconfig                          |    7 ++
 kernel/power/Makefile                         |    1 +
 kernel/power/user_suspend_blocker.c           |  143 +++++++++++++++++++++++++
 6 files changed, 184 insertions(+), 1 deletions(-)
 create mode 100644 kernel/power/user_suspend_blocker.c

diff --git a/Documentation/ioctl/ioctl-number.txt b/Documentation/ioctl/ioctl-number.txt
index dd5806f..e2458f7 100644
--- a/Documentation/ioctl/ioctl-number.txt
+++ b/Documentation/ioctl/ioctl-number.txt
@@ -254,7 +254,8 @@ Code  Seq#(hex)	Include File		Comments
 'q'	80-FF	linux/telephony.h	Internet PhoneJACK, Internet LineJACK
 		linux/ixjuser.h		<http://www.quicknet.net>
 'r'	00-1F	linux/msdos_fs.h and fs/fat/dir.c
-'s'	all	linux/cdk.h
+'s'	all	linux/cdk.h	conflict!
+'s'	all	linux/suspend_block_dev.h	conflict!
 't'	00-7F	linux/if_ppp.h
 't'	80-8F	linux/isdn_ppp.h
 't'	90	linux/toshiba.h
diff --git a/Documentation/power/opportunistic-suspend.txt b/Documentation/power/opportunistic-suspend.txt
index 4bee7bc..93f4c24 100644
--- a/Documentation/power/opportunistic-suspend.txt
+++ b/Documentation/power/opportunistic-suspend.txt
@@ -127,3 +127,30 @@ if (list_empty(&state->pending_work))
 	suspend_unblock(&state->suspend_blocker);
 else
 	suspend_block(&state->suspend_blocker);
+
+User space API
+==============
+
+To create a suspend blocker from user space, open the suspend_blocker special
+device file:
+
+    fd = open("/dev/suspend_blocker", O_RDWR | O_CLOEXEC);
+
+then optionally call:
+
+    ioctl(fd, SUSPEND_BLOCKER_IOCTL_SET_NAME(strlen(name)), name);
+
+To activate the suspend blocker call:
+
+    ioctl(fd, SUSPEND_BLOCKER_IOCTL_BLOCK);
+
+To deactivate it call:
+
+    ioctl(fd, SUSPEND_BLOCKER_IOCTL_UNBLOCK);
+
+To destroy the suspend blocker, close the device:
+
+    close(fd);
+
+If the first ioctl called is not SUSPEND_BLOCKER_IOCTL_SET_NAME the suspend
+blocker will get the default name "(userspace)".
diff --git a/include/linux/suspend_ioctls.h b/include/linux/suspend_ioctls.h
index 0b30382..b95a6b2 100644
--- a/include/linux/suspend_ioctls.h
+++ b/include/linux/suspend_ioctls.h
@@ -30,4 +30,8 @@ struct resume_swap_area {
 #define SNAPSHOT_ALLOC_SWAP_PAGE	_IOR(SNAPSHOT_IOC_MAGIC, 20, __kernel_loff_t)
 #define SNAPSHOT_IOC_MAXNR	20
 
+#define SUSPEND_BLOCKER_IOCTL_SET_NAME(len)	_IOC(_IOC_WRITE, 's', 0, len)
+#define SUSPEND_BLOCKER_IOCTL_BLOCK		_IO('s', 1)
+#define SUSPEND_BLOCKER_IOCTL_UNBLOCK		_IO('s', 2)
+
 #endif /* _LINUX_SUSPEND_IOCTLS_H */
diff --git a/kernel/power/Kconfig b/kernel/power/Kconfig
index 6d11a45..2e665cd 100644
--- a/kernel/power/Kconfig
+++ b/kernel/power/Kconfig
@@ -146,6 +146,13 @@ config OPPORTUNISTIC_SUSPEND
 	  determines the sleep state the system will be put into when there are
 	  no active suspend blockers.
 
+config USER_SUSPEND_BLOCKERS
+	bool "User space suspend blockers"
+	depends on OPPORTUNISTIC_SUSPEND
+	---help---
+	  User space suspend blockers API.  Creates a misc device allowing user
+	  space to create, use and destroy suspend blockers.
+
 config HIBERNATION_NVS
 	bool
 
diff --git a/kernel/power/Makefile b/kernel/power/Makefile
index 95d8e6d..2015594 100644
--- a/kernel/power/Makefile
+++ b/kernel/power/Makefile
@@ -8,6 +8,7 @@ obj-$(CONFIG_PM_SLEEP)		+= console.o
 obj-$(CONFIG_FREEZER)		+= process.o
 obj-$(CONFIG_SUSPEND)		+= suspend.o
 obj-$(CONFIG_OPPORTUNISTIC_SUSPEND)	+= opportunistic_suspend.o
+obj-$(CONFIG_USER_SUSPEND_BLOCKERS)	+= user_suspend_blocker.o
 obj-$(CONFIG_PM_TEST_SUSPEND)	+= suspend_test.o
 obj-$(CONFIG_HIBERNATION)	+= hibernate.o snapshot.o swap.o user.o
 obj-$(CONFIG_HIBERNATION_NVS)	+= hibernate_nvs.o
diff --git a/kernel/power/user_suspend_blocker.c b/kernel/power/user_suspend_blocker.c
new file mode 100644
index 0000000..d53f939
--- /dev/null
+++ b/kernel/power/user_suspend_blocker.c
@@ -0,0 +1,143 @@
+/*
+ * kernel/power/user_suspend_blocker.c
+ *
+ * Copyright (C) 2009-2010 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/suspend.h>
+#include <linux/suspend_ioctls.h>
+
+enum {
+	DEBUG_FAILURE	= BIT(0),
+};
+static int debug_mask = DEBUG_FAILURE;
+module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
+
+static DEFINE_MUTEX(ioctl_lock);
+
+#define USER_SUSPEND_BLOCKER_NAME_LEN 31
+
+struct user_suspend_blocker {
+	struct suspend_blocker	blocker;
+	char			name[USER_SUSPEND_BLOCKER_NAME_LEN + 1];
+	bool			registered;
+};
+
+static int user_suspend_blocker_open(struct inode *inode, struct file *filp)
+{
+	struct user_suspend_blocker *blocker;
+
+	blocker = kzalloc(sizeof(*blocker), GFP_KERNEL);
+	if (!blocker)
+		return -ENOMEM;
+
+	nonseekable_open(inode, filp);
+	strcpy(blocker->name, "(userspace)");
+	blocker->blocker.name = blocker->name;
+	filp->private_data = blocker;
+
+	return 0;
+}
+
+static int suspend_blocker_set_name(struct user_suspend_blocker *blocker,
+				    void __user *name, size_t name_len)
+{
+	if (blocker->registered)
+		return -EBUSY;
+
+	if (name_len > USER_SUSPEND_BLOCKER_NAME_LEN)
+		name_len = USER_SUSPEND_BLOCKER_NAME_LEN;
+
+	if (copy_from_user(blocker->name, name, name_len))
+		return -EFAULT;
+	blocker->name[name_len] = '\0';
+
+	return 0;
+}
+
+static long user_suspend_blocker_ioctl(struct file *filp, unsigned int cmd,
+					unsigned long _arg)
+{
+	void __user *arg = (void __user *)_arg;
+	struct user_suspend_blocker *blocker = filp->private_data;
+	long ret = 0;
+
+	mutex_lock(&ioctl_lock);
+	if ((cmd & ~IOCSIZE_MASK) == SUSPEND_BLOCKER_IOCTL_SET_NAME(0)) {
+		ret = suspend_blocker_set_name(blocker, arg, _IOC_SIZE(cmd));
+		goto done;
+	}
+	if (!blocker->registered) {
+		suspend_blocker_register(&blocker->blocker);
+		blocker->registered = true;
+	}
+	switch (cmd) {
+	case SUSPEND_BLOCKER_IOCTL_BLOCK:
+		suspend_block(&blocker->blocker);
+		break;
+
+	case SUSPEND_BLOCKER_IOCTL_UNBLOCK:
+		suspend_unblock(&blocker->blocker);
+		break;
+
+	default:
+		ret = -ENOTTY;
+	}
+done:
+	if (ret && (debug_mask & DEBUG_FAILURE))
+		pr_err("user_suspend_blocker_ioctl: cmd %x failed, %ld\n",
+			cmd, ret);
+	mutex_unlock(&ioctl_lock);
+	return ret;
+}
+
+static int user_suspend_blocker_release(struct inode *inode, struct file *filp)
+{
+	struct user_suspend_blocker *blocker = filp->private_data;
+
+	if (blocker->registered)
+		suspend_blocker_unregister(&blocker->blocker);
+	kfree(blocker);
+
+	return 0;
+}
+
+const struct file_operations user_suspend_blocker_fops = {
+	.open = user_suspend_blocker_open,
+	.release = user_suspend_blocker_release,
+	.unlocked_ioctl = user_suspend_blocker_ioctl,
+};
+
+struct miscdevice user_suspend_blocker_device = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = "suspend_blocker",
+	.fops = &user_suspend_blocker_fops,
+};
+
+static int __init user_suspend_blocker_init(void)
+{
+	return misc_register(&user_suspend_blocker_device);
+}
+
+static void __exit user_suspend_blocker_exit(void)
+{
+	misc_deregister(&user_suspend_blocker_device);
+}
+
+module_init(user_suspend_blocker_init);
+module_exit(user_suspend_blocker_exit);
-- 
1.6.5.1

_______________________________________________
linux-pm mailing list
linux-pm@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/linux-pm

  reply	other threads:[~2010-05-14  4:11 UTC|newest]

Thread overview: 89+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1273810273-3039-1-git-send-email-arve@android.com>
2010-05-14  4:11 ` [PATCH 1/8] PM: Add suspend block api Arve Hjønnevåg
2010-05-14  4:11   ` Arve Hjønnevåg [this message]
2010-05-14  4:11     ` [PATCH 3/8] PM: suspend_block: Abort task freezing if a suspend_blocker is active Arve Hjønnevåg
2010-05-14  4:11       ` [PATCH 4/8] PM: suspend_block: Add debugfs file Arve Hjønnevåg
2010-05-14  4:11         ` [PATCH 5/8] PM: suspend_block: Add suspend_blocker stats Arve Hjønnevåg
2010-05-14  4:11           ` [PATCH 6/8] PM: Add suspend blocking work Arve Hjønnevåg
     [not found]           ` <1273810273-3039-7-git-send-email-arve@android.com>
2010-05-14  4:11             ` [PATCH 7/8] Input: Block suspend while event queue is not empty Arve Hjønnevåg
2010-05-14  4:11               ` [PATCH 8/8] power_supply: Block suspend while power supply change notifications are pending Arve Hjønnevåg
2010-05-14  6:13   ` [PATCH 1/8] PM: Add suspend block api Paul Walmsley
2010-05-14  6:27   ` Paul Walmsley
2010-05-14  7:14     ` Arve Hjønnevåg
2010-05-18  2:17       ` Paul Walmsley
2010-05-18  3:06         ` Arve Hjønnevåg
2010-05-18  3:34           ` Paul Walmsley
2010-05-18  3:51             ` Arve Hjønnevåg
2010-05-19 15:55               ` Paul Walmsley
2010-05-20  0:35                 ` Arve Hjønnevåg
2010-05-18 13:11   ` Pavel Machek
     [not found]   ` <20100518131111.GB1563@ucw.cz>
2010-05-20  9:11     ` Florian Mickler
     [not found]     ` <20100520111111.333beb73@schatten.dmk.lab>
2010-05-20  9:26       ` Florian Mickler
     [not found]       ` <20100520112642.74d93d26@schatten.dmk.lab>
2010-05-20 22:18         ` Rafael J. Wysocki
     [not found]         ` <201005210018.43576.rjw@sisk.pl>
2010-05-21  6:04           ` Florian Mickler
2010-05-27 15:41           ` Pavel Machek
2010-05-14 21:08 ` [PATCH 0/8] Suspend block api (version 7) Rafael J. Wysocki
2010-05-16 19:42 ` Rafael J. Wysocki
     [not found] ` <201005162142.39343.rjw@sisk.pl>
2010-05-17  4:16   ` Arve Hjønnevåg
     [not found]   ` <AANLkTinP68yHYfVCW-urCrRbeSb0Mlz8JsvAs-FKoUPZ@mail.gmail.com>
2010-05-17 20:40     ` Rafael J. Wysocki
     [not found]     ` <201005172240.35742.rjw@sisk.pl>
2010-05-17 20:51       ` Brian Swetland
     [not found]       ` <AANLkTim2b8cKnzPgN7m5zImb0hVDfqVjRidKNVoYi1yO@mail.gmail.com>
2010-05-17 21:44         ` Rafael J. Wysocki
     [not found]         ` <201005172344.46222.rjw@sisk.pl>
2010-05-17 23:32           ` Arve Hjønnevåg
     [not found]           ` <AANLkTikENyp0UOKP2NzwjKye1EiZitdE0o4k1Z4ygCsT@mail.gmail.com>
2010-05-18 19:38             ` Rafael J. Wysocki
     [not found]             ` <201005182138.04610.rjw@sisk.pl>
2010-05-18 20:35               ` Arve Hjønnevåg
     [not found]               ` <AANLkTileYiu6pL4eX3lpvpYLuu76A8auZp4xrcDX94BQ@mail.gmail.com>
2010-05-18 21:14                 ` Rafael J. Wysocki
     [not found]                 ` <201005182314.08761.rjw@sisk.pl>
2010-05-18 22:21                   ` Arve Hjønnevåg
     [not found]                   ` <AANLkTikm69OKbRUvqiqGh4KRMn7u166tyt4kx0-bjR3Z@mail.gmail.com>
2010-05-18 22:56                     ` Rafael J. Wysocki
     [not found]                     ` <201005190056.36804.rjw@sisk.pl>
2010-05-18 23:06                       ` Arve Hjønnevåg
     [not found]                       ` <AANLkTikc9MmR_r1NkdCwDaPD4kJa9l9KUwcWAxilcs3l@mail.gmail.com>
2010-05-19 20:40                         ` Rafael J. Wysocki
     [not found] ` <201005142308.48386.rjw@sisk.pl>
2010-05-17  4:50   ` Arve Hjønnevåg
     [not found]   ` <AANLkTilX0uhqrJtUSOC4smdH1M8V-h14J7YtRG4PPxOQ@mail.gmail.com>
2010-05-17 19:01     ` Mike Snitzer
2010-05-17 21:42     ` Rafael J. Wysocki
     [not found]     ` <201005172342.52660.rjw@sisk.pl>
2010-05-17 22:16       ` Kevin Hilman
2010-05-18  0:52       ` Arve Hjønnevåg
2010-05-18 16:18         ` Kevin Hilman
2010-05-18 16:18         ` Kevin Hilman
     [not found]         ` <87tyq5qj3v.fsf@deeprootsystems.com>
2010-05-18 18:52           ` Rafael J. Wysocki
     [not found]           ` <201005182052.28778.rjw@sisk.pl>
2010-05-18 22:04             ` Kevin Hilman
     [not found]             ` <87y6fgn9y1.fsf@deeprootsystems.com>
2010-05-18 22:29               ` Rafael J. Wysocki
2010-05-19  0:00           ` Arve Hjønnevåg
2010-05-18 19:13         ` Rafael J. Wysocki
     [not found]         ` <201005182113.10448.rjw@sisk.pl>
2010-05-18 20:47           ` Arve Hjønnevåg
     [not found]           ` <AANLkTinPC94F1wj-CEb3vvSo7__u625DsZxErlmTymEw@mail.gmail.com>
2010-05-18 21:48             ` Rafael J. Wysocki
     [not found]             ` <201005182348.16026.rjw@sisk.pl>
2010-05-18 22:03               ` Arve Hjønnevåg
     [not found]               ` <AANLkTin-A5eXvxDEo-vi2x2jjXtZbAl2526w62noNMIK@mail.gmail.com>
2010-05-18 22:34                 ` Rafael J. Wysocki
     [not found]                 ` <201005190034.26085.rjw@sisk.pl>
2010-05-18 22:52                   ` Arve Hjønnevåg
     [not found]                   ` <AANLkTime0yZ_t32m8WjqJEtSeNlBJYZy15X8HwKzMqOl@mail.gmail.com>
2010-05-18 23:19                     ` Rafael J. Wysocki
     [not found]                     ` <201005190119.07710.rjw@sisk.pl>
2010-05-18 23:42                       ` Arve Hjønnevåg
     [not found]                       ` <AANLkTimFbvbDZdAFFTk0VCYhDtDk9B_LcEO60ZP70vjS@mail.gmail.com>
2010-05-19 20:39                         ` Rafael J. Wysocki
     [not found]                         ` <201005192239.25892.rjw@sisk.pl>
2010-05-19 21:34                           ` Arve Hjønnevåg
     [not found]                           ` <AANLkTimoU9BrzbAl7q-LZzGeRolO7gUKBoGjkPWtWn1G@mail.gmail.com>
2010-05-20 22:21                             ` Rafael J. Wysocki
     [not found] <Pine.LNX.4.44L0.1005261815480.1328-100000@iolanthe.rowland.org>
2010-05-26 22:34 ` [PATCH 2/8] PM: suspend_block: Add driver to access suspend blockers from user-space Rafael J. Wysocki
     [not found] <201005262357.28933.rjw@sisk.pl>
2010-05-26 22:18 ` Alan Stern
2010-05-26 22:18 ` Alan Stern
     [not found] <1274482015-30899-1-git-send-email-arve@android.com>
     [not found] ` <1274482015-30899-2-git-send-email-arve@android.com>
2010-05-21 22:46   ` Arve Hjønnevåg
     [not found]   ` <1274482015-30899-3-git-send-email-arve@android.com>
2010-05-26  8:43     ` Peter Zijlstra
     [not found]     ` <1274863428.5882.4860.camel@twins>
2010-05-26 10:47       ` Arve Hjønnevåg
     [not found]       ` <AANLkTimTnzK1ByG2bE2gGjINxD_Pbvez7PFHo7Kzo9yk@mail.gmail.com>
2010-05-26 10:50         ` Peter Zijlstra
2010-05-26 10:51         ` Florian Mickler
     [not found]         ` <20100526125136.53f2dc82@schatten.dmk.lab>
2010-05-26 11:06           ` Peter Zijlstra
     [not found]         ` <1274871005.5882.5315.camel@twins>
2010-05-26 23:13           ` Arve Hjønnevåg
2010-05-26 21:57       ` Rafael J. Wysocki
     [not found] <Pine.LNX.4.44L0.1005021755320.30701-100000@netrider.rowland.org>
2010-05-03 15:03 ` Rafael J. Wysocki
     [not found] ` <201005031703.11448.rjw@sisk.pl>
2010-05-03 21:26   ` Arve Hjønnevåg
     [not found]   ` <m2yd6200be21005031426n87ea43a0k968fd5898ce0b873@mail.gmail.com>
2010-05-03 21:49     ` Rafael J. Wysocki
     [not found]     ` <201005032349.00876.rjw@sisk.pl>
2010-05-03 22:01       ` Arve Hjønnevåg
     [not found]       ` <x2id6200be21005031501p7f151324wb8bbf928bc2cb25e@mail.gmail.com>
2010-05-04 20:02         ` Rafael J. Wysocki
2010-05-04  4:16   ` Pavel Machek
     [not found] <1272667021-21312-1-git-send-email-arve@android.com>
     [not found] ` <1272667021-21312-2-git-send-email-arve@android.com>
2010-04-30 22:36   ` Arve Hjønnevåg
2010-05-02  7:04     ` Pavel Machek
2010-05-02 21:23     ` Rafael J. Wysocki
2010-05-02 21:56       ` Alan Stern
     [not found] <1272429119-12103-1-git-send-email-arve@android.com>
     [not found] ` <1272429119-12103-2-git-send-email-arve@android.com>
2010-04-28  4:31   ` Arve Hjønnevåg
2010-04-28 20:58     ` Rafael J. Wysocki
     [not found]     ` <201004282258.51354.rjw@sisk.pl>
2010-04-28 22:31       ` Arve Hjønnevåg
     [not found]       ` <k2gd6200be21004281531y270549d7g8383f2c8a55038d4@mail.gmail.com>
2010-04-28 23:05         ` Rafael J. Wysocki
     [not found]         ` <201004290105.15707.rjw@sisk.pl>
2010-04-28 23:38           ` Arve Hjønnevåg
     [not found]           ` <h2jd6200be21004281638y6b751abesfcf6416707e6fee4@mail.gmail.com>
2010-04-29 21:11             ` Rafael J. Wysocki
2010-04-29 23:41               ` Arve Hjønnevåg
2009-04-15  1:41 [RFC][PATCH 0/8] Suspend block api Arve Hjønnevåg
2009-04-15  1:41 ` [PATCH 1/8] PM: Add suspend " Arve Hjønnevåg
2009-04-15  1:41   ` [PATCH 2/8] PM: suspend_block: Add driver to access suspend blockers from user-space Arve Hjønnevåg
2009-04-29 22:52     ` Rafael J. Wysocki

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=1273810273-3039-3-git-send-email-arve@android.com \
    --to=arve@android.com \
    --cc=akpm@linux-foundation.org \
    --cc=avi@redhat.com \
    --cc=damm@igel.co.jp \
    --cc=gregkh@suse.de \
    --cc=jim.collar@eqware.net \
    --cc=konishi.ryusuke@lab.ntt.co.jp \
    --cc=len.brown@intel.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@lists.linux-foundation.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