Commit 29f5c67a authored by Wu Fengguang's avatar Wu Fengguang Committed by james toy

Introduce size_inside_page() to replace duplicate /dev/mem code.

Also apply it to /dev/kmem, whose alignment logic was buggy.
Signed-off-by: default avatarWu Fengguang <fengguang.wu@intel.com>
Acked-by: default avatarAndi Kleen <ak@linux.intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Greg Kroah-Hartman <gregkh@suse.de>
Cc: Mark Brown <broonie@opensource.wolfsonmicro.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: Avi Kivity <avi@qumranet.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 78571f7b
...@@ -35,6 +35,19 @@ ...@@ -35,6 +35,19 @@
# include <linux/efi.h> # include <linux/efi.h>
#endif #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 * Architectures vary in how they handle caching for addresses
* outside of main memory. * outside of main memory.
...@@ -142,15 +155,7 @@ static ssize_t read_mem(struct file * file, char __user * buf, ...@@ -142,15 +155,7 @@ static ssize_t read_mem(struct file * file, char __user * buf,
#endif #endif
while (count > 0) { while (count > 0) {
/* sz = size_inside_page(p, count);
* 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);
if (!range_is_allowed(p >> PAGE_SHIFT, count)) if (!range_is_allowed(p >> PAGE_SHIFT, count))
return -EPERM; return -EPERM;
...@@ -209,15 +214,7 @@ static ssize_t write_mem(struct file * file, const char __user * buf, ...@@ -209,15 +214,7 @@ static ssize_t write_mem(struct file * file, const char __user * buf,
#endif #endif
while (count > 0) { while (count > 0) {
/* sz = size_inside_page(p, count);
* 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);
if (!range_is_allowed(p >> PAGE_SHIFT, sz)) if (!range_is_allowed(p >> PAGE_SHIFT, sz))
return -EPERM; return -EPERM;
...@@ -430,15 +427,7 @@ static ssize_t read_kmem(struct file *file, char __user *buf, ...@@ -430,15 +427,7 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
} }
#endif #endif
while (low_count > 0) { while (low_count > 0) {
/* sz = size_inside_page(p, low_count);
* 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);
/* /*
* On ia64 if a page has been mapped somewhere as * On ia64 if a page has been mapped somewhere as
...@@ -462,10 +451,8 @@ static ssize_t read_kmem(struct file *file, char __user *buf, ...@@ -462,10 +451,8 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
if (!kbuf) if (!kbuf)
return -ENOMEM; return -ENOMEM;
while (count > 0) { 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); len = vread(kbuf, (char *)p, len);
if (!len) if (!len)
break; break;
...@@ -510,15 +497,8 @@ do_write_kmem(void *p, unsigned long realp, const char __user * buf, ...@@ -510,15 +497,8 @@ do_write_kmem(void *p, unsigned long realp, const char __user * buf,
while (count > 0) { while (count > 0) {
char *ptr; 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 * On ia64 if a page has been mapped somewhere as
...@@ -578,10 +558,8 @@ static ssize_t write_kmem(struct file * file, const char __user * buf, ...@@ -578,10 +558,8 @@ static ssize_t write_kmem(struct file * file, const char __user * buf,
if (!kbuf) if (!kbuf)
return wrote ? wrote : -ENOMEM; return wrote ? wrote : -ENOMEM;
while (count > 0) { 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); written = copy_from_user(kbuf, buf, len);
if (written) { if (written) {
if (wrote + virtr) if (wrote + virtr)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment