0

I watched a testcase for linux. it test for mmap like follows:

#define HIGH_ADDR       (void *)(0x1000000000000)
addr = mmap(HIGH_ADDR, map_sz, PROT_READ,
                MAP_SHARED | MAP_FIXED, fildes, 0);

In that case under PPC64, the Expected results is get an errno ENOMEM.
But Actual results under PPC64 is get an errno Invalid argument.
Anyway, it works well on X86_64 arch. I think maybe the PPC64 haven't assigned enough memory. So, I try to modify the HIGH_ADDR's define to:
#define HIGH_ADDR (void *)(0x7FFFFFFFFFF)
But it still get Invalid argument.
I just want to konw, How the linux for PPC64 manage his memory. I mean, I want to know the userspace program's memory geography. Or what's the MAX address Should I use.

malat
  • 12,152
  • 13
  • 89
  • 158
madper
  • 806
  • 1
  • 10
  • 25

2 Answers2

2

From man 2 mmap:

       EINVAL We don't like addr, length, or offset (e.g., they are too large,
              or not aligned on a page boundary).

       EINVAL (since Linux 2.6.12) length was 0.

       EINVAL flags  contained neither MAP_PRIVATE or MAP_SHARED, or contained
              both of these values.

So mmap will signal an error with EINVAL if addr is too large. This is therefore expected behavior.

Recommendation: Don't use MAP_FIXED, unless you have a really good reason.

Footnote: Not all 64-bit architectures support 64-bit addresses.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • Hi, Epp. This code is only for test memory alloc (also test the mmap system call). It's part of Linux-test-project. I think the MAP_FIXED may be need for test? I can't find the MAX addr that I can mmaped. I only find that PPC64 provide from 0x0 to 0x7FFFFFFFFFF for User program(64bit). Thank you. :) – madper Aug 28 '12 at 06:11
  • You don't need `MAP_FIXED` to allocate memory (in fact, it's a bad idea, see R's answer). Why do you need to know the maximum address? I can think of reasons, but they're rather esoteric. – Dietrich Epp Aug 28 '12 at 06:24
  • Because this code is used for test for that if the kernel can map file to "high_addr". It's just for testing but not for daily using. :) – madper Aug 28 '12 at 06:32
  • Also I have read the part of man page about mmap. I still think that I need this flag. Sorry. Anyway, thank you very very much~ – madper Aug 28 '12 at 06:33
2

Using MAP_FIXED like this is invalid. Even if it worked, it would potentially map over top of something you don't want to clobber, like a loaded shared library or the dynamic linker. The only safe way to use MAP_FIXED is to map over top of address ranges already assigned to your program (e.g. by a prior call to mmap without MAP_FIXED).

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Hi, R. This code is only for test memory alloc (also test the mmap system call). It's part of Linux-test-project. I think the MAP_FIXED may be need for test? Also I will try to run it again after I remove that flag. Thank you very much! – madper Aug 28 '12 at 06:06
  • Hi, R. I read the man page for mmap. And think the flag-MAP_FIXED can't be removed in this case. :-( – madper Aug 28 '12 at 06:28
  • What is the test trying to assert? I'm nearly certain that whatever it's trying to do is wrong. – R.. GitHub STOP HELPING ICE Aug 28 '12 at 12:38