[v6,07/33] util: introduce qemu_file_get_page_size()
diff mbox

Message ID 1446184587-142784-8-git-send-email-guangrong.xiao@linux.intel.com
State New
Headers show

Commit Message

Xiao Guangrong Oct. 30, 2015, 5:56 a.m. UTC
There are three places use the some logic to get the page size on
the file path or file fd

This patch introduces qemu_file_get_page_size() to unify the code

Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
---
 include/qemu/osdep.h |  1 +
 target-ppc/kvm.c     | 21 +++------------------
 util/oslib-posix.c   | 16 ++++++++++++++++
 util/oslib-win32.c   |  5 +++++
 4 files changed, 25 insertions(+), 18 deletions(-)

Comments

Vladimir Sementsov-Ogievskiy Oct. 30, 2015, 1:26 p.m. UTC | #1
logic is changed:
     in old version gethugepagesize on statfs error generates exit(1)
     in new it returns getpagesize() in this case (through fd_getpagesize)
(I think, fd_getpagesize should be fixed to handle error)

also, in new version for windows we have getpagesize(), when in old 
version there was no difference (how did it work?). May be it's ok, but 
should be mentioned in commit message


On 30.10.2015 08:56, Xiao Guangrong wrote:
> There are three places use the some logic to get the page size on
> the file path or file fd
>
> This patch introduces qemu_file_get_page_size() to unify the code
>
> Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
> ---
>   include/qemu/osdep.h |  1 +
>   target-ppc/kvm.c     | 21 +++------------------
>   util/oslib-posix.c   | 16 ++++++++++++++++
>   util/oslib-win32.c   |  5 +++++
>   4 files changed, 25 insertions(+), 18 deletions(-)
>
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index b568424..d4dde02 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -302,4 +302,5 @@ int qemu_read_password(char *buf, int buf_size);
>    */
>   pid_t qemu_fork(Error **errp);
>   
> +size_t qemu_file_get_page_size(const char *mem_path);
>   #endif
> diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c
> index ac70f08..c661f1c 100644
> --- a/target-ppc/kvm.c
> +++ b/target-ppc/kvm.c
> @@ -308,28 +308,13 @@ static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
>   
>   static long gethugepagesize(const char *mem_path)
>   {
> -    struct statfs fs;
> -    int ret;
> -
> -    do {
> -        ret = statfs(mem_path, &fs);
> -    } while (ret != 0 && errno == EINTR);
> +    long size = qemu_file_get_page_size(mem_path);
>   
> -    if (ret != 0) {
> -        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
> -                strerror(errno));
> +    if (!size) {
>           exit(1);
>       }
>   
> -#define HUGETLBFS_MAGIC       0x958458f6
> -
> -    if (fs.f_type != HUGETLBFS_MAGIC) {
> -        /* Explicit mempath, but it's ordinary pages */
> -        return getpagesize();
> -    }
> -
> -    /* It's hugepage, return the huge page size */
> -    return fs.f_bsize;
> +    return size;
>   }
>   
>   static int find_max_supported_pagesize(Object *obj, void *opaque)
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 914cef5..ad94c5a 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -360,6 +360,22 @@ static size_t fd_getpagesize(int fd)
>       return getpagesize();
>   }
>   
> +size_t qemu_file_get_page_size(const char *path)
> +{
> +    size_t size = 0;
> +    int fd = qemu_open(path, O_RDONLY);
> +
> +    if (fd < 0) {
> +        fprintf(stderr, "Could not open %s.\n", path);
> +        goto exit;
> +    }
> +
> +    size = fd_getpagesize(fd);
> +    qemu_close(fd);
> +exit:
> +    return size;
> +}
> +
>   void os_mem_prealloc(int fd, char *area, size_t memory)
>   {
>       int ret;
> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> index 09f9e98..a18aa87 100644
> --- a/util/oslib-win32.c
> +++ b/util/oslib-win32.c
> @@ -462,6 +462,11 @@ size_t getpagesize(void)
>       return system_info.dwPageSize;
>   }
>   
> +size_t qemu_file_get_page_size(const char *path)
> +{
> +    return getpagesize();
> +}
> +
>   void os_mem_prealloc(int fd, char *area, size_t memory)
>   {
>       int i;
Eduardo Habkost Oct. 30, 2015, 3:54 p.m. UTC | #2
On Fri, Oct 30, 2015 at 01:56:01PM +0800, Xiao Guangrong wrote:
> There are three places use the some logic to get the page size on
> the file path or file fd
> 
> This patch introduces qemu_file_get_page_size() to unify the code
> 
> Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
[...]
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 914cef5..ad94c5a 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -360,6 +360,22 @@ static size_t fd_getpagesize(int fd)
>      return getpagesize();
>  }
>  
> +size_t qemu_file_get_page_size(const char *path)
> +{
> +    size_t size = 0;
> +    int fd = qemu_open(path, O_RDONLY);
> +
> +    if (fd < 0) {
> +        fprintf(stderr, "Could not open %s.\n", path);
> +        goto exit;

Have you considered using a Error** argument here?

> +    }
> +
> +    size = fd_getpagesize(fd);
> +    qemu_close(fd);
> +exit:
> +    return size;
> +}
> +
> diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c
> index ac70f08..c661f1c 100644
> --- a/target-ppc/kvm.c
> +++ b/target-ppc/kvm.c
> @@ -308,28 +308,13 @@ static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
>  
>  static long gethugepagesize(const char *mem_path)
>  {
> -    struct statfs fs;
> -    int ret;
> -
> -    do {
> -        ret = statfs(mem_path, &fs);
> -    } while (ret != 0 && errno == EINTR);
> +    long size = qemu_file_get_page_size(mem_path);
>  
> -    if (ret != 0) {
> -        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
> -                strerror(errno));
> +    if (!size) {
>          exit(1);
>      }
>  
> -#define HUGETLBFS_MAGIC       0x958458f6
> -
> -    if (fs.f_type != HUGETLBFS_MAGIC) {
> -        /* Explicit mempath, but it's ordinary pages */
> -        return getpagesize();
> -    }
> -
> -    /* It's hugepage, return the huge page size */
> -    return fs.f_bsize;
> +    return size;
>  }

Why are you changing target-ppc/kvm.c:gethugepagesize() to use the new
funtion, but not the copy at exec.c? To make it simpler, we could
eliminate both gethugepagesize() functions completely and replace them
with qemu_file_get_page_size() calls (maybe as part of this patch, maybe
in a separate patch, I'm not sure).
Xiao Guangrong Oct. 31, 2015, 7:26 a.m. UTC | #3
On 10/30/2015 09:26 PM, Vladimir Sementsov-Ogievskiy wrote:
> logic is changed:
>      in old version gethugepagesize on statfs error generates exit(1)
>      in new it returns getpagesize() in this case (through fd_getpagesize)
> (I think, fd_getpagesize should be fixed to handle error)

Indeed. I will let fd_getpagesize() return 0 if statfs is failed, then the caller
handle the error properly.

>
> also, in new version for windows we have getpagesize(), when in old version there was no difference
> (how did it work?). May be it's ok, but should be mentioned in commit message

Windows did not support file hugepage, so it will return normal page for this
case. And this interface has not been used on windows so far.

I will document it in the commit message as your suggestion.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Xiao Guangrong Oct. 31, 2015, 8:09 a.m. UTC | #4
On 10/30/2015 11:54 PM, Eduardo Habkost wrote:
> On Fri, Oct 30, 2015 at 01:56:01PM +0800, Xiao Guangrong wrote:
>> There are three places use the some logic to get the page size on
>> the file path or file fd
>>
>> This patch introduces qemu_file_get_page_size() to unify the code
>>
>> Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
> [...]
>> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
>> index 914cef5..ad94c5a 100644
>> --- a/util/oslib-posix.c
>> +++ b/util/oslib-posix.c
>> @@ -360,6 +360,22 @@ static size_t fd_getpagesize(int fd)
>>       return getpagesize();
>>   }
>>
>> +size_t qemu_file_get_page_size(const char *path)
>> +{
>> +    size_t size = 0;
>> +    int fd = qemu_open(path, O_RDONLY);
>> +
>> +    if (fd < 0) {
>> +        fprintf(stderr, "Could not open %s.\n", path);
>> +        goto exit;
>
> Have you considered using a Error** argument here?
>
>> +    }
>> +
>> +    size = fd_getpagesize(fd);
>> +    qemu_close(fd);
>> +exit:
>> +    return size;
>> +}
>> +
>> diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c
>> index ac70f08..c661f1c 100644
>> --- a/target-ppc/kvm.c
>> +++ b/target-ppc/kvm.c
>> @@ -308,28 +308,13 @@ static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
>>
>>   static long gethugepagesize(const char *mem_path)
>>   {
>> -    struct statfs fs;
>> -    int ret;
>> -
>> -    do {
>> -        ret = statfs(mem_path, &fs);
>> -    } while (ret != 0 && errno == EINTR);
>> +    long size = qemu_file_get_page_size(mem_path);
>>
>> -    if (ret != 0) {
>> -        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
>> -                strerror(errno));
>> +    if (!size) {
>>           exit(1);
>>       }
>>
>> -#define HUGETLBFS_MAGIC       0x958458f6
>> -
>> -    if (fs.f_type != HUGETLBFS_MAGIC) {
>> -        /* Explicit mempath, but it's ordinary pages */
>> -        return getpagesize();
>> -    }
>> -
>> -    /* It's hugepage, return the huge page size */
>> -    return fs.f_bsize;
>> +    return size;
>>   }
>
> Why are you changing target-ppc/kvm.c:gethugepagesize() to use the new
> funtion, but not the copy at exec.c? To make it simpler, we could
> eliminate both gethugepagesize() functions completely and replace them
> with qemu_file_get_page_size() calls (maybe as part of this patch, maybe
> in a separate patch, I'm not sure).
>

The gethugepagesize() in exec.c will be eliminated in later patch :).

And the gethugepagesize() in ppc platform has error handling logic
and has multiple caller. It's not so bad to keep it.


--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Xiao Guangrong Oct. 31, 2015, 8:25 a.m. UTC | #5
On 10/30/2015 11:54 PM, Eduardo Habkost wrote:
> On Fri, Oct 30, 2015 at 01:56:01PM +0800, Xiao Guangrong wrote:
>> There are three places use the some logic to get the page size on
>> the file path or file fd
>>
>> This patch introduces qemu_file_get_page_size() to unify the code
>>
>> Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
> [...]
>> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
>> index 914cef5..ad94c5a 100644
>> --- a/util/oslib-posix.c
>> +++ b/util/oslib-posix.c
>> @@ -360,6 +360,22 @@ static size_t fd_getpagesize(int fd)
>>       return getpagesize();
>>   }
>>
>> +size_t qemu_file_get_page_size(const char *path)
>> +{
>> +    size_t size = 0;
>> +    int fd = qemu_open(path, O_RDONLY);
>> +
>> +    if (fd < 0) {
>> +        fprintf(stderr, "Could not open %s.\n", path);
>> +        goto exit;
>
> Have you considered using a Error** argument here?

No.

But it looks it a good way to detect error by check if Error
is NULL. Will use it. :)

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Vladimir Sementsov-Ogievskiy Oct. 31, 2015, 9:37 a.m. UTC | #6
On 31.10.2015 10:26, Xiao Guangrong wrote:
>
>
> On 10/30/2015 09:26 PM, Vladimir Sementsov-Ogievskiy wrote:
>> logic is changed:
>>      in old version gethugepagesize on statfs error generates exit(1)
>>      in new it returns getpagesize() in this case (through 
>> fd_getpagesize)
>> (I think, fd_getpagesize should be fixed to handle error)
>
> Indeed. I will let fd_getpagesize() return 0 if statfs is failed, then 
> the caller
> handle the error properly.

Why not use classic error codes < 0? Just change size_t to ssize_t and 
return exactly fstatfs return value.

>
>>
>> also, in new version for windows we have getpagesize(), when in old 
>> version there was no difference
>> (how did it work?). May be it's ok, but should be mentioned in commit 
>> message
>
> Windows did not support file hugepage, so it will return normal page 
> for this
> case. And this interface has not been used on windows so far.
>
> I will document it in the commit message as your suggestion.
Xiao Guangrong Oct. 31, 2015, 2:06 p.m. UTC | #7
On 10/31/2015 05:37 PM, Vladimir Sementsov-Ogievskiy wrote:
> On 31.10.2015 10:26, Xiao Guangrong wrote:
>>
>>
>> On 10/30/2015 09:26 PM, Vladimir Sementsov-Ogievskiy wrote:
>>> logic is changed:
>>>      in old version gethugepagesize on statfs error generates exit(1)
>>>      in new it returns getpagesize() in this case (through fd_getpagesize)
>>> (I think, fd_getpagesize should be fixed to handle error)
>>
>> Indeed. I will let fd_getpagesize() return 0 if statfs is failed, then the caller
>> handle the error properly.
>
> Why not use classic error codes < 0? Just change size_t to ssize_t and return exactly fstatfs return
> value.

Yup, it's better, however, i am planning to introduce a Error parameter and error
condition can be checked by if Error is NULL as Eduardo suggested.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Eduardo Habkost Oct. 31, 2015, 2:11 p.m. UTC | #8
On Sat, Oct 31, 2015 at 04:09:56PM +0800, Xiao Guangrong wrote:
> On 10/30/2015 11:54 PM, Eduardo Habkost wrote:
> >On Fri, Oct 30, 2015 at 01:56:01PM +0800, Xiao Guangrong wrote:
> >>There are three places use the some logic to get the page size on
> >>the file path or file fd
> >>
> >>This patch introduces qemu_file_get_page_size() to unify the code
> >>
> >>Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
[...]
> >>diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c
> >>index ac70f08..c661f1c 100644
> >>--- a/target-ppc/kvm.c
> >>+++ b/target-ppc/kvm.c
> >>@@ -308,28 +308,13 @@ static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
> >>
> >>  static long gethugepagesize(const char *mem_path)
> >>  {
> >>-    struct statfs fs;
> >>-    int ret;
> >>-
> >>-    do {
> >>-        ret = statfs(mem_path, &fs);
> >>-    } while (ret != 0 && errno == EINTR);
> >>+    long size = qemu_file_get_page_size(mem_path);
> >>
> >>-    if (ret != 0) {
> >>-        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
> >>-                strerror(errno));
> >>+    if (!size) {
> >>          exit(1);
> >>      }
> >>
> >>-#define HUGETLBFS_MAGIC       0x958458f6
> >>-
> >>-    if (fs.f_type != HUGETLBFS_MAGIC) {
> >>-        /* Explicit mempath, but it's ordinary pages */
> >>-        return getpagesize();
> >>-    }
> >>-
> >>-    /* It's hugepage, return the huge page size */
> >>-    return fs.f_bsize;
> >>+    return size;
> >>  }
> >
> >Why are you changing target-ppc/kvm.c:gethugepagesize() to use the new
> >funtion, but not the copy at exec.c? To make it simpler, we could
> >eliminate both gethugepagesize() functions completely and replace them
> >with qemu_file_get_page_size() calls (maybe as part of this patch, maybe
> >in a separate patch, I'm not sure).
> >
> 
> The gethugepagesize() in exec.c will be eliminated in later patch :).

That's true. I was just expecting to see the change that eliminates
gethugepagesize() in exec.c here instead of patch 08/33. Simple cleanups
that just eliminate code duplication without change in behavior are
easier to review and more likely to be included before the rest of the
series.


> 
> And the gethugepagesize() in ppc platform has error handling logic
> and has multiple caller. It's not so bad to keep it.

Well, in case there's still room for cleanup in the ppc code, it may be
done later. No problem.
Xiao Guangrong Oct. 31, 2015, 5:03 p.m. UTC | #9
On 10/31/2015 10:11 PM, Eduardo Habkost wrote:
> On Sat, Oct 31, 2015 at 04:09:56PM +0800, Xiao Guangrong wrote:
>> On 10/30/2015 11:54 PM, Eduardo Habkost wrote:
>>> On Fri, Oct 30, 2015 at 01:56:01PM +0800, Xiao Guangrong wrote:
>>>> There are three places use the some logic to get the page size on
>>>> the file path or file fd
>>>>
>>>> This patch introduces qemu_file_get_page_size() to unify the code
>>>>
>>>> Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
> [...]
>>>> diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c
>>>> index ac70f08..c661f1c 100644
>>>> --- a/target-ppc/kvm.c
>>>> +++ b/target-ppc/kvm.c
>>>> @@ -308,28 +308,13 @@ static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
>>>>
>>>>   static long gethugepagesize(const char *mem_path)
>>>>   {
>>>> -    struct statfs fs;
>>>> -    int ret;
>>>> -
>>>> -    do {
>>>> -        ret = statfs(mem_path, &fs);
>>>> -    } while (ret != 0 && errno == EINTR);
>>>> +    long size = qemu_file_get_page_size(mem_path);
>>>>
>>>> -    if (ret != 0) {
>>>> -        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
>>>> -                strerror(errno));
>>>> +    if (!size) {
>>>>           exit(1);
>>>>       }
>>>>
>>>> -#define HUGETLBFS_MAGIC       0x958458f6
>>>> -
>>>> -    if (fs.f_type != HUGETLBFS_MAGIC) {
>>>> -        /* Explicit mempath, but it's ordinary pages */
>>>> -        return getpagesize();
>>>> -    }
>>>> -
>>>> -    /* It's hugepage, return the huge page size */
>>>> -    return fs.f_bsize;
>>>> +    return size;
>>>>   }
>>>
>>> Why are you changing target-ppc/kvm.c:gethugepagesize() to use the new
>>> funtion, but not the copy at exec.c? To make it simpler, we could
>>> eliminate both gethugepagesize() functions completely and replace them
>>> with qemu_file_get_page_size() calls (maybe as part of this patch, maybe
>>> in a separate patch, I'm not sure).
>>>
>>
>> The gethugepagesize() in exec.c will be eliminated in later patch :).
>
> That's true. I was just expecting to see the change that eliminates
> gethugepagesize() in exec.c here instead of patch 08/33. Simple cleanups
> that just eliminate code duplication without change in behavior are
> easier to review and more likely to be included before the rest of the
> series.

Completely agree, will move the replacement from 08/33 to here in
the next version.

>
>
>>
>> And the gethugepagesize() in ppc platform has error handling logic
>> and has multiple caller. It's not so bad to keep it.
>
> Well, in case there's still room for cleanup in the ppc code, it may be
> done later. No problem.
>

Thank you, Eduardo!

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Michael S. Tsirkin Nov. 9, 2015, 10:33 a.m. UTC | #10
On Fri, Oct 30, 2015 at 01:56:01PM +0800, Xiao Guangrong wrote:
> There are three places use the some logic to get the page size on
> the file path or file fd
> 
> This patch introduces qemu_file_get_page_size() to unify the code
> 
> Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
> ---
>  include/qemu/osdep.h |  1 +
>  target-ppc/kvm.c     | 21 +++------------------
>  util/oslib-posix.c   | 16 ++++++++++++++++
>  util/oslib-win32.c   |  5 +++++
>  4 files changed, 25 insertions(+), 18 deletions(-)
> 
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index b568424..d4dde02 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -302,4 +302,5 @@ int qemu_read_password(char *buf, int buf_size);
>   */
>  pid_t qemu_fork(Error **errp);
>  
> +size_t qemu_file_get_page_size(const char *mem_path);
>  #endif
> diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c
> index ac70f08..c661f1c 100644
> --- a/target-ppc/kvm.c
> +++ b/target-ppc/kvm.c
> @@ -308,28 +308,13 @@ static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
>  
>  static long gethugepagesize(const char *mem_path)
>  {
> -    struct statfs fs;
> -    int ret;
> -
> -    do {
> -        ret = statfs(mem_path, &fs);
> -    } while (ret != 0 && errno == EINTR);
> +    long size = qemu_file_get_page_size(mem_path);
>  
> -    if (ret != 0) {
> -        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
> -                strerror(errno));
> +    if (!size) {
>          exit(1);
>      }
>  
> -#define HUGETLBFS_MAGIC       0x958458f6
> -
> -    if (fs.f_type != HUGETLBFS_MAGIC) {
> -        /* Explicit mempath, but it's ordinary pages */
> -        return getpagesize();
> -    }
> -
> -    /* It's hugepage, return the huge page size */
> -    return fs.f_bsize;
> +    return size;
>  }
>  
>  static int find_max_supported_pagesize(Object *obj, void *opaque)
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 914cef5..ad94c5a 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -360,6 +360,22 @@ static size_t fd_getpagesize(int fd)
>      return getpagesize();
>  }
>  
> +size_t qemu_file_get_page_size(const char *path)
> +{
> +    size_t size = 0;
> +    int fd = qemu_open(path, O_RDONLY);
> +
> +    if (fd < 0) {
> +        fprintf(stderr, "Could not open %s.\n", path);
> +        goto exit;
> +    }
> +
> +    size = fd_getpagesize(fd);
> +    qemu_close(fd);
> +exit:
> +    return size;
> +}
> +
>  void os_mem_prealloc(int fd, char *area, size_t memory)
>  {
>      int ret;

So this is opening the file for the sole purpose of
doing the fstatfs on it. Seems strange, just do statfs instead.
In fact, maybe we want statfs_getpagesize.

> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> index 09f9e98..a18aa87 100644
> --- a/util/oslib-win32.c
> +++ b/util/oslib-win32.c
> @@ -462,6 +462,11 @@ size_t getpagesize(void)
>      return system_info.dwPageSize;
>  }
>  
> +size_t qemu_file_get_page_size(const char *path)
> +{
> +    return getpagesize();
> +}
> +
>  void os_mem_prealloc(int fd, char *area, size_t memory)
>  {
>      int i;

And why is this needed on win32?

> -- 
> 1.8.3.1
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Xiao Guangrong Nov. 9, 2015, 10:55 a.m. UTC | #11
On 11/09/2015 06:33 PM, Michael S. Tsirkin wrote:
> On Fri, Oct 30, 2015 at 01:56:01PM +0800, Xiao Guangrong wrote:
>> There are three places use the some logic to get the page size on
>> the file path or file fd
>>
>> This patch introduces qemu_file_get_page_size() to unify the code
>>
>> Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
>> ---
>>   include/qemu/osdep.h |  1 +
>>   target-ppc/kvm.c     | 21 +++------------------
>>   util/oslib-posix.c   | 16 ++++++++++++++++
>>   util/oslib-win32.c   |  5 +++++
>>   4 files changed, 25 insertions(+), 18 deletions(-)
>>
>> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
>> index b568424..d4dde02 100644
>> --- a/include/qemu/osdep.h
>> +++ b/include/qemu/osdep.h
>> @@ -302,4 +302,5 @@ int qemu_read_password(char *buf, int buf_size);
>>    */
>>   pid_t qemu_fork(Error **errp);
>>
>> +size_t qemu_file_get_page_size(const char *mem_path);
>>   #endif
>> diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c
>> index ac70f08..c661f1c 100644
>> --- a/target-ppc/kvm.c
>> +++ b/target-ppc/kvm.c
>> @@ -308,28 +308,13 @@ static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
>>
>>   static long gethugepagesize(const char *mem_path)
>>   {
>> -    struct statfs fs;
>> -    int ret;
>> -
>> -    do {
>> -        ret = statfs(mem_path, &fs);
>> -    } while (ret != 0 && errno == EINTR);
>> +    long size = qemu_file_get_page_size(mem_path);
>>
>> -    if (ret != 0) {
>> -        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
>> -                strerror(errno));
>> +    if (!size) {
>>           exit(1);
>>       }
>>
>> -#define HUGETLBFS_MAGIC       0x958458f6
>> -
>> -    if (fs.f_type != HUGETLBFS_MAGIC) {
>> -        /* Explicit mempath, but it's ordinary pages */
>> -        return getpagesize();
>> -    }
>> -
>> -    /* It's hugepage, return the huge page size */
>> -    return fs.f_bsize;
>> +    return size;
>>   }
>>
>>   static int find_max_supported_pagesize(Object *obj, void *opaque)
>> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
>> index 914cef5..ad94c5a 100644
>> --- a/util/oslib-posix.c
>> +++ b/util/oslib-posix.c
>> @@ -360,6 +360,22 @@ static size_t fd_getpagesize(int fd)
>>       return getpagesize();
>>   }
>>
>> +size_t qemu_file_get_page_size(const char *path)
>> +{
>> +    size_t size = 0;
>> +    int fd = qemu_open(path, O_RDONLY);
>> +
>> +    if (fd < 0) {
>> +        fprintf(stderr, "Could not open %s.\n", path);
>> +        goto exit;
>> +    }
>> +
>> +    size = fd_getpagesize(fd);
>> +    qemu_close(fd);
>> +exit:
>> +    return size;
>> +}
>> +
>>   void os_mem_prealloc(int fd, char *area, size_t memory)
>>   {
>>       int ret;
>
> So this is opening the file for the sole purpose of
> doing the fstatfs on it. Seems strange, just do statfs instead.
> In fact, maybe we want statfs_getpagesize.

It is just to reuse the code of fd_getpagesize() which already has
the logic to check pagesize.

>
>> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
>> index 09f9e98..a18aa87 100644
>> --- a/util/oslib-win32.c
>> +++ b/util/oslib-win32.c
>> @@ -462,6 +462,11 @@ size_t getpagesize(void)
>>       return system_info.dwPageSize;
>>   }
>>
>> +size_t qemu_file_get_page_size(const char *path)
>> +{
>> +    return getpagesize();
>> +}
>> +
>>   void os_mem_prealloc(int fd, char *area, size_t memory)
>>   {
>>       int i;
>
> And why is this needed on win32?

It is not actually used, just make osdep.h happy which has
qemu_file_get_page_size() declare.

BTW, i will drop this patch for now on.

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Michael S. Tsirkin Nov. 9, 2015, 8 p.m. UTC | #12
On Sat, Oct 31, 2015 at 04:09:56PM +0800, Xiao Guangrong wrote:
> 
> 
> On 10/30/2015 11:54 PM, Eduardo Habkost wrote:
> >On Fri, Oct 30, 2015 at 01:56:01PM +0800, Xiao Guangrong wrote:
> >>There are three places use the some logic to get the page size on
> >>the file path or file fd
> >>
> >>This patch introduces qemu_file_get_page_size() to unify the code
> >>
> >>Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
> >[...]
> >>diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> >>index 914cef5..ad94c5a 100644
> >>--- a/util/oslib-posix.c
> >>+++ b/util/oslib-posix.c
> >>@@ -360,6 +360,22 @@ static size_t fd_getpagesize(int fd)
> >>      return getpagesize();
> >>  }
> >>
> >>+size_t qemu_file_get_page_size(const char *path)
> >>+{
> >>+    size_t size = 0;
> >>+    int fd = qemu_open(path, O_RDONLY);
> >>+
> >>+    if (fd < 0) {
> >>+        fprintf(stderr, "Could not open %s.\n", path);
> >>+        goto exit;
> >
> >Have you considered using a Error** argument here?
> >
> >>+    }
> >>+
> >>+    size = fd_getpagesize(fd);
> >>+    qemu_close(fd);
> >>+exit:
> >>+    return size;
> >>+}
> >>+
> >>diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c
> >>index ac70f08..c661f1c 100644
> >>--- a/target-ppc/kvm.c
> >>+++ b/target-ppc/kvm.c
> >>@@ -308,28 +308,13 @@ static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
> >>
> >>  static long gethugepagesize(const char *mem_path)
> >>  {
> >>-    struct statfs fs;
> >>-    int ret;
> >>-
> >>-    do {
> >>-        ret = statfs(mem_path, &fs);
> >>-    } while (ret != 0 && errno == EINTR);
> >>+    long size = qemu_file_get_page_size(mem_path);
> >>
> >>-    if (ret != 0) {
> >>-        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
> >>-                strerror(errno));
> >>+    if (!size) {
> >>          exit(1);
> >>      }
> >>
> >>-#define HUGETLBFS_MAGIC       0x958458f6
> >>-
> >>-    if (fs.f_type != HUGETLBFS_MAGIC) {
> >>-        /* Explicit mempath, but it's ordinary pages */
> >>-        return getpagesize();
> >>-    }
> >>-
> >>-    /* It's hugepage, return the huge page size */
> >>-    return fs.f_bsize;
> >>+    return size;
> >>  }
> >
> >Why are you changing target-ppc/kvm.c:gethugepagesize() to use the new
> >funtion, but not the copy at exec.c? To make it simpler, we could
> >eliminate both gethugepagesize() functions completely and replace them
> >with qemu_file_get_page_size() calls (maybe as part of this patch, maybe
> >in a separate patch, I'm not sure).
> >
> 
> The gethugepagesize() in exec.c will be eliminated in later patch :).

That's why it's not a good idea to split patchset like this,
where patch 1 adds a new function, patch 2 uses it.
It's better if user is in the same patchset.
An exception if when a completely separate group of people
should review the function and the usage,
e.g. some logic in memory core versus caller in acpi.


> And the gethugepagesize() in ppc platform has error handling logic
> and has multiple caller. It's not so bad to keep it.
>

Patch
diff mbox

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index b568424..d4dde02 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -302,4 +302,5 @@  int qemu_read_password(char *buf, int buf_size);
  */
 pid_t qemu_fork(Error **errp);
 
+size_t qemu_file_get_page_size(const char *mem_path);
 #endif
diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c
index ac70f08..c661f1c 100644
--- a/target-ppc/kvm.c
+++ b/target-ppc/kvm.c
@@ -308,28 +308,13 @@  static void kvm_get_smmu_info(PowerPCCPU *cpu, struct kvm_ppc_smmu_info *info)
 
 static long gethugepagesize(const char *mem_path)
 {
-    struct statfs fs;
-    int ret;
-
-    do {
-        ret = statfs(mem_path, &fs);
-    } while (ret != 0 && errno == EINTR);
+    long size = qemu_file_get_page_size(mem_path);
 
-    if (ret != 0) {
-        fprintf(stderr, "Couldn't statfs() memory path: %s\n",
-                strerror(errno));
+    if (!size) {
         exit(1);
     }
 
-#define HUGETLBFS_MAGIC       0x958458f6
-
-    if (fs.f_type != HUGETLBFS_MAGIC) {
-        /* Explicit mempath, but it's ordinary pages */
-        return getpagesize();
-    }
-
-    /* It's hugepage, return the huge page size */
-    return fs.f_bsize;
+    return size;
 }
 
 static int find_max_supported_pagesize(Object *obj, void *opaque)
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 914cef5..ad94c5a 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -360,6 +360,22 @@  static size_t fd_getpagesize(int fd)
     return getpagesize();
 }
 
+size_t qemu_file_get_page_size(const char *path)
+{
+    size_t size = 0;
+    int fd = qemu_open(path, O_RDONLY);
+
+    if (fd < 0) {
+        fprintf(stderr, "Could not open %s.\n", path);
+        goto exit;
+    }
+
+    size = fd_getpagesize(fd);
+    qemu_close(fd);
+exit:
+    return size;
+}
+
 void os_mem_prealloc(int fd, char *area, size_t memory)
 {
     int ret;
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 09f9e98..a18aa87 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -462,6 +462,11 @@  size_t getpagesize(void)
     return system_info.dwPageSize;
 }
 
+size_t qemu_file_get_page_size(const char *path)
+{
+    return getpagesize();
+}
+
 void os_mem_prealloc(int fd, char *area, size_t memory)
 {
     int i;