I am using Linux 2.4.20 Kernel. I am runing a application which will
malloc and give a virtual memory address. I need to get the physical
memory location of that. Can any one help me in this ?
Thanks in advance,
-Hari
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
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
There isn't one. You haven't allocated any physical memory, so the
kernel doesn't give you any.
DS
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, ti...@probo.com
Providenza & Boekelheide, Inc.
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
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
> 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.
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
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
> 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.
> "David Schwartz" <dav...@webmaster.com> 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
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
> "David Schwartz" <dav...@webmaster.com> 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
> 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 <stdlib.h>
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
You have to do this in the kernel.
Firstly, the page obviously has to be nailed down (unswappable).
Otherwise it has no fixed physical address. When paged out, it has no
address at all, and may get a different physical address each time it
is paged out.
You can do this using the get_user_pages() kernel function, which will
reduce a range of user space addresses into an array of ``struct page
*'' pointers which are, in the same step, also nailed down, and
refcounted up (so even if the process subsequently exits, those
references remain valid, and are never swapped out). Once you are done
with these pages, you eventually have to drop their references, and
also give a hint to the VM system whether you made them dirty (i.e.
changed the contents).
Given a ``struct page *'', you can use the page_to_pfn() function to
determine which page it is in the array of pages (pfn = physical frame
number).
The physical address is just that number times the page size.
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).