summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2013-02-01 02:48:49 -0600
committerKyle Kaminski <kyle@kkaminsk.com>2013-02-01 02:48:49 -0600
commit2a64bd55f015dde0b32c752377d5820d9d280873 (patch)
tree31d3ca4c80d1ad63ab0b1985577bcd6231dcf2c8
parent20c1a6d14c9447b87394221fa42a0e85aca1b060 (diff)
downloadkernelhello-2a64bd55f015dde0b32c752377d5820d9d280873.tar.gz
kernelhello-2a64bd55f015dde0b32c752377d5820d9d280873.tar.bz2
kernelhello-2a64bd55f015dde0b32c752377d5820d9d280873.zip
fix improper ptr check, fix offset calc, ratelimit printk messages
-rw-r--r--main.c20
-rw-r--r--main.h6
2 files changed, 17 insertions, 9 deletions
diff --git a/main.c b/main.c
index 7036144..579a621 100644
--- a/main.c
+++ b/main.c
@@ -146,7 +146,7 @@ static int hello_trim(struct hello_dev *dev)
static struct hello_ll *hello_follow(struct hello_dev *dev, int n)
{
struct hello_ll *nptr = dev->mylist, *tmp;
- PDEBUG("follow() called with n: %i\n", n);
+ PRDEBUG("follow() called with n: %i\n", n);
/* allocate first node explicitly if it needs to be */
if (!nptr) {
@@ -162,8 +162,12 @@ static struct hello_ll *hello_follow(struct hello_dev *dev, int n)
/* follow the list */
while (n--) {
/* almost like testing for NULL, remember this is circular list */
- if (nptr->list.next == &nptr->list) {
- PDEBUG("allocating additional node");
+ PRDEBUG("while loop: n is: %i, nptr->list is at: %p, ntpr->list.next is at %p\n",
+ n, /* &nptr->list */ &dev->mylist->list, nptr->list.next);
+
+ /* there was a bug below, took 2 hours to track down */
+ if (nptr->list.next == /* &nptr->list */ &dev->mylist->list) {
+ PRDEBUG("allocating additional node");
tmp = kmalloc(sizeof(struct hello_ll), GFP_KERNEL);
if (!tmp)
return NULL;
@@ -187,7 +191,7 @@ static ssize_t hello_read(struct file *filp, char __user *buff, size_t count, lo
ssize_t ret = 0; /* end of file, also in case of out of bound */
struct hello_ll *cptr = NULL; /* node where right chunk is */
int chunk_pos = 0;
- PDEBUG("read() called\n");
+ PRDEBUG("read() called\n");
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
@@ -220,7 +224,7 @@ static ssize_t hello_read(struct file *filp, char __user *buff, size_t count, lo
count = dev->chunk_sz - chunk_pos;
/* write to userspace memory by reading only up to the end of chunk */
- if (copy_to_user(buff, cptr->chunk + (*f_pos % dev->chunk_sz), count))
+ if (copy_to_user(buff, cptr->chunk + chunk_pos, count))
{
ret = -EFAULT;
goto out;
@@ -240,12 +244,12 @@ static ssize_t hello_write(struct file *filp, const char __user *buff, size_t co
ssize_t ret = -ENOMEM;
struct hello_ll *cptr = NULL; /* node where right chunk is */
int chunk_pos = 0;
- PDEBUG("write() called\n");
+ PRDEBUG("write() called, user buffer is at: %p, f_pos: %lli, c: %lu\n", buff, *f_pos, count);
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
- chunk_pos = *f_pos / dev->chunk_sz;
+ chunk_pos = *f_pos % dev->chunk_sz;
cptr = hello_follow(dev, *f_pos / dev->chunk_sz);
if (!cptr)
@@ -342,7 +346,7 @@ static int __init hello_init(void)
ret = alloc_chrdev_region(&devnum, 0 /* first minor */, 1, "helloc");
hello_major = MAJOR(devnum);
hello_minor = MINOR(devnum);
- printk(KERN_INFO "[hello] major: %d, minor: %d\n", hello_major, hello_minor);
+ PDEBUG("[hello] major: %d, minor: %d\n", hello_major, hello_minor);
}
if (ret < 0) {
printk(KERN_WARNING "[hello] can't get the major %d number\n", hello_major);
diff --git a/main.h b/main.h
index cb751d0..e2727db 100644
--- a/main.h
+++ b/main.h
@@ -9,8 +9,12 @@
*/
#ifdef HELLO_DEBUG
#define PDEBUG(fmt, args...) printk( KERN_DEBUG "[hello] " fmt, ##args)
+ #define PRDEBUG(fmt, args...) if (printk_ratelimit()) \
+ printk( KERN_DEBUG "[hello] " fmt, ##args)
#else
- #define PDEBUG(fmt, args...) /* not debugging: nothing */
+ /* not debugging: nothing */
+ #define PDEBUG(fmt, args...)
+ #define PRDEBUG(fmt, args...)
#endif
#ifndef HELLO_MAJOR