From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pz2-f6.google.com (mail-pz2-f6.google.com [74.125.228.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B18BB5592E0 for ; Wed, 14 Oct 2026 16:33:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.228.6 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791995640; cv=none; b=IrQcgQGaolsfT8GTcK91FGRwzOJKQu5E6T8N3WErW17rtj9bh79MDguYgJbGpL6XQuJbgcd1rm7jI+EqRXiObsuSokcmSu1hZo2tJIcAbARe+U0D27lLk35ETeWnUpBwLYOtRAor5Wdgv03KwoLmq8pzLu750K4sFZFyM7dOXSs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791995640; c=relaxed/simple; bh=03prvWYuPM9S8kmgrWoC3fCmFozkGleIo9XhNkVgCtI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=od1ODIx2Z8pVyFApgiwLAFDmdx5sXeTluMBhoVlEUyF8JB+CfFp6EuG8cfq3QA/xeIFdZxp9KeBRyEDFkZnboEGMhVJiPQBHgqYo6i8g0/o1WY5OjjI7f8SFxy3HH1F9SqLSwQ69Y5zToU+U1fzGAvY3xMysVq36x6INOWs/KuQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Pd7irBGa; arc=none smtp.client-ip=74.125.228.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Pd7irBGa" Received: by mail-pz2-f6.google.com with SMTP id 41be03b00d2f7-cc790c60953so252208a12.1 for ; Wed, 14 Oct 2026 09:33:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791995639; x=1792600439; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=Gzw9aMKz/xODwzz6jPerfVV+ZDaTeiwtt+GIjli64uI=; b=Pd7irBGaIfGk47A4W8byWcdXxiyBIqvKEbA9YhOh3SRQ6mJyIKIRRLuIPMYQAvnKMs TiiQbQe1X9LW8PMlgREmTed2G73VpjPu9b/hQC6M7174NlPobQQvYC7JbWww84NvkGVc jaPpkkunMPeYYbs7LRpYE1NB3orJeiitk5w+ztYeo5vRK1StrQIIlrqN8LPbbwn/q7pl LyTa9PdacwnhwmfVzvAYeCHjMCxA/kI1BexjubiZ4KYMJIByLay2i2hNOuiNpYiGNnZr oredyfHyKPao1I1MMDdfl/UgbblGTGEjrFSK49edHqKhn/nAtTq070F5HAFN7V6SOwU9 2M8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791995639; x=1792600439; 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=Gzw9aMKz/xODwzz6jPerfVV+ZDaTeiwtt+GIjli64uI=; b=rX3cSAUUvLV6tSZ2XKIKco1/8m5kr0qJMXhbO6EC2OJLtNAC/T06wPxTBvEZZR5wL8 /V1TK4iS4vuUU2dTuVHtpR1h0BwPBMAVLZU9xpnlyF/ouIj0tuI2W+0PshI2YNqEC4zd 32hD6ikpBVoZNdo8BvdXyVRBBfSC67szq4abalADdVIeT2/CicCUzkXCB9A68o9JbEnp ZOOuqnjUncq9yhvErPByKATv2aEnd9jYB/h3/L9nmDd+KDXlM4OfczGDbZ3CKz4H1Fdu EdFmK4IbtoNM5o8mF/D+bBUMmq3Og7nZot5KQMpv8YswoLPLiNn5t8G/O7CGJBrvvyRK q4nQ== X-Forwarded-Encrypted: i=1; AKwUvBxiQWOt5vG19imY+nYQkFYu8kSjpAfnlPacW1rTnjpsQ8Cm94VGT/AGNZJxxM8jF908RMEe1OjXlC7XtA==@vger.kernel.org X-Gm-Message-State: AFq9FYLsn/JD2hxEy/WUAmAHnDew1iGKvDHXhGs/bfAiffekzdb01CyC heJndmjjwYCUkw4wASeNDD72ATJ07R6y4Q40Bt/k6McULyZYg7wtj0Ph X-Gm-Gg: AYBFou3jxpiTrTieuo8gQFyQ2v/AD38YP1dvB7bi7S+Ex8g+m951LoLtOYFkFT17/an vQRFjtMJEynQRPgycZucQuOfyyk4YvL4J9odF7YVPQ+G4i6g+BO9MFahYQEiVegCmgYA915NmmY DVXmvHlvE5G01LMtFTYnWipOrt+xkWm6kasV43hL2VGOTVKmx9FgQczt5rKnLYxTjy8FOBj4Y4J Zp2IRYl7OPZFbZLCsn/SFL+yZEEqjBa7Ks+svPP1yk1c9tavhlKxm6eDHf4PJZO6x30P/d7XJYg Ib2OfKz0hs2VD57wfwXxH74TxOm6VhmvpxfEBFknhaN0HGEhrH3C7yi2zXV0YkXPN7h1urpgSlS lxETPJtZHcBFNVAuASELVwN5wcLsSaBnXbof7sBswB4kIBY/qwcmEtbmSVX1+y4V5n29ULpq0Oe 2yMhVNL0M0fCMJrkjLXYmdqKCJyxslzZakZ5u1oF7lrYfJ5gNtOnqH1qac0IBilCIb X-Received: by 2002:a05:6a00:8c4:b0:891:a63c:df97 with SMTP id d2e1a72fcca58-897c6443681mr15560946b3a.15.1791995639007; Wed, 14 Oct 2026 09:33:59 -0700 (PDT) Received: from localhost ([2a03:2880:2ff:59::]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-89d80511d47sm1291926b3a.54.2026.10.14.09.33.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Oct 2026 09:33:58 -0700 (PDT) Date: Wed, 14 Oct 2026 09:33:38 -0700 From: Stanislav Fomichev To: Mina Almasry Cc: Jakub Kicinski , David Wei , Pavel Begunkov , Taehee Yoo , Stanislav Fomichev , Paolo Abeni , Kaiyuan Zhang , Bobby Eshleman , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, Andrew Lunn , "David S. Miller" , Eric Dumazet , Simon Horman , Shuah Khan , Sumit Semwal , Christian =?utf-8?B?S8O2bmln?= , Nikolay Aleksandrov , Daniel Borkmann , Tariq Toukan , Kaifeng Wang Subject: Re: [PATCH net v2 1/4] net: devmem: unmap dma_buf synchronously on queue uninstall Message-ID: References: <20261012174300.2338784-1-almasrymina@google.com> <20261012174300.2338784-2-almasrymina@google.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20261012174300.2338784-2-almasrymina@google.com> On 10/12, Mina Almasry wrote: > When a bound net_device is unregistered while userspace still holds a > netlink or TCP socket reference on a devmem binding, > mp_dmabuf_devmem_uninstall() clears binding->dev to NULL so > netdev_nl_sock_priv_destroy() will not touch the unregistered > net_device, but leaves the dma_buf attachment mapped until > __net_devmem_dmabuf_binding_free() runs later on socket close. > > Because dma_buf_attach() does not take a reference on dma_dev, device > removal frees dma_dev while the attachment is still mapped. Closing the > netlink socket afterwards triggers a slab use-after-free in > dma_unmap_sg_attrs(): > > BUG: KASAN: slab-use-after-free in dma_unmap_sg_attrs+0x76/0x2b0 > Read of size 8 at addr ffff88810fae64b0 by task kworker/3:1/53 > Workqueue: events __net_devmem_dmabuf_binding_free > Call Trace: > dma_unmap_sg_attrs+0x76/0x2b0 > unmap_udmabuf+0x8e/0x100 > dma_buf_unmap_attachment+0x145/0x330 > dma_buf_unmap_attachment_unlocked+0x96/0x130 > __net_devmem_dmabuf_binding_free+0x156/0x2e0 > Freed by task 9: > kfree+0x1b8/0x550 > nsim_bus_dev_release+0xe/0x60 > device_release+0xcf/0x250 > > Add net_devmem_dmabuf_binding_unmap() serialized under binding->lock > and null out binding->sgt and binding->attachment after unmapping so > unmapping is idempotent across queue uninstall and deferred binding > destruction. Call it synchronously when the last bound RX queue is > uninstalled in mp_dmabuf_devmem_uninstall(), except when a leased > queue is unleased: the physical device is still registered and, if > restarting its queue failed, may still be using the binding. > > The same use-after-free happens when the netlink socket is closed > before the device is removed but RX tokens or the pending release work > keep the binding alive. Such bindings are no longer installed on any > queue and were already erased from net_devmem_dmabuf_bindings. Keep > bindings in net_devmem_dmabuf_bindings until they are freed, hide > unbound ones from TX lookups with an xarray mark, and on > NETDEV_UNREGISTER clear binding->dev of unbound RX bindings for the > device and unmap them, unless the device is a virtual device leasing [..] > queues, for the same reason as above. net_devmem_lock serializes that > walk with the final free. Can you explain net_devmem_lock a bit more? In net_devmem_dev_unregister, why can you not net_devmem_lookup_dmabuf and then rely on the binding->lock? The rest looks good, but the new lock seems a bit excessive?