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: 7534871 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0D0C89F327 for ; Mon, 2 Nov 2015 09:20:03 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 23E98205DD for ; Mon, 2 Nov 2015 09:20:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0912820591 for ; Mon, 2 Nov 2015 09:20:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753041AbbKBJTp (ORCPT ); Mon, 2 Nov 2015 04:19:45 -0500 Received: from mga02.intel.com ([134.134.136.20]:20659 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753425AbbKBJTl (ORCPT ); Mon, 2 Nov 2015 04:19:41 -0500 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP; 02 Nov 2015 01:19:41 -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 Cc: gleb@kernel.org, mtosatti@redhat.com, stefanha@redhat.com, mst@redhat.com, rth@twiddle.net, ehabkost@redhat.com, dan.j.williams@intel.com, kvm@vger.kernel.org, qemu-devel@nongnu.org, vsementsov@virtuozzo.com, eblake@redhat.com, Xiao Guangrong Subject: [PATCH v7 07/35] util: introduce qemu_file_get_page_size() 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> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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;