From patchwork Wed May 31 09:43:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 769020 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wd5G20tqjz9s7j for ; Wed, 31 May 2017 19:46:18 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="etLlpqkf"; dkim-atps=neutral Received: from localhost ([::1]:58146 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG0Cl-0004Hu-Pv for incoming@patchwork.ozlabs.org; Wed, 31 May 2017 05:46:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41703) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dG0AJ-0002Uk-Tf for qemu-devel@nongnu.org; Wed, 31 May 2017 05:43:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dG0AJ-0001lZ-4v for qemu-devel@nongnu.org; Wed, 31 May 2017 05:43:43 -0400 Received: from mail-wr0-x243.google.com ([2a00:1450:400c:c0c::243]:35699) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dG0AI-0001kn-Ow; Wed, 31 May 2017 05:43:42 -0400 Received: by mail-wr0-x243.google.com with SMTP id g76so1057193wrd.2; Wed, 31 May 2017 02:43:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=vb1KVQsMDiu62H9MfN3KCI8VtQhYNDai1n0dNk5tGvw=; b=etLlpqkfZdgb1xRUFghgwNn5v1KEQ1VeTa4OQC+eyKuo83Fchf3ms+z+g3E1rBS9Bg tmWbaJ9zlbFNTkXOHPF/AQqZEXl4sbVGaKkmuYgao8NShg4QC8MesTks3Db1S3Xu08tH fwkRGqH8fdgWJf4GREkXwczwBxmk2uWJJ1IM+cR06SGeg8H9ZFe3gfchZ60DOnqWTCRg Z9ZEY0RFAYCUF1/zxwpnlNm4SZBzbtvHdrhkNfftFLwFecHLR2qMA/3uJN5wJFEIaqaQ hlta7TUy2cH6Mhqe72UiCHSUE2bmsiF8FmSxjCGeRRh2XQMxbJDsPVK5tFPIyJne3e03 +uUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=vb1KVQsMDiu62H9MfN3KCI8VtQhYNDai1n0dNk5tGvw=; b=ek+SaRSS5P/uVT0KSMJPW8TWRDUDVanaOlYejvterLtXOmVLW4KSkaX5Z5mS+lDx4Y dPQtZl001Y6vtPGR1mH1YN1xBNZrow0v+Gan+eMOthKZbcuk7odKT3p5ZAwAvJhvCvW6 TQY9VJx8e6qscmFJJQnkk75nzwUGVc6R8ENaC4olRnerbZ7g3WQulQE3PS9lIyMsalm5 zJygRPh2ZW0QgHyxDe0K7q3V7qfGYC+PWClarFASH8lSi46m4pY6qT8cqzw3vbAL9kVC W/tJncXsTN04ig4q0EM3LlmfdEOnHvtA8dzqbCI5QJkqHfXfB1NovDHCOQciop/cnljG nrFg== X-Gm-Message-State: AODbwcBZM1afa4g8TolFkhEFXyfRpc+J8PRskRR4fsQ6qIvotFFTf0fh z5Cur1ETdlW9iPMGfsY= X-Received: by 10.223.161.65 with SMTP id r1mr20193053wrr.114.1496223821385; Wed, 31 May 2017 02:43:41 -0700 (PDT) Received: from localhost.localdomain (94-39-157-43.adsl-ull.clienti.tiscali.it. [94.39.157.43]) by smtp.gmail.com with ESMTPSA id b30sm18150162wra.6.2017.05.31.02.43.40 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 31 May 2017 02:43:40 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 31 May 2017 11:43:25 +0200 Message-Id: <20170531094330.1808-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170531094330.1808-1-pbonzini@redhat.com> References: <20170531094330.1808-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::243 Subject: [Qemu-devel] [PATCH 06/11] nbd: make it thread-safe 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: kwolf@redhat.com, famz@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Paolo Bonzini Reviewed-by: Eric Blake --- block/nbd-client.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/block/nbd-client.c b/block/nbd-client.c index 1e2952fdae..43e0292ac1 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -114,6 +114,10 @@ static int nbd_co_send_request(BlockDriverState *bs, int rc, ret, i; qemu_co_mutex_lock(&s->send_mutex); + while (s->in_flight == MAX_NBD_REQUESTS) { + qemu_co_queue_wait(&s->free_sema, &s->send_mutex); + } + s->in_flight++; for (i = 0; i < MAX_NBD_REQUESTS; i++) { if (s->recv_coroutine[i] == NULL) { @@ -176,20 +180,6 @@ static void nbd_co_receive_reply(NBDClientSession *s, } } -static void nbd_coroutine_start(NBDClientSession *s, - NBDRequest *request) -{ - /* Poor man semaphore. The free_sema is locked when no other request - * can be accepted, and unlocked after receiving one reply. */ - if (s->in_flight == MAX_NBD_REQUESTS) { - qemu_co_queue_wait(&s->free_sema, NULL); - assert(s->in_flight < MAX_NBD_REQUESTS); - } - s->in_flight++; - - /* s->recv_coroutine[i] is set as soon as we get the send_lock. */ -} - static void nbd_coroutine_end(BlockDriverState *bs, NBDRequest *request) { @@ -197,13 +187,16 @@ static void nbd_coroutine_end(BlockDriverState *bs, int i = HANDLE_TO_INDEX(s, request->handle); s->recv_coroutine[i] = NULL; - s->in_flight--; - qemu_co_queue_next(&s->free_sema); /* Kick the read_reply_co to get the next reply. */ if (s->read_reply_co) { aio_co_wake(s->read_reply_co); } + + qemu_co_mutex_lock(&s->send_mutex); + s->in_flight--; + qemu_co_queue_next(&s->free_sema); + qemu_co_mutex_unlock(&s->send_mutex); } int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset, @@ -221,7 +214,6 @@ int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset, assert(bytes <= NBD_MAX_BUFFER_SIZE); assert(!flags); - nbd_coroutine_start(client, &request); ret = nbd_co_send_request(bs, &request, NULL); if (ret < 0) { reply.error = -ret; @@ -251,7 +243,6 @@ int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset, assert(bytes <= NBD_MAX_BUFFER_SIZE); - nbd_coroutine_start(client, &request); ret = nbd_co_send_request(bs, &request, qiov); if (ret < 0) { reply.error = -ret; @@ -286,7 +277,6 @@ int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, request.flags |= NBD_CMD_FLAG_NO_HOLE; } - nbd_coroutine_start(client, &request); ret = nbd_co_send_request(bs, &request, NULL); if (ret < 0) { reply.error = -ret; @@ -311,7 +301,6 @@ int nbd_client_co_flush(BlockDriverState *bs) request.from = 0; request.len = 0; - nbd_coroutine_start(client, &request); ret = nbd_co_send_request(bs, &request, NULL); if (ret < 0) { reply.error = -ret; @@ -337,7 +326,6 @@ int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, int count) return 0; } - nbd_coroutine_start(client, &request); ret = nbd_co_send_request(bs, &request, NULL); if (ret < 0) { reply.error = -ret;