Skip to contentSkip to conversation

shmat(2) returns page size aligned memory address

PATCH In review Yubin Ruan 3 participants 4 messages Oct 8, 2017 – Oct 9, 2017

Subsystems (2)
lore
Share & tools
lore

Yubin Ruan

author
#0 lore raw

Hi Michael,
At the current man page for shmat(2)[1], there is no mentioning whether the returned memory address of shmat(2) will be page size aligned or not. As that is quite important to many applications(e.g., those that use locks heavily and would like to avoid some locks by some atomic guarantees provided by the CPU), it would be great to specify that for Linux.

I walked down the current implementation of shmat(2) in the latest kernel src and found that shmat(2) does return a page size aligned memory address:

SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
-> do_shmat(...)
-> do_mmap_pgoff(...)
-> do_mmap(...)
-> get_unmapped_area(...)
-> get_area(...) -> offset_in_page(addr)

there is a `offset_in_page(addr)' assertion at the end and if that is true a -EINVAL would be returned, by which we can be sure that shmat(2) will return a page size aligned memory address on success[2].

I will create a patch later if that is acceptable.

Thanks,
Yubin

[1]: http://man7.org/linux/man-pages/man2/shmat.2.html
[2]: there is also a `offset_in_page(2)' in get_unmapped_area(...), but that doesn't lead to -EINVAL...I am not sure whether the logic of that code is right.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#1 lore raw

Cc linux-mm

2017-10-08 23:37 GMT+08:00 Yubin Ruan <[email protected]>:

Quoted 25 lines

Hi Michael,
At the current man page for shmat(2)[1], there is no mentioning whether the returned memory address of shmat(2) will be page size aligned or not. As that is quite important to many applications(e.g., those that use locks heavily and would like to avoid some locks by some atomic guarantees provided by the CPU), it would be great to specify that for Linux.

I walked down the current implementation of shmat(2) in the latest kernel src and found that shmat(2) does return a page size aligned memory address:

SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
-> do_shmat(...)
-> do_mmap_pgoff(...)
-> do_mmap(...)
-> get_unmapped_area(...)
-> get_area(...) -> offset_in_page(addr)

there is a `offset_in_page(addr)' assertion at the end and if that is true a -EINVAL would be returned, by which we can be sure that shmat(2) will return a page size aligned memory address on success[2].

I will create a patch later if that is acceptable.

Quoted 7 lines

Thanks,
Yubin

[1]: http://man7.org/linux/man-pages/man2/shmat.2.html
[2]: there is also a `offset_in_page(2)' in get_unmapped_area(...), but that doesn't lead to -EINVAL...I am not sure whether the logic of that code is right.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw

PATCH 1/1 shmat(2) returns page size aligned memory address

In reply to Yubin Ruan
Quoted 25 lines · Yubin Ruan

Hi Michael,
At the current man page for shmat(2)[1], there is no mentioning whether the returned memory address of shmat(2) will be page size aligned or not. As that is quite important to many applications(e.g., those that use locks heavily and would like to avoid some locks by some atomic guarantees provided by the CPU), it would be great to specify that for Linux.

I walked down the current implementation of shmat(2) in the latest kernel src and found that shmat(2) does return a page size aligned memory address:

SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
-> do_shmat(...)
-> do_mmap_pgoff(...)
-> do_mmap(...)
-> get_unmapped_area(...)
-> get_area(...) -> offset_in_page(addr)

there is a `offset_in_page(addr)' assertion at the end and if that is true a -EINVAL would be returned, by which we can be sure that shmat(2) will return a page size aligned memory address on success[2].

I will create a patch later if that is acceptable.

Quoted 7 lines

Thanks,
Yubin

[1]: http://man7.org/linux/man-pages/man2/shmat.2.html
[2]: there is also a `offset_in_page(2)' in get_unmapped_area(...), but that doesn't lead to -EINVAL...I am not sure whether the logic of that code is right.

add the page-alignment attribute of the return address of shmat(2)
---

man2/shmop.2 +1 -1
index 849529f..b8d7595 100644
@@ -63,7 +63,7 @@ with one of the following criteria:
 If
 .I shmaddr
 is NULL,
-the system chooses a suitable (unused) address at which to attach
+the system chooses a suitable (unused) page-aligned address to attach
 the segment.
 .IP *
 If

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

Thanks, Yubin. Patch applied.

Cheers,

Michael

Quoted 43 lines · Yubin Ruan
Quoted 25 lines · Yubin Ruan

Hi Michael,
At the current man page for shmat(2)[1], there is no mentioning whether the returned memory address of shmat(2) will be page size aligned or not. As that is quite important to many applications(e.g., those that use locks heavily and would like to avoid some locks by some atomic guarantees provided by the CPU), it would be great to specify that for Linux.

I walked down the current implementation of shmat(2) in the latest kernel src and found that shmat(2) does return a page size aligned memory address:

SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
-> do_shmat(...)
-> do_mmap_pgoff(...)
-> do_mmap(...)
-> get_unmapped_area(...)
-> get_area(...) -> offset_in_page(addr)

there is a `offset_in_page(addr)' assertion at the end and if that is true a -EINVAL would be returned, by which we can be sure that shmat(2) will return a page size aligned memory address on success[2].

I will create a patch later if that is acceptable.

Quoted 7 lines

Thanks,
Yubin

[1]: http://man7.org/linux/man-pages/man2/shmat.2.html
[2]: there is also a `offset_in_page(2)' in get_unmapped_area(...), but that doesn't lead to -EINVAL...I am not sure whether the logic of that code is right.

add the page-alignment attribute of the return address of shmat(2)
---

man2/shmop.2 +0 -0
index 849529f..b8d7595 100644

man2/shmop.2 with one of the following criteria: · line 69

Quoted 8 lines
man2/shmop.2 +1 -1
@@ -63,7 +63,7 @@ with one of the following criteria:
  If
  .I shmaddr
  is NULL,
-the system chooses a suitable (unused) address at which to attach
+the system chooses a suitable (unused) page-aligned address to attach
  the segment.
  .IP *
  If

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html