From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796058983; b=VCd5ttcvDMmA0S4QZS0mwR1FOxNAP0cFKpWQoHxgi3AN0KnmUJbah2EX0IzjVnE5rzJC TZ3Vcy3nrFl6OMiwt+yEJQAi51btJiHpWvJ42QE7zOTaWqDZJjNeScCSdkpV4SaqRj2TE 0iUInEGGxnlGNYFrz5cBQxaPn+Vso64LxQGpLr2pTMpnoy+q/QfWBfG7inCa07SCESrwz QYZzhid7d3djbnv79EwYMMe+LML1O3mbKmc+Dxx/FlTrSXFBdTYwwLpyxC/xOPMGSYHtW U6Yqi/4K6XyPTtgEPIVQc7N/Pgz59jLWLXwbaRi37sO4IN6pwxX0oWOc9+egWrvGMHA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796058983; h=DMARC-Filter:DKIM-Signature:X-MC-Rep:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: X-Received:Received:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:In-Reply-To:X-Mimecast-Spam-Score:X-Mimecast-MFC-PROC-ID: X-Mimecast-Originator:Content-Type:Content-Disposition; bh=riq4Vz3MjNKChjPzPN8jugjfFySNttO73Cw2yZRICQ8=; b=Z5eOZfg4ucuKI4ArnP7sfCCd1DQDhST6tWV5qHpjcGuEdbnaDefWNp/m8CD2e/wBSA8t AZWtqGyVpQlIjvnehNI5XFqEFMzsVym9F8agD8UcY97UZTavzdGJNXbCec5vfgFQ2Lxfj PyDGJGf4b5ZVmMbdmjVMhYHSf0Vuoli9qbQRVD0aAvXFuL9t/oNvtp9rSIh+h+BYqRIZ+ vtzMBkdS5CqkyPbsI4BAmnlKi3/ob7AhOL5eNZ4IaDb0/uxfrXahorOfcF4BNQ8FzWb8o 8257mpqxg8ajcepZOYlZLn67s27Kd0N9cy5CVRRxD2kS5uwnK4v4A8EPsclTQfzJokw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=OcG22hVy; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A48BB606A329 for ; Mon, 30 Nov 2026 17:16:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A48BB606A329 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=OcG22hVy DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796058981; 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: in-reply-to:in-reply-to:references:references; bh=riq4Vz3MjNKChjPzPN8jugjfFySNttO73Cw2yZRICQ8=; b=OcG22hVyjX5rptSbWct1MzZIeLKZIMZWSwklSRef+4glSjKXNZBXtaGMuuVR249GzNsp59 1V4c2srPCXLrtMDp3slJYAn+EY+YXgH0I/Wz7I9L2tmkjZYs11UIgr7WTIHhJB0ejr1Q0o LvosY+EMrD3sJi31DZZv/DngTL2YPHc= X-MC-Rep: binary re single Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-681-wnR-tZhqNNmeLyLCmQynOA-1; Mon, 30 Nov 2026 17:16:19 +0000 X-MC-Unique: wnR-tZhqNNmeLyLCmQynOA-1 X-Mimecast-MFC-AGG-ID: wnR-tZhqNNmeLyLCmQynOA_1796058978 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-499ca65ae87so5123618f8f.1 for ; Mon, 30 Nov 2026 09:16:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796058978; x=1796663778; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=riq4Vz3MjNKChjPzPN8jugjfFySNttO73Cw2yZRICQ8=; b=xLBWBYRuqNAbCVQ8D7bfjzLYdkUdHgdOQ0Zf5QrsD1RWl4GX0nbJMEKlab92Q0DeK9 Bp1qK+FNxcYMK8lavSas+8uK3eHCVzlO7Equfl7ix+yIxhcowuLZddIEkiV8MP06ZuVE hcaeDqdQqpvVqrOYbEfbcdIxBWSXujD7a6K+JDRyzYgGdIi4ZocVO27Ep/+xiaaHidVw /oNKfy+ROsTc0uAA88O63EqjQdegiTPaJ3LV+w5C+vRTte9M9Lazr4p4fEC0G2l7NyH3 E2GKLa2LZZSd6k2N2LiSLnUB7iyXTIkMQqR8O/K1jUadBH6v1bWLZbwRfK3JGzpTDXRQ u+3g== X-Forwarded-Encrypted: i=1; AL+hErGimtQXwaCKyx+UfYG8dkaELzNuBwg/isgxM4kXGD85u+Rcz57DwyU3OUtepadvHgCx7/BkxERc9Era6jwSbw==@lists.linux.dev X-Gm-Message-State: APuU4Z8ErKLYcDITiFaig6mmWg0Mgaord65QXPRM2xx5auYqzB0Kkp6r 3jCspsk79AXL93Mf6rsjGT0MblQls8Q5bi4Q833ENk1cHUOewpqpnp5vYie0bY7nlh2//f+VZQ/ fBds/Zbw/hD/ChajnNegZE2FBMpR1lyMhc7P1Olh8GmPQauzVEJQKgvRlxz6KXN7UHQbl X-Gm-Gg: AZFdTZfcR4KIrc9oGORLxu1ddncgUd2J5WBoEv5Vnc5S38ZsKIphc7DEtbCECO32deA CuPqdMjMNLYocaN4WJ4+XuUiv4vOkPGIRGDrqr9xzsC+xpXYATlyTbTln7ej9ioIIS6XttDiBsm ok0fDpojO5GHgXDFNFx7OJm1LXU1A2TmV7mET+QjoXf2GvZsSUmy2TrpXKFG119YaQspOoFPHaN 6Xaq9077KGUUqtf+T6t4yAGWl9I8FPxCz4t4GcZM3AOjSvHhEWbqFsDOgqQi4+RwPIhOpjEETyN sjAjQhFL8ErJRngYlFnxk+W7PNMk2B32cFzhJLcEaLgqPV09wm+5vo+g+7IWn2vYD5yN1x3aiNu xiuGjEF6wIi5SOIg6POIW9c1JY1sBvSPS6Kj+IrG1+so= X-Received: by 2002:a05:6000:2288:b0:4ab:1d39:92cb with SMTP id ffacd0b85a97d-4abab6de737mr62399587f8f.2.1796058978160; Mon, 30 Nov 2026 09:16:18 -0800 (PST) X-Received: by 2002:a05:6000:2288:b0:4ab:1d39:92cb with SMTP id ffacd0b85a97d-4abab6de737mr62399498f8f.2.1796058977470; Mon, 30 Nov 2026 09:16:17 -0800 (PST) Received: from redhat.com ([2a0d:6fc0:1529:2e00:86ec:bef7:c309:ba1f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6c61f558sm67832250f8f.58.2026.11.30.09.16.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 09:16:16 -0800 (PST) Date: Mon, 30 Nov 2026 12:16:14 -0500 From: "Michael S. Tsirkin" To: Dragos Tatulea Cc: "eperezma@redhat.com" , Linfeng Sun , "virtualization@lists.linux.dev" , Jason Wang Subject: Re: [REGRESSION] vhost_iotlb: bound map allocation in add_range breaks vhost-vdpa on hosts with fragmented memory Message-ID: <20261130121224-mutt-send-email-mst@kernel.org> References: <0f51918f-88c9-4930-ae5a-616bbe73e7cb@nvidia.com> <20261028085839-mutt-send-email-mst@kernel.org> <808a6e4e-085d-4723-9489-f17f023949c0@nvidia.com> <20261028110041-mutt-send-email-mst@kernel.org> X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 1_HFIyrHUdCI-pICLG99fF_RVrwF2Es4erKdHdpwK3k_1796058978 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Oct 30, 2026 at 02:47:13PM +0100, Dragos Tatulea wrote: > On 28.10.26 16:17, Michael S. Tsirkin wrote: > > On Wed, Oct 28, 2026 at 03:46:05PM +0100, Dragos Tatulea wrote: > >> On 28.10.26 14:05, Michael S. Tsirkin wrote: > >>> On Wed, Oct 28, 2026 at 01:44:30PM +0100, Dragos Tatulea wrote: > >>>> Hi, > >>>> > >>>> We've started seeing regressions for a while in our verification CI for > >>>> vDPA Live Migration tests. Occasionally the FW returns a syndrome > >>>> related to invalid mappings during device reset. > >>>> > >>>> We've narrowed this down to commit 1ed35ac7f3fe "vhost_iotlb: bound map > >>>> allocation in add_range" which limits the iotlbs to 2K by default. On a > >>>> host with fragmented memory you can get easily more than 2K entries. > >>>> This results in truncated/incomplete iotlbs mapped to the device and when > >>>> the rings happen to be outside of the first 2K mappings the FW will complain > >>>> because it got addresses that are not mapped. > >>>> > >>>> I'm not sure how to fix this though: > >>>> > >>>> - LLM suggests setting the 2K limit only in VA mode and using 0 as > >>>> before in PA mode because PA mode is limited anyway. Does this > >>>> work? > >>> > >>> Well no, we do need the limit in PA mode I think. > >>> > >>> But we should maybe bound from what's actually being mapped rather than > >>> a fixed count. e.g. X entries per pinned page? > >> You could have many 1 PAGE_SIZE mappings though. It wouldn't help > >> then. > > > > it still mostly is 1 entry=1 pinned page > > > > sharing is possible but uncommon > > > >>> Bounding by bytes instead of entry count is another option.How to get this limit from userspace though? Would RLIMIT_MEMLOCK > >> be an option? Although `ulimit -l unlimited` would need to be > >> capped to some value... > > > > guest ram no? > > > >> > >> Also, if the entry limit is still there some kind of precedence between > >> memory and entry limit needs to be decided. > > > > I'd steal a page from rlimit then use that for the entries, when out of > > that - steal another page. > > > Do you mean something like this [1]? I worry about poking at mm internals like this. No nice helpers to use? Maybe add some? At least pls Cc mm people. > Remaining questions: > 1) Is it ok to delete max_iotlb_entries? Or keep it initialized to max? then we need some other limit, no? > 2) Is it ok to delete the module specific max_iotlb_entries param? For > example mlx5_vdpa_max_iotlb_entries. I don't see much use for it ... you are the guy for mlx5. I thought it bounds some other resources? > 3) Shouldn't the core or the driver track holes encounter due to > incomplete mappings? It would be good to not give bad addresses > to the hw/fw... Big issue is userspace isn't really prepared to handle mapping errors. > > Thanks, > Dragos > > [1]: > --- > drivers/vhost/vdpa.c | 72 +++++++++++++++++++++++++++++++++----------- > 1 file changed, 54 insertions(+), 18 deletions(-) > > diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c > index a4d0ce3dac04..224f950a22bd 100644 > --- a/drivers/vhost/vdpa.c > +++ b/drivers/vhost/vdpa.c > @@ -34,19 +34,18 @@ enum { > > #define VHOST_VDPA_DEV_MAX (1U << MINORBITS) > > -static int max_iotlb_entries = 2048; > -module_param(max_iotlb_entries, int, 0444); > -MODULE_PARM_DESC(max_iotlb_entries, > - "Maximum number of iotlb entries. (default: 2048)"); > - > #define VHOST_VDPA_IOTLB_BUCKETS 16 > > struct vhost_vdpa_as { > struct hlist_node hash_link; > struct vhost_iotlb iotlb; > + /* pages charged to RLIMIT_MEMLOCK for the IOTLB entries */ > + unsigned long charged_pages; > u32 id; > }; > > struct vhost_vdpa { > struct vhost_dev vdev; > struct iommu_domain *domain; > @@ -75,11 +74,41 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, > struct vhost_iotlb *iotlb, u64 start, > u64 last, u32 asid); > > +static inline struct vhost_vdpa_as *iotlb_to_as(struct vhost_iotlb *iotlb) > +{ > + return container_of(iotlb, struct vhost_vdpa_as, iotlb); > +} > + > static inline u32 iotlb_to_asid(struct vhost_iotlb *iotlb) > { > - struct vhost_vdpa_as *as = container_of(iotlb, struct > - vhost_vdpa_as, iotlb); > - return as->id; > + return iotlb_to_as(iotlb)->id; > +} > + > +static int vhost_vdpa_charge_maps(struct vhost_vdpa *v, > + struct vhost_vdpa_as *as, > + unsigned int nmaps) > +{ > + unsigned long pages = DIV_ROUND_UP(nmaps, (PAGE_SIZE / sizeof(struct vhost_iotlb_map))); > + struct mm_struct *mm = v->vdev.mm; > + unsigned long lock_limit; > + > + if (pages > as->charged_pages) { > + lock_limit = PFN_DOWN(rlimit(RLIMIT_MEMLOCK)); > + if (pages - as->charged_pages + > + atomic64_read(&mm->pinned_vm) > lock_limit) > + return -ENOMEM; > + atomic64_add(pages - as->charged_pages, &mm->pinned_vm); > + } else { > + atomic64_sub(as->charged_pages - pages, &mm->pinned_vm); > + } > + as->charged_pages = pages; > + > + return 0; > } > > static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid) > @@ -114,14 +143,11 @@ static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid) > > if (asid >= v->vdpa->nas) > return NULL; > - if (max_iotlb_entries <= 0) > - return NULL; > - > as = kmalloc_obj(*as); > if (!as) > return NULL; > > - vhost_iotlb_init(&as->iotlb, max_iotlb_entries, 0); > + vhost_iotlb_init(&as->iotlb, UINT_MAX, 0); > as->id = asid; > hlist_add_head(&as->hash_link, head); > > @@ -978,9 +1004,11 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, > struct vdpa_device *vdpa = v->vdpa; > > if (vdpa->use_va) > - return vhost_vdpa_va_unmap(v, iotlb, start, last, asid); > + vhost_vdpa_va_unmap(v, iotlb, start, last, asid); > + else > + vhost_vdpa_pa_unmap(v, iotlb, start, last, asid); > > - return vhost_vdpa_pa_unmap(v, iotlb, start, last, asid); > + vhost_vdpa_charge_maps(v, iotlb_to_as(iotlb), iotlb->nmaps); > } > > static int perm_to_iommu_flags(u32 perm) > @@ -1011,13 +1039,18 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, > struct vhost_dev *dev = &v->vdev; > struct vdpa_device *vdpa = v->vdpa; > const struct vdpa_config_ops *ops = vdpa->config; > - u32 asid = iotlb_to_asid(iotlb); > + struct vhost_vdpa_as *as = iotlb_to_as(iotlb); > + u32 asid = as->id; > int r = 0; > > + r = vhost_vdpa_charge_maps(v, as, iotlb->nmaps + 1); > + if (r) > + return r; > + > r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1, > pa, perm, opaque); > if (r) > - return r; > + goto uncharge; > > if (ops->dma_map) { > r = ops->dma_map(vdpa, asid, iova, size, pa, perm, opaque); > @@ -1031,13 +1064,16 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, > } > if (r) { > vhost_iotlb_del_range(iotlb, iova, iova + size - 1); > - return r; > + goto uncharge; > } > > if (!vdpa->use_va) > atomic64_add(PFN_DOWN(size), &dev->mm->pinned_vm); > > - return 0; > +uncharge: > + vhost_vdpa_charge_maps(v, as, iotlb->nmaps); > + return r; > } > > static void vhost_vdpa_unmap(struct vhost_vdpa *v, > -- > 2.55.0