From: "Jan Beulich" Subject: [PATCH] linux: make /dev/mem handling more consistent with native Date: Thu, 02 Mar 2006 15:19:47 +0100 Message-ID: <44070D13.76F0.0078.0@novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartC6E38913.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartC6E38913.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline While native code range-checks the requested address, possibly truncates the range, and then reads/writes page-wise until possibly encountering a -EFAULT issue, Xen code accessed the whole range in a single step, thus not allowing partially succeeding accesses. Jan --=__PartC6E38913.0__= Content-Type: text/plain; name="xenlinux-dev-mem.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="xenlinux-dev-mem.patch" Index: head-2006-02-23/drivers/xen/char/mem.c =================================================================== --- head-2006-02-23.orig/drivers/xen/char/mem.c 2006-02-23 11:57:23.000000000 +0100 +++ head-2006-02-23/drivers/xen/char/mem.c 2006-03-01 15:07:36.000000000 +0100 @@ -43,49 +43,85 @@ static inline int uncached_access(struct static ssize_t read_mem(struct file * file, char __user * buf, size_t count, loff_t *ppos) { - unsigned long i, p = *ppos; - ssize_t read = -EFAULT; + unsigned long p = *ppos, ignored; + ssize_t read = 0, sz; void __iomem *v; - if ((v = ioremap(p, count)) == NULL) { + while (count > 0) { /* - * Some programs (e.g., dmidecode) groove off into weird RAM - * areas where no table scan possibly exist (because Xen will - * have stomped on them!). These programs get rather upset if - * we let them know that Xen failed their access, so we fake - * out a read of all zeroes. :-) + * Handle first page in case it's not aligned */ - for (i = 0; i < count; i++) - if (put_user(0, buf+i)) + if (-p & (PAGE_SIZE - 1)) + sz = -p & (PAGE_SIZE - 1); + else + sz = PAGE_SIZE; + + sz = min_t(unsigned long, sz, count); + + if ((v = ioremap(p, sz)) == NULL) { + /* + * Some programs (e.g., dmidecode) groove off into weird RAM + * areas where no tables can possibly exist (because Xen will + * have stomped on them!). These programs get rather upset if + * we let them know that Xen failed their access, so we fake + * out a read of all zeroes. :-) + */ + if (clear_user(buf, count)) return -EFAULT; - return count; + read += count; + break; + } + + ignored = copy_to_user(buf, v, sz); + iounmap(v); + if (ignored) + return -EFAULT; + buf += sz; + p += sz; + count -= sz; + read += sz; } - if (copy_to_user(buf, v, count)) - goto out; - read = count; *ppos += read; -out: - iounmap(v); return read; } static ssize_t write_mem(struct file * file, const char __user * buf, size_t count, loff_t *ppos) { - unsigned long p = *ppos; - ssize_t written = -EFAULT; + unsigned long p = *ppos, ignored; + ssize_t written = 0, sz; void __iomem *v; - if ((v = ioremap(p, count)) == NULL) - return -EFAULT; - if (copy_from_user(v, buf, count)) - goto out; + 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); + + if ((v = ioremap(p, sz)) == NULL) + break; + + ignored = copy_from_user(v, buf, sz); + iounmap(v); + if (ignored) { + written += sz - ignored; + if (written) + break; + return -EFAULT; + } + buf += sz; + p += sz; + count -= sz; + written += sz; + } - written = count; *ppos += written; -out: - iounmap(v); return written; } --=__PartC6E38913.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__PartC6E38913.0__=--