From patchwork Fri Oct 9 19:35:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 271675 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MIME_BASE64_TEXT,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4EBDC832E1 for ; Fri, 9 Oct 2020 19:55:25 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 21E37225A9 for ; Fri, 9 Oct 2020 19:55:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PYe/cHA8" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 21E37225A9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:36970 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kQyU7-0005Ra-SQ for qemu-devel@archiver.kernel.org; Fri, 09 Oct 2020 15:55:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47998) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kQyCB-0001YG-0D for qemu-devel@nongnu.org; Fri, 09 Oct 2020 15:36:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:46086) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kQyC9-0002z7-7r for qemu-devel@nongnu.org; Fri, 09 Oct 2020 15:36:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1602272208; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=X+QE+ha5fJzUlptXByqU2+dcTMFzxzMcUxOdUKkjuO4=; b=PYe/cHA8AIOLs7nQ60MHkCtYDpX6l4SZLEx3e5y4Ndes2Uy8tBDOKDYkSgFiClbjYsrgRK v520Zr4U2JHYCZm660/vlIv7nqMZZ9tsfgsTOLZDO3GaLQQb7ZxGZNu5w8i4wbpSjmAj/L l4fkDT5HOttH2npqtG8uUjVgvea2Jls= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-593-4pdCEfqqP0265IzBZLtS-g-1; Fri, 09 Oct 2020 15:36:44 -0400 X-MC-Unique: 4pdCEfqqP0265IzBZLtS-g-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 455021DDE3; Fri, 9 Oct 2020 19:36:43 +0000 (UTC) Received: from localhost (ovpn-112-20.ams2.redhat.com [10.36.112.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 168135C1D7; Fri, 9 Oct 2020 19:36:39 +0000 (UTC) From: Stefan Hajnoczi To: Peter Maydell , qemu-devel@nongnu.org Subject: [PULL 12/30] block/export: consolidate request structs into VuBlockReq Date: Fri, 9 Oct 2020 20:35:11 +0100 Message-Id: <20201009193529.322822-13-stefanha@redhat.com> In-Reply-To: <20201009193529.322822-1-stefanha@redhat.com> References: <20201009193529.322822-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Received-SPF: pass client-ip=216.205.24.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/09 02:34:37 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -3 X-Spam_score: -0.4 X-Spam_bar: / X-Spam_report: (-0.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Kevin Wolf , Thomas Huth , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , qemu-block@nongnu.org, Markus Armbruster , "Dr. David Alan Gilbert" , Coiby Xu , Max Reitz , Stefan Hajnoczi , Cleber Rosa , Paolo Bonzini , Fam Zheng Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Only one struct is needed per request. Drop req_data and the separate VuBlockReq instance. Instead let vu_queue_pop() allocate everything at once. This fixes the req_data memory leak in vu_block_virtio_process_req(). Signed-off-by: Stefan Hajnoczi Message-id: 20200924151549.913737-6-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- block/export/vhost-user-blk-server.c | 68 +++++++++------------------- 1 file changed, 21 insertions(+), 47 deletions(-) diff --git a/block/export/vhost-user-blk-server.c b/block/export/vhost-user-blk-server.c index fb7764a730..ef07a87eb1 100644 --- a/block/export/vhost-user-blk-server.c +++ b/block/export/vhost-user-blk-server.c @@ -25,7 +25,7 @@ struct virtio_blk_inhdr { }; typedef struct VuBlockReq { - VuVirtqElement *elem; + VuVirtqElement elem; int64_t sector_num; size_t size; struct virtio_blk_inhdr *in; @@ -39,14 +39,10 @@ static void vu_block_req_complete(VuBlockReq *req) VuDev *vu_dev = &req->server->vu_dev; /* IO size with 1 extra status byte */ - vu_queue_push(vu_dev, req->vq, req->elem, req->size + 1); + vu_queue_push(vu_dev, req->vq, &req->elem, req->size + 1); vu_queue_notify(vu_dev, req->vq); - if (req->elem) { - free(req->elem); - } - - g_free(req); + free(req); } static VuBlockDev *get_vu_block_device_by_server(VuServer *server) @@ -89,20 +85,12 @@ static void coroutine_fn vu_block_flush(VuBlockReq *req) blk_co_flush(backend); } -struct req_data { - VuServer *server; - VuVirtq *vq; - VuVirtqElement *elem; -}; - static void coroutine_fn vu_block_virtio_process_req(void *opaque) { - struct req_data *data = opaque; - VuServer *server = data->server; - VuVirtq *vq = data->vq; - VuVirtqElement *elem = data->elem; + VuBlockReq *req = opaque; + VuServer *server = req->server; + VuVirtqElement *elem = &req->elem; uint32_t type; - VuBlockReq *req; VuBlockDev *vdev_blk = get_vu_block_device_by_server(server); BlockBackend *backend = vdev_blk->backend; @@ -111,18 +99,13 @@ static void coroutine_fn vu_block_virtio_process_req(void *opaque) struct iovec *out_iov = elem->out_sg; unsigned in_num = elem->in_num; unsigned out_num = elem->out_num; + /* refer to hw/block/virtio_blk.c */ if (elem->out_num < 1 || elem->in_num < 1) { error_report("virtio-blk request missing headers"); - free(elem); - return; + goto err; } - req = g_new0(VuBlockReq, 1); - req->server = server; - req->vq = vq; - req->elem = elem; - if (unlikely(iov_to_buf(out_iov, out_num, 0, &req->out, sizeof(req->out)) != sizeof(req->out))) { error_report("virtio-blk request outhdr too short"); @@ -202,36 +185,27 @@ static void coroutine_fn vu_block_virtio_process_req(void *opaque) err: free(elem); - g_free(req); - return; } static void vu_block_process_vq(VuDev *vu_dev, int idx) { - VuServer *server; - VuVirtq *vq; - struct req_data *req_data; + VuServer *server = container_of(vu_dev, VuServer, vu_dev); + VuVirtq *vq = vu_get_queue(vu_dev, idx); - server = container_of(vu_dev, VuServer, vu_dev); - assert(server); - - vq = vu_get_queue(vu_dev, idx); - assert(vq); - VuVirtqElement *elem; while (1) { - elem = vu_queue_pop(vu_dev, vq, sizeof(VuVirtqElement) + - sizeof(VuBlockReq)); - if (elem) { - req_data = g_new0(struct req_data, 1); - req_data->server = server; - req_data->vq = vq; - req_data->elem = elem; - Coroutine *co = qemu_coroutine_create(vu_block_virtio_process_req, - req_data); - aio_co_enter(server->ioc->ctx, co); - } else { + VuBlockReq *req; + + req = vu_queue_pop(vu_dev, vq, sizeof(VuBlockReq)); + if (!req) { break; } + + req->server = server; + req->vq = vq; + + Coroutine *co = + qemu_coroutine_create(vu_block_virtio_process_req, req); + qemu_coroutine_enter(co); } }