/dev/mem cleanups and bug fix
cover /dev/mem cleanups and bug fix
Hi Andrew,
Here are three patches for /dev/[k]mem.
They are mostly code cleanups, plus a minor bug fix:
if someone read high mem via /dev/kmem starting from an unaligned address, it's calculation of 'len' will go wrong.
I would recommend them for .32.
Thanks,
Fengguang
PATCH 2/3 devmem: introduce size_inside_page()
Introduce size_inside_page() to replace duplicate /dev/mem code.
Also apply it to /dev/kmem, whose alignment logic was buggy.
--- drivers/char/mem.c | 60 +++++++++++++------------------------------ 1 file changed, 19 insertions(+), 41 deletions(-)
drivers/char/mem.c +19 -41
@@ -35,6 +35,19 @@# include <linux/efi.h> #endif +static inline unsigned long size_inside_page (unsigned long start, +unsigned long size) +{ +unsigned long sz; + +if (-start & (PAGE_SIZE -1 )) + sz = -start & (PAGE_SIZE -1 ); +else + sz =PAGE_SIZE ; + + return min_t(unsigned long, sz, size); +} +/* * Architectures vary in how they handle caching for addresses * outside of main memory.
@@ -142,15 +155,7 @@ static ssize_t read_mem(struct file * fi#endif while (count >0 ) { -/* -* Handle first page in case it's not aligned -*/ -if (-p & (PAGE_SIZE -1 )) - sz = -p & (PAGE_SIZE -1 ); -else - sz =PAGE_SIZE ; - - sz = min_t(unsigned long, sz, count); + sz =size_inside_page (p, count);if (!range_is_allowed (p >>PAGE_SHIFT , count))return -EPERM ;
@@ -209,15 +214,7 @@ static ssize_t write_mem(struct file * f#endif while (count >0 ) { -/* -* Handle first page in case it's not aligned -*/ -if (-p & (PAGE_SIZE -1 )) - sz = -p & (PAGE_SIZE -1 ); -else - sz =PAGE_SIZE ; - - sz = min_t(unsigned long, sz, count); + sz =size_inside_page (p, count);if (!range_is_allowed (p >>PAGE_SHIFT , sz))return -EPERM ;
@@ -430,15 +427,7 @@ static ssize_t read_kmem(struct file *fi }#endif while (low_count >0 ) { -/* -* Handle first page in case it's not aligned -*/ -if (-p & (PAGE_SIZE -1 )) - sz = -p & (PAGE_SIZE -1 ); -else - sz =PAGE_SIZE ; - - sz = min_t(unsigned long, sz, low_count); + sz =size_inside_page (p, low_count);/* * On ia64 if a page has been mapped somewhere as
@@ -462,10 +451,8 @@ static ssize_t read_kmem(struct file *fiif (!kbuf)return -ENOMEM ;while (count >0 ) { -int len = count; +int len =size_inside_page (p, count); -if (len >PAGE_SIZE ) - len =PAGE_SIZE ; len =vread (kbuf, (char *)p, len);if (!len)break ;
@@ -510,15 +497,8 @@ do_write_kmem(void *p, unsigned long reawhile (count >0 ) {char *ptr; -/* -* Handle first page in case it's not aligned -*/ -if (-realp & (PAGE_SIZE -1 )) - sz = -realp & (PAGE_SIZE -1 ); -else - sz =PAGE_SIZE ; - sz = min_t(unsigned long, sz, count); + sz =size_inside_page (realp, count);/* * On ia64 if a page has been mapped somewhere as
@@ -578,10 +558,8 @@ static ssize_t write_kmem(struct file *if (!kbuf)return wrote ? wrote : -ENOMEM ;while (count >0 ) { -int len = count; +int len =size_inside_page (p, count); -if (len >PAGE_SIZE ) - len =PAGE_SIZE ; written =copy_from_user (kbuf, buf, len);if (written) {if (wrote + virtr)
PATCH 3/3 devmem: cleanup unxlate_dev_mem_ptr() calls
In reply to Wu FengguangNo behavior change.
--- drivers/char/mem.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/char/mem.c +5 -8
@@ -131,6 +131,7 @@ static ssize_t read_mem(struct file * fi size_t count, loff_t *ppos) { unsigned long p = *ppos; +unsigned long ret;ssize_t read, sz;char *ptr;
@@ -169,12 +170,10 @@ static ssize_t read_mem(struct file * fiif (!ptr)return -EFAULT ; -if (copy_to_user (buf, ptr, sz)) { -unxlate_dev_mem_ptr (p, ptr); -return -EFAULT ; - } - + ret =copy_to_user (buf, ptr, sz);unxlate_dev_mem_ptr (p, ptr); +if (ret) +return -EFAULT ; buf += sz; p += sz;
@@ -232,16 +231,14 @@ static ssize_t write_mem(struct file * f } copied =copy_from_user (ptr, buf, sz); +unxlate_dev_mem_ptr (p, ptr);if (copied) { written += sz - copied; -unxlate_dev_mem_ptr (p, ptr);if (written)break ;return -EFAULT ; } -unxlate_dev_mem_ptr (p, ptr); - buf += sz; p += sz; count -= sz;
PATCH 1/3 devmem: remove redundant test on len
In reply to Wu FengguangThe len test in write_kmem() is always true, so can be reduced.
--- drivers/char/mem.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/char/mem.c +6 -8
@@ -580,18 +580,16 @@ static ssize_t write_kmem(struct file *while (count >0 ) {int len = count;if (len >PAGE_SIZE ) len =PAGE_SIZE ; -if (len) { - written =copy_from_user (kbuf, buf, len); -if (written) { -if (wrote + virtr) -break ; -free_page ((unsigned long )kbuf); -return -EFAULT ; - } + written =copy_from_user (kbuf, buf, len); +if (written) { +if (wrote + virtr) +break ; +free_page ((unsigned long )kbuf); +return -EFAULT ; } len =vwrite (kbuf, (char *)p, len); count -= len; buf += len; virtr += len;
Quoted 9 lines · Wu Fengguang
Hi Andrew,
Here are three patches for /dev/[k]mem.
They are mostly code cleanups, plus a minor bug fix:
if someone read high mem via/dev/kmemstarting from an unaligned address, it's calculation of 'len' will go wrong.I would recommend them for .32.
I reviewed the three patches and they all look good to me.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only.
Re: [PATCH 2/3] devmem: introduce size_inside_page()
In reply to Wu FengguangQuoted 18 lines · On Fri, 11 Sep 2009 10:23:35 +0800 Wu Fengguang <[email protected]> wrote:
Introduce
size_inside_page()to replace duplicate/dev/memcode.Also apply it to
/dev/kmem, whose alignment logic was buggy.CC Marcelo Tosatti <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC Mark Brown <[email protected]>CC Johannes Berg <[email protected]>CC Avi Kivity <[email protected]>Signed-off-by Wu Fengguang <[email protected]>--- drivers/char/mem.c | 60 +++++++++++++------------------------------ 1 file changed, 19 insertions(+), 41 deletions(-)
drivers/char/mem.c line 44
Quoted 10 lines
drivers/char/mem.c +7
-0@@ -35,6 +35,19 @@# include <linux/efi.h> #endif +static inline unsigned long size_inside_page (unsigned long start, +unsigned long size) +{ +unsigned long sz; + +if (-start & (PAGE_SIZE -1 )) + sz = -start & (PAGE_SIZE -1 );
What on earth is this doing? Negating an unsigned number?
Can we get rid of these party tricks and use something more conventional here? In a separate patch I guess.
drivers/char/mem.c line 48
Quoted 4 lines
drivers/char/mem.c +4
-0+else + sz =PAGE_SIZE ; + + return min_t(unsigned long, sz, size);
Can use min() here.
drivers/char/mem.c line 49
Quoted 1 line
drivers/char/mem.c +1
-0+}
Please have a think about the types. Should we be using unsigned long, or size_t? Which makes more sense? Which maps better onto reality?
I suspect that the min_t which you inherited was added somewhere because someone didn't get the types right: int-vs-size_t or something. If we actually get the types right, this sort of thing goes away.
drivers/char/mem.c static ssize_t read_kmem(struct file *fi · line 454
Quoted 6 lines
drivers/char/mem.c +1
-1@@ -462,10 +451,8 @@ static ssize_t read_kmem(struct file *fiif (!kbuf)return -ENOMEM ;while (count >0 ) { -int len = count; +int len =size_inside_page (p, count);
int?
Re: [PATCH 1/3] devmem: remove redundant test on len
In reply to Wu FengguangQuoted 29 lines · On Fri, 11 Sep 2009 10:23:34 +0800 Wu Fengguang <[email protected]> wrote:
The len test in
write_kmem()is always true, so can be reduced.CC Marcelo Tosatti <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC Mark Brown <[email protected]>CC Johannes Berg <[email protected]>CC Avi Kivity <[email protected]>Signed-off-by Wu Fengguang <[email protected]>--- drivers/char/mem.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)drivers/char/mem.c +1
-8@@ -580,18 +580,16 @@ static ssize_t write_kmem(struct file *while (count >0 ) {int len = count;if (len >PAGE_SIZE ) len =PAGE_SIZE ; -if (len) { - written =copy_from_user (kbuf, buf, len); -if (written) { -if (wrote + virtr) -break ; -free_page ((unsigned long )kbuf); -return -EFAULT ; - } + written =copy_from_user (kbuf, buf, len);
drivers/char/mem.c static ssize_t write_kmem(struct file * · line 595
Quoted 10 lines
drivers/char/mem.c +5
-0+if (written) { +if (wrote + virtr) +break ; +free_page ((unsigned long )kbuf); +return -EFAULT ; } len =vwrite (kbuf, (char *)p, len); count -= len; buf += len; virtr += len;
humpf. But take a closer look at what remains.
Local var `written' is unneeded here.
Which makes us look at what `written' _does_ do:
if (written != wrote) return written; wrote = written;
lolwhowrotethat?
local var `written' can at least be made local to the first loop.
write_kmem() has a lot of typecasts which indicates that the choices of types were inappropriate.
Re: [PATCH 3/3] devmem: cleanup unxlate_dev_mem_ptr() calls
In reply to Wu FengguangQuoted 28 lines · On Fri, 11 Sep 2009 10:23:36 +0800 Wu Fengguang <[email protected]> wrote:
No behavior change.
CC Marcelo Tosatti <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC Mark Brown <[email protected]>CC Johannes Berg <[email protected]>CC Avi Kivity <[email protected]>Signed-off-by Wu Fengguang <[email protected]>--- drivers/char/mem.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)drivers/char/mem.c +1
-2@@ -131,6 +131,7 @@ static ssize_t read_mem(struct file * fi size_t count, loff_t *ppos) { unsigned long p = *ppos; +unsigned long ret;ssize_t read, sz;char *ptr;@@ -169,12 +170,10 @@ static ssize_t read_mem(struct file * fiif (!ptr)return -EFAULT ; -if (copy_to_user (buf, ptr, sz)) { -unxlate_dev_mem_ptr (p, ptr);
drivers/char/mem.c static ssize_t read_mem(struct file * fi · line 179
Quoted 10 lines
drivers/char/mem.c +3
-3-return -EFAULT ; - } - + ret =copy_to_user (buf, ptr, sz);unxlate_dev_mem_ptr (p, ptr); +if (ret) +return -EFAULT ; buf += sz; p += sz;
- local var `ret' didn't need function-wide scope. I think it's better to reduce its scope if poss.
- conventionally the identifier ret' refers to "the value which this function will return". Ditto retval' and `rc'.
But that's not what ret' does here so let's call it something else? remaining' is rather verbose and formal, but accurate.
drivers/char/mem.c renamed from drivers/char/mem.c~dev-mem-cleanup-unxlate_dev_mem_ptr-calls-fix +4 -3
@@ -131,7 +131,6 @@ static ssize_t read_mem(struct file * fi size_t count, loff_t *ppos) { unsigned long p = *ppos; -unsigned long ret;ssize_t read, sz;char *ptr;
@@ -156,6 +155,8 @@ static ssize_t read_mem(struct file * fi#endif while (count >0 ) { +unsigned long remaining; + sz =size_inside_page (p, count);if (!range_is_allowed (p >>PAGE_SHIFT , count))
@@ -170,9 +171,9 @@ static ssize_t read_mem(struct file * fiif (!ptr)return -EFAULT ; - ret =copy_to_user (buf, ptr, sz); + remaining =copy_to_user (buf, ptr, sz);unxlate_dev_mem_ptr (p, ptr); -if (ret) +if (remaining)return -EFAULT ; buf += sz;
_
Re: [PATCH 1/3] devmem: remove redundant test on len
In reply to Andrew MortonQuoted 36 lines · Andrew Morton
Quoted 33 lines · On Fri, 11 Sep 2009 10:23:34 +0800 Wu Fengguang <[email protected]> wrote:
The len test in
write_kmem()is always true, so can be reduced.CC Marcelo Tosatti <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC Mark Brown <[email protected]>CC Johannes Berg <[email protected]>CC Avi Kivity <[email protected]>Signed-off-by Wu Fengguang <[email protected]>--- drivers/char/mem.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)drivers/char/mem.c +5
-8@@ -580,18 +580,16 @@ static ssize_t write_kmem(struct file *while (count >0 ) {int len = count;if (len >PAGE_SIZE ) len =PAGE_SIZE ; -if (len) { - written =copy_from_user (kbuf, buf, len); -if (written) { -if (wrote + virtr) -break ; -free_page ((unsigned long )kbuf); -return -EFAULT ; - } + written =copy_from_user (kbuf, buf, len); +if (written) { +if (wrote + virtr) +break ; +free_page ((unsigned long )kbuf);
Quoted 8 lines
drivers/char/mem.c static ssize_t write_kmem(struct file * · line 595
Quoted 6 lines
drivers/char/mem.c +1
-0+return -EFAULT ; } len =vwrite (kbuf, (char *)p, len); count -= len; buf += len; virtr += len;humpf. But take a closer look at what remains.
Yeah, it asks for more cleanup patches..
Quoted 6 lines
Local var `written' is unneeded here.
Which makes us look at what `written' _does_ do:
Quoted 7 lines
if (written != wrote) return written; wrote = written;lolwhowrotethat?
local var `written' can at least be made local to the first loop.
Right, I'll make this straight.
Quoted 2 lines
write_kmem()has a lot of typecasts which indicates that the choices of types were inappropriate.
It seems hard to improve because of a fundamental issue: the same value is taken as both virtual and physical address, and also be compared and calculated as numbers..
Thanks,
Fengguang
Re: [PATCH 2/3] devmem: introduce size_inside_page()
In reply to Andrew MortonQuoted 34 lines · Andrew Morton
Quoted 18 lines · On Fri, 11 Sep 2009 10:23:35 +0800 Wu Fengguang <[email protected]> wrote:
Introduce
size_inside_page()to replace duplicate/dev/memcode.Also apply it to
/dev/kmem, whose alignment logic was buggy.CC Marcelo Tosatti <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC Mark Brown <[email protected]>CC Johannes Berg <[email protected]>CC Avi Kivity <[email protected]>Signed-off-by Wu Fengguang <[email protected]>--- drivers/char/mem.c | 60 +++++++++++++------------------------------ 1 file changed, 19 insertions(+), 41 deletions(-)drivers/char/mem.c line 44
Quoted 10 lines
drivers/char/mem.c +7
-0@@ -35,6 +35,19 @@# include <linux/efi.h> #endif +static inline unsigned long size_inside_page (unsigned long start, +unsigned long size) +{ +unsigned long sz; + +if (-start & (PAGE_SIZE -1 )) + sz = -start & (PAGE_SIZE -1 );What on earth is this doing? Negating an unsigned number?
Quoted 2 lines
Can we get rid of these party tricks and use something more conventional here? In a separate patch I guess.
OK. See the followed patches.
Quoted 6 lines
drivers/char/mem.c line 48
Quoted 4 lines
drivers/char/mem.c +4
-0+else + sz =PAGE_SIZE ; + + return min_t(unsigned long, sz, size);Can use
min()here.
Done.
Quoted 8 lines
drivers/char/mem.c line 49
Quoted 1 line
drivers/char/mem.c +1
-0+}Please have a think about the types. Should we be using unsigned long, or
size_t? Which makes more sense? Which maps better onto reality?I suspect that the
min_twhich you inherited was added somewhere because someone didn't get the types right: int-vs-size_t or something. If we actually get the types right, this sort of thing goes away.
I tend to just use unsigned long because even though the value itself is small, it will be elevated to unsigned long in majority use cases.
Does that make sense?
Quoted 9 lines
drivers/char/mem.c static ssize_t read_kmem(struct file *fi · line 454
Quoted 7 lines
drivers/char/mem.c +1
-1@@ -462,10 +451,8 @@ static ssize_t read_kmem(struct file *fiif (!kbuf)return -ENOMEM ;while (count >0 ) { -int len = count; +int len =size_inside_page (p, count);int?
Err, changed it to unsigned long sz.
Thanks,
Fengguang
Re: [PATCH 3/3] devmem: cleanup unxlate_dev_mem_ptr() calls
In reply to Andrew MortonQuoted 39 lines · Andrew Morton
Quoted 36 lines · On Fri, 11 Sep 2009 10:23:36 +0800 Wu Fengguang <[email protected]> wrote:
No behavior change.
CC Marcelo Tosatti <[email protected]>CC Greg Kroah-Hartman <[email protected]>CC Mark Brown <[email protected]>CC Johannes Berg <[email protected]>CC Avi Kivity <[email protected]>Signed-off-by Wu Fengguang <[email protected]>--- drivers/char/mem.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)drivers/char/mem.c +4
-5@@ -131,6 +131,7 @@ static ssize_t read_mem(struct file * fi size_t count, loff_t *ppos) { unsigned long p = *ppos; +unsigned long ret;ssize_t read, sz;char *ptr;@@ -169,12 +170,10 @@ static ssize_t read_mem(struct file * fiif (!ptr)return -EFAULT ; -if (copy_to_user (buf, ptr, sz)) { -unxlate_dev_mem_ptr (p, ptr); -return -EFAULT ; - } - + ret =copy_to_user (buf, ptr, sz);unxlate_dev_mem_ptr (p, ptr); +if (ret) +return -EFAULT ;
Quoted 5 lines
drivers/char/mem.c static ssize_t read_mem(struct file * fi · line 179
Quoted 2 lines
drivers/char/mem.c +0
-0buf += sz; p += sz;- local var `ret' didn't need function-wide scope. I think it's better to reduce its scope if poss.
OK.
Quoted 2 lines
- conventionally the identifier
ret' refers to "the value which this function will return". Dittoretval' and `rc'.
Good to know that, thanks!
drivers/char/mem.c static ssize_t read_mem(struct file * fi · line 134
Quoted 11 lines
But that's not what
ret' does here so let's call it something else?remaining' is rather verbose and formal, but accurate.drivers/char/mem.c renamed from drivers/char/mem.c~dev-mem-cleanup-unxlate_dev_mem_ptr-calls-fix +0
-1@@ -131,7 +131,6 @@ static ssize_t read_mem(struct file * fi size_t count, loff_t *ppos) { unsigned long p = *ppos; -unsigned long ret;
I got compile error because another place in read_mem() used ret. I'll send you an updated fix.
Thanks,
Fengguang
Quoted 25 lines
drivers/char/mem.c renamed from drivers/char/mem.c~dev-mem-cleanup-unxlate_dev_mem_ptr-calls-fix +4
-2ssize_t read, sz;char *ptr;@@ -156,6 +155,8 @@ static ssize_t read_mem(struct file * fi#endif while (count >0 ) { +unsigned long remaining; + sz =size_inside_page (p, count);if (!range_is_allowed (p >>PAGE_SHIFT , count))@@ -170,9 +171,9 @@ static ssize_t read_mem(struct file * fiif (!ptr)return -EFAULT ; - ret =copy_to_user (buf, ptr, sz); + remaining =copy_to_user (buf, ptr, sz);unxlate_dev_mem_ptr (p, ptr); -if (ret) +if (remaining)return -EFAULT ; buf += sz;_