From patchwork Mon Nov 2 09:13:09 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Guangrong X-Patchwork-Id: 538908 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id E4DE8140E31 for ; Mon, 2 Nov 2015 20:24:37 +1100 (AEDT) Received: from localhost ([::1]:40859 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZtBLv-0006cS-Ou for incoming@patchwork.ozlabs.org; Mon, 02 Nov 2015 04:24:35 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56961) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZtBHE-0006wW-K1 for qemu-devel@nongnu.org; Mon, 02 Nov 2015 04:19:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZtBHB-0003l5-Uj for qemu-devel@nongnu.org; Mon, 02 Nov 2015 04:19:44 -0500 Received: from mga11.intel.com ([192.55.52.93]:24492) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZtBHB-0003jr-Ju for qemu-devel@nongnu.org; Mon, 02 Nov 2015 04:19:41 -0500 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP; 02 Nov 2015 01:19:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,234,1444719600"; d="scan'208";a="840963086" Received: from xiaoreal1.sh.intel.com (HELO xiaoreal1.sh.intel.com.sh.intel.com) ([10.239.48.79]) by fmsmga002.fm.intel.com with ESMTP; 02 Nov 2015 01:19:18 -0800 From: Xiao Guangrong To: pbonzini@redhat.com, imammedo@redhat.com Date: Mon, 2 Nov 2015 17:13:09 +0800 Message-Id: <1446455617-129562-8-git-send-email-guangrong.xiao@linux.intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1446455617-129562-1-git-send-email-guangrong.xiao@linux.intel.com> References: <1446455617-129562-1-git-send-email-guangrong.xiao@linux.intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 Cc: vsementsov@virtuozzo.com, Xiao Guangrong , ehabkost@redhat.com, kvm@vger.kernel.org, mst@redhat.com, gleb@kernel.org, mtosatti@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com, dan.j.williams@intel.com, rth@twiddle.net Subject: [Qemu-devel] [PATCH v7 07/35] util: introduce qemu_file_get_page_size() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org There are three places use the some logic to get the page size on the file path or file fd 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 This patch introduces qemu_file_get_page_size() to unify the code Signed-off-by: Xiao Guangrong Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Eduardo Habkost --- exec.c | 33 ++++++--------------------------- include/qemu/osdep.h | 1 + target-ppc/kvm.c | 23 +++++------------------ util/oslib-posix.c | 37 +++++++++++++++++++++++++++++++++---- util/oslib-win32.c | 5 +++++ 5 files changed, 50 insertions(+), 49 deletions(-) diff --git a/exec.c b/exec.c index 8af2570..9de38be 100644 --- a/exec.c +++ b/exec.c @@ -1174,32 +1174,6 @@ void qemu_mutex_unlock_ramlist(void) } #ifdef __linux__ - -#include - -#define HUGETLBFS_MAGIC 0x958458f6 - -static long gethugepagesize(const char *path, Error **errp) -{ - struct statfs fs; - int ret; - - do { - ret = statfs(path, &fs); - } while (ret != 0 && errno == EINTR); - - if (ret != 0) { - error_setg_errno(errp, errno, "failed to get page size of file %s", - path); - return 0; - } - - if (fs.f_type != HUGETLBFS_MAGIC) - fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path); - - return fs.f_bsize; -} - static void *file_ram_alloc(RAMBlock *block, ram_addr_t memory, const char *path, @@ -1213,11 +1187,16 @@ static void *file_ram_alloc(RAMBlock *block, uint64_t hpagesize; Error *local_err = NULL; - hpagesize = gethugepagesize(path, &local_err); + hpagesize = qemu_file_get_page_size(path, &local_err); if (local_err) { error_propagate(errp, local_err); goto error; } + + if (hpagesize == getpagesize()) { + fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path); + } + block->mr->align = hpagesize; if (memory < hpagesize) { diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index b568424..dbc17dc 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, Error **errp); #endif diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c index ac70f08..d8760ea 100644 --- a/target-ppc/kvm.c +++ b/target-ppc/kvm.c @@ -308,28 +308,15 @@ 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); + Error *local_err = NULL; + long size = qemu_file_get_page_size(mem_path, local_err); - if (ret != 0) { - fprintf(stderr, "Couldn't statfs() memory path: %s\n", - strerror(errno)); + if (local_err) { + error_report_err(local_err); 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..51437ff 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -340,7 +340,7 @@ static void sigbus_handler(int signal) siglongjmp(sigjump, 1); } -static size_t fd_getpagesize(int fd) +static size_t fd_getpagesize(int fd, Error **errp) { #ifdef CONFIG_LINUX struct statfs fs; @@ -351,7 +351,12 @@ static size_t fd_getpagesize(int fd) ret = fstatfs(fd, &fs); } while (ret != 0 && errno == EINTR); - if (ret == 0 && fs.f_type == HUGETLBFS_MAGIC) { + if (ret) { + error_setg_errno(errp, errno, "fstatfs is failed"); + return 0; + } + + if (fs.f_type == HUGETLBFS_MAGIC) { return fs.f_bsize; } } @@ -360,6 +365,22 @@ static size_t fd_getpagesize(int fd) return getpagesize(); } +size_t qemu_file_get_page_size(const char *path, Error **errp) +{ + size_t size = 0; + int fd = qemu_open(path, O_RDONLY); + + if (fd < 0) { + error_setg_file_open(errp, errno, path); + goto exit; + } + + size = fd_getpagesize(fd, errp); + qemu_close(fd); +exit: + return size; +} + void os_mem_prealloc(int fd, char *area, size_t memory) { int ret; @@ -387,8 +408,16 @@ void os_mem_prealloc(int fd, char *area, size_t memory) exit(1); } else { int i; - size_t hpagesize = fd_getpagesize(fd); - size_t numpages = DIV_ROUND_UP(memory, hpagesize); + Error *local_err = NULL; + size_t hpagesize = fd_getpagesize(fd, &local_err); + size_t numpages; + + if (local_err) { + error_report_err(local_err); + exit(1); + } + + numpages = DIV_ROUND_UP(memory, hpagesize); /* MAP_POPULATE silently ignores failures */ for (i = 0; i < numpages; i++) { diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 09f9e98..dada6b6 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, Error **errp) +{ + return getpagesize(); +} + void os_mem_prealloc(int fd, char *area, size_t memory) { int i;