trivial changes on util/mmap-alloc
v4 cover trivial changes on util/mmap-alloc
Cao jin (2):
util/mmap-alloc: check parameter before using util/mmap-alloc: refactor a little bit for readability
util/mmap-alloc.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
Signature · 1 line
2.1.0
PATCH v4 1/2 util/mmap-alloc: check parameter before using
--- util/mmap-alloc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
util/mmap-alloc.c +4 -3
@@ -12,6 +12,7 @@#include "qemu/osdep.h" #include "qemu/mmap-alloc.h" +#include "qemu/host-utils.h" #define HUGETLBFS_MAGIC 0x958458f6
@@ -61,18 +62,18 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)#else void *ptr =mmap (0 , total,PROT_NONE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );#endif -size_t offset =QEMU_ALIGN_UP ((uintptr_t )ptr, align) - (uintptr_t )ptr; +size_t offset;void *ptr1;if (ptr ==MAP_FAILED ) {return MAP_FAILED ; } -/* Make sure align is a power of 2 */ -assert (!(align & (align -1 ))); +assert (is_power_of_2 (align));/* Always align to host page size */ assert (align >=getpagesize ()); + offset =QEMU_ALIGN_UP ((uintptr_t )ptr, align) - (uintptr_t )ptr; ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE, MAP_FIXED | (fd == -1 ? MAP_ANONYMOUS : 0) |
Signature · 1 line
2.1.0
Re: [Qemu-devel] [PATCH v4 1/2] util/mmap-alloc: check parameter before using
Quoted 30 lines · Cao jin
Signed-off-by Cao jin <[email protected]>--- util/mmap-alloc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)util/mmap-alloc.c +2
-1@@ -12,6 +12,7 @@#include "qemu/osdep.h" #include "qemu/mmap-alloc.h" +#include "qemu/host-utils.h" #define HUGETLBFS_MAGIC 0x958458f6@@ -61,18 +62,18 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)#else void *ptr =mmap (0 , total,PROT_NONE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );#endif -size_t offset =QEMU_ALIGN_UP ((uintptr_t )ptr, align) - (uintptr_t )ptr; +size_t offset;void *ptr1;if (ptr ==MAP_FAILED ) {return MAP_FAILED ; }
util/mmap-alloc.c void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) · line 79
Quoted 10 lines
util/mmap-alloc.c +2
-2-/* Make sure align is a power of 2 */ -assert (!(align & (align -1 ))); +assert (is_power_of_2 (align));/* Always align to host page size */ assert (align >=getpagesize ()); + offset =QEMU_ALIGN_UP ((uintptr_t )ptr, align) - (uintptr_t )ptr; ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE, MAP_FIXED | (fd == -1 ? MAP_ANONYMOUS : 0) |
Re: [Qemu-devel] [PATCH v4 1/2] util/mmap-alloc: check parameter before using
In reply to Cao jinping
Quoted 40 lines · Cao jin
Signed-off-by Cao jin <[email protected]>--- util/mmap-alloc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)util/mmap-alloc.c +4
-3@@ -12,6 +12,7 @@#include "qemu/osdep.h" #include "qemu/mmap-alloc.h" +#include "qemu/host-utils.h" #define HUGETLBFS_MAGIC 0x958458f6@@ -61,18 +62,18 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)#else void *ptr =mmap (0 , total,PROT_NONE ,MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 );#endif -size_t offset =QEMU_ALIGN_UP ((uintptr_t )ptr, align) - (uintptr_t )ptr; +size_t offset;void *ptr1;if (ptr ==MAP_FAILED ) {return MAP_FAILED ; } -/* Make sure align is a power of 2 */ -assert (!(align & (align -1 ))); +assert (is_power_of_2 (align));/* Always align to host page size */ assert (align >=getpagesize ()); + offset =QEMU_ALIGN_UP ((uintptr_t )ptr, align) - (uintptr_t )ptr; ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE, MAP_FIXED | (fd == -1 ? MAP_ANONYMOUS : 0) |
Signature · 2 lines
Sincerely, Cao jin
PATCH v4 2/2 util/mmap-alloc: refactor a little bit for readability
In reply to Cao jin1st mmap returns *ptr* which aligns to host page size,
| size + align |
------------------------------------------
ptr
input param *align* could be 1M, or 2M, or host page size. After QEMU_ALIGN_UP, offset will >= 0
2nd mmap use flag MAP_FIXED, then it return ptr+offset, or else fail. If it success, then we will have something like:
| offset | size |
--------------------------------------
ptr ptr1
*ptr1* is what we really want to return, it equals ptr+offset.
--- util/mmap-alloc.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
This one may be controversial, take it or not as you will:)
util/mmap-alloc.c +4 -6
@@ -84,22 +84,20 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)return MAP_FAILED ; } - ptr += offset; - total -= offset; -if (offset >0 ) { -munmap (ptr - offset, offset); +munmap (ptr, offset); }/* * Leave a single PROT_NONE page allocated after the RAM block, to serve as * a guard page guarding against potential buffer overflows. */ + total -= offset;if (total > size +getpagesize ()) { -munmap (ptr + size +getpagesize (), total - size -getpagesize ()); +munmap (ptr1 + size +getpagesize (), total - size -getpagesize ()); } -return ptr; +return ptr1; }void qemu_ram_munmap (void *ptr,size_t size)
Signature · 1 line
2.1.0
Quoted 6 lines · Cao jin
Cao jin (2):
util/mmap-alloc: check parameter before using util/mmap-alloc: refactor a little bit for readabilityutil/mmap-alloc.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
Applied (finally) to -trivial, thank you very much!
/mjt