From nobody Mon May 6 15:45:42 2024 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 1498639093188702.3779653314058; Wed, 28 Jun 2017 01:38:13 -0700 (PDT) Received: from localhost ([::1]:59728 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQ8UF-0004vF-6i for importer@patchew.org; Wed, 28 Jun 2017 04:38:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36512) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQ8TO-0004eQ-1m for qemu-devel@nongnu.org; Wed, 28 Jun 2017 04:37:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dQ8TJ-0006yQ-CF for qemu-devel@nongnu.org; Wed, 28 Jun 2017 04:37:18 -0400 Received: from mga04.intel.com ([192.55.52.120]:24537) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dQ8TJ-0006wd-3C for qemu-devel@nongnu.org; Wed, 28 Jun 2017 04:37:13 -0400 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Jun 2017 01:37:09 -0700 Received: from hz-desktop.sh.intel.com (HELO localhost) ([10.239.159.142]) by fmsmga002.fm.intel.com with ESMTP; 28 Jun 2017 01:37:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.40,274,1496127600"; d="scan'208";a="1187868216" From: Haozhong Zhang To: qemu-devel@nongnu.org Date: Wed, 28 Jun 2017 16:37:04 +0800 Message-Id: <20170628083704.24997-1-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.11.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.120 Subject: [Qemu-devel] [PATCH v2] exec: fix access to ram_list.dirty_memory when sync dirty bitmap 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: Paolo Bonzini , Xiao Guangrong , Haozhong Zhang , Stefan Hajnoczi , Juan Quintela 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" In cpu_physical_memory_sync_dirty_bitmap(rb, start, ...), the 2nd argument 'start' is relative to the start of the ramblock 'rb'. When it's used to access the dirty memory bitmap of ram_list (i.e. ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION]->blocks[]), an offset to the start of all RAM (i.e. rb->offset) should be added to it, which has however been missed since c/s 6b6712efcc. For a ramblock of host memory backend whose offset is not zero, cpu_physical_memory_sync_dirty_bitmap() synchronizes the incorrect part of the dirty memory bitmap of ram_list to the per ramblock dirty bitmap. As a result, a guest with host memory backend may crash after migration. Fix it by adding the offset of ramblock when accessing the dirty memory bitmap of ram_list in cpu_physical_memory_sync_dirty_bitmap(). Reported-by: Stefan Hajnoczi Signed-off-by: Haozhong Zhang Acked-by: Paolo Bonzini Tested-by: Stefan Hajnoczi --- Changes in v2: * Avoid shadowing variable 'offset'. (Paolo) --- include/exec/ram_addr.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h index 73d1bea8b6..c04f4f67f6 100644 --- a/include/exec/ram_addr.h +++ b/include/exec/ram_addr.h @@ -386,8 +386,9 @@ uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBlock= *rb, int k; int nr =3D BITS_TO_LONGS(length >> TARGET_PAGE_BITS); unsigned long * const *src; - unsigned long idx =3D (page * BITS_PER_LONG) / DIRTY_MEMORY_BLOCK_= SIZE; - unsigned long offset =3D BIT_WORD((page * BITS_PER_LONG) % + unsigned long word =3D BIT_WORD((start + rb->offset) >> TARGET_PAG= E_BITS); + unsigned long idx =3D (word * BITS_PER_LONG) / DIRTY_MEMORY_BLOCK_= SIZE; + unsigned long offset =3D BIT_WORD((word * BITS_PER_LONG) % DIRTY_MEMORY_BLOCK_SIZE); =20 rcu_read_lock(); @@ -414,9 +415,11 @@ uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBloc= k *rb, =20 rcu_read_unlock(); } else { + ram_addr_t offset =3D rb->offset; + for (addr =3D 0; addr < length; addr +=3D TARGET_PAGE_SIZE) { if (cpu_physical_memory_test_and_clear_dirty( - start + addr, + start + addr + offset, TARGET_PAGE_SIZE, DIRTY_MEMORY_MIGRATION)) { *real_dirty_pages +=3D 1; --=20 2.11.0