hpfall: reduce risk that hpfall can do harm
Subsystems (2)
PATCH 1/2 hpfall: reduce risk that hpfall can do harm
From: Christian Thaeter <[email protected]>
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.
---
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.
Documentation/hwmon/hpfall.c +8 -0
@@ -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 , ¶m); +mlockall (MCL_CURRENT |MCL_FUTURE ); +signal (SIGALRM , ignore_me);for (;;) {
PATCH 2/2 hpfall: accept disk device to unload as argument
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.
Documentation/hwmon/hpfall.c +42 -3
@@ -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 ; }
Re: [PATCH 2/2] hpfall: accept disk device to unload as argument
Quoted 8 lines · Frans Pop
Allows users who use an IDE driver for their disk to use hpfall without having to modify the source. By default
/dev/sdais 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/freefallfails.Signed-off-by Frans Pop <[email protected]>Cc Pavel Machek <[email protected]>
ACK.
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Jiri Kosina
AppliedRe: [PATCH 2/2] hpfall: accept disk device to unload as argument
Quoted 11 lines · Pavel Machek
Quoted 8 lines · Frans Pop
Allows users who use an IDE driver for their disk to use hpfall without having to modify the source. By default
/dev/sdais 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/freefallfails.Signed-off-by Frans Pop <[email protected]>Cc Pavel Machek <[email protected]>ACK.
Queued, thanks.
Signature · 2 lines
Jiri Kosina SUSE Labs
Jiri Kosina
AppliedQuoted 7 lines · Frans Pop
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!
Signature · 2 lines
Jiri Kosina SUSE Labs