Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61311C64EC4 for ; Fri, 10 Mar 2023 08:40:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231250AbjCJIkj (ORCPT ); Fri, 10 Mar 2023 03:40:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231355AbjCJIj5 (ORCPT ); Fri, 10 Mar 2023 03:39:57 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC16810A124 for ; Fri, 10 Mar 2023 00:37:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678437451; 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=O8ovMxuqTav+RM59yevfCqZhcFC/qRlWg3JJtflmVmY=; b=GY6G42BoTJlkX5lWNmM1dhaPuFIaCAJ+YouFbQKNy1rnrXdKHUMpUNmWP76FNoyUGn5TTL W9urTHoB1cDDCSuOzGvShowk1hPhYUYS7/I5c6lpyAS3GZRXG7vaU5Q29/8wB4LxUPFjnW FSBFmLFhyIxyaxguvt5tEY7V+m37N1w= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-637-yE87zvQxPIG4zm3vicwVAg-1; Fri, 10 Mar 2023 03:37:30 -0500 X-MC-Unique: yE87zvQxPIG4zm3vicwVAg-1 Received: by mail-wm1-f69.google.com with SMTP id l31-20020a05600c1d1f00b003e8626cdd42so1525452wms.3 for ; Fri, 10 Mar 2023 00:37:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678437449; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=O8ovMxuqTav+RM59yevfCqZhcFC/qRlWg3JJtflmVmY=; b=sjLpniEOwhmye3+RQnETt7rtztHU6pO2y8XzmeSfsuKvWhcwHXzbE2LuB3JwEGf31I xEJxII+smoJGnZv17qao7eeQPnBRlL+3pr6ZzhHc1ydYsv+ERygLqVkY3LpZGLhQrkor Z6cEbCQ8fQb5I5Z1W8VIjUecn9TjjWuu3ZDEo2vqrZw645g8qw5Q2EeDClbBhdD/lNGv ify2P8EPLEFVrrVaSNeSK70gvLew8Zo7iTHjfi9g4dACOIDfYqYBdyDaGkzZ7qfcqT9R VuJvXQ9HYcPXvImIFGE9RemYYAR8Xz5Qm0FDwLEXU6xJCU881ZHUqPx3PRUjYRPpOTAA EAWg== X-Gm-Message-State: AO0yUKXFEKgZk6p8C/YFILGub/69CwT0eH9rGlKOOKQSlF6ay9uTMA9o QOnZsUhrh55pT0RsaYLdiytV7u5Y7GG/36Ylem5xw1gY8odcMz+qPSdC4v+NIxLod8iiiq+bLEc apvcnK5uLwBEfjWiz/6bSvnO7 X-Received: by 2002:a5d:6605:0:b0:2c7:7701:2578 with SMTP id n5-20020a5d6605000000b002c777012578mr15657389wru.54.1678437449506; Fri, 10 Mar 2023 00:37:29 -0800 (PST) X-Google-Smtp-Source: AK7set+y1SLlYCLoZWlLhVUFLLO4mwRgZFYa2PT7ddRAXzE9lqne3QFfWheWewGObdmXBjz49fAnmw== X-Received: by 2002:a5d:6605:0:b0:2c7:7701:2578 with SMTP id n5-20020a5d6605000000b002c777012578mr15657381wru.54.1678437449202; Fri, 10 Mar 2023 00:37:29 -0800 (PST) Received: from redhat.com ([2.52.9.88]) by smtp.gmail.com with ESMTPSA id c14-20020adffb0e000000b002c7107ce17fsm1562448wrr.3.2023.03.10.00.37.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 00:37:28 -0800 (PST) Date: Fri, 10 Mar 2023 03:37:25 -0500 From: "Michael S. Tsirkin" To: "Longpeng (Mike, Cloud Infrastructure Service Product Dept.)" Cc: Jason Wang , arei.gonglei@huawei.com, yechuan@huawei.com, huangzhichao@huawei.com, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: Re: [PATCH] vhost-vdpa: cleanup memory maps when closing vdpa fds Message-ID: <20230310033706-mutt-send-email-mst@kernel.org> References: <20230131145310.2069-1-longpeng2@huawei.com> <35b94992-0c6b-a190-1fce-5dda9c8dcf4b@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 15, 2023 at 01:15:55PM +0800, Longpeng (Mike, Cloud Infrastructure Service Product Dept.) wrote: > > > 在 2023/2/15 10:56, Jason Wang 写道: > > On Wed, Feb 15, 2023 at 10:49 AM Longpeng (Mike, Cloud Infrastructure > > Service Product Dept.) wrote: > > > > > > > > > > > > 在 2023/2/15 10:00, Jason Wang 写道: > > > > On Tue, Feb 14, 2023 at 2:28 PM Longpeng (Mike, Cloud Infrastructure > > > > Service Product Dept.) wrote: > > > > > > > > > > > > > > > > > > > > 在 2023/2/14 14:16, Jason Wang 写道: > > > > > > > > > > > > 在 2023/1/31 22:53, Longpeng(Mike) 写道: > > > > > > > From: Longpeng > > > > > > > > > > > > > > We must cleanup all memory maps when closing the vdpa fds, otherwise > > > > > > > some critical resources (e.g. memory, iommu map) will leaked if the > > > > > > > userspace exits unexpectedly (e.g. kill -9). > > > > > > > > > > > > > > > > > > Sounds like a bug of the kernel, should we fix there? > > > > > > > > > > > > > > > > For example, the iommu map is setup when QEMU calls VHOST_IOTLB_UPDATE > > > > > ioctl and it'll be freed if QEMU calls VHOST_IOTLB_INVALIDATE ioctl. > > > > > > > > > > So maybe we release these resources in vdpa framework in kernel is a > > > > > suitable choice? > > > > > > > > I think I need understand what does "resources" mean here: > > > > > > > > For iommu mapping, it should be freed by vhost_vdpa_free_domain() in > > > > vhost_vdpa_release()? > > > > > > > > > > Please consider the following lifecycle of the vdpa device: > > > > > > 1. vhost_vdpa_open > > > vhost_vdpa_alloc_domain > > > > > > 2. vhost_vdpa_pa_map > > > pin_user_pages > > > vhost_vdpa_map > > > iommu_map > > > > > > 3. kill QEMU > > > > > > 4. vhost_vdpa_release > > > vhost_vdpa_free_domain > > > > > > In this case, we have no opportunity to invoke unpin_user_pages or > > > iommu_unmap to free the memory. > > > > We do: > > > > vhost_vdpa_cleanup() > > vhost_vdpa_remove_as() > > vhost_vdpa_iotlb_unmap() > > vhost_vdpa_pa_unmap() > > unpin_user_pages() > > vhost_vdpa_general_unmap() > > iommu_unmap() > > ? > > > Oh, my codebase is linux-6.2-rc2 and the commit c070c1912a8 (vhost-vdpa: fix > an iotlb memory leak) already fixed this bug in linux-6.2-rc3. OK I dropped this. > > Btw, it looks like we should call vhost_vdpa_free_domain() *after* > > vhost_vdpa_cleanup() otherwise it's a UAF? > > > I think so, the v->domain is set to NULL in vhost_vdpa_free_domain(), it > seems would trigger null-pointer access in my case. > > > Thanks > > > > > > > > > static int vhost_vdpa_release(struct inode *inode, struct file *filep) > > > > { > > > > struct vhost_vdpa *v = filep->private_data; > > > > struct vhost_dev *d = &v->vdev; > > > > > > > > mutex_lock(&d->mutex); > > > > filep->private_data = NULL; > > > > vhost_vdpa_clean_irq(v); > > > > vhost_vdpa_reset(v); > > > > vhost_dev_stop(&v->vdev); > > > > vhost_vdpa_free_domain(v); > > > > vhost_vdpa_config_put(v); > > > > vhost_vdpa_cleanup(v); > > > > mutex_unlock(&d->mutex); > > > > > > > > atomic_dec(&v->opened); > > > > complete(&v->completion); > > > > > > > > return 0; > > > > } > > > > > > > > > > > > > > By the way, Jason, can you reproduce the problem in your machine? > > > > > > > > > > > > > Haven't got time in doing this but it should be the responsibility of > > > > the author to validate this anyhow. > > > > > > > > Thanks > > > > > > > > > > Thanks > > > > > > > > > > > > > > > > > > > > > > > > > > Signed-off-by: Longpeng > > > > > > > --- > > > > > > > drivers/vhost/vdpa.c | 13 +++++++++++++ > > > > > > > 1 file changed, 13 insertions(+) > > > > > > > > > > > > > > diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c > > > > > > > index a527eeeac637..37477cffa5aa 100644 > > > > > > > --- a/drivers/vhost/vdpa.c > > > > > > > +++ b/drivers/vhost/vdpa.c > > > > > > > @@ -823,6 +823,18 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v, > > > > > > > vhost_vdpa_remove_as(v, asid); > > > > > > > } > > > > > > > +static void vhost_vdpa_clean_map(struct vhost_vdpa *v) > > > > > > > +{ > > > > > > > + struct vhost_vdpa_as *as; > > > > > > > + u32 asid; > > > > > > > + > > > > > > > + for (asid = 0; asid < v->vdpa->nas; asid++) { > > > > > > > + as = asid_to_as(v, asid); > > > > > > > + if (as) > > > > > > > + vhost_vdpa_unmap(v, &as->iotlb, 0ULL, 0ULL - 1); > > > > > > > + } > > > > > > > +} > > > > > > > + > > > > > > > static int vhost_vdpa_va_map(struct vhost_vdpa *v, > > > > > > > struct vhost_iotlb *iotlb, > > > > > > > u64 iova, u64 size, u64 uaddr, u32 perm) > > > > > > > @@ -1247,6 +1259,7 @@ static int vhost_vdpa_release(struct inode > > > > > > > *inode, struct file *filep) > > > > > > > vhost_vdpa_clean_irq(v); > > > > > > > vhost_vdpa_reset(v); > > > > > > > vhost_dev_stop(&v->vdev); > > > > > > > + vhost_vdpa_clean_map(v); > > > > > > > vhost_vdpa_free_domain(v); > > > > > > > vhost_vdpa_config_put(v); > > > > > > > vhost_vdpa_cleanup(v); > > > > > > > > > > > > . > > > > > > > > > > > > > . > > > > > > > .