Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4034000ybv; Tue, 25 Feb 2020 11:51:56 -0800 (PST) X-Google-Smtp-Source: APXvYqybIGjXxQj0iZdul7ZOGP5wg7e2qg3bUOLM2KsLWk8AWwmfL5GHrQ9OWFuITAkjIGut3XkQ X-Received: by 2002:aca:fcc1:: with SMTP id a184mr446733oii.36.1582660316772; Tue, 25 Feb 2020 11:51:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582660316; cv=none; d=google.com; s=arc-20160816; b=sL9y6moMGWYgpb0zfkrxD7kgcQOmZCs/l5duFPlKEcJ65/efD/26S46Vk5FLe5vPT+ Txm8H/Hq0qp6JpM4egCKn9TYwDd2dDuYHCqVD7pSpGTTZg0dIn1hP6oasUgwfrocxV12 IDxOQ+9OVLms6euptkzmMBFk+YYgX+H/BtwhFkxM9MZkwI11yFxWfzAcR5Qvfne/x5Hb 17lRKMNxr382Tap4AS/vopFGqoElaQnD0ylJoWLiuQXREFkdKYbk6mMmAM1SDG+xyYHu uRexYnd+tLN1p4NImwoKOSXLMFLOSEQSoJtQ42CSdzj9uUxRMSOFqNnJg3IWePCuHm18 3WWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=0h+nCmhnPTm+AmSxpQrAQWVhspSA8igiHXC6K5jdZ+0=; b=tLpdcTDiu43a4gGv8UQGkDd48lSmgQEYHxo4HikBK9r2Zjzhx2bJdNxY5xnfGuX52v 25D75C0EBt42WcDR/mvv/TfsWFuQfPHOMiGcVdxxaN0T0Hpc1Btn0RLD6CFjBSQFGUQ9 yGJx4BKTYUfwUDDzR2Pp67ebtL+4XPUQ6fJOQLMbrsAjYotmJEiL+DNhpOF/4dPAWKUo CmLmZf/7wtLtVn40Fu5ZCLP0G2uw3NH4gZ0wmk6zYI3v5tQGFh++b3bnxXcCZ71A4Y1F SwvTE5ZgWLlVdxYsFnr9pDmnttSd3RhJfbiQwgadr4vOX8OewBaV0YC1ny8LxUzamM8C Vq+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=bl1xy3cQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c7si126283otf.87.2020.02.25.11.51.42; Tue, 25 Feb 2020 11:51:56 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=bl1xy3cQ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730422AbgBYTvh (ORCPT + 99 others); Tue, 25 Feb 2020 14:51:37 -0500 Received: from fllv0016.ext.ti.com ([198.47.19.142]:41410 "EHLO fllv0016.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728443AbgBYTvg (ORCPT ); Tue, 25 Feb 2020 14:51:36 -0500 Received: from fllv0035.itg.ti.com ([10.64.41.0]) by fllv0016.ext.ti.com (8.15.2/8.15.2) with ESMTP id 01PGprxU059312; Tue, 25 Feb 2020 10:51:53 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1582649513; bh=0h+nCmhnPTm+AmSxpQrAQWVhspSA8igiHXC6K5jdZ+0=; h=Subject:To:CC:References:From:Date:In-Reply-To; b=bl1xy3cQ+PDdt/XPMLXVfsyZTCfJxByP4KaL4YmYWsDj6i+sVPvRsxGjrGyIGo6jj ehQszO9pMmr7EchOe455Im5yIoWVdtb7BUGa52oJJKKtOp4k/z1UkcR4f81FlCSTGJ oE4lb1K0sQn1llZ2mz0dYWgyf7tWvsLg0MYtbJ4g= Received: from DFLE115.ent.ti.com (dfle115.ent.ti.com [10.64.6.36]) by fllv0035.itg.ti.com (8.15.2/8.15.2) with ESMTP id 01PGpr20102297; Tue, 25 Feb 2020 10:51:53 -0600 Received: from DFLE111.ent.ti.com (10.64.6.32) by DFLE115.ent.ti.com (10.64.6.36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1847.3; Tue, 25 Feb 2020 10:51:53 -0600 Received: from fllv0040.itg.ti.com (10.64.41.20) by DFLE111.ent.ti.com (10.64.6.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1847.3 via Frontend Transport; Tue, 25 Feb 2020 10:51:53 -0600 Received: from [128.247.58.153] (ileax41-snat.itg.ti.com [10.172.224.153]) by fllv0040.itg.ti.com (8.15.2/8.15.2) with ESMTP id 01PGpq6Q073429; Tue, 25 Feb 2020 10:51:53 -0600 Subject: Re: [PATCH] virtio_ring: Fix mem leak with vring_new_virtqueue() To: Jason Wang , "Michael S. Tsirkin" CC: Tiwei Bie , "David S. Miller" , , , References: <20200224212643.30672-1-s-anna@ti.com> From: Suman Anna Message-ID: <0ace3a3b-cf2f-7977-5337-f74f530afbe1@ti.com> Date: Tue, 25 Feb 2020 10:51:52 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 8bit X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Jason, On 2/24/20 11:39 PM, Jason Wang wrote: > > On 2020/2/25 上午5:26, Suman Anna wrote: >> The functions vring_new_virtqueue() and __vring_new_virtqueue() are used >> with split rings, and any allocations within these functions are managed >> outside of the .we_own_ring flag. The commit cbeedb72b97a ("virtio_ring: >> allocate desc state for split ring separately") allocates the desc state >> within the __vring_new_virtqueue() but frees it only when the >> .we_own_ring >> flag is set. This leads to a memory leak when freeing such allocated >> virtqueues with the vring_del_virtqueue() function. >> >> Fix this by moving the desc_state free code outside the flag and only >> for split rings. Issue was discovered during testing with remoteproc >> and virtio_rpmsg. >> >> Fixes: cbeedb72b97a ("virtio_ring: allocate desc state for split ring >> separately") >> Signed-off-by: Suman Anna >> --- >>   drivers/virtio/virtio_ring.c | 4 ++-- >>   1 file changed, 2 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c >> index 867c7ebd3f10..58b96baa8d48 100644 >> --- a/drivers/virtio/virtio_ring.c >> +++ b/drivers/virtio/virtio_ring.c >> @@ -2203,10 +2203,10 @@ void vring_del_virtqueue(struct virtqueue *_vq) >>                        vq->split.queue_size_in_bytes, >>                        vq->split.vring.desc, >>                        vq->split.queue_dma_addr); >> - >> -            kfree(vq->split.desc_state); >>           } >>       } >> +    if (!vq->packed_ring) >> +        kfree(vq->split.desc_state); > > > Nitpick, it looks to me it would be more clear if we just free > desc_state unconditionally here (and remove the kfree for packed above). OK, are you sure you want that to be folded into this patch? It looks to me a separate cleanup/consolidation patch, and packed desc_state does not suffer this memleak, and need not be backported into stable kernels. regards Suman > Anyway desc_state will be allocated by use even if !we_own_ring. > > Thanks > > >>       list_del(&_vq->list); >>       kfree(vq); >>   } >