[IMPROVE] New sleep style and some redesign
authorAlexander Aksenov <a.aksenov@samsung.com>
Wed, 10 Jul 2013 16:44:49 +0000 (20:44 +0400)
committerAlexander Aksenov <a.aksenov@samsung.com>
Wed, 10 Jul 2013 16:44:49 +0000 (20:44 +0400)
driver_new/Kbuild
driver_new/device_driver.c
driver_new/device_driver.h
driver_new/driver_to_buffer.c
driver_new/driver_to_msg.h [new file with mode: 0644]
driver_new/swap_driver_module.c [new file with mode: 0644]
driver_new/swap_ioctl.h
parser/swap_msg_parser.c

index 2a8e7ae..b276966 100644 (file)
@@ -1,5 +1,6 @@
 EXTRA_CFLAGS := $(extra_cflags)
 
 obj-m := swap_driver_new.o
-swap_driver_new-y := device_driver.o \
+swap_driver_new-y := swap_driver_module.o \
+                     device_driver.o \
                      driver_to_buffer.o
index 3d7b1dd..5a33474 100644 (file)
@@ -2,13 +2,14 @@
 #include <linux/fs.h>
 #include <linux/cdev.h>
 #include <linux/err.h>
-#include <linux/module.h>
 #include <linux/device.h>
 #include <linux/ioctl.h>
 #include <linux/slab.h>
 #include <linux/mm.h>
 #include <linux/splice.h>
 #include <linux/sched.h>
+#include <linux/module.h>
+#include <linux/wait.h>
 #include <asm/uaccess.h>
 
 #include "device_driver.h"
@@ -18,7 +19,7 @@
 #include "driver_defs.h"
 #include "device_driver_to_driver_to_buffer.h"
 
-#include <ksyms/ksyms.h>
+#include "../ksyms/ksyms.h"
 
 #define SWAP_DEVICE_NAME "swap_device"
 
@@ -193,51 +194,49 @@ static ssize_t swap_device_read(struct file *filp, char __user *buf,
 {
     /* Wait queue item that consists current task. It is used to be added in
      * swap_device_wait queue if there is no data to be read. */
-    DECLARE_WAITQUEUE(wait, current);
+    DEFINE_WAIT(wait);
     int result;
 
-    /* Add process to the swap_device_wait queue and set the current task state
-     * TASK_INTERRUPTIBLE. If there is any data to be read, then the current 
-     * task is removed from the swap_device_wait queue and its state is changed
-     * to this. */
-    add_wait_queue(&swap_device_wait, &wait);
-    __set_current_state(TASK_INTERRUPTIBLE);
-
     //TODO : Think about spin_locks to prevent reading race condition.
-    do {
-        result = driver_to_buffer_next_buffer_to_read();
+    while((result = driver_to_buffer_next_buffer_to_read()) != E_SD_SUCCESS) {
+
+        /* Add process to the swap_device_wait queue and set the current task
+         * state TASK_INTERRUPTIBLE. If there is any data to be read, then the
+         * current task is removed from the swap_device_wait queue and its state
+         * is changed to this. */
+        prepare_to_wait(&swap_device_wait, &wait, TASK_INTERRUPTIBLE);
+
         if (result < 0) {
             result = 0;
-            goto swap_device_read_out;
-        } else if (result == E_SD_SUCCESS) {
-            break;
+            goto swap_device_read_error;
         } else if (result == E_SD_NO_DATA_TO_READ) {
             /* Yes, E_SD_NO_DATA_TO_READ should be positive, cause it's not
              * really an error */
             if (filp->f_flags & O_NONBLOCK) {
                 result = -EAGAIN;
-                goto swap_device_read_out;
+                goto swap_device_read_error;
             }
             if (signal_pending(current)) {
                 result = -ERESTARTSYS;
-                goto swap_device_read_out;
+                goto swap_device_read_error;
             }
-            // TODO Check for sleep conditions
             schedule();
+            finish_wait(&swap_device_wait, &wait);
         }
-    } while (1);
+    }
 
     result = driver_to_buffer_read(buf, count);
     /* If there is an error - return 0 */
     if (result < 0)
         result = 0;
 
-swap_device_read_out:
-    __set_current_state(TASK_RUNNING);
-    remove_wait_queue(&swap_device_wait, &wait);
 
     return result;
 
+swap_device_read_error:
+    finish_wait(&swap_device_wait, &wait);
+
+    return result;
 }
 
 static ssize_t swap_device_write(struct file *filp, const char __user *buf,
@@ -322,14 +321,13 @@ static long swap_device_ioctl(struct file *filp, unsigned int cmd,
         }
         default:
         {
-            print_debug("SWAP_DRIVER_BUFFER MESSAGE\n");
-            if (msg_handler) {
-                result = msg_handler((void __user *)arg);
-            } else {
-//                print_warn("Unknown command %d\n", cmd);
+//            if (msg_handler) {
+//                result = msg_handler((void __user *)arg);
+//            } else {
+                print_warn("Unknown command %d\n", cmd);
                 result = -EINVAL;
-            }
-            break;
+//            }
+//            break;
         }
     }
     return result;
@@ -363,7 +361,8 @@ static ssize_t swap_device_splice_read(struct file *filp, loff_t *ppos,
 {
     /* Wait queue item that consists current task. It is used to be added in
      * swap_device_wait queue if there is no data to be read. */
-    DECLARE_WAITQUEUE(wait, current);
+    DEFINE_WAIT(wait);
+
     int result;
     struct page *pages[PIPE_DEF_BUFFERS];
     struct partial_page partial[PIPE_DEF_BUFFERS];
@@ -379,36 +378,33 @@ static ssize_t swap_device_splice_read(struct file *filp, loff_t *ppos,
            .spd_release = swap_device_page_release,
     };
 
-    /* Add process to the swap_device_wait queue and set the current task state
-     * TASK_INTERRUPTIBLE. If there is any data to be read, then the current 
-     * task is removed from the swap_device_wait queue and its state is changed
-     * to this. */
-    add_wait_queue(&swap_device_wait, &wait);
-    __set_current_state(TASK_INTERRUPTIBLE);
-
     /* Get next buffer to read */
     //TODO : Think about spin_locks to prevent reading race condition.
-    do {
-        result = driver_to_buffer_next_buffer_to_read();
+    while((result = driver_to_buffer_next_buffer_to_read()) != E_SD_SUCCESS) {
+
+        /* Add process to the swap_device_wait queue and set the current task
+         * state TASK_INTERRUPTIBLE. If there is any data to be read, then the
+         * current task is removed from the swap_device_wait queue and its state
+         * is changed. */
+        prepare_to_wait(&swap_device_wait, &wait, TASK_INTERRUPTIBLE);
         if (result < 0) {
             print_err("driver_to_buffer_next_buffer_to_read error %d\n", result);
+            //TODO Error return to OS
             result = 0;
-            goto swap_device_splice_read_out;
-        } else if (result == E_SD_SUCCESS) {
-            break;
+            goto swap_device_splice_read_error;
         } else if (result == E_SD_NO_DATA_TO_READ) {
             if (filp->f_flags & O_NONBLOCK) {
                 result = -EAGAIN;
-                goto swap_device_splice_read_out;
+                goto swap_device_splice_read_error;
             }
             if (signal_pending(current)) {
                 result = -ERESTARTSYS;
-                goto swap_device_splice_read_out;
+                goto swap_device_splice_read_error;
             }
-            // TODO Check for sleep conditions
             schedule();
+            finish_wait(&swap_device_wait, &wait);
         }
-    } while (1);
+    }
 
     if (splice_grow_spd_p(pipe, &spd)) {
         result = -ENOMEM;
@@ -427,8 +423,10 @@ swap_device_shrink_spd:
     splice_shrink_spd_p(&spd);
 
 swap_device_splice_read_out:
-    __set_current_state(TASK_RUNNING);
-    remove_wait_queue(&swap_device_wait, &wait);
+    return result;
+
+swap_device_splice_read_error:
+    finish_wait(&swap_device_wait, &wait);
 
     return result;
 }
@@ -443,20 +441,3 @@ void set_msg_handler(msg_handler_t mh)
        msg_handler = mh;
 }
 EXPORT_SYMBOL_GPL(set_msg_handler);
-
-static int __init swap_driver_init(void)
-{
-       swap_device_init();
-
-       return 0;
-}
-
-static void __exit swap_driver_exit(void)
-{
-       swap_device_exit();
-}
-
-module_init(swap_driver_init);
-module_exit(swap_driver_exit);
-
-MODULE_LICENSE("GPL");
index 738f46b..d809e1b 100644 (file)
@@ -1,9 +1,10 @@
-#ifndef __SWAP_DRIVER_DEVICE_DRIVER__
-#define __SWAP_DRIVER_DEVICE_DRIVER__
+#ifndef __SWAP_DRIVER_DEVICE_DRIVER_H__
+#define __SWAP_DRIVER_DEVICE_DRIVER_H__
 
-typedef int (*msg_handler_t)(void __user *data);
+/* Create and register device */
+int swap_device_init(void);
 
-/* Set the message handler */
-void set_msg_handler(msg_handler_t mh);
+/* Delete device */
+void swap_device_exit(void);
 
-#endif /* __SWAP_DRIVER_DEVICE_DRIVER__ */
+#endif /* __SWAP_DRIVER_DEVICE_DRIVER_H__ */
index 5206841..b69c8b2 100644 (file)
@@ -84,6 +84,7 @@ ssize_t driver_to_buffer_write(size_t size, void* data)
         print_err("swap_buffer_write error %d\n", result);
         return -E_SD_WRITE_ERROR;
     }
+    print_debug("swap_buffer writed %d\n", result);
 
     return result;
 }
diff --git a/driver_new/driver_to_msg.h b/driver_new/driver_to_msg.h
new file mode 100644 (file)
index 0000000..5830f18
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef __SWAP_DRIVER_DRIVER_TO_MSG__
+#define __SWAP_DRIVER_DRIVER_TO_MSG__
+
+typedef int (*msg_handler_t)(void __user *data);
+
+/* Set the message handler */
+void set_msg_handler(msg_handler_t mh);
+
+#endif /* __SWAP_DRIVER_DRIVER_TO_MSG__ */
diff --git a/driver_new/swap_driver_module.c b/driver_new/swap_driver_module.c
new file mode 100644 (file)
index 0000000..9581128
--- /dev/null
@@ -0,0 +1,23 @@
+#include <linux/module.h>
+
+#include "driver_defs.h"
+#include "device_driver.h"
+
+static int __init swap_driver_init(void)
+{
+       swap_device_init();
+    print_msg("Driver module initialized\n");
+
+       return 0;
+}
+
+static void __exit swap_driver_exit(void)
+{
+       swap_device_exit();
+    print_msg("Driver module uninitialized\n");
+}
+
+module_init(swap_driver_init);
+module_exit(swap_driver_exit);
+
+MODULE_LICENSE("GPL");
index ef64957..fe313f2 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef _SWAP_IOCTL_H
-#define _SWAP_IOCTL_H
+#ifndef __SWAP_IOCTL_H__
+#define __SWAP_IOCTL_H__
 
 #include <linux/ioctl.h>
 
@@ -22,4 +22,4 @@ struct buffer_initialize {
 #define SWAP_DRIVER_MSG                                _IOW(SWAP_DRIVER_IOC_MAGIC, 5, \
                                                     void *)
 
-#endif /* _SWAP_IOCTL_H */
+#endif /* __SWAP_IOCTL_H__ */
index e126ea1..0f223a6 100644 (file)
@@ -6,7 +6,7 @@
 #include "msg_buf.h"
 #include "msg_cmd.h"
 
-#include <driver_new/device_driver.h>
+#include <driver_new/driver_to_msg.h>
 #include <driver_new/swap_ioctl.h>
 
 enum MSG_ID {