how to get physical memory address from virtual memory address - Linux

This is a discussion on how to get physical memory address from virtual memory address - Linux ; Hi , I am using Linux 2.4.20 Kernel. I am runing an application which will malloc and returns a virtual memory address. I need to get the physical memory address for that virtual memory address. How can I get that? ...

+ Reply to Thread
Results 1 to 15 of 15

Thread: how to get physical memory address from virtual memory address

  1. how to get physical memory address from virtual memory address

    Hi ,

    I am using Linux 2.4.20 Kernel. I am runing an application which will
    malloc and returns a virtual memory address. I need to get the physical
    memory address for that virtual memory address. How can I get that?

    Thanks in advance,
    -Hari


  2. Re: how to get physical memory address from virtual memory address

    Hari wrote:
    > Hi ,
    >
    > I am using Linux 2.4.20 Kernel. I am runing an application which will
    > malloc and returns a virtual memory address. I need to get the physical
    > memory address for that virtual memory address. How can I get that?


    Get it for what purpose? As it is process virtual memory, the physical
    address may change at any time due to paging activity!

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


  3. Re: how to get physical memory address from virtual memory address


    Hari wrote:
    > Hi ,
    >
    > I am using Linux 2.4.20 Kernel. I am runing an application which will
    > malloc and returns a virtual memory address. I need to get the physical
    > memory address for that virtual memory address. How can I get that?
    >
    > Thanks in advance,
    > -Hari


    There isn't one. You haven't allocated any physical memory, so the
    kernel doesn't give you any.

    DS


  4. Re: how to get physical memory address from virtual memory address

    "David Schwartz" wrote:
    >Hari wrote:
    >> Hi ,
    >>
    >> I am using Linux 2.4.20 Kernel. I am runing an application which will
    >> malloc and returns a virtual memory address. I need to get the physical
    >> memory address for that virtual memory address. How can I get that?

    >
    >There isn't one. You haven't allocated any physical memory, so the
    >kernel doesn't give you any.


    What?? This is entirely incorrect. EVERY valid virtual address has an
    associated physical address. In the case of malloc-ed memory, that
    physical address will be in physical memory.

    Addressing the OP, while you are in user mode, the physical address can
    change at any time. The only reliable way to get the physical address is
    within a kernel driver.
    --
    - Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

  5. Re: how to get physical memory address from virtual memory address

    Tim Roberts wrote:
    > "David Schwartz" wrote:
    >
    >>Hari wrote:
    >>
    >>>Hi ,
    >>>
    >>>I am using Linux 2.4.20 Kernel. I am runing an application which will
    >>>malloc and returns a virtual memory address. I need to get the physical
    >>>memory address for that virtual memory address. How can I get that?

    >>
    >>There isn't one. You haven't allocated any physical memory, so the
    >>kernel doesn't give you any.

    >
    >
    > What?? This is entirely incorrect. EVERY valid virtual address has an
    > associated physical address. In the case of malloc-ed memory, that
    > physical address will be in physical memory.


    The virtual address allocation *does not* allocate physical
    pages to the allocated block - you only get a slice of the
    address space of your process.

    The virtual address is connected (maybe temporarily) to a
    physical page when it's accessed and a page fault handler
    decides to give the physical addresses.

    To the OP:

    There's no sense in the question as long as you're not telling
    what are you using the physical address information for.

    --

    Tauno Voipio
    tauno voipio (at) iki fi

  6. Re: how to get physical memory address from virtual memory address

    Hi all,

    Thanks for the response from you all.

    My aim is to execute SDRAM diagnostics code to find which part of SDRAM
    chip is failed. So in code, I am using malloc to allocte available free
    memory and then locking the memory using mlock. I think once the mlock
    is done the pages what ever is allocted during malloc will be in the
    RAM and will not change until freed. After mlock the memory is tested
    by writing and reading some standrad patterns. While testing, if any
    location is failed, I need to get the physical address for that
    particular location. This can be achieved only if I am able to map the
    virtual memory to physical memory address.

    -Hari



    Tauno Voipio wrote:
    > Tim Roberts wrote:
    > > "David Schwartz" wrote:
    > >
    > >>Hari wrote:
    > >>
    > >>>Hi ,
    > >>>
    > >>>I am using Linux 2.4.20 Kernel. I am runing an application which will
    > >>>malloc and returns a virtual memory address. I need to get the physical
    > >>>memory address for that virtual memory address. How can I get that?
    > >>
    > >>There isn't one. You haven't allocated any physical memory, so the
    > >>kernel doesn't give you any.

    > >
    > >
    > > What?? This is entirely incorrect. EVERY valid virtual address has an
    > > associated physical address. In the case of malloc-ed memory, that
    > > physical address will be in physical memory.

    >
    > The virtual address allocation *does not* allocate physical
    > pages to the allocated block - you only get a slice of the
    > address space of your process.
    >
    > The virtual address is connected (maybe temporarily) to a
    > physical page when it's accessed and a page fault handler
    > decides to give the physical addresses.
    >
    > To the OP:
    >
    > There's no sense in the question as long as you're not telling
    > what are you using the physical address information for.
    >
    > --
    >
    > Tauno Voipio
    > tauno voipio (at) iki fi



  7. Re: how to get physical memory address from virtual memory address

    On Fri, 1 Sep 2006, Tim Roberts wrote:

    > What?? This is entirely incorrect. EVERY valid virtual address has an
    > associated physical address.


    Not true. When the respective page is swapped out, there is no physical
    address associated to given virtual address.

    --
    JiKos.

  8. Re: how to get physical memory address from virtual memory address

    Tim Roberts wrote:
    > "David Schwartz" wrote:
    >> Hari wrote:
    >>> Hi ,
    >>>
    >>> I am using Linux 2.4.20 Kernel. I am runing an application which will
    >>> malloc and returns a virtual memory address. I need to get the physical
    >>> memory address for that virtual memory address. How can I get that?

    >> There isn't one. You haven't allocated any physical memory, so the
    >> kernel doesn't give you any.

    >
    > What?? This is entirely incorrect. EVERY valid virtual address has an
    > associated physical address. In the case of malloc-ed memory, that
    > physical address will be in physical memory.
    >
    > Addressing the OP, while you are in user mode, the physical address can
    > change at any time. The only reliable way to get the physical address is
    > within a kernel driver.


    Where does the physical memory come from when you turn on virtual memory
    overcommits? Look at Documentation/vm/overcommit-accounting in your
    kernel sources. Setting vm.overcommit_memory=1 is indeed useful in some
    circumstances but can lead to unpredictable process deaths.

    --
    Dr. Douglas O'Neal
    Manager, Bioinformatics Center
    Delaware Biotechnology Institute
    (302) 831-3456

  9. Re: how to get physical memory address from virtual memory address


    Tim Roberts wrote:
    > "David Schwartz" wrote:
    > >Hari wrote:
    > >> Hi ,
    > >>
    > >> I am using Linux 2.4.20 Kernel. I am runing an application which will
    > >> malloc and returns a virtual memory address. I need to get the physical
    > >> memory address for that virtual memory address. How can I get that?

    > >
    > >There isn't one. You haven't allocated any physical memory, so the
    > >kernel doesn't give you any.

    >
    > What?? This is entirely incorrect. EVERY valid virtual address has an
    > associated physical address. In the case of malloc-ed memory, that
    > physical address will be in physical memory.


    Of course, you are wrong.

    Virtual addresses /only/ have corresponding physical addresses when
    they are used in a memory reference.

    Pure "allocation" (malloc()) doesn't result in a memory reference, so
    no real address is assigned. (FWIW, this is why the OOM killer works
    the way it does - it kills processes that have allocated memory
    (grabbed a block of virtual address space), but not populated it with
    data.)

    Pages that are swapped out still have virtual addresses, but /do not/
    have physical memory addresses. They only get physical memory addresses
    when the pages are swapped back in to memory.

    The physical memory address of any virtual address can change
    (drastically) over the lifetime of the process.

    HTH
    --
    Lew Pitcher


  10. Re: how to get physical memory address from virtual memory address

    On Fri, 1 Sep 2006, Hari wrote:

    > My aim is to execute SDRAM diagnostics code to find which part of SDRAM
    > chip is failed. So in code, I am using malloc to allocte available free
    > memory and then locking the memory using mlock. I think once the mlock
    > is done the pages what ever is allocted during malloc will be in the RAM
    > and will not change until freed. After mlock the memory is tested by
    > writing and reading some standrad patterns. While testing, if any
    > location is failed, I need to get the physical address for that
    > particular location. This can be achieved only if I am able to map the
    > virtual memory to physical memory address.


    This is not a task that should be performed by the user-level process. You
    can't for example test the memory which is permanently mapped by kernel
    (which user-level process is not allowed to access).

    For memory diagnostics, tools specifically crafted for such tasks should
    be used (such as memtest86).

    If you still think that this is a good idea, then:

    - you state you use mlock(), that implies that your code is running as
    root
    - therefore you can write simple kernel module, which you modprobe at the
    time you want to perform a pagetable walk, to obtain a physical address
    - how to perform a pagetable walk - see for example __follow_page()
    function in mm/memory.c

    --
    JiKos.

  11. Re: how to get physical memory address from virtual memory address

    Tim Roberts writes:

    > "David Schwartz" wrote:
    > >Hari wrote:
    > >> Hi ,
    > >>
    > >> I am using Linux 2.4.20 Kernel. I am runing an application which will
    > >> malloc and returns a virtual memory address. I need to get the physical
    > >> memory address for that virtual memory address. How can I get that?

    > >
    > >There isn't one. You haven't allocated any physical memory, so the
    > >kernel doesn't give you any.

    >
    > What?? This is entirely incorrect. EVERY valid virtual address has an
    > associated physical address. In the case of malloc-ed memory, that
    > physical address will be in physical memory.


    Unless of course the memory is swapped out at the moment, which is
    entirely possible after a fresh malloc() of a big chunk of memory.

    > Addressing the OP, while you are in user mode, the physical address can
    > change at any time. The only reliable way to get the physical address is
    > within a kernel driver.


    Correct
    --
    Joseph J. Pfeiffer, Jr., Ph.D. Phone -- (505) 646-1605
    Department of Computer Science FAX -- (505) 646-1002
    New Mexico State University http://www.cs.nmsu.edu/~pfeiffer

  12. Re: how to get physical memory address from virtual memory address

    Tim Roberts wrote:
    > "David Schwartz" wrote:
    >> Hari wrote:
    >>> Hi ,
    >>>
    >>> I am using Linux 2.4.20 Kernel. I am runing an application which will
    >>> malloc and returns a virtual memory address. I need to get the physical
    >>> memory address for that virtual memory address. How can I get that?

    >> There isn't one. You haven't allocated any physical memory, so the
    >> kernel doesn't give you any.

    >
    > What?? This is entirely incorrect. EVERY valid virtual address has an
    > associated physical address. In the case of malloc-ed memory, that
    > physical address will be in physical memory.


    Really? So what happens when the page is swapped out? It's still a valid
    virtual address, but there's no valid physical address behind it, until it's
    swapped back in by a page-fault or the prefetcher.

    -n

  13. Re: how to get physical memory address from virtual memory address


    Tim Roberts wrote:

    > "David Schwartz" wrote:


    > >Hari wrote:


    > >> I am using Linux 2.4.20 Kernel. I am runing an application which will
    > >> malloc and returns a virtual memory address. I need to get the physical
    > >> memory address for that virtual memory address. How can I get that?


    > >There isn't one. You haven't allocated any physical memory, so the
    > >kernel doesn't give you any.


    > What?? This is entirely incorrect. EVERY valid virtual address has an
    > associated physical address. In the case of malloc-ed memory, that
    > physical address will be in physical memory.


    So what happens if you 'malloc' more memory than is physically present?
    What happens if three programs each 'malloc' a block that is equal in
    size to half of physical memory? What would be the point of swap if
    this were the case?

    > Addressing the OP, while you are in user mode, the physical address can
    > change at any time. The only reliable way to get the physical address is
    > within a kernel driver.


    Yes, it can change at any time. It can even change such that there is
    not one. And because Linux is demand paged, when you first allocate or
    map it, it likely will not have any physical address at all (unless for
    some reason the page just happened to already be resident).

    DS


  14. Re: how to get physical memory address from virtual memory address


    Tim Roberts wrote:

    > What?? This is entirely incorrect. EVERY valid virtual address has an
    > associated physical address. In the case of malloc-ed memory, that
    > physical address will be in physical memory.


    Let's see if this is true. I have a machine with 1GB of physical
    memory:

    $ free
    total used free shared buffers
    cached
    Mem: 1035736 996412 39324 0 252808
    368224
    -/+ buffers/cache: 375380 660356
    Swap: 12937128 23420 12913708

    And I write a program to allocate more than 1GB of virtual memory:

    $ cat bigmalloc.c
    #include

    int main(void)
    {
    char *h=malloc(1323434434);
    printf("h=%x\n", (int) h);
    return 0;
    }

    Then I run it:

    $ gcc bigmalloc.c -o bigmalloc
    $ ./bigmalloc
    h=691c0008

    Now, that I have allocated about 1.3GB of virtual memory and only have
    1GB of physical memory, how can every valid virtual address have an
    associated physical address in physical memory?

    DS


  15. Re: how to get physical memory address from virtual memory address

    Tim Roberts wrote:
    > What?? This is entirely incorrect. EVERY valid virtual address has an
    > associated physical address. In the case of malloc-ed memory, that
    > physical address will be in physical memory.


    Correct, if by "valid" you mean "mapped and present" so that touching
    the page doesn't give rise to an exception.

    However, there can be mapped pages which are not present.

    > Addressing the OP, while you are in user mode, the physical address can
    > change at any time.


    Change or disappear temporarily (become not present).


+ Reply to Thread