From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57953) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yn6gk-0007FJ-3w for qemu-devel@nongnu.org; Tue, 28 Apr 2015 10:40:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yn6ge-0000rD-Gl for qemu-devel@nongnu.org; Tue, 28 Apr 2015 10:40:42 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49026) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yn6ge-0000r1-33 for qemu-devel@nongnu.org; Tue, 28 Apr 2015 10:40:36 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id B9C4E8E6EA for ; Tue, 28 Apr 2015 14:40:35 +0000 (UTC) From: Paolo Bonzini Date: Tue, 28 Apr 2015 16:40:09 +0200 Message-Id: <1430232029-9457-3-git-send-email-pbonzini@redhat.com> In-Reply-To: <1430232029-9457-1-git-send-email-pbonzini@redhat.com> References: <1430232029-9457-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PULL 02/22] exec: Atomic access to bounce buffer List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Fam Zheng From: Fam Zheng There could be a race condition when two processes call address_space_map concurrently and both want to use the bounce buffer. Add an in_use flag in BounceBuffer to sync it. Signed-off-by: Fam Zheng Message-Id: <1426496617-10702-2-git-send-email-famz@redhat.com> Signed-off-by: Paolo Bonzini --- exec.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/exec.c b/exec.c index 874ecfc..5a1c700 100644 --- a/exec.c +++ b/exec.c @@ -2482,6 +2482,7 @@ typedef struct { void *buffer; hwaddr addr; hwaddr len; + bool in_use; } BounceBuffer; static BounceBuffer bounce; @@ -2570,7 +2571,7 @@ void *address_space_map(AddressSpace *as, l = len; mr = address_space_translate(as, addr, &xlat, &l, is_write); if (!memory_access_is_direct(mr, is_write)) { - if (bounce.buffer) { + if (atomic_xchg(&bounce.in_use, true)) { return NULL; } /* Avoid unbounded allocations */ @@ -2640,6 +2641,7 @@ void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, qemu_vfree(bounce.buffer); bounce.buffer = NULL; memory_region_unref(bounce.mr); + atomic_mb_set(&bounce.in_use, false); cpu_notify_map_clients(); } -- 2.3.5