Re: [PATCH] Fix mmap_kmem (was: [question] What's the difference between /dev/kmem and /dev/mem)

Hugh Dickins
2005-08-12T14:23:27Z

On Thu, 11 Aug 2005, Steven Rostedt wrote:
>
> Found the problem. It is a bug with mmap_kmem. The order of checks is
> wrong, so here's the patch.
> - if (!pfn_valid(vma->vm_pgoff))
> - return -EIO;
> val = (u64)vma->vm_pgoff << PAGE_SHIFT;
> vma->vm_pgoff = __pa(val) >> PAGE_SHIFT;
> + if (!pfn_valid(vma->vm_pgoff))
> + return -EIO;
> return mmap_mem(file, vma);

Good find, looks right to me, so far as it goes (why does this check
pfn_valid just on the first? and remap_pfn_range will not behave as
you'd expect on most of kmem, not before Nick kills PageReserved;
and there's the red-penned issue of vmalloc'ed areas too).

Perhaps you're the first to mmap /dev/kmem: before those 2.6.11 changes,
going back beyond 2.4.0, it seems to have expected to caller to subtract
PAGE_OFFSET from the virtual address to give the file offset (when doing
mmap, but not when doing read/write - senseless, especially given the
variable behaviour of lseek to negative offset before the read/write).

Hugh