From nobody Wed Nov 5 15:53:25 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 14967340877621008.6562105003119; Tue, 6 Jun 2017 00:28:07 -0700 (PDT) Received: from localhost ([::1]:36581 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI8uL-00050P-5l for importer@patchew.org; Tue, 06 Jun 2017 03:28:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38873) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI8pL-0000mi-Q1 for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dI8pH-0005KX-2K for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:55 -0400 Received: from mga03.intel.com ([134.134.136.65]:58638) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dI8pG-0005K6-Qh for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:51 -0400 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Jun 2017 00:22:49 -0700 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.149]) by fmsmga006.fm.intel.com with ESMTP; 06 Jun 2017 00:22:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.39,305,1493708400"; d="scan'208";a="111383286" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Tue, 6 Jun 2017 15:22:26 +0800 Message-Id: <20170606072229.9302-2-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170606072229.9302-1-haozhong.zhang@intel.com> References: <20170606072229.9302-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.65 Subject: [Qemu-devel] [PATCH v2 1/4] nvdimm: add a macro for property "label-size" X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , "Michael S. Tsirkin" , Stefan Hajnoczi , Xiao Guangrong , Igor Mammedov , Dan Williams Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Haozhong Zhang Reviewed-by: Stefan Hajnoczi --- hw/mem/nvdimm.c | 2 +- include/hw/mem/nvdimm.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/hw/mem/nvdimm.c b/hw/mem/nvdimm.c index db896b0bb6..a9b0863f20 100644 --- a/hw/mem/nvdimm.c +++ b/hw/mem/nvdimm.c @@ -66,7 +66,7 @@ out: =20 static void nvdimm_init(Object *obj) { - object_property_add(obj, "label-size", "int", + object_property_add(obj, NVDIMM_LABEL_SIZE_PROP, "int", nvdimm_get_label_size, nvdimm_set_label_size, NULL, NULL, NULL); } diff --git a/include/hw/mem/nvdimm.h b/include/hw/mem/nvdimm.h index 03e1ff9558..f1f3987055 100644 --- a/include/hw/mem/nvdimm.h +++ b/include/hw/mem/nvdimm.h @@ -47,6 +47,9 @@ #define NVDIMM_CLASS(oc) OBJECT_CLASS_CHECK(NVDIMMClass, (oc), TYPE_NVDIMM) #define NVDIMM_GET_CLASS(obj) OBJECT_GET_CLASS(NVDIMMClass, (obj), \ TYPE_NVDIMM) + +#define NVDIMM_LABEL_SIZE_PROP "label-size" + struct NVDIMMDevice { /* private */ PCDIMMDevice parent_obj; --=20 2.11.0 From nobody Wed Nov 5 15:53:25 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149673393357039.30136609555336; Tue, 6 Jun 2017 00:25:33 -0700 (PDT) Received: from localhost ([::1]:36574 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI8rr-0002lF-7L for importer@patchew.org; Tue, 06 Jun 2017 03:25:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38876) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI8pL-0000mp-Ur for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dI8pH-0005L2-UU for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:55 -0400 Received: from mga03.intel.com ([134.134.136.65]:58638) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dI8pH-0005K6-Jd for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:51 -0400 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Jun 2017 00:22:51 -0700 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.149]) by fmsmga006.fm.intel.com with ESMTP; 06 Jun 2017 00:22:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.39,305,1493708400"; d="scan'208";a="111383295" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Tue, 6 Jun 2017 15:22:27 +0800 Message-Id: <20170606072229.9302-3-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170606072229.9302-1-haozhong.zhang@intel.com> References: <20170606072229.9302-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.65 Subject: [Qemu-devel] [PATCH v2 2/4] nvdimm: warn if the backend is not a DAX device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , "Michael S. Tsirkin" , Stefan Hajnoczi , Xiao Guangrong , Igor Mammedov , Dan Williams Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Applications in Linux guest that use device-dax never trigger flush that can be trapped by KVM/QEMU. Meanwhile, if the host backend is not device-dax, QEMU cannot guarantee the persistence of guest writes. Before solving this flushing problem, QEMU should warn users if the host backend is not device-dax. Signed-off-by: Haozhong Zhang Message-id: CAPcyv4hV2-ZW8SMCRtD0P_86KgR3DHOvNe+6T5SY2u7wXg3gEg@mail.gmail.= com --- hw/mem/nvdimm.c | 6 ++++++ include/qemu/osdep.h | 9 ++++++++ util/osdep.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 76 insertions(+) diff --git a/hw/mem/nvdimm.c b/hw/mem/nvdimm.c index a9b0863f20..b23542fbdf 100644 --- a/hw/mem/nvdimm.c +++ b/hw/mem/nvdimm.c @@ -26,6 +26,7 @@ #include "qapi/error.h" #include "qapi/visitor.h" #include "hw/mem/nvdimm.h" +#include "qemu/error-report.h" =20 static void nvdimm_get_label_size(Object *obj, Visitor *v, const char *nam= e, void *opaque, Error **errp) @@ -84,6 +85,11 @@ static void nvdimm_realize(PCDIMMDevice *dimm, Error **e= rrp) NVDIMMDevice *nvdimm =3D NVDIMM(dimm); uint64_t align, pmem_size, size =3D memory_region_size(mr); =20 + if (!qemu_fd_is_dev_dax(memory_region_get_fd(mr))) { + error_report("warning: nvdimm backend does not look like a DAX dev= ice, " + "unable to guarantee persistence of guest writes"); + } + align =3D memory_region_get_alignment(mr); =20 pmem_size =3D size - nvdimm->label_size; diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 1c9f5e260c..7f26af371e 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -470,4 +470,13 @@ char *qemu_get_pid_name(pid_t pid); */ pid_t qemu_fork(Error **errp); =20 +/** + * qemu_fd_is_dev_dax: + * + * Check whether @fd describes a DAX device. + * + * Returns true if it is; otherwise, return false. + */ +bool qemu_fd_is_dev_dax(int fd); + #endif diff --git a/util/osdep.c b/util/osdep.c index a2863c8e53..02881f96bc 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -471,3 +471,64 @@ writev(int fd, const struct iovec *iov, int iov_cnt) return readv_writev(fd, iov, iov_cnt, true); } #endif + +#ifdef __linux__ +static ssize_t qemu_dev_dax_sysfs_read(int fd, const char *entry, + char *buf, size_t len) +{ + ssize_t read_bytes; + struct stat st; + unsigned int major, minor; + char *path, *pos; + int sysfs_fd; + + if (fstat(fd, &st)) { + return 0; + } + + major =3D major(st.st_rdev); + minor =3D minor(st.st_rdev); + path =3D g_strdup_printf("/sys/dev/char/%u:%u/%s", major, minor, entry= ); + + sysfs_fd =3D open(path, O_RDONLY); + g_free(path); + if (sysfs_fd =3D=3D -1) { + return 0; + } + + read_bytes =3D read(sysfs_fd, buf, len - 1); + close(sysfs_fd); + if (read_bytes > 0) { + buf[read_bytes] =3D '\0'; + pos =3D g_strstr_len(buf, read_bytes, "\n"); + if (pos) { + *pos =3D '\0'; + } + } + + return read_bytes; +} +#endif /* __linux__ */ + +bool qemu_fd_is_dev_dax(int fd) +{ + bool is_dax =3D false; + +#ifdef __linux__ + char devtype[7]; + ssize_t len; + + if (fd =3D=3D -1) { + return false; + } + + len =3D qemu_dev_dax_sysfs_read(fd, "device/devtype", + devtype, sizeof(devtype)); + if (len <=3D 0) { + return false; + } + is_dax =3D !strncmp(devtype, "nd_dax", len); +#endif /* __linux__ */ + + return is_dax; +} --=20 2.11.0 From nobody Wed Nov 5 15:53:25 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496733924385394.34496499692193; Tue, 6 Jun 2017 00:25:24 -0700 (PDT) Received: from localhost ([::1]:36569 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI8ri-0002bK-N0 for importer@patchew.org; Tue, 06 Jun 2017 03:25:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38878) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI8pL-0000mr-Vp for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dI8pK-0005LQ-02 for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:55 -0400 Received: from mga03.intel.com ([134.134.136.65]:58638) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dI8pJ-0005K6-KL for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:53 -0400 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Jun 2017 00:22:53 -0700 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.149]) by fmsmga006.fm.intel.com with ESMTP; 06 Jun 2017 00:22:51 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.39,305,1493708400"; d="scan'208";a="111383309" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Tue, 6 Jun 2017 15:22:28 +0800 Message-Id: <20170606072229.9302-4-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170606072229.9302-1-haozhong.zhang@intel.com> References: <20170606072229.9302-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.65 Subject: [Qemu-devel] [PATCH v2 3/4] nvdimm: add a boolean option "restrict" X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , "Michael S. Tsirkin" , Stefan Hajnoczi , Xiao Guangrong , Igor Mammedov , Dan Williams Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If a vNVDIMM device is not backed by a DAX device and its "restrict" option is enabled, bit 3 of state flags in its region mapping structure will be set, in order to notify the guest of the lack of write persistence guarantee. Once this bit is set, the guest OS may mark the vNVDIMM device as read-only. This option is disabled by default for backwards compatibility. It's recommended to enable for the formal usage. Signed-off-by: Haozhong Zhang --- hw/acpi/nvdimm.c | 16 ++++++++++++++++ hw/mem/nvdimm.c | 38 +++++++++++++++++++++++++++++++++++++- include/hw/mem/nvdimm.h | 5 +++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/hw/acpi/nvdimm.c b/hw/acpi/nvdimm.c index 8e7d6ec034..fd1ef6dc65 100644 --- a/hw/acpi/nvdimm.c +++ b/hw/acpi/nvdimm.c @@ -138,6 +138,8 @@ struct NvdimmNfitMemDev { } QEMU_PACKED; typedef struct NvdimmNfitMemDev NvdimmNfitMemDev; =20 +#define ACPI_NFIT_MEM_NOT_ARMED (1 << 3) + /* * NVDIMM Control Region Structure * @@ -289,6 +291,10 @@ nvdimm_build_structure_memdev(GArray *structures, Devi= ceState *dev) int slot =3D object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, NULL); uint32_t handle =3D nvdimm_slot_to_handle(slot); + bool dev_dax =3D object_property_get_bool(OBJECT(dev), NVDIMM_DEV_DAX_= PROP, + NULL); + bool restrict_mode =3D object_property_get_bool(OBJECT(dev), + NVDIMM_RESTRICT_PROP, NU= LL); =20 nfit_memdev =3D acpi_data_push(structures, sizeof(*nfit_memdev)); =20 @@ -312,6 +318,16 @@ nvdimm_build_structure_memdev(GArray *structures, Devi= ceState *dev) =20 /* Only one interleave for PMEM. */ nfit_memdev->interleave_ways =3D cpu_to_le16(1); + + /* + * If a vNVDIMM device in the restrict mode and is not backed by a + * DAX device, QEMU will set ACPI_NFIT_MEM_NOT_ARMED bit of state + * flags in its region mapping structure, in order to notify the + * guest of the lack of write persistence guarantee. + */ + if (!dev_dax && restrict_mode) { + nfit_memdev->flags =3D cpu_to_le16(ACPI_NFIT_MEM_NOT_ARMED); + } } =20 /* diff --git a/hw/mem/nvdimm.c b/hw/mem/nvdimm.c index b23542fbdf..cda416e5c8 100644 --- a/hw/mem/nvdimm.c +++ b/hw/mem/nvdimm.c @@ -65,11 +65,46 @@ out: error_propagate(errp, local_err); } =20 +static bool nvdimm_get_backend_dev_dax(Object *obj, Error **errp) +{ + NVDIMMDevice *nvdimm =3D NVDIMM(obj); + + return nvdimm->backend_dev_dax; +} + +static bool nvdimm_get_restrict(Object *obj, Error **errp) +{ + NVDIMMDevice *nvdimm =3D NVDIMM(obj); + + return nvdimm->restrict_mode; +} + +static void nvdimm_set_restrict(Object *obj, bool val, Error **errp) +{ + DeviceState *dev =3D DEVICE(obj); + NVDIMMDevice *nvdimm =3D NVDIMM(obj); + Error *local_err =3D NULL; + + if (dev->realized) { + error_setg(&local_err, "cannot change property value"); + goto out; + } + + nvdimm->restrict_mode =3D val; + + out: + error_propagate(errp, local_err); +} + static void nvdimm_init(Object *obj) { object_property_add(obj, NVDIMM_LABEL_SIZE_PROP, "int", nvdimm_get_label_size, nvdimm_set_label_size, NULL, NULL, NULL); + object_property_add_bool(obj, NVDIMM_DEV_DAX_PROP, + nvdimm_get_backend_dev_dax, NULL, NULL); + object_property_add_bool(obj, NVDIMM_RESTRICT_PROP, + nvdimm_get_restrict, nvdimm_set_restrict, NUL= L); } =20 static MemoryRegion *nvdimm_get_memory_region(PCDIMMDevice *dimm) @@ -85,7 +120,8 @@ static void nvdimm_realize(PCDIMMDevice *dimm, Error **e= rrp) NVDIMMDevice *nvdimm =3D NVDIMM(dimm); uint64_t align, pmem_size, size =3D memory_region_size(mr); =20 - if (!qemu_fd_is_dev_dax(memory_region_get_fd(mr))) { + nvdimm->backend_dev_dax =3D qemu_fd_is_dev_dax(memory_region_get_fd(mr= )); + if (!nvdimm->backend_dev_dax) { error_report("warning: nvdimm backend does not look like a DAX dev= ice, " "unable to guarantee persistence of guest writes"); } diff --git a/include/hw/mem/nvdimm.h b/include/hw/mem/nvdimm.h index f1f3987055..2fbe0d7858 100644 --- a/include/hw/mem/nvdimm.h +++ b/include/hw/mem/nvdimm.h @@ -49,6 +49,8 @@ TYPE_NVDIMM) =20 #define NVDIMM_LABEL_SIZE_PROP "label-size" +#define NVDIMM_DEV_DAX_PROP "dev-dax" +#define NVDIMM_RESTRICT_PROP "restrict" =20 struct NVDIMMDevice { /* private */ @@ -74,6 +76,9 @@ struct NVDIMMDevice { * guest via ACPI NFIT and _FIT method if NVDIMM hotplug is supported. */ MemoryRegion nvdimm_mr; + + bool backend_dev_dax; + bool restrict_mode; }; typedef struct NVDIMMDevice NVDIMMDevice; =20 --=20 2.11.0 From nobody Wed Nov 5 15:53:25 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496733993112962.8244123425064; Tue, 6 Jun 2017 00:26:33 -0700 (PDT) Received: from localhost ([::1]:36577 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI8sp-0003aj-Ts for importer@patchew.org; Tue, 06 Jun 2017 03:26:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38906) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI8pM-0000nD-Ub for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dI8pL-0005NA-Se for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:56 -0400 Received: from mga03.intel.com ([134.134.136.65]:58638) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dI8pL-0005K6-Jx for qemu-devel@nongnu.org; Tue, 06 Jun 2017 03:22:55 -0400 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Jun 2017 00:22:55 -0700 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.149]) by fmsmga006.fm.intel.com with ESMTP; 06 Jun 2017 00:22:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.39,305,1493708400"; d="scan'208";a="111383321" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Tue, 6 Jun 2017 15:22:29 +0800 Message-Id: <20170606072229.9302-5-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170606072229.9302-1-haozhong.zhang@intel.com> References: <20170606072229.9302-1-haozhong.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.65 Subject: [Qemu-devel] [PATCH v2 4/4] util/mmap-alloc: account for DAX device in qemu_fd_getpagesize X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Haozhong Zhang , "Michael S. Tsirkin" , Stefan Hajnoczi , Xiao Guangrong , Igor Mammedov , Dan Williams Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" A DAX device may require a page size different than getpagesize(). Signed-off-by: Haozhong Zhang Reviewed-by: Stefan Hajnoczi --- include/qemu/osdep.h | 10 ++++++++++ util/mmap-alloc.c | 6 ++++++ util/osdep.c | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 7f26af371e..51aca88f0d 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -479,4 +479,14 @@ pid_t qemu_fork(Error **errp); */ bool qemu_fd_is_dev_dax(int fd); =20 +/** + * qemu_get_dev_dax_align: + * + * Get the suggested alignment of a DAX device descried by @fd. + * + * Return a non-zero suggested alignment on success; return 0 if @fd + * does not describe a DAX device, or if it fails to get alignment + * of the DAX device. + */ +size_t qemu_get_dev_dax_align(int fd); #endif diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 3ec029a9ea..2d8af64b44 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -25,8 +25,14 @@ size_t qemu_fd_getpagesize(int fd) #ifdef CONFIG_LINUX struct statfs fs; int ret; + size_t align; =20 if (fd !=3D -1) { + align =3D qemu_get_dev_dax_align(fd); + if (align) { + return align; + } + do { ret =3D fstatfs(fd, &fs); } while (ret !=3D 0 && errno =3D=3D EINTR); diff --git a/util/osdep.c b/util/osdep.c index 02881f96bc..e7837d30b4 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -532,3 +532,26 @@ bool qemu_fd_is_dev_dax(int fd) =20 return is_dax; } + +size_t qemu_get_dev_dax_align(int fd) +{ + size_t align =3D 0; + +#ifdef __linux__ + char buf[12]; + ssize_t len; + + if (!qemu_fd_is_dev_dax(fd)) { + return 0; + } + len =3D qemu_dev_dax_sysfs_read(fd, "device/align", buf, sizeof(buf)); + if (len <=3D 0) { + return 0; + } + if (qemu_strtosz(buf, NULL, &align) < 0) { + align =3D 0; + } +#endif /* __linux__ */ + + return align; +} --=20 2.11.0