Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp219801lqp; Wed, 22 May 2024 02:30:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCURsLM3eDqL6IeUhQC6Vwx81UyZnZvU3M9XaPpU+mgSl6A769c1431+7vPOHXubO/mdeHaCpsJpbCzio6PnGBW9BO8r2vMgdlJH3fcuHQ== X-Google-Smtp-Source: AGHT+IEsBtL7Nd2TkHt8d5zcfoE1GU398tpajAbDjV0swVGJS+en7PB4IYN4Q5Em/FyiByZOEsYD X-Received: by 2002:a05:6808:6d4:b0:3c9:958f:1552 with SMTP id 5614622812f47-3cdb5384dcemr1406894b6e.21.1716370245565; Wed, 22 May 2024 02:30:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716370245; cv=pass; d=google.com; s=arc-20160816; b=01+74HqFCUGil67veHEya/GpFb/ynUv+BB7C1zO0EZ3D8LjwwWrMMBMqSaKRNLz0Ep XHvdhFsub2Gr4x/WtGokPcGaXzVPPIipUO8IZnbxzJg06K/SFyp/cBqWdb6H/iEER0JT duIbdoEd+lqCMAH+qNMwIwoH6lZMRm9p3RSa/Gz2onrrLddBtwDEsi2wA2HLXKlDI7Bp o1MDG0H5x8EJtWWKZ4Dr68VxXRHHGfha3NMxPNOQBvFnsL71rtHKW6IO79hX0khWSib/ aMfY7ExMadeGu0ns71iySKyQHJtK8K1NgPrOel3aQLaBisrYB1LSW4PShWXwsXa4m+Hy aPTw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=kd0axXJ7nbL1O8OHlVZj4aZytG28Syj4yJKy6QKBubo=; fh=zDbdc48tP89DNo5wgmPwOB+1Dsiv/pWtGPeK0oE7+Dw=; b=nEr8C7+RyHJk3psFMgo8MahcAG+/3GstjkHGJE0Gn+ohrRZcgQyxdzsuujAxldMSgM ySWQUWdG+krjZ8irU9DAObQqviE28ncazf+bEPlYxhVTyawSfuIuLcZz0tVIm7BKyhkl WbUEXZa4VCJ8umFVidSkQdH5HKLUwW86bYvr9ZZqqZCUO+UlGQxVwGO2nKxQECDY9j89 SFYhqYvnuxouCGiz3g49lV4oRJY5Fue4hiORrV3y9kWqOFb6CcxM8WlT9gfGoPu42Vov PGSF1uYnUGZp7NUU+0xSktZAhq4Wd9bWZ0ip/X7qxs7ZfjY10Pkgz+iBGlS/llKIO0w5 FG/g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=kylinos.cn); spf=pass (google.com: domain of linux-kernel+bounces-185990-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185990-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id af79cd13be357-792dca876cbsi430408085a.468.2024.05.22.02.30.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 02:30:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-185990-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=kylinos.cn); spf=pass (google.com: domain of linux-kernel+bounces-185990-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185990-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 166D51C20B8A for ; Wed, 22 May 2024 09:30:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CFD9480630; Wed, 22 May 2024 09:30:39 +0000 (UTC) Received: from mailgw.kylinos.cn (mailgw.kylinos.cn [124.126.103.232]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8AE61E51E for ; Wed, 22 May 2024 09:30:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=124.126.103.232 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716370239; cv=none; b=Y8JowCdb42xB+6z7trL3e7PVevjD355Me9+znk9DIrOwz9tsqNRAZlF3WeMkSQUOXfMseEHuoDNzwRzEMhU88JKkFA8AmEapVnVOao/LZa6Q1zTpBaYRMGPqKskHutAAyUQ7yPXuyar14pmWPhmH053tNf9//vu8D1V5qcUHoIY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716370239; c=relaxed/simple; bh=b4x2StJjDGXWYMl11hb3SNY+I3gmk55FoDFhh7uOoVo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=dum6OwCBIkCZ7Spg9kXCFrPuLidQZLENTcdlLXxqCk/FSNSKXXUi7ibl6U6gVQAhYwaT5xpa9liMF4cX9WzqScOp98JS379eMdRZJYFfp41q11DLR0bLqAEf8+5uC8GgbYuR5ZjzAuspnDyFFyCpVDzLUsg+Wdm+ZLEEazsdJiU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn; spf=pass smtp.mailfrom=kylinos.cn; arc=none smtp.client-ip=124.126.103.232 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=kylinos.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kylinos.cn X-UUID: 729bb2ce181c11ef9305a59a3cc225df-20240522 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.38,REQID:1fc4183e-3257-42ad-a3cd-2c24b11b9557,IP:15, URL:0,TC:0,Content:0,EDM:25,RT:0,SF:-11,FILE:0,BULK:0,RULE:Release_Ham,ACT ION:release,TS:29 X-CID-INFO: VERSION:1.1.38,REQID:1fc4183e-3257-42ad-a3cd-2c24b11b9557,IP:15,UR L:0,TC:0,Content:0,EDM:25,RT:0,SF:-11,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:29 X-CID-META: VersionHash:82c5f88,CLOUDID:a4e93706d0755d1bbfcfaf8d67cf5860,BulkI D:2405221719554BORZ5U8,BulkQuantity:0,Recheck:0,SF:66|38|25|17|19|44|102,T C:nil,Content:0,EDM:5,IP:-2,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil, COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_FSI,TF_CID_SPAM_SNR,TF_CID_SPAM_FAS,TF_CID_SPAM_FSD X-CTIC-Tags: HR_CC_COUNT, HR_CC_DOMAIN_COUNT, HR_CC_NO_NAME, HR_CTE_8B, HR_CTT_MISS HR_DATE_H, HR_DATE_WKD, HR_DATE_ZONE, HR_FROM_NAME, HR_SJ_LANG HR_SJ_LEN, HR_SJ_LETTER, HR_SJ_NOR_SYM, HR_SJ_PHRASE, HR_SJ_PHRASE_LEN HR_SJ_WS, HR_TO_COUNT, HR_TO_DOMAIN_COUNT, HR_TO_NO_NAME, IP_TRUSTED SRC_TRUSTED, DN_TRUSTED, SA_EXISTED, SN_EXISTED, SPF_NOPASS DKIM_NOPASS, DMARC_NOPASS, CIE_BAD, CIE_GOOD, CIE_GOOD_SPF GTI_FG_BS, GTI_RG_INFO, GTI_C_BU, AMN_T1, AMN_GOOD AMN_C_TI, AMN_C_BU, ABX_MISS_RDNS X-UUID: 729bb2ce181c11ef9305a59a3cc225df-20240522 X-User: liweishi@kylinos.cn Received: from localhost.localdomain [(116.128.244.171)] by mailgw.kylinos.cn (envelope-from ) (Generic MTA) with ESMTP id 1322552919; Wed, 22 May 2024 17:19:53 +0800 From: Weishi Li To: airlied@redhat.com, kraxel@redhat.com, gurchetansingh@chromium.org, olvaffe@gmail.com, maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, daniel@ffwll.ch Cc: dri-devel@lists.freedesktop.org, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, liweishi@kylinos.cn Subject: [PATCH] [PATCH RESEND] drm/virtio: fix memory leak of vbuf Date: Wed, 22 May 2024 17:20:31 +0800 Message-Id: <20240522092031.135304-1-liweishi@kylinos.cn> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Both virtio_gpu_queue_ctrl_buffer and virtio_gpu_queue_cursor use virtqueue_add_sgs to upload the structure virtio_gpu_vbuffer * vbuf to virtqueue. However, when the vbuf fails to upload and virtqueue_add_sgs returns -EIO or -ENOMEM, the vbuf will not be able to be free by virtio_gpu_dequeue_*_func, resulting in a continuous increase in memory allocated to vgdev ->vbufs. Therefore, make virtio_gpu_queue_ctrl_sgs and virtio_gpu_queue_cursor free vbuf directly after virtqueue_add_sgs returns -EIO or -ENOMEM. Signed-off-by: Weishi Li --- drivers/gpu/drm/virtio/virtgpu_vq.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/virtio/virtgpu_vq.c b/drivers/gpu/drm/virtio/virtgpu_vq.c index b1a00c0c25a7..6701ce9d0ee8 100644 --- a/drivers/gpu/drm/virtio/virtgpu_vq.c +++ b/drivers/gpu/drm/virtio/virtgpu_vq.c @@ -356,12 +356,14 @@ static int virtio_gpu_queue_ctrl_sgs(struct virtio_gpu_device *vgdev, ret = virtqueue_add_sgs(vq, sgs, outcnt, incnt, vbuf, GFP_ATOMIC); WARN_ON(ret); + if (ret < 0 && ret != -ENOSPC) { + free_vbuf(vgdev, vbuf); + } else { + vbuf->seqno = ++vgdev->ctrlq.seqno; + trace_virtio_gpu_cmd_queue(vq, virtio_gpu_vbuf_ctrl_hdr(vbuf), vbuf->seqno); - vbuf->seqno = ++vgdev->ctrlq.seqno; - trace_virtio_gpu_cmd_queue(vq, virtio_gpu_vbuf_ctrl_hdr(vbuf), vbuf->seqno); - - atomic_inc(&vgdev->pending_commands); - + atomic_inc(&vgdev->pending_commands); + } spin_unlock(&vgdev->ctrlq.qlock); drm_dev_exit(idx); @@ -469,6 +471,9 @@ static void virtio_gpu_queue_cursor(struct virtio_gpu_device *vgdev, wait_event(vgdev->cursorq.ack_queue, vq->num_free >= outcnt); spin_lock(&vgdev->cursorq.qlock); goto retry; + } else if (ret < 0) { + free_vbuf(vgdev, vbuf); + notify = false; } else { vbuf->seqno = ++vgdev->cursorq.seqno; trace_virtio_gpu_cmd_queue(vq, -- 2.25.1