qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Amit Shah <amit.shah@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: qemu list <qemu-devel@nongnu.org>,
	Amit Shah <amit.shah@redhat.com>,
	zhanghailiang <zhang.zhanghailiang@huawei.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	Juan Quintela <quintela@redhat.com>
Subject: [Qemu-devel] [PULL 1/9] ram: Split host_from_stream_offset() into two helper functions
Date: Fri,  5 Feb 2016 19:26:46 +0530	[thread overview]
Message-ID: <4c4bad486186fed9631b4ceb7c06d24e9fa65e6f.1454680535.git.amit.shah@redhat.com> (raw)
In-Reply-To: <cover.1454680535.git.amit.shah@redhat.com>
In-Reply-To: <cover.1454680535.git.amit.shah@redhat.com>

From: zhanghailiang <zhang.zhanghailiang@huawei.com>

Split host_from_stream_offset() into two parts:
One is to get ram block, which the block idstr may be get from migration
stream, the other is to get hva (host) address from block and the offset.
Besides, we will do the check working in a new helper offset_in_ramblock().

Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Amit Shah <amit.shah@redhat.com>
Message-Id: <1452829066-9764-2-git-send-email-zhang.zhanghailiang@huawei.com>
Signed-off-by: Amit Shah <amit.shah@redhat.com>
---
 include/exec/ram_addr.h |  8 ++++++--
 migration/ram.c         | 40 +++++++++++++++++++++++++---------------
 2 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h
index ef1489d..606e277 100644
--- a/include/exec/ram_addr.h
+++ b/include/exec/ram_addr.h
@@ -38,10 +38,14 @@ struct RAMBlock {
     int fd;
 };
 
+static inline bool offset_in_ramblock(RAMBlock *b, ram_addr_t offset)
+{
+    return (b && b->host && offset < b->used_length) ? true : false;
+}
+
 static inline void *ramblock_ptr(RAMBlock *block, ram_addr_t offset)
 {
-    assert(offset < block->used_length);
-    assert(block->host);
+    assert(offset_in_ramblock(block, offset));
     return (char *)block->host + offset;
 }
 
diff --git a/migration/ram.c b/migration/ram.c
index 40d0533..b541ceb 100644
--- a/migration/ram.c
+++ b/migration/ram.c
@@ -2124,28 +2124,24 @@ static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
  * Returns a pointer from within the RCU-protected ram_list.
  */
 /*
- * Read a RAMBlock ID from the stream f, find the host address of the
- * start of that block and add on 'offset'
+ * Read a RAMBlock ID from the stream f.
  *
  * f: Stream to read from
- * offset: Offset within the block
  * flags: Page flags (mostly to see if it's a continuation of previous block)
  */
-static inline void *host_from_stream_offset(QEMUFile *f,
-                                            ram_addr_t offset,
-                                            int flags)
+static inline RAMBlock *ram_block_from_stream(QEMUFile *f,
+                                              int flags)
 {
     static RAMBlock *block = NULL;
     char id[256];
     uint8_t len;
 
     if (flags & RAM_SAVE_FLAG_CONTINUE) {
-        if (!block || block->max_length <= offset) {
+        if (!block) {
             error_report("Ack, bad migration stream!");
             return NULL;
         }
-
-        return block->host + offset;
+        return block;
     }
 
     len = qemu_get_byte(f);
@@ -2153,12 +2149,22 @@ static inline void *host_from_stream_offset(QEMUFile *f,
     id[len] = 0;
 
     block = qemu_ram_block_by_name(id);
-    if (block && block->max_length > offset) {
-        return block->host + offset;
+    if (!block) {
+        error_report("Can't find block %s", id);
+        return NULL;
     }
 
-    error_report("Can't find block %s", id);
-    return NULL;
+    return block;
+}
+
+static inline void *host_from_ram_block_offset(RAMBlock *block,
+                                               ram_addr_t offset)
+{
+    if (!offset_in_ramblock(block, offset)) {
+        return NULL;
+    }
+
+    return block->host + offset;
 }
 
 /*
@@ -2302,7 +2308,9 @@ static int ram_load_postcopy(QEMUFile *f)
         trace_ram_load_postcopy_loop((uint64_t)addr, flags);
         place_needed = false;
         if (flags & (RAM_SAVE_FLAG_COMPRESS | RAM_SAVE_FLAG_PAGE)) {
-            host = host_from_stream_offset(f, addr, flags);
+            RAMBlock *block = ram_block_from_stream(f, flags);
+
+            host = host_from_ram_block_offset(block, addr);
             if (!host) {
                 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
                 ret = -EINVAL;
@@ -2433,7 +2441,9 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
 
         if (flags & (RAM_SAVE_FLAG_COMPRESS | RAM_SAVE_FLAG_PAGE |
                      RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) {
-            host = host_from_stream_offset(f, addr, flags);
+            RAMBlock *block = ram_block_from_stream(f, flags);
+
+            host = host_from_ram_block_offset(block, addr);
             if (!host) {
                 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
                 ret = -EINVAL;
-- 
2.5.0

  reply	other threads:[~2016-02-05 13:57 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-05 13:56 [Qemu-devel] [PULL 0/9] migration: small fixes Amit Shah
2016-02-05 13:56 ` Amit Shah [this message]
2016-02-05 13:56 ` [Qemu-devel] [PULL 2/9] migration: rename 'file' in MigrationState to 'to_dst_file' Amit Shah
2016-02-05 13:56 ` [Qemu-devel] [PULL 3/9] savevm: Split load vm state function qemu_loadvm_state Amit Shah
2016-02-05 13:56 ` [Qemu-devel] [PULL 4/9] migration/ram: Fix some helper functions' parameter to use PageSearchStatus Amit Shah
2016-02-05 13:56 ` [Qemu-devel] [PULL 5/9] qmp-commands.hx: Fix the missing options for migration parameters commands Amit Shah
2016-02-05 13:56 ` [Qemu-devel] [PULL 6/9] qmp-commands.hx: Document the missing options for migration capability commands Amit Shah
2016-02-05 13:56 ` [Qemu-devel] [PULL 7/9] migration: remove useless code Amit Shah
2016-02-05 13:56 ` [Qemu-devel] [PULL 8/9] static checker: e1000-82540em got aliased to e1000 Amit Shah
2016-02-11 13:04   ` Paolo Bonzini
2016-02-22 12:39     ` Amit Shah
2016-02-23  7:02       ` Jason Wang
2016-02-23  7:41         ` Amit Shah
2016-04-05 13:32           ` Dr. David Alan Gilbert
2016-04-06  1:48             ` Jason Wang
2016-04-06 13:52               ` Amit Shah
2016-04-07  7:25                 ` Jason Wang
2016-02-05 13:56 ` [Qemu-devel] [PULL 9/9] migration: fix bad string passed to error_report() Amit Shah
2016-02-05 15:06 ` [Qemu-devel] [PULL 0/9] migration: small fixes Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4c4bad486186fed9631b4ceb7c06d24e9fa65e6f.1454680535.git.amit.shah@redhat.com \
    --to=amit.shah@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=zhang.zhanghailiang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).