mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] hpfall: reduce risk that hpfall can do harm
@ 2009-07-25 18:55 Frans Pop
  2009-07-25 19:00 ` [PATCH 2/2] hpfall: accept disk device to unload as argument Frans Pop
  2009-08-08  1:01 ` [PATCH 1/2] hpfall: reduce risk that hpfall can do harm Jiri Kosina
  0 siblings, 2 replies; 5+ messages in thread
From: Frans Pop @ 2009-07-25 18:55 UTC (permalink / raw)
  To: linux-kernel; +Cc: Jiri Kosina, Christian Thaeter, Pavel Machek

From: Christian Thaeter <ct@pipapo.org>

Improve the example code to be at least useable, as in not causing
harm (as shown below). Code can still be improved further, but this
adds some basic safeguards.

1. hpfall *MUST* mlockall(MCL_CURRENT|MCL_FUTURE); itself!
Since the Program sits and waits most of the time it becomes very likely
swapped out. If it gets woken up when the laptop drops from the table
while it is swapped out it actually triggers harddrive activity!

2. Daemonize hpfall using 'daemon(0,0)' (quick and dirty).

3. Give hpfall realtime priority.
Should give a chance that it has less latency when woken up.

Signed-off-by: Christian Thaeter <ct@pipapo.org>
Signed-off-by: Frans Pop <elendil@planet.nl>
Acked-by: Pavel Machek <pavel@ucw.cz>
---

This patch was posted some time back by Christian and acked by Pavel, but
was never picked up: http://lkml.org/lkml/2009/3/25/505.
All I've done is cleaned up whitespace and the commit message.

The patch is on top of my patch to clean up checkpatch errors which is
already in the trivial tree for .32. As this is only example code, I guess
this could go through trivial too.


diff --git a/Documentation/hwmon/hpfall.c b/Documentation/hwmon/hpfall.c
index d2f6711..a3cfe1a 100644
--- a/Documentation/hwmon/hpfall.c
+++ b/Documentation/hwmon/hpfall.c
@@ -16,6 +16,8 @@
 #include <stdint.h>
 #include <errno.h>
 #include <signal.h>
+#include <sys/mman.h>
+#include <sched.h>
 
 void write_int(char *path, int i)
 {
@@ -62,6 +64,7 @@ void ignore_me(void)
 int main(int argc, char *argv[])
 {
 	int fd, ret;
+	struct sched_param param;
 
 	fd = open("/dev/freefall", O_RDONLY);
 	if (fd < 0) {
@@ -69,6 +72,11 @@ int main(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
+	daemon(0, 0);
+	param.sched_priority = sched_get_priority_max(SCHED_FIFO);
+	sched_setscheduler(0, SCHED_FIFO, &param);
+	mlockall(MCL_CURRENT|MCL_FUTURE);
+
 	signal(SIGALRM, ignore_me);
 
 	for (;;) {

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/2] hpfall: accept disk device to unload as argument
  2009-07-25 18:55 [PATCH 1/2] hpfall: reduce risk that hpfall can do harm Frans Pop
@ 2009-07-25 19:00 ` Frans Pop
  2009-07-25 20:48   ` Pavel Machek
  2009-08-08  1:01 ` [PATCH 1/2] hpfall: reduce risk that hpfall can do harm Jiri Kosina
  1 sibling, 1 reply; 5+ messages in thread
From: Frans Pop @ 2009-07-25 19:00 UTC (permalink / raw)
  To: linux-kernel; +Cc: Jiri Kosina, Christian Thaeter, Pavel Machek

Allows users who use an IDE driver for their disk to use hpfall without
having to modify the source. By default /dev/sda is used.
Suggested by Christian Thaeter in http://lkml.org/lkml/2009/3/25/505.

While we're add it, improve error message if opening /dev/freefall fails.

Signed-off-by: Frans Pop <elendil@planet.nl>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Christian Thaeter <ct@pipapo.org>

diff --git a/Documentation/hwmon/hpfall.c b/Documentation/hwmon/hpfall.c
index a3cfe1a..681ec22 100644
--- a/Documentation/hwmon/hpfall.c
+++ b/Documentation/hwmon/hpfall.c
@@ -19,6 +19,32 @@
 #include <sys/mman.h>
 #include <sched.h>
 
+char unload_heads_path[64];
+
+int set_unload_heads_path(char *device)
+{
+	char devname[64];
+
+	if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
+		return -EINVAL;
+	strncpy(devname, device + 5, sizeof(devname));
+
+	snprintf(unload_heads_path, sizeof(unload_heads_path),
+				"/sys/block/%s/device/unload_heads", devname);
+	return 0;
+}
+int valid_disk(void)
+{
+	int fd = open(unload_heads_path, O_RDONLY);
+	if (fd < 0) {
+		perror(unload_heads_path);
+		return 0;
+	}
+
+	close(fd);
+	return 1;
+}
+
 void write_int(char *path, int i)
 {
 	char buf[1024];
@@ -42,7 +68,7 @@ void set_led(int on)
 
 void protect(int seconds)
 {
-	write_int("/sys/block/sda/device/unload_heads", seconds*1000);
+	write_int(unload_heads_path, seconds*1000);
 }
 
 int on_ac(void)
@@ -61,14 +87,27 @@ void ignore_me(void)
 	set_led(0);
 }
 
-int main(int argc, char *argv[])
+int main(int argc, char **argv)
 {
 	int fd, ret;
 	struct sched_param param;
 
+	if (argc == 1)
+		ret = set_unload_heads_path("/dev/sda");
+	else if (argc == 2)
+		ret = set_unload_heads_path(argv[1]);
+	else
+		ret = -EINVAL;
+
+	if (ret || !valid_disk()) {
+		fprintf(stderr, "usage: %s <device> (default: /dev/sda)\n",
+				argv[0]);
+		exit(1);
+	}
+
 	fd = open("/dev/freefall", O_RDONLY);
 	if (fd < 0) {
-		perror("open");
+		perror("/dev/freefall");
 		return EXIT_FAILURE;
 	}
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] hpfall: accept disk device to unload as argument
  2009-07-25 19:00 ` [PATCH 2/2] hpfall: accept disk device to unload as argument Frans Pop
@ 2009-07-25 20:48   ` Pavel Machek
  2009-08-08  1:03     ` Jiri Kosina
  0 siblings, 1 reply; 5+ messages in thread
From: Pavel Machek @ 2009-07-25 20:48 UTC (permalink / raw)
  To: Frans Pop; +Cc: linux-kernel, Jiri Kosina, Christian Thaeter

On Sat 2009-07-25 21:00:12, Frans Pop wrote:
> Allows users who use an IDE driver for their disk to use hpfall without
> having to modify the source. By default /dev/sda is used.
> Suggested by Christian Thaeter in http://lkml.org/lkml/2009/3/25/505.
> 
> While we're add it, improve error message if opening /dev/freefall fails.
> 
> Signed-off-by: Frans Pop <elendil@planet.nl>
> Cc: Pavel Machek <pavel@ucw.cz>

ACK.
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] hpfall: reduce risk that hpfall can do harm
  2009-07-25 18:55 [PATCH 1/2] hpfall: reduce risk that hpfall can do harm Frans Pop
  2009-07-25 19:00 ` [PATCH 2/2] hpfall: accept disk device to unload as argument Frans Pop
@ 2009-08-08  1:01 ` Jiri Kosina
  1 sibling, 0 replies; 5+ messages in thread
From: Jiri Kosina @ 2009-08-08  1:01 UTC (permalink / raw)
  To: Frans Pop; +Cc: linux-kernel, Christian Thaeter, Pavel Machek

On Sat, 25 Jul 2009, Frans Pop wrote:

> This patch was posted some time back by Christian and acked by Pavel, but
> was never picked up: http://lkml.org/lkml/2009/3/25/505.
> All I've done is cleaned up whitespace and the commit message.
> 
> The patch is on top of my patch to clean up checkpatch errors which is
> already in the trivial tree for .32. As this is only example code, I guess
> this could go through trivial too.

Applied to trivial queue as a documentation update, thanks Frans!

-- 
Jiri Kosina
SUSE Labs

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] hpfall: accept disk device to unload as argument
  2009-07-25 20:48   ` Pavel Machek
@ 2009-08-08  1:03     ` Jiri Kosina
  0 siblings, 0 replies; 5+ messages in thread
From: Jiri Kosina @ 2009-08-08  1:03 UTC (permalink / raw)
  To: Pavel Machek; +Cc: Frans Pop, linux-kernel, Christian Thaeter

On Sat, 25 Jul 2009, Pavel Machek wrote:

> On Sat 2009-07-25 21:00:12, Frans Pop wrote:
> > Allows users who use an IDE driver for their disk to use hpfall without
> > having to modify the source. By default /dev/sda is used.
> > Suggested by Christian Thaeter in http://lkml.org/lkml/2009/3/25/505.
> > 
> > While we're add it, improve error message if opening /dev/freefall fails.
> > 
> > Signed-off-by: Frans Pop <elendil@planet.nl>
> > Cc: Pavel Machek <pavel@ucw.cz>
> 
> ACK.

Queued, thanks.

-- 
Jiri Kosina
SUSE Labs

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-08-08  1:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-25 18:55 [PATCH 1/2] hpfall: reduce risk that hpfall can do harm Frans Pop
2009-07-25 19:00 ` [PATCH 2/2] hpfall: accept disk device to unload as argument Frans Pop
2009-07-25 20:48   ` Pavel Machek
2009-08-08  1:03     ` Jiri Kosina
2009-08-08  1:01 ` [PATCH 1/2] hpfall: reduce risk that hpfall can do harm Jiri Kosina

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®