From nobody Mon Apr 29 13:07:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585711129; cv=none; d=zohomail.com; s=zohoarc; b=at1eMBHtFPzbedQVzmqOZovGUHpK1+4S4UwIrtbdTs3oV5wgCyllaoyZeVlZHpUBN/zuTp6Tj3d7pePPe0fsDUlNKaTH0pwzx68NYb2hX6NHDbyzZRhHNOK1O5ypwYewSq2hVyPeMz9cdyg9YVDNYagQi3gStgT8Pa5lvXdWa0A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585711129; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=UnKHufbwdWjZnuXZRCuLy2gGWewYPZgqYL51LdWS8hE=; b=dtHNV2d5ysIKZDPcfULOgj3fZqaU5xHqSLzW/j4M0dt9mMoOOtspnneW/ComIYpMtKJqo2MdtPVPn0d05hMxE4RtQvH+OIWS0GQ3nIOG6ZWjFK9KYfNhpnRJ9kr1f9GKzdVwYTLvxckHts681FV9ksx5b2cjKrq7SPK0b48aQEQ= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1585711129069753.8513733281021; Tue, 31 Mar 2020 20:18:49 -0700 (PDT) Received: from localhost ([::1]:46458 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJTtv-0004jI-Dr for importer@patchew.org; Tue, 31 Mar 2020 23:18:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36471) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jJTsz-00040K-Q8 for qemu-devel@nongnu.org; Tue, 31 Mar 2020 23:17:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jJTsy-0000Jf-2s for qemu-devel@nongnu.org; Tue, 31 Mar 2020 23:17:49 -0400 Received: from mga06.intel.com ([134.134.136.31]:50506) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jJTsx-0008Ne-Qe for qemu-devel@nongnu.org; Tue, 31 Mar 2020 23:17:48 -0400 Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2020 20:17:42 -0700 Received: from dazhang1-mobile.sh.intel.com ([10.239.48.70]) by orsmga004.jf.intel.com with ESMTP; 31 Mar 2020 20:17:39 -0700 IronPort-SDR: i8VvI+NbUy4CaUMq6wZU5Glanqnk1IMbKFOpGK4fC7/HAGfdDrsCLY8oivid7lf+PwoZB7m/Ce i4fFXXfikyKg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: +oaTlhXYm2c1Ekhk104vd7EENYZ9C5YNd3ixfDIjjDl07qKb88kMr8ti4bosBrGJf39WwCUZYp AnfS/+gYXh6Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,330,1580803200"; d="scan'208";a="395798143" From: Jingqi Liu To: Paolo Bonzini , Richard Henderson Subject: [PATCH] exec: fetch the alignment of Linux devdax pmem character device nodes Date: Wed, 1 Apr 2020 11:13:14 +0800 Message-Id: <20200401031314.11592-1-jingqi.liu@intel.com> X-Mailer: git-send-email 2.17.1 X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.31 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jingqi Liu , Dan Williams , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If the backend file is devdax pmem character device, the alignment specified by the option 'align=3DNUM' in the '-object memory-backend-file' needs to match the alignment requirement of the devdax pmem character devic= e. This patch fetches the devdax pmem file 'align', so that we can compare it with the NUM of 'align=3DNUM'. The NUM needs to be larger than or equal to the devdax pmem file 'align'. It also fixes the problem that mmap() returns failure in qemu_ram_mmap() when the NUM of 'align=3DNUM' is less than the devdax pmem file 'align'. Cc: Dan Williams Signed-off-by: Jingqi Liu --- exec.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/exec.c b/exec.c index de9d949902..8221abffec 100644 --- a/exec.c +++ b/exec.c @@ -1736,6 +1736,42 @@ static int64_t get_file_size(int fd) return size; } =20 +static int64_t get_file_align(int fd) +{ + int64_t align =3D -1; +#if defined(__linux__) + struct stat st; + + if (fstat(fd, &st) < 0) { + return -errno; + } + + /* Special handling for devdax character devices */ + if (S_ISCHR(st.st_mode)) { + g_autofree char *subsystem_path =3D NULL; + g_autofree char *subsystem =3D NULL; + + subsystem_path =3D g_strdup_printf("/sys/dev/char/%d:%d/subsystem", + major(st.st_rdev), minor(st.st_rd= ev)); + subsystem =3D g_file_read_link(subsystem_path, NULL); + + if (subsystem && g_str_has_suffix(subsystem, "/dax")) { + g_autofree char *align_path =3D NULL; + g_autofree char *align_str =3D NULL; + + align_path =3D g_strdup_printf("/sys/dev/char/%d:%d/device/ali= gn", + major(st.st_rdev), minor(st.st_rdev)); + + if (g_file_get_contents(align_path, &align_str, NULL, NULL)) { + return g_ascii_strtoll(align_str, NULL, 0); + } + } + } +#endif /* defined(__linux__) */ + + return align; +} + static int file_ram_open(const char *path, const char *region_name, bool *created, @@ -2275,7 +2311,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Mem= oryRegion *mr, { RAMBlock *new_block; Error *local_err =3D NULL; - int64_t file_size; + int64_t file_size, file_align; =20 /* Just support these ram flags by now. */ assert((ram_flags & ~(RAM_SHARED | RAM_PMEM)) =3D=3D 0); @@ -2311,6 +2347,14 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, Me= moryRegion *mr, return NULL; } =20 + file_align =3D get_file_align(fd); + if (file_align > 0 && mr && file_align > mr->align) { + error_setg(errp, "backing store align 0x%" PRIx64 + " is larger than 'align' option 0x" RAM_ADDR_FMT, + file_align, mr->align); + return NULL; + } + new_block =3D g_malloc0(sizeof(*new_block)); new_block->mr =3D mr; new_block->used_length =3D size; --=20 2.17.1