All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wei Liu <liuw@liuw.name>
To: Eric Camachat <eric.camachat@gmail.com>
Cc: xen-devel@lists.xensource.com
Subject: Re: mmap in PV xen-4.0.1
Date: Wed, 10 Aug 2011 17:12:56 +0800	[thread overview]
Message-ID: <20110810091256.GA1537@limbo> (raw)
In-Reply-To: <CACeEFf5yDb3dh+M3nycmYMaW5Vf+6C57QXZjXEBQZhrwkTHkHg@mail.gmail.com>

On Tue, Aug 09, 2011 at 11:29:51PM -0700, Eric Camachat wrote:
> Hi,
> 
> I have a problem to map kernel memory to userspace via /dev/mem.
> The mmap() succeeded, but when I try to access it, the program will
> hang forever (until press ctrl-c to terminate it).
> 
> # memtest-user
> memtest_vma_open: virt 0x7fbc90085000, phys 0x3eee8000
> paddr = 0x3eee8000
>  mem = 0x7fbc90089000
>  map = 0x7fbc90085000
> map[0]= 4c4c4c4c
> map[1]= 4c4c4c4c
> *** Hang here, it cannot (finish) access the memory mapped via /dev/mem ***
> 
> My test source below, and it runs properly on HVM, VirtualBox, QEM and
> physical machines.
> What mistake I did?
> 
> My kernel module look like this:
> =================================================================================

[...snip...]

> memtest_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
>               unsigned long arg)
> {
>        int ret = -ENOIOCTLCMD;
>        phys_addr_t *paddr;
>        unsigned long *vaddr;
>        uint32_t *size;
> 
>        switch(cmd) {
>        case MEMTEST_DMA_SIZE:
>                size = (uint32_t*)arg;
>                *size = _size;

Though your output shows that this assignment works, shouldn't this
kind of direct assignment across kernel space and user land be
avoided? It is bad practice to do direct assignment I think.

copy_{from,to}_user should do the job.

>                ret = 0;
>                break;
>        case MEMTEST_DMA_PADDR:
>                paddr = (phys_addr_t*)arg;
>                *paddr = _pbase;
>                ret = 0;
>                break;
>        case MEMTEST_DMA_VADDR:
>                vaddr = (unsigned long*)arg;
>                *vaddr = _vbase;
>                ret = 0;
>                break;
>        }
>        return ret;
> }
> 
>

[...snip...]
 
> static struct file_operations memtest_fops = {
>        .owner          = THIS_MODULE,
>        .llseek         = no_llseek,
>        .ioctl          = memtest_ioctl,

My kernel doesn't have field called 'ioctl' in file_operations.

So which kernel do you use? 2.6.18? I don't have old kernel at the
moment so I can't help you much...

>        .mmap           = memtest_mmap,
> };
> 
>

[...snip...]
 
> static void __exit memtest_exit(void)
> {
>        if (_vbase != 0)
>                free_page(_vbase);

I suppose you should use free_pages here, since you use
__get_free_pages when allocating.

>        unregister_chrdev(MEMTEST_MAJOR, MEMTEST_NAME);
> }
> 
> 
> MODULE_LICENSE("GPL");
> 
> module_init(memtest_init);
> module_exit(memtest_exit);
> =================================================================================
> 
> Here is my user program:
> 
> =================================================================================
>

[...snip...]
 
> 	if (map)
> 	{
> 		printf("map[0]= %x\n", map[0]);
> 		printf("map[1]= %x\n", map[1]);

This confuses me. You did write different values in _vbase[0],
_vbase[1]. But the output '4C4C4C4C' shows that the value is 'L'.

I just skimmed the output and the code. I don't run your code since I
don't have a suitible environment at the moment...

Wei.

  reply	other threads:[~2011-08-10  9:12 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CACeEFf77eOLLNgsJwEYdzeiuxm+qDcO2zs09FK-Waas0sxcwLQ@mail.gmail.com>
2011-08-10  6:29 ` mmap in PV xen-4.0.1 Eric Camachat
2011-08-10  9:12   ` Wei Liu [this message]
2011-08-10 17:14     ` Ranjith Ravi
2011-08-10 17:50       ` Konrad Rzeszutek Wilk
2011-08-10 18:31     ` Eric Camachat
2011-08-10 19:25       ` Konrad Rzeszutek Wilk
2011-08-10 19:42         ` Ian Campbell
2011-08-10 19:45           ` Eric Camachat
2011-08-10 20:59             ` BUG: soft lockup - CPU#0 stuck for 61s! [swapper:0] Mark Schneider
2011-08-13 15:15               ` Konrad Rzeszutek Wilk
2011-08-10 19:45         ` mmap in PV xen-4.0.1 Eric Camachat
2011-08-11  1:31           ` Wei Liu
2011-08-11  3:10             ` Eric Camachat
2011-08-11 17:11               ` Eric Camachat
2011-08-12  4:26                 ` Wei Liu
2011-08-12 17:20                   ` Eric Camachat
2011-08-16  3:13                     ` Ranjith Ravi
2011-08-16  5:06                       ` Konrad Rzeszutek Wilk
2011-08-16 18:27                         ` Ranjith Ravi
2011-08-17  2:20                           ` Konrad Rzeszutek Wilk
2011-08-11  0:33         ` Eric Camachat
2011-08-11  1:21       ` Wei Liu
2011-08-11  1:29         ` Eric Camachat

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110810091256.GA1537@limbo \
    --to=liuw@liuw.name \
    --cc=eric.camachat@gmail.com \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.