From patchwork Thu Feb 4 12:20:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhanghailiang X-Patchwork-Id: 8220701 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id DBEFFBEEE5 for ; Thu, 4 Feb 2016 12:23:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2B63B2038A for ; Thu, 4 Feb 2016 12:23:10 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5B18F2034C for ; Thu, 4 Feb 2016 12:23:09 +0000 (UTC) Received: from localhost ([::1]:41256 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aRIwG-0004Ul-KE for patchwork-qemu-devel@patchwork.kernel.org; Thu, 04 Feb 2016 07:23:08 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57496) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aRIuB-0000tY-E2 for qemu-devel@nongnu.org; Thu, 04 Feb 2016 07:21:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aRIu8-0007pw-E3 for qemu-devel@nongnu.org; Thu, 04 Feb 2016 07:20:59 -0500 Received: from szxga03-in.huawei.com ([119.145.14.66]:44743) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aRIu7-0007pp-Rk for qemu-devel@nongnu.org; Thu, 04 Feb 2016 07:20:56 -0500 Received: from 172.24.1.50 (EHLO SZXEML423-HUB.china.huawei.com) ([172.24.1.50]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id BVR62884; Thu, 04 Feb 2016 20:20:47 +0800 (CST) Received: from localhost (10.177.24.212) by SZXEML423-HUB.china.huawei.com (10.82.67.154) with Microsoft SMTP Server id 14.3.235.1; Thu, 4 Feb 2016 20:20:36 +0800 From: zhanghailiang To: Date: Thu, 4 Feb 2016 20:20:21 +0800 Message-ID: <1454588426-12372-2-git-send-email-zhang.zhanghailiang@huawei.com> X-Mailer: git-send-email 1.9.0.msysgit.0 In-Reply-To: <1454588426-12372-1-git-send-email-zhang.zhanghailiang@huawei.com> References: <1454588426-12372-1-git-send-email-zhang.zhanghailiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.24.212] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020201.56B3421F.0141, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 8fd7c3800b3ea3a5bde5861692f83f4b X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 119.145.14.66 Cc: amit.shah@redhat.com, zhanghailiang , dgilbert@redhat.com, quintela@redhat.com Subject: [Qemu-devel] [PATCH v2 1/6] ram: Split host_from_stream_offset() into two helper functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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 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 Reviewed-by: Dr. David Alan Gilbert Reviewed-by: Amit Shah --- 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;