Sophie

Sophie

distrib > Mageia > 7 > aarch64 > by-pkgid > 265a7483afc48e27c236b36e810be507 > files > 87

lkmpg-1.1.0-23.mga7.noarch.rpm

<HTML
><HEAD
><TITLE
>Scheduling Tasks</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="The Linux Kernel Module Programming Guide"
HREF="book1.htm"><LINK
REL="PREVIOUS"
TITLE="Flashing keyboard LEDs"
HREF="x1181.htm"><LINK
REL="NEXT"
TITLE="Interrupt Handlers"
HREF="c1256.htm"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>The Linux Kernel Module Programming Guide</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x1181.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c1256.htm"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="AEN1196"
></A
>Chapter 11. Scheduling Tasks</H1
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN1198"
></A
>Scheduling Tasks</H1
><A
NAME="AEN1200"
></A
><A
NAME="AEN1202"
></A
><A
NAME="AEN1205"
></A
><A
NAME="AEN1207"
></A
><P
>Very often, we have <SPAN
CLASS="QUOTE"
>"housekeeping"</SPAN
> tasks which have to be done at a certain time, or every so often. If the
	task is to be done by a process, we do it by putting it in the <TT
CLASS="FILENAME"
>crontab</TT
> file.  If the task is to be done
	by a kernel module, we have two possibilities.  The first is to put a process in the <TT
CLASS="FILENAME"
>crontab</TT
> file which
	will wake up the module by a system call when necessary, for example by opening a file. This is terribly inefficient, however
	-- we run a new process off of <TT
CLASS="FILENAME"
>crontab</TT
>, read a new executable to memory, and all this just to wake up a
	kernel module which is in memory anyway.</P
><A
NAME="AEN1214"
></A
><A
NAME="AEN1216"
></A
><A
NAME="AEN1218"
></A
><A
NAME="AEN1220"
></A
><P
>Instead of doing that, we can create a function that will be called once for every timer interrupt.  The way we do this
	is we create a task, held in a <SPAN
CLASS="STRUCTNAME"
>tq_struct</SPAN
> structure, which will hold a pointer to the function.  Then,
	we use <TT
CLASS="FUNCTION"
>queue_task</TT
> to put that task on a task list called <SPAN
CLASS="STRUCTNAME"
>tq_timer</SPAN
>, which is the
	list of tasks to be executed on the next timer interrupt.  Because we want the function to keep on being executed, we need to
	put it back on <SPAN
CLASS="STRUCTNAME"
>tq_timer</SPAN
> whenever it is called, for the next timer interrupt.</P
><A
NAME="AEN1227"
></A
><A
NAME="AEN1229"
></A
><A
NAME="AEN1231"
></A
><P
>There's one more point we need to remember here.  When a module is removed by <B
CLASS="COMMAND"
>rmmod</B
>, first its
	reference count is checked.  If it is zero, <TT
CLASS="FUNCTION"
>module_cleanup</TT
> is called.  Then, the module is removed from
	memory with all its functions.  Nobody checks to see if the timer's task list happens to contain a pointer to one of those
	functions, which will no longer be available.  Ages later (from the computer's perspective, from a human perspective it's
	nothing, less than a hundredth of a second), the kernel has a timer interrupt and tries to call the function on the task list.
	Unfortunately, the function is no longer there.  In most cases, the memory page where it sat is unused, and you get an ugly
	error message.  But if some other code is now sitting at the same memory location, things could get <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>very</I
></SPAN
>
	ugly.  Unfortunately, we don't have an easy way to unregister a task from a task list.</P
><A
NAME="AEN1237"
></A
><A
NAME="AEN1239"
></A
><P
>Since <TT
CLASS="FUNCTION"
>cleanup_module</TT
> can't return with an error code (it's a void function), the solution is to not
	let it return at all.  Instead, it calls <TT
CLASS="FUNCTION"
>sleep_on</TT
> or
	<TT
CLASS="FUNCTION"
>module_sleep_on</TT
><A
NAME="AEN1245"
HREF="#FTN.AEN1245"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
> to put the
	<B
CLASS="COMMAND"
>rmmod</B
> process to sleep.  Before that, it informs the function called on the timer interrupt to stop
	attaching itself by setting a global variable.  Then, on the next timer interrupt, the <B
CLASS="COMMAND"
>rmmod</B
> process will
	be woken up, when our function is no longer in the queue and it's safe to remove the module.</P
><A
NAME="AEN1249"
></A
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN1252"
></A
><P
><B
>Example 11-1. sched.c</B
></P
><PRE
CLASS="PROGRAMLISTING"
>/*
 *  sched.c - scheduale a function to be called on every timer interrupt.
 *
 *  Copyright (C) 2001 by Peter Jay Salzman
 */

/* 
 * The necessary header files 
 */

/* 
 * Standard in kernel modules 
 */
#include &#60;linux/kernel.h&#62;	/* We're doing kernel work */
#include &#60;linux/module.h&#62;	/* Specifically, a module */
#include &#60;linux/proc_fs.h&#62;	/* Necessary because we use the proc fs */
#include &#60;linux/workqueue.h&#62;	/* We scheduale tasks here */
#include &#60;linux/sched.h&#62;	/* We need to put ourselves to sleep 
				   and wake up later */
#include &#60;linux/init.h&#62;		/* For __init and __exit */
#include &#60;linux/interrupt.h&#62;	/* For irqreturn_t */

struct proc_dir_entry *Our_Proc_File;
#define PROC_ENTRY_FILENAME "sched"
#define MY_WORK_QUEUE_NAME "WQsched.c"

/* 
 * The number of times the timer interrupt has been called so far 
 */
static int TimerIntrpt = 0;

static void intrpt_routine(void *);

static int die = 0;		/* set this to 1 for shutdown */

/* 
 * The work queue structure for this task, from workqueue.h 
 */
static struct workqueue_struct *my_workqueue;

static struct work_struct Task;
static DECLARE_WORK(Task, intrpt_routine, NULL);

/* 
 * This function will be called on every timer interrupt. Notice the void*
 * pointer - task functions can be used for more than one purpose, each time
 * getting a different parameter.
 */
static void intrpt_routine(void *irrelevant)
{
	/* 
	 * Increment the counter 
	 */
	TimerIntrpt++;

	/* 
	 * If cleanup wants us to die
	 */
	if (die == 0)
		queue_delayed_work(my_workqueue, &#38;Task, 100);
}

/* 
 * Put data into the proc fs file. 
 */
ssize_t
procfile_read(char *buffer,
	      char **buffer_location,
	      off_t offset, int buffer_length, int *eof, void *data)
{
	int len;		/* The number of bytes actually used */

	/* 
	 * It's static so it will still be in memory 
	 * when we leave this function
	 */
	static char my_buffer[80];

	static int count = 1;

	/* 
	 * We give all of our information in one go, so if the anybody asks us
	 * if we have more information the answer should always be no.
	 */
	if (offset &#62; 0)
		return 0;

	/* 
	 * Fill the buffer and get its length 
	 */
	len = sprintf(my_buffer, "Timer called %d times so far\n", TimerIntrpt);
	count++;

	/* 
	 * Tell the function which called us where the buffer is 
	 */
	*buffer_location = my_buffer;

	/* 
	 * Return the length 
	 */
	return len;
}

/* 
 * Initialize the module - register the proc file 
 */
int __init init_module()
{
	int rv = 0;
	/* 
	 * Put the task in the work_timer task queue, so it will be executed at
	 * next timer interrupt
	 */
	my_workqueue = create_workqueue(MY_WORK_QUEUE_NAME);
	queue_delayed_work(my_workqueue, &#38;Task, 100);

	Our_Proc_File = create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);
	Our_Proc_File-&#62;read_proc = procfile_read;
	Our_Proc_File-&#62;owner = THIS_MODULE;
	Our_Proc_File-&#62;mode = S_IFREG | S_IRUGO;
	Our_Proc_File-&#62;uid = 0;
	Our_Proc_File-&#62;gid = 0;
	Our_Proc_File-&#62;size = 80;

	if (Our_Proc_File == NULL) {
		rv = -ENOMEM;
		remove_proc_entry(PROC_ENTRY_FILENAME, &#38;proc_root);
		printk(KERN_INFO "Error: Could not initialize /proc/%s\n",
		       PROC_ENTRY_FILENAME);
	}

	return rv;
}

/* 
 * Cleanup
 */
void __exit cleanup_module()
{
	/* 
	 * Unregister our /proc file 
	 */
	remove_proc_entry(PROC_ENTRY_FILENAME, &#38;proc_root);
	printk(KERN_INFO "/proc/%s removed\n", PROC_ENTRY_FILENAME);

	die = 1;		/* keep intrp_routine from queueing itself */
	cancel_delayed_work(&#38;Task);	/* no "new ones" */
	flush_workqueue(my_workqueue);	/* wait till all "old ones" finished */
	destroy_workqueue(my_workqueue);

	/* 
	 * Sleep until intrpt_routine is called one last time. This is 
	 * necessary, because otherwise we'll deallocate the memory holding 
	 * intrpt_routine and Task while work_timer still references them.
	 * Notice that here we don't allow signals to interrupt us.
	 *
	 * Since WaitQ is now not NULL, this automatically tells the interrupt
	 * routine it's time to die.
	 */

}

/* 
 * some work_queue related functions
 * are just available to GPL licensed Modules 
 */
MODULE_LICENSE("GPL");</PRE
></DIV
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN1245"
HREF="c1196.htm#AEN1245"
><SPAN
CLASS="footnote"
>[1]</SPAN
></A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>They're really the same.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x1181.htm"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.htm"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c1256.htm"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Flashing keyboard LEDs</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Interrupt Handlers</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>