From nobody Sun Feb 8 13:39:11 2026 Received: from unimail.uni-dortmund.de (mx1.hrz.uni-dortmund.de [129.217.128.51]) (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 2CA753A0B33; Wed, 7 Jan 2026 21:06:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.217.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819978; cv=none; b=r11U4dauRE04ejvZzo35KaN6AvQt9KELkU0+bNJE6jZTQcn8UY1f1i6uQvFGdGvu4Nla3NRA6OxHQT/oTOQiXml6v0fDA7NpvYpzsC12TbLUFpr3n4b6aayu0/1xvX07C46/xibi3JjZU55DvdxxEVomIxN+uCyGY98enyn9XQ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819978; c=relaxed/simple; bh=i6EXM/dz88Cyoq8LtFGDSx5xkGpNbJe1oE+QY2KW7uQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CKTV/gpk6n17oC04T781pji2k2OOEPtMCznJPuu7zHw1sOfnKB3gdsJ5FZZyv9Unw75xoINPncV35cgqZ6Ur5cQ1BJQJx3HdQa8Htsl5bW6OchTSr1PXupMOnhLPKA+KKt6TQeqS4gVRG+4EDrr/NtIo0iPiKXe7fgwK0JYhpxQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de; spf=pass smtp.mailfrom=tu-dortmund.de; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b=lTDO/r9U; arc=none smtp.client-ip=129.217.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b="lTDO/r9U" Received: from simon-Latitude-5450.fritz.box (p5dc880d2.dip0.t-ipconnect.de [93.200.128.210]) (authenticated bits=0) by unimail.uni-dortmund.de (8.18.1.16/8.18.1.16) with ESMTPSA id 607L5t9H026667 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Jan 2026 22:05:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tu-dortmund.de; s=unimail; t=1767819960; bh=i6EXM/dz88Cyoq8LtFGDSx5xkGpNbJe1oE+QY2KW7uQ=; h=From:To:Subject:Date:In-Reply-To:References; b=lTDO/r9UTXkuMXbSINWBD7E+CPaAPkqmT7IPIn58KsophOcArTZxKHTU3djCpgFqq TdYgigqJn0aNSEmP5iFl+5fl1j5LKHt4H93NTZNuoBCUN4WrYK6rpmvqr04Lj4bs3S IDJenQI6/2eFYftf8bS1pWX5Gs6mYRK21n9566BY= From: Simon Schippers To: willemdebruijn.kernel@gmail.com, jasowang@redhat.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, eperezma@redhat.com, leiyang@redhat.com, stephen@networkplumber.org, jon@nutanix.com, tim.gebauer@tu-dortmund.de, simon.schippers@tu-dortmund.de, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH net-next v7 1/9] ptr_ring: move free-space check into separate helper Date: Wed, 7 Jan 2026 22:04:40 +0100 Message-ID: <20260107210448.37851-2-simon.schippers@tu-dortmund.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> References: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch moves the check for available free space for a new entry into a separate function. As a result, __ptr_ring_produce() remains logically unchanged, while the new helper allows callers to determine in advance whether subsequent __ptr_ring_produce() calls will succeed. This information can, for example, be used to temporarily stop producing until __ptr_ring_peek() indicates that space is available again. Co-developed-by: Tim Gebauer Signed-off-by: Tim Gebauer Signed-off-by: Simon Schippers --- include/linux/ptr_ring.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h index 534531807d95..a5a3fa4916d3 100644 --- a/include/linux/ptr_ring.h +++ b/include/linux/ptr_ring.h @@ -96,6 +96,14 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r) return ret; } =20 +static inline int __ptr_ring_produce_peek(struct ptr_ring *r) +{ + if (unlikely(!r->size) || r->queue[r->producer]) + return -ENOSPC; + + return 0; +} + /* Note: callers invoking this in a loop must use a compiler barrier, * for example cpu_relax(). Callers must hold producer_lock. * Callers are responsible for making sure pointer that is being queued @@ -103,8 +111,10 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r) */ static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr) { - if (unlikely(!r->size) || r->queue[r->producer]) - return -ENOSPC; + int p =3D __ptr_ring_produce_peek(r); + + if (p) + return p; =20 /* Make sure the pointer we are storing points to a valid data. */ /* Pairs with the dependency ordering in __ptr_ring_consume. */ --=20 2.43.0 From nobody Sun Feb 8 13:39:11 2026 Received: from unimail.uni-dortmund.de (mx1.hrz.uni-dortmund.de [129.217.128.51]) (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 B4337321428; Wed, 7 Jan 2026 21:06:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.217.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819979; cv=none; b=BFwdzF9zA7hKYC2LL+GAtwC5amDQb/DrewHjwEJDxpJ8SIE4205S/bHraiNkbG44tHsmvh2gRwo97gW2mUSd5nhXonqtHdjMbSGbzAY+Kg6kNfnqHrBVaIGiabNswGQJQevYUyRLILjLGHEgFnam0UhbC+cI+diP8kH89Yzjsdw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819979; c=relaxed/simple; bh=jJEomV68RwsvP53WBudoctRPvBFglCe7Oe95ccXv+98=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lRIj7Qd6uoV/kiBTR4KoZa1uEfY7+fLkCb5uO/FGHpxkxzYTh2818+4tOfApL3su31bO6STMVjjHdzA5W9hvpADG1qd+9C1WGcP82lwopn2VUY7kUgVuoPlJU459SEaLiXWnLffVkL0+4OUPY92Z65BF+iv5chN5FLZJDFAQRr4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de; spf=pass smtp.mailfrom=tu-dortmund.de; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b=m+0kD3JN; arc=none smtp.client-ip=129.217.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b="m+0kD3JN" Received: from simon-Latitude-5450.fritz.box (p5dc880d2.dip0.t-ipconnect.de [93.200.128.210]) (authenticated bits=0) by unimail.uni-dortmund.de (8.18.1.16/8.18.1.16) with ESMTPSA id 607L5t9J026667 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Jan 2026 22:06:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tu-dortmund.de; s=unimail; t=1767819960; bh=jJEomV68RwsvP53WBudoctRPvBFglCe7Oe95ccXv+98=; h=From:To:Subject:Date:In-Reply-To:References; b=m+0kD3JNyvTIIinmt68ZFElyRLtdu4hok1yqyuwxcl3jtM52HmJ81rhSB5Kz3jgNV /Q8UxAF9sb7vw1mCNjK8U+yg6en23U9ctS4WZx8y+bsAvDd92/rTiM8msXyasyBeNM E1PiTD0RoCz9VF6WGqpN2jRppraZx9YX5bLYlp5o= From: Simon Schippers To: willemdebruijn.kernel@gmail.com, jasowang@redhat.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, eperezma@redhat.com, leiyang@redhat.com, stephen@networkplumber.org, jon@nutanix.com, tim.gebauer@tu-dortmund.de, simon.schippers@tu-dortmund.de, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH net-next v7 2/9] ptr_ring: add helper to detect newly freed space on consume Date: Wed, 7 Jan 2026 22:04:41 +0100 Message-ID: <20260107210448.37851-3-simon.schippers@tu-dortmund.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> References: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This proposed function checks whether __ptr_ring_zero_tail() was invoked within the last n calls to __ptr_ring_consume(), which indicates that new free space was created. Since __ptr_ring_zero_tail() moves the tail to the head - and no other function modifies either the head or the tail, aside from the wrap-around case described below - detecting such a movement is sufficient to detect the invocation of __ptr_ring_zero_tail(). The implementation detects this movement by checking whether the tail is at most n positions behind the head. If this condition holds, the shift of the tail to its current position must have occurred within the last n calls to __ptr_ring_consume(), indicating that __ptr_ring_zero_tail() was invoked and that new free space was created. This logic also correctly handles the wrap-around case in which __ptr_ring_zero_tail() is invoked and the head and the tail are reset to 0. Since this reset likewise moves the tail to the head, the same detection logic applies. Co-developed-by: Tim Gebauer Signed-off-by: Tim Gebauer Signed-off-by: Simon Schippers --- include/linux/ptr_ring.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h index a5a3fa4916d3..7cdae6d1d400 100644 --- a/include/linux/ptr_ring.h +++ b/include/linux/ptr_ring.h @@ -438,6 +438,19 @@ static inline int ptr_ring_consume_batched_bh(struct p= tr_ring *r, return ret; } =20 +/* Returns true if the consume of the last n elements has created space + * in the ring buffer (i.e., a new element can be produced). + * + * Note: Because of batching, a successful call to __ptr_ring_consume() / + * __ptr_ring_consume_batched() does not guarantee that the next call to + * __ptr_ring_produce() will succeed. + */ +static inline bool __ptr_ring_consume_created_space(struct ptr_ring *r, + int n) +{ + return r->consumer_head - r->consumer_tail < n; +} + /* Cast to structure type and call a function without discarding from FIFO. * Function must return a value. * Callers must take consumer_lock. --=20 2.43.0 From nobody Sun Feb 8 13:39:11 2026 Received: from unimail.uni-dortmund.de (mx1.hrz.uni-dortmund.de [129.217.128.51]) (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 2CB012EC08C; Wed, 7 Jan 2026 21:06:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.217.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819979; cv=none; b=KoOIruqpjxWgR9z68DAfGJhdEcnaIeXn0aD9eMdM/qv/eVo8MsBnsG19SQ5XRjZQZB+SzEVC6Jca8V9/9csZDgfznaz/KpBhtu5dU4m1f6PdQUv1MFwLm22JhlulNrB3kAqQpmj19qhcPFQ1kvh/a5gTaQS5tsUwszKQN81S5hU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819979; c=relaxed/simple; bh=HCYmM7s6dRW4FQSt1RT7rihNxVFUvMIz4wDTbzVtfKY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ujKpfQvXUkasSzD8S9yj25wuDQJZxuD/4Q0h/gMi8QY38sNMILnHaLvZpo2a75sc2BGfPIWAa9p63ubdWHVKTb8ZQMmJvnAXWWlMamjGJpVmspy1H6Lrvf3qxIdm/KZdNN7+KHLwamZxGC15Smox9MpSxdIOg/xm/PpTXJdmHLY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de; spf=pass smtp.mailfrom=tu-dortmund.de; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b=MZSj2aIN; arc=none smtp.client-ip=129.217.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b="MZSj2aIN" Received: from simon-Latitude-5450.fritz.box (p5dc880d2.dip0.t-ipconnect.de [93.200.128.210]) (authenticated bits=0) by unimail.uni-dortmund.de (8.18.1.16/8.18.1.16) with ESMTPSA id 607L5t9L026667 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Jan 2026 22:06:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tu-dortmund.de; s=unimail; t=1767819961; bh=HCYmM7s6dRW4FQSt1RT7rihNxVFUvMIz4wDTbzVtfKY=; h=From:To:Subject:Date:In-Reply-To:References; b=MZSj2aINfNzYv20xgimT8AkTfn37kMqJ7jcnfk34vLqS0dL90WgOOC5a2rrJolemK WszEobn7ee4EvEkozDEisv/rwCmto6DSeYse+CGdkcRbW1IvLKxZFn7zprO6cJRiMa zfCpTr4uAaovfiq2+0MffQrkD0km4mP1ok/YiVFc= From: Simon Schippers To: willemdebruijn.kernel@gmail.com, jasowang@redhat.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, eperezma@redhat.com, leiyang@redhat.com, stephen@networkplumber.org, jon@nutanix.com, tim.gebauer@tu-dortmund.de, simon.schippers@tu-dortmund.de, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH net-next v7 3/9] tun/tap: add ptr_ring consume helper with netdev queue wakeup Date: Wed, 7 Jan 2026 22:04:42 +0100 Message-ID: <20260107210448.37851-4-simon.schippers@tu-dortmund.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> References: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce {tun,tap}_ring_consume() helpers that wrap __ptr_ring_consume() and wake the corresponding netdev subqueue when consuming an entry frees space in the underlying ptr_ring. Stopping of the netdev queue when the ptr_ring is full will be introduced in an upcoming commit. Co-developed-by: Tim Gebauer Signed-off-by: Tim Gebauer Signed-off-by: Simon Schippers --- drivers/net/tap.c | 23 ++++++++++++++++++++++- drivers/net/tun.c | 25 +++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/drivers/net/tap.c b/drivers/net/tap.c index 1197f245e873..2442cf7ac385 100644 --- a/drivers/net/tap.c +++ b/drivers/net/tap.c @@ -753,6 +753,27 @@ static ssize_t tap_put_user(struct tap_queue *q, return ret ? ret : total; } =20 +static void *tap_ring_consume(struct tap_queue *q) +{ + struct ptr_ring *ring =3D &q->ring; + struct net_device *dev; + void *ptr; + + spin_lock(&ring->consumer_lock); + + ptr =3D __ptr_ring_consume(ring); + if (unlikely(ptr && __ptr_ring_consume_created_space(ring, 1))) { + rcu_read_lock(); + dev =3D rcu_dereference(q->tap)->dev; + netif_wake_subqueue(dev, q->queue_index); + rcu_read_unlock(); + } + + spin_unlock(&ring->consumer_lock); + + return ptr; +} + static ssize_t tap_do_read(struct tap_queue *q, struct iov_iter *to, int noblock, struct sk_buff *skb) @@ -774,7 +795,7 @@ static ssize_t tap_do_read(struct tap_queue *q, TASK_INTERRUPTIBLE); =20 /* Read frames from the queue */ - skb =3D ptr_ring_consume(&q->ring); + skb =3D tap_ring_consume(q); if (skb) break; if (noblock) { diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 8192740357a0..7148f9a844a4 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -2113,13 +2113,34 @@ static ssize_t tun_put_user(struct tun_struct *tun, return total; } =20 +static void *tun_ring_consume(struct tun_file *tfile) +{ + struct ptr_ring *ring =3D &tfile->tx_ring; + struct net_device *dev; + void *ptr; + + spin_lock(&ring->consumer_lock); + + ptr =3D __ptr_ring_consume(ring); + if (unlikely(ptr && __ptr_ring_consume_created_space(ring, 1))) { + rcu_read_lock(); + dev =3D rcu_dereference(tfile->tun)->dev; + netif_wake_subqueue(dev, tfile->queue_index); + rcu_read_unlock(); + } + + spin_unlock(&ring->consumer_lock); + + return ptr; +} + static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err) { DECLARE_WAITQUEUE(wait, current); void *ptr =3D NULL; int error =3D 0; =20 - ptr =3D ptr_ring_consume(&tfile->tx_ring); + ptr =3D tun_ring_consume(tfile); if (ptr) goto out; if (noblock) { @@ -2131,7 +2152,7 @@ static void *tun_ring_recv(struct tun_file *tfile, in= t noblock, int *err) =20 while (1) { set_current_state(TASK_INTERRUPTIBLE); - ptr =3D ptr_ring_consume(&tfile->tx_ring); + ptr =3D tun_ring_consume(tfile); if (ptr) break; if (signal_pending(current)) { --=20 2.43.0 From nobody Sun Feb 8 13:39:11 2026 Received: from unimail.uni-dortmund.de (mx1.hrz.uni-dortmund.de [129.217.128.51]) (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 B44EA33AD89; Wed, 7 Jan 2026 21:06:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.217.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819978; cv=none; b=pXoXvv00gn63sNnXIIRq/xYfdoFP45VU+0+Bb7VxEj0QWktctpgTeS3m9dFpmVMTilFRi/jfHTz+RbBkOb4ZmAdbr1Ifj5E3mCluzvkLkXKMgzeFzhLV2wAXdtodl9m8kidipbzgcjoAJGoBWcvlrwMOKWtdIfiR5+ArlhHO+ps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819978; c=relaxed/simple; bh=5XhV857b4Zt0CASKZBxygyRwR2i/i6QOZmWNbvIntIQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RAX+2aFhkWg0/4RK0yWAwJdqAM4FZA1Ms1aXJ017a0G/JwsVzuJL/kcy0jGXQL1dSFqruZYQi+1AfRDLQUgqiyKHYf8zcbvh2RCczVK7dKOEwgNqKNg8SEaM1WBrYiROOku89Suvy7TyLEdHd/Q9HqhOxSnJ8cTzg/6YbVqEuCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de; spf=pass smtp.mailfrom=tu-dortmund.de; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b=Zlxs0MP0; arc=none smtp.client-ip=129.217.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b="Zlxs0MP0" Received: from simon-Latitude-5450.fritz.box (p5dc880d2.dip0.t-ipconnect.de [93.200.128.210]) (authenticated bits=0) by unimail.uni-dortmund.de (8.18.1.16/8.18.1.16) with ESMTPSA id 607L5t9N026667 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Jan 2026 22:06:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tu-dortmund.de; s=unimail; t=1767819961; bh=5XhV857b4Zt0CASKZBxygyRwR2i/i6QOZmWNbvIntIQ=; h=From:To:Subject:Date:In-Reply-To:References; b=Zlxs0MP0MeZWEY5o8s83hir/dpKxKP6jSSDNvqfqUeCHyp9VE0USm3WYlne0P52OI 1Fl8Qk9hMaGSPB6PSpGTj3Tjv2gUNPzAdOdkQC9zuqAmPJtAvEnZTdlNJmR4kOiuc1 A2c8FMVix1qgvV8eCQfehr2KKWUgJf+gWcJQN6TQ= From: Simon Schippers To: willemdebruijn.kernel@gmail.com, jasowang@redhat.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, eperezma@redhat.com, leiyang@redhat.com, stephen@networkplumber.org, jon@nutanix.com, tim.gebauer@tu-dortmund.de, simon.schippers@tu-dortmund.de, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH net-next v7 4/9] tun/tap: add batched ptr_ring consume functions with netdev queue wakeup Date: Wed, 7 Jan 2026 22:04:43 +0100 Message-ID: <20260107210448.37851-5-simon.schippers@tu-dortmund.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> References: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add {tun,tap}_ring_consume_batched() that wrap __ptr_ring_consume_batched() and wake the corresponding netdev subqueue when consuming the entries frees space in the ptr_ring. These wrappers are supposed to be used by vhost-net in an upcoming commit. Co-developed-by: Tim Gebauer Signed-off-by: Tim Gebauer Signed-off-by: Simon Schippers --- drivers/net/tap.c | 23 +++++++++++++++++++++++ drivers/net/tun.c | 23 +++++++++++++++++++++++ include/linux/if_tap.h | 6 ++++++ include/linux/if_tun.h | 7 +++++++ 4 files changed, 59 insertions(+) diff --git a/drivers/net/tap.c b/drivers/net/tap.c index 2442cf7ac385..7e3b4eed797c 100644 --- a/drivers/net/tap.c +++ b/drivers/net/tap.c @@ -774,6 +774,29 @@ static void *tap_ring_consume(struct tap_queue *q) return ptr; } =20 +int tap_ring_consume_batched(struct file *file, void **array, int n) +{ + struct tap_queue *q =3D file->private_data; + struct ptr_ring *ring =3D &q->ring; + struct net_device *dev; + int i; + + spin_lock(&ring->consumer_lock); + + i =3D __ptr_ring_consume_batched(ring, array, n); + if (__ptr_ring_consume_created_space(ring, i)) { + rcu_read_lock(); + dev =3D rcu_dereference(q->tap)->dev; + netif_wake_subqueue(dev, q->queue_index); + rcu_read_unlock(); + } + + spin_unlock(&ring->consumer_lock); + + return i; +} +EXPORT_SYMBOL_GPL(tap_ring_consume_batched); + static ssize_t tap_do_read(struct tap_queue *q, struct iov_iter *to, int noblock, struct sk_buff *skb) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 7148f9a844a4..db3b72025cfb 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -3736,6 +3736,29 @@ struct socket *tun_get_socket(struct file *file) } EXPORT_SYMBOL_GPL(tun_get_socket); =20 +int tun_ring_consume_batched(struct file *file, void **array, int n) +{ + struct tun_file *tfile =3D file->private_data; + struct ptr_ring *ring =3D &tfile->tx_ring; + struct net_device *dev; + int i; + + spin_lock(&ring->consumer_lock); + + i =3D __ptr_ring_consume_batched(ring, array, n); + if (__ptr_ring_consume_created_space(ring, i)) { + rcu_read_lock(); + dev =3D rcu_dereference(tfile->tun)->dev; + netif_wake_subqueue(dev, tfile->queue_index); + rcu_read_unlock(); + } + + spin_unlock(&ring->consumer_lock); + + return i; +} +EXPORT_SYMBOL_GPL(tun_ring_consume_batched); + struct ptr_ring *tun_get_tx_ring(struct file *file) { struct tun_file *tfile; diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h index 553552fa635c..cf8b90320b8d 100644 --- a/include/linux/if_tap.h +++ b/include/linux/if_tap.h @@ -11,6 +11,7 @@ struct socket; #if IS_ENABLED(CONFIG_TAP) struct socket *tap_get_socket(struct file *); struct ptr_ring *tap_get_ptr_ring(struct file *file); +int tap_ring_consume_batched(struct file *file, void **array, int n); #else #include #include @@ -22,6 +23,11 @@ static inline struct ptr_ring *tap_get_ptr_ring(struct f= ile *f) { return ERR_PTR(-EINVAL); } +static inline int tap_ring_consume_batched(struct file *f, + void **array, int n) +{ + return 0; +} #endif /* CONFIG_TAP */ =20 /* diff --git a/include/linux/if_tun.h b/include/linux/if_tun.h index 80166eb62f41..444dda75a372 100644 --- a/include/linux/if_tun.h +++ b/include/linux/if_tun.h @@ -22,6 +22,7 @@ struct tun_msg_ctl { #if defined(CONFIG_TUN) || defined(CONFIG_TUN_MODULE) struct socket *tun_get_socket(struct file *); struct ptr_ring *tun_get_tx_ring(struct file *file); +int tun_ring_consume_batched(struct file *file, void **array, int n); =20 static inline bool tun_is_xdp_frame(void *ptr) { @@ -55,6 +56,12 @@ static inline struct ptr_ring *tun_get_tx_ring(struct fi= le *f) return ERR_PTR(-EINVAL); } =20 +static inline int tun_ring_consume_batched(struct file *file, + void **array, int n) +{ + return 0; +} + static inline bool tun_is_xdp_frame(void *ptr) { return false; --=20 2.43.0 From nobody Sun Feb 8 13:39:11 2026 Received: from unimail.uni-dortmund.de (mx1.hrz.uni-dortmund.de [129.217.128.51]) (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 B43EE32939F; Wed, 7 Jan 2026 21:06:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.217.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819979; cv=none; b=UiM/3ncAF2AkrknXJOe0HqCWQnSDE1snAOc4oNTgRHyGAnwMdIKnPwiB90pVWQ5cF3VhZLosjC/OU+esFsAB/70cofC+Y3Fn4z/yY93wQMzBhg/k93ITMffODvlUtksPLgjM0g9fykQ2NUBxaTq0QopEnCB61SMRd93ZMtj8PrE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819979; c=relaxed/simple; bh=mE6aOBUjGg0nHy0ZV8Lx2Ex+VEWWhdSTTr16919jAIw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XGq1Zab/QnHwNSmRfJFgCvkoSf2FDcMf5bxrizWbG38OhwhT7JlTn2Y8njtlMb+eEbJjnbn9jeA1AYHWJBpLPr+iROUdVevJBhStZJJxp+z0/jZ+63LZCCZt/HGhZB7x9MlSNOw1zm5Q8ZQ5xeUlrUkZ7WJAu+101RFxzFV1tmo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de; spf=pass smtp.mailfrom=tu-dortmund.de; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b=d8Qok6D1; arc=none smtp.client-ip=129.217.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b="d8Qok6D1" Received: from simon-Latitude-5450.fritz.box (p5dc880d2.dip0.t-ipconnect.de [93.200.128.210]) (authenticated bits=0) by unimail.uni-dortmund.de (8.18.1.16/8.18.1.16) with ESMTPSA id 607L5t9P026667 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Jan 2026 22:06:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tu-dortmund.de; s=unimail; t=1767819962; bh=mE6aOBUjGg0nHy0ZV8Lx2Ex+VEWWhdSTTr16919jAIw=; h=From:To:Subject:Date:In-Reply-To:References; b=d8Qok6D18JHIjXiEFrGaKUbFJt8b7jFKiJPWsGKLQ23xREaFtZFgQnZvjQOnhVcU9 fD7uegoWms5iUXzyySKj4PJqVLJnXYL/6jCyBmcZcoQaZdZqPxZ1ESDfgLkL1VXZ4i dBrx4/cOQ/7VWbJoTRyXlZ2nM5dXdSCUkE9K+V84= From: Simon Schippers To: willemdebruijn.kernel@gmail.com, jasowang@redhat.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, eperezma@redhat.com, leiyang@redhat.com, stephen@networkplumber.org, jon@nutanix.com, tim.gebauer@tu-dortmund.de, simon.schippers@tu-dortmund.de, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH net-next v7 5/9] tun/tap: add unconsume function for returning entries to ptr_ring Date: Wed, 7 Jan 2026 22:04:44 +0100 Message-ID: <20260107210448.37851-6-simon.schippers@tu-dortmund.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> References: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add {tun,tap}_ring_unconsume() wrappers to allow external modules (e.g. vhost-net) to return previously consumed entries back to the ptr_ring. The functions delegate to ptr_ring_unconsume() and take a destroy callback for entries that cannot be returned to the ring. Co-developed-by: Tim Gebauer Signed-off-by: Tim Gebauer Co-developed by: Jon Kohler Signed-off-by: Jon Kohler Signed-off-by: Simon Schippers --- drivers/net/tap.c | 10 ++++++++++ drivers/net/tun.c | 10 ++++++++++ include/linux/if_tap.h | 4 ++++ include/linux/if_tun.h | 5 +++++ 4 files changed, 29 insertions(+) diff --git a/drivers/net/tap.c b/drivers/net/tap.c index 7e3b4eed797c..4ffe4e95b5a6 100644 --- a/drivers/net/tap.c +++ b/drivers/net/tap.c @@ -797,6 +797,16 @@ int tap_ring_consume_batched(struct file *file, void *= *array, int n) } EXPORT_SYMBOL_GPL(tap_ring_consume_batched); =20 +void tap_ring_unconsume(struct file *file, void **batch, int n, + void (*destroy)(void *)) +{ + struct tap_queue *q =3D file->private_data; + struct ptr_ring *ring =3D &q->ring; + + ptr_ring_unconsume(ring, batch, n, destroy); +} +EXPORT_SYMBOL_GPL(tap_ring_unconsume); + static ssize_t tap_do_read(struct tap_queue *q, struct iov_iter *to, int noblock, struct sk_buff *skb) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index db3b72025cfb..d44d206c65e8 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -3759,6 +3759,16 @@ int tun_ring_consume_batched(struct file *file, void= **array, int n) } EXPORT_SYMBOL_GPL(tun_ring_consume_batched); =20 +void tun_ring_unconsume(struct file *file, void **batch, int n, + void (*destroy)(void *)) +{ + struct tun_file *tfile =3D file->private_data; + struct ptr_ring *ring =3D &tfile->tx_ring; + + ptr_ring_unconsume(ring, batch, n, destroy); +} +EXPORT_SYMBOL_GPL(tun_ring_unconsume); + struct ptr_ring *tun_get_tx_ring(struct file *file) { struct tun_file *tfile; diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h index cf8b90320b8d..28326a69745a 100644 --- a/include/linux/if_tap.h +++ b/include/linux/if_tap.h @@ -12,6 +12,8 @@ struct socket; struct socket *tap_get_socket(struct file *); struct ptr_ring *tap_get_ptr_ring(struct file *file); int tap_ring_consume_batched(struct file *file, void **array, int n); +void tap_ring_unconsume(struct file *file, void **batch, int n, + void (*destroy)(void *)); #else #include #include @@ -28,6 +30,8 @@ static inline int tap_ring_consume_batched(struct file *f, { return 0; } +static inline void tap_ring_unconsume(struct file *file, void **batch, + int n, void (*destroy)(void *)) {} #endif /* CONFIG_TAP */ =20 /* diff --git a/include/linux/if_tun.h b/include/linux/if_tun.h index 444dda75a372..1274c6b34eb6 100644 --- a/include/linux/if_tun.h +++ b/include/linux/if_tun.h @@ -23,6 +23,8 @@ struct tun_msg_ctl { struct socket *tun_get_socket(struct file *); struct ptr_ring *tun_get_tx_ring(struct file *file); int tun_ring_consume_batched(struct file *file, void **array, int n); +void tun_ring_unconsume(struct file *file, void **batch, int n, + void (*destroy)(void *)); =20 static inline bool tun_is_xdp_frame(void *ptr) { @@ -62,6 +64,9 @@ static inline int tun_ring_consume_batched(struct file *f= ile, return 0; } =20 +static inline void tun_ring_unconsume(struct file *file, void **batch, + int n, void (*destroy)(void *)) {} + static inline bool tun_is_xdp_frame(void *ptr) { return false; --=20 2.43.0 From nobody Sun Feb 8 13:39:11 2026 Received: from unimail.uni-dortmund.de (mx1.hrz.uni-dortmund.de [129.217.128.51]) (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 F3FD333AD95; Wed, 7 Jan 2026 21:06:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.217.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819980; cv=none; b=P8ZP3WWO7X5lOLQAgkBEmn3eB9o5vtbdEYGUZQL0m68sWbg+3R2zMqacbtIqKSvVeg3+dMKcRODsRwkN+ExDe/kK0PMm+MkYG2UAgOEDhtfbf8mu22duJuLI0POtszIJFaKaAfg6WntC4W8DqPI6j0J3olewI5d5hg5niDRoEos= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819980; c=relaxed/simple; bh=KiuMB2Wyf6BiXs5XrrnUBgf1JzoWq8Vm5fO1hFPgi80=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DSizL2bgDK+cHgXnyHCpvW/ITKHTPG6TKd49CLDcg5/qOvHLtQCmZJaOVlXtoMD4sGd6ZVMh3rXhbB/8F/81WUj3GX4oVmtoyOUYAVz0nuE7k2tP0fsm+Ni/Na3oqSviP40g8PY/S8rbUHl9P4YG5PoWpQvb9YAGTt5Vnx/RZkk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de; spf=pass smtp.mailfrom=tu-dortmund.de; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b=JYyCYhjB; arc=none smtp.client-ip=129.217.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b="JYyCYhjB" Received: from simon-Latitude-5450.fritz.box (p5dc880d2.dip0.t-ipconnect.de [93.200.128.210]) (authenticated bits=0) by unimail.uni-dortmund.de (8.18.1.16/8.18.1.16) with ESMTPSA id 607L5t9R026667 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Jan 2026 22:06:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tu-dortmund.de; s=unimail; t=1767819963; bh=KiuMB2Wyf6BiXs5XrrnUBgf1JzoWq8Vm5fO1hFPgi80=; h=From:To:Subject:Date:In-Reply-To:References; b=JYyCYhjBhv2Gq6QHhleTfJlkZetlTFZT0fXfgujH+tsJqKII7pxujQw0IlbM0qacm qoRF68BcXbYkKt8uapq6eX4cfRSb+VFwHtBNJb9q6JqvgHCjXUciv/oJ2SSpif+UGr 1yqkScOSPJ04xcng4DLlsuLnx8ohG+MCAjLWjAFQ= From: Simon Schippers To: willemdebruijn.kernel@gmail.com, jasowang@redhat.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, eperezma@redhat.com, leiyang@redhat.com, stephen@networkplumber.org, jon@nutanix.com, tim.gebauer@tu-dortmund.de, simon.schippers@tu-dortmund.de, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH net-next v7 6/9] tun/tap: add helper functions to check file type Date: Wed, 7 Jan 2026 22:04:45 +0100 Message-ID: <20260107210448.37851-7-simon.schippers@tu-dortmund.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> References: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add tun_is_tun_file() and tap_is_tap_file() helper functions to check if a file is a TUN or TAP file, which will be utilized by vhost-net. Co-developed-by: Tim Gebauer Signed-off-by: Tim Gebauer Co-developed by: Jon Kohler Signed-off-by: Jon Kohler Signed-off-by: Simon Schippers --- drivers/net/tap.c | 13 +++++++++++++ drivers/net/tun.c | 13 +++++++++++++ include/linux/if_tap.h | 5 +++++ include/linux/if_tun.h | 6 ++++++ 4 files changed, 37 insertions(+) diff --git a/drivers/net/tap.c b/drivers/net/tap.c index 4ffe4e95b5a6..cf19d7181c2f 100644 --- a/drivers/net/tap.c +++ b/drivers/net/tap.c @@ -1243,6 +1243,19 @@ struct ptr_ring *tap_get_ptr_ring(struct file *file) } EXPORT_SYMBOL_GPL(tap_get_ptr_ring); =20 +bool tap_is_tap_file(struct file *file) +{ + struct tap_queue *q; + + if (file->f_op !=3D &tap_fops) + return false; + q =3D file->private_data; + if (!q) + return false; + return true; +} +EXPORT_SYMBOL_GPL(tap_is_tap_file); + int tap_queue_resize(struct tap_dev *tap) { struct net_device *dev =3D tap->dev; diff --git a/drivers/net/tun.c b/drivers/net/tun.c index d44d206c65e8..9d6f98e00661 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -3782,6 +3782,19 @@ struct ptr_ring *tun_get_tx_ring(struct file *file) } EXPORT_SYMBOL_GPL(tun_get_tx_ring); =20 +bool tun_is_tun_file(struct file *file) +{ + struct tun_file *tfile; + + if (file->f_op !=3D &tun_fops) + return false; + tfile =3D file->private_data; + if (!tfile) + return false; + return true; +} +EXPORT_SYMBOL_GPL(tun_is_tun_file); + module_init(tun_init); module_exit(tun_cleanup); MODULE_DESCRIPTION(DRV_DESCRIPTION); diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h index 28326a69745a..14194342b784 100644 --- a/include/linux/if_tap.h +++ b/include/linux/if_tap.h @@ -14,6 +14,7 @@ struct ptr_ring *tap_get_ptr_ring(struct file *file); int tap_ring_consume_batched(struct file *file, void **array, int n); void tap_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)); +bool tap_is_tap_file(struct file *file); #else #include #include @@ -32,6 +33,10 @@ static inline int tap_ring_consume_batched(struct file *= f, } static inline void tap_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)) {} +static inline bool tap_is_tap_file(struct file *f) +{ + return false; +} #endif /* CONFIG_TAP */ =20 /* diff --git a/include/linux/if_tun.h b/include/linux/if_tun.h index 1274c6b34eb6..0910c6dbac20 100644 --- a/include/linux/if_tun.h +++ b/include/linux/if_tun.h @@ -25,6 +25,7 @@ struct ptr_ring *tun_get_tx_ring(struct file *file); int tun_ring_consume_batched(struct file *file, void **array, int n); void tun_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)); +bool tun_is_tun_file(struct file *file); =20 static inline bool tun_is_xdp_frame(void *ptr) { @@ -67,6 +68,11 @@ static inline int tun_ring_consume_batched(struct file *= file, static inline void tun_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)) {} =20 +static inline bool tun_is_tun_file(struct file *f) +{ + return false; +} + static inline bool tun_is_xdp_frame(void *ptr) { return false; --=20 2.43.0 From nobody Sun Feb 8 13:39:11 2026 Received: from unimail.uni-dortmund.de (mx1.hrz.uni-dortmund.de [129.217.128.51]) (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 E6CA133AD94; Wed, 7 Jan 2026 21:06:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.217.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819979; cv=none; b=lSgGSxudGqz3y4u0LBp7Kfp7ZZl+vI7VmoXnWuJ71QWN5SM1gNhoSOLk/OasiJlgXr6EE13Pxn0z88J8rltsdbeZUNZWNlLyZBMMyr+E1N4vTuVTIVsLC38MAX2z8zJnBnl/TSbEVypNN7n1Otv2Ne/6Z1i0iXvDktOX9RyBTBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819979; c=relaxed/simple; bh=/fsS1OkVQACr2Zwm9U45KQEbYSEtJ4FVNb6QeUMmHXo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=doIKtFEjKTrvnO48PP9JYsMUQD3yC0glF40ryCwoX9qF6D64n5jouY798bT4/NK8rgeQWXKvPvOBTJn1GKleu8KcrzxbaHxus+0fz6Rc6yqebczG6E2d0YKUUOZaj1mAQ0skoRXrNECH+D7v1Qkc/zMshs01G+ryvm2yBAUZFjc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de; spf=pass smtp.mailfrom=tu-dortmund.de; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b=gCUB2Ysd; arc=none smtp.client-ip=129.217.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b="gCUB2Ysd" Received: from simon-Latitude-5450.fritz.box (p5dc880d2.dip0.t-ipconnect.de [93.200.128.210]) (authenticated bits=0) by unimail.uni-dortmund.de (8.18.1.16/8.18.1.16) with ESMTPSA id 607L5t9T026667 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Jan 2026 22:06:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tu-dortmund.de; s=unimail; t=1767819963; bh=/fsS1OkVQACr2Zwm9U45KQEbYSEtJ4FVNb6QeUMmHXo=; h=From:To:Subject:Date:In-Reply-To:References; b=gCUB2Ysdt6Nb/jBGRKuCjGiyWwz/Yg0gfAbvwRK6KlTI0kp7N6PKWrb4KdBGQ4Fvn tXku3hGWwn2rcUQrv/Bg5uMYh4sYYl5nauAAM9lpgZ7ilhu1qoltXB9aumOeHzPROj sg8NNPtbh1DXJ9n3v1kox4rZvf0jtW/xf4Hzb6yQ= From: Simon Schippers To: willemdebruijn.kernel@gmail.com, jasowang@redhat.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, eperezma@redhat.com, leiyang@redhat.com, stephen@networkplumber.org, jon@nutanix.com, tim.gebauer@tu-dortmund.de, simon.schippers@tu-dortmund.de, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH net-next v7 7/9] vhost-net: vhost-net: replace rx_ring with tun/tap ring wrappers Date: Wed, 7 Jan 2026 22:04:46 +0100 Message-ID: <20260107210448.37851-8-simon.schippers@tu-dortmund.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> References: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the direct use of ptr_ring in the vhost-net virtqueue with tun/tap ring wrapper helpers. Instead of storing an rx_ring pointer, the virtqueue now stores the interface type (IF_TUN, IF_TAP, or IF_NONE) and dispatches to the corresponding tun/tap helpers for ring produce, consume, and unconsume operations. Routing ring operations through the tun/tap helpers enables netdev queue wakeups, which are required for upcoming netdev queue flow control support shared by tun/tap and vhost-net. No functional change is intended beyond switching to the wrapper helpers. Co-developed-by: Tim Gebauer Signed-off-by: Tim Gebauer Co-developed by: Jon Kohler Signed-off-by: Jon Kohler Signed-off-by: Simon Schippers --- drivers/vhost/net.c | 92 +++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c index 7f886d3dba7d..215556f7cd40 100644 --- a/drivers/vhost/net.c +++ b/drivers/vhost/net.c @@ -90,6 +90,12 @@ enum { VHOST_NET_VQ_MAX =3D 2, }; =20 +enum if_type { + IF_NONE =3D 0, + IF_TUN =3D 1, + IF_TAP =3D 2, +}; + struct vhost_net_ubuf_ref { /* refcount follows semantics similar to kref: * 0: object is released @@ -127,10 +133,11 @@ struct vhost_net_virtqueue { /* Reference counting for outstanding ubufs. * Protected by vq mutex. Writers must also take device mutex. */ struct vhost_net_ubuf_ref *ubufs; - struct ptr_ring *rx_ring; struct vhost_net_buf rxq; /* Batched XDP buffs */ struct xdp_buff *xdp; + /* Interface type */ + enum if_type type; }; =20 struct vhost_net { @@ -176,24 +183,50 @@ static void *vhost_net_buf_consume(struct vhost_net_b= uf *rxq) return ret; } =20 -static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq) +static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq, + struct sock *sk) { + struct file *file =3D sk->sk_socket->file; struct vhost_net_buf *rxq =3D &nvq->rxq; =20 rxq->head =3D 0; - rxq->tail =3D ptr_ring_consume_batched(nvq->rx_ring, rxq->queue, - VHOST_NET_BATCH); + switch (nvq->type) { + case IF_TUN: + rxq->tail =3D tun_ring_consume_batched(file, rxq->queue, + VHOST_NET_BATCH); + break; + case IF_TAP: + rxq->tail =3D tap_ring_consume_batched(file, rxq->queue, + VHOST_NET_BATCH); + break; + case IF_NONE: + return 0; + } return rxq->tail; } =20 -static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq) +static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq, + struct socket *sk) { struct vhost_net_buf *rxq =3D &nvq->rxq; - - if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) { - ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head, - vhost_net_buf_get_size(rxq), - tun_ptr_free); + struct file *file; + + if (sk && !vhost_net_buf_is_empty(rxq)) { + file =3D sk->file; + switch (nvq->type) { + case IF_TUN: + tun_ring_unconsume(file, rxq->queue + rxq->head, + vhost_net_buf_get_size(rxq), + tun_ptr_free); + break; + case IF_TAP: + tap_ring_unconsume(file, rxq->queue + rxq->head, + vhost_net_buf_get_size(rxq), + tun_ptr_free); + break; + case IF_NONE: + return; + } rxq->head =3D rxq->tail =3D 0; } } @@ -209,14 +242,15 @@ static int vhost_net_buf_peek_len(void *ptr) return __skb_array_len_with_tag(ptr); } =20 -static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq) +static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq, + struct sock *sk) { struct vhost_net_buf *rxq =3D &nvq->rxq; =20 if (!vhost_net_buf_is_empty(rxq)) goto out; =20 - if (!vhost_net_buf_produce(nvq)) + if (!vhost_net_buf_produce(nvq, sk)) return 0; =20 out: @@ -996,8 +1030,8 @@ static int peek_head_len(struct vhost_net_virtqueue *r= vq, struct sock *sk) int len =3D 0; unsigned long flags; =20 - if (rvq->rx_ring) - return vhost_net_buf_peek(rvq); + if (rvq->type) + return vhost_net_buf_peek(rvq, sk); =20 spin_lock_irqsave(&sk->sk_receive_queue.lock, flags); head =3D skb_peek(&sk->sk_receive_queue); @@ -1212,7 +1246,7 @@ static void handle_rx(struct vhost_net *net) goto out; } busyloop_intr =3D false; - if (nvq->rx_ring) + if (nvq->type) msg.msg_control =3D vhost_net_buf_consume(&nvq->rxq); /* On overrun, truncate and discard */ if (unlikely(headcount > UIO_MAXIOV)) { @@ -1368,7 +1402,6 @@ static int vhost_net_open(struct inode *inode, struct= file *f) n->vqs[i].batched_xdp =3D 0; n->vqs[i].vhost_hlen =3D 0; n->vqs[i].sock_hlen =3D 0; - n->vqs[i].rx_ring =3D NULL; vhost_net_buf_init(&n->vqs[i].rxq); } vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX, @@ -1398,8 +1431,8 @@ static struct socket *vhost_net_stop_vq(struct vhost_= net *n, sock =3D vhost_vq_get_backend(vq); vhost_net_disable_vq(n, vq); vhost_vq_set_backend(vq, NULL); - vhost_net_buf_unproduce(nvq); - nvq->rx_ring =3D NULL; + vhost_net_buf_unproduce(nvq, sock); + nvq->type =3D IF_NONE; mutex_unlock(&vq->mutex); return sock; } @@ -1479,18 +1512,13 @@ static struct socket *get_raw_socket(int fd) return ERR_PTR(r); } =20 -static struct ptr_ring *get_tap_ptr_ring(struct file *file) +static enum if_type get_if_type(struct file *file) { - struct ptr_ring *ring; - ring =3D tun_get_tx_ring(file); - if (!IS_ERR(ring)) - goto out; - ring =3D tap_get_ptr_ring(file); - if (!IS_ERR(ring)) - goto out; - ring =3D NULL; -out: - return ring; + if (tap_is_tap_file(file)) + return IF_TAP; + if (tun_is_tun_file(file)) + return IF_TUN; + return IF_NONE; } =20 static struct socket *get_tap_socket(int fd) @@ -1572,7 +1600,7 @@ static long vhost_net_set_backend(struct vhost_net *n= , unsigned index, int fd) =20 vhost_net_disable_vq(n, vq); vhost_vq_set_backend(vq, sock); - vhost_net_buf_unproduce(nvq); + vhost_net_buf_unproduce(nvq, sock); r =3D vhost_vq_init_access(vq); if (r) goto err_used; @@ -1581,9 +1609,9 @@ static long vhost_net_set_backend(struct vhost_net *n= , unsigned index, int fd) goto err_used; if (index =3D=3D VHOST_NET_VQ_RX) { if (sock) - nvq->rx_ring =3D get_tap_ptr_ring(sock->file); + nvq->type =3D get_if_type(sock->file); else - nvq->rx_ring =3D NULL; + nvq->type =3D IF_NONE; } =20 oldubufs =3D nvq->ubufs; --=20 2.43.0 From nobody Sun Feb 8 13:39:11 2026 Received: from unimail.uni-dortmund.de (mx1.hrz.uni-dortmund.de [129.217.128.51]) (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 D422833AD9E; Wed, 7 Jan 2026 21:06:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.217.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819980; cv=none; b=J2354vCq5WCYgt7PVLKCwkzEh1dTQMK9MXPWjmF9oA7+TXoJWcj/tauE3f+hBDaxppPFwzqmIXBQye8LCp97m4r8/GzoKPVGevtlAeUP/JZ/OaSNctTnrQliLd8V8uUnjXkYIZcwGORhe2B4sA9p3WM+lphrOxs3yQAxwB5l/Yg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819980; c=relaxed/simple; bh=uzllYQOl4EpJPNyCjjkXSvTvdwq8YWcwQkZuwDteH4A=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mYzs3unnpea41tX1bKJoZaszAl8DNYS5bVl9RS4D5K2Q/cQ8qZVqZ6JpmBkZFUq6+mB1gNJ+ErfMrprulOaRyanGgDBKcaa+jVMvRAlK627B3aF6Gy4pOPVN0P7oPbKkytxEDtB5SOlJuzaakD+ABaUruaw3KgAroFwjFLc4OVM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de; spf=pass smtp.mailfrom=tu-dortmund.de; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b=rhWxugf0; arc=none smtp.client-ip=129.217.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b="rhWxugf0" Received: from simon-Latitude-5450.fritz.box (p5dc880d2.dip0.t-ipconnect.de [93.200.128.210]) (authenticated bits=0) by unimail.uni-dortmund.de (8.18.1.16/8.18.1.16) with ESMTPSA id 607L5t9V026667 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Jan 2026 22:06:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tu-dortmund.de; s=unimail; t=1767819964; bh=uzllYQOl4EpJPNyCjjkXSvTvdwq8YWcwQkZuwDteH4A=; h=From:To:Subject:Date:In-Reply-To:References; b=rhWxugf0Qdwjzfh69CGytGHf0e5aioOwvsXeHk9GSeA8O/XJY77wa07PMHsxiX0AB cB8bb907oB4dWUMx7E1LR/GMCsw7rQYvwIUjHrTzK/p5qWZ8GVeh4Txfc9rh0jYKvs YXX2+t7Jgjcvrp+kiDkuLFu7qxQxDSrTFj0go3ic= From: Simon Schippers To: willemdebruijn.kernel@gmail.com, jasowang@redhat.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, eperezma@redhat.com, leiyang@redhat.com, stephen@networkplumber.org, jon@nutanix.com, tim.gebauer@tu-dortmund.de, simon.schippers@tu-dortmund.de, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH net-next v7 8/9] tun/tap: drop get ring exports Date: Wed, 7 Jan 2026 22:04:47 +0100 Message-ID: <20260107210448.37851-9-simon.schippers@tu-dortmund.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> References: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" tun_get_tx_ring and tap_get_ptr_ring no longer have in-tree consumers and can be dropped. Co-developed-by: Tim Gebauer Signed-off-by: Tim Gebauer Co-developed by: Jon Kohler Signed-off-by: Jon Kohler Signed-off-by: Simon Schippers --- drivers/net/tap.c | 13 ------------- drivers/net/tun.c | 13 ------------- include/linux/if_tap.h | 5 ----- include/linux/if_tun.h | 6 ------ 4 files changed, 37 deletions(-) diff --git a/drivers/net/tap.c b/drivers/net/tap.c index cf19d7181c2f..8821f26d0baa 100644 --- a/drivers/net/tap.c +++ b/drivers/net/tap.c @@ -1230,19 +1230,6 @@ struct socket *tap_get_socket(struct file *file) } EXPORT_SYMBOL_GPL(tap_get_socket); =20 -struct ptr_ring *tap_get_ptr_ring(struct file *file) -{ - struct tap_queue *q; - - if (file->f_op !=3D &tap_fops) - return ERR_PTR(-EINVAL); - q =3D file->private_data; - if (!q) - return ERR_PTR(-EBADFD); - return &q->ring; -} -EXPORT_SYMBOL_GPL(tap_get_ptr_ring); - bool tap_is_tap_file(struct file *file) { struct tap_queue *q; diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 9d6f98e00661..71b6981d07d7 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -3769,19 +3769,6 @@ void tun_ring_unconsume(struct file *file, void **ba= tch, int n, } EXPORT_SYMBOL_GPL(tun_ring_unconsume); =20 -struct ptr_ring *tun_get_tx_ring(struct file *file) -{ - struct tun_file *tfile; - - if (file->f_op !=3D &tun_fops) - return ERR_PTR(-EINVAL); - tfile =3D file->private_data; - if (!tfile) - return ERR_PTR(-EBADFD); - return &tfile->tx_ring; -} -EXPORT_SYMBOL_GPL(tun_get_tx_ring); - bool tun_is_tun_file(struct file *file) { struct tun_file *tfile; diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h index 14194342b784..0e427b979c11 100644 --- a/include/linux/if_tap.h +++ b/include/linux/if_tap.h @@ -10,7 +10,6 @@ struct socket; =20 #if IS_ENABLED(CONFIG_TAP) struct socket *tap_get_socket(struct file *); -struct ptr_ring *tap_get_ptr_ring(struct file *file); int tap_ring_consume_batched(struct file *file, void **array, int n); void tap_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)); @@ -22,10 +21,6 @@ static inline struct socket *tap_get_socket(struct file = *f) { return ERR_PTR(-EINVAL); } -static inline struct ptr_ring *tap_get_ptr_ring(struct file *f) -{ - return ERR_PTR(-EINVAL); -} static inline int tap_ring_consume_batched(struct file *f, void **array, int n) { diff --git a/include/linux/if_tun.h b/include/linux/if_tun.h index 0910c6dbac20..80b734173a80 100644 --- a/include/linux/if_tun.h +++ b/include/linux/if_tun.h @@ -21,7 +21,6 @@ struct tun_msg_ctl { =20 #if defined(CONFIG_TUN) || defined(CONFIG_TUN_MODULE) struct socket *tun_get_socket(struct file *); -struct ptr_ring *tun_get_tx_ring(struct file *file); int tun_ring_consume_batched(struct file *file, void **array, int n); void tun_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)); @@ -54,11 +53,6 @@ static inline struct socket *tun_get_socket(struct file = *f) return ERR_PTR(-EINVAL); } =20 -static inline struct ptr_ring *tun_get_tx_ring(struct file *f) -{ - return ERR_PTR(-EINVAL); -} - static inline int tun_ring_consume_batched(struct file *file, void **array, int n) { --=20 2.43.0 From nobody Sun Feb 8 13:39:11 2026 Received: from unimail.uni-dortmund.de (mx1.hrz.uni-dortmund.de [129.217.128.51]) (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 D42E033ADA2; Wed, 7 Jan 2026 21:06:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.217.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819980; cv=none; b=a2I/bm+f0vnV76GvMaVRhHpT4BwlDU8/5yeVDqfPiFjnYQm9DvGSNOJmEEZJ97ZYE5/FjCEjUpArSX7rTUP0htdQU1TDvPlgdx/ZklHCF65y4EKZvQe1zG7XvM3WlaFS5s23b23NfouW7PzinNyEwpzUPUvgI0D/TUkVCtpoEqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819980; c=relaxed/simple; bh=ek1z7wPVwY/RL+3lP3ES8isx76ej3dySxD1Usm9nlMM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vd4OyNrCGPFL5ElQ8Y7vQcGdC1RtDBwmOV42KXXOx4eeqngkHXZvpEIgVVLXQlaxDEUTpzJYO3eh/dckwI5wOxMiF3LzUzrAXHejcW35OrNgs1a2O8+cimVQ5wPbvvj4Xh47nGn/lBFQsI8eFcbhewLimu3B4PadbjwXmD76bwI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de; spf=pass smtp.mailfrom=tu-dortmund.de; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b=fd2Hco7e; arc=none smtp.client-ip=129.217.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tu-dortmund.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=tu-dortmund.de header.i=@tu-dortmund.de header.b="fd2Hco7e" Received: from simon-Latitude-5450.fritz.box (p5dc880d2.dip0.t-ipconnect.de [93.200.128.210]) (authenticated bits=0) by unimail.uni-dortmund.de (8.18.1.16/8.18.1.16) with ESMTPSA id 607L5t9X026667 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 7 Jan 2026 22:06:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tu-dortmund.de; s=unimail; t=1767819964; bh=ek1z7wPVwY/RL+3lP3ES8isx76ej3dySxD1Usm9nlMM=; h=From:To:Subject:Date:In-Reply-To:References; b=fd2Hco7eBApgutlmX/fYDbwXKtsij9SMRhL6m0GIzT4XD6+PTcRKadTa3DcjK5p5T MOcX0C6calTyOUdoa/2BvBvPYdySrb/yIFMCOGTmzRn1PbHWYx6dSsTCT1Iua3mdOg zGrY2OFjdvbgKORtqVTjq3AdfS8A7i2IQAWkPGhQ= From: Simon Schippers To: willemdebruijn.kernel@gmail.com, jasowang@redhat.com, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mst@redhat.com, eperezma@redhat.com, leiyang@redhat.com, stephen@networkplumber.org, jon@nutanix.com, tim.gebauer@tu-dortmund.de, simon.schippers@tu-dortmund.de, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH net-next v7 9/9] tun/tap & vhost-net: avoid ptr_ring tail-drop when qdisc is present Date: Wed, 7 Jan 2026 22:04:48 +0100 Message-ID: <20260107210448.37851-10-simon.schippers@tu-dortmund.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> References: <20260107210448.37851-1-simon.schippers@tu-dortmund.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This commit prevents tail-drop when a qdisc is present and the ptr_ring becomes full. Once an entry is successfully produced and the ptr_ring reaches capacity, the netdev queue is stopped instead of dropping subsequent packets. If producing an entry fails anyways, the tun_net_xmit returns NETDEV_TX_BUSY, again avoiding a drop. Such failures are expected because LLTX is enabled and the transmit path operates without the usual locking. As a result, concurrent calls to tun_net_xmit() are not prevented. The existing __{tun,tap}_ring_consume functions free space in the ptr_ring and wake the netdev queue. Races between this wakeup and the queue-stop logic could leave the queue stopped indefinitely. To prevent this, a memory barrier is enforced (as discussed in a similar implementation in [1]), followed by a recheck that wakes the queue if space is already available. If no qdisc is present, the previous tail-drop behavior is preserved. +-------------------------+-----------+---------------+----------------+ | pktgen benchmarks to | Stock | Patched with | Patched with | | Debian VM, i5 6300HQ, | | noqueue qdisc | fq_codel qdisc | | 10M packets | | | | +-----------+-------------+-----------+---------------+----------------+ | TAP | Transmitted | 196 Kpps | 195 Kpps | 185 Kpps | | +-------------+-----------+---------------+----------------+ | | Lost | 1618 Kpps | 1556 Kpps | 0 | +-----------+-------------+-----------+---------------+----------------+ | TAP | Transmitted | 577 Kpps | 582 Kpps | 578 Kpps | | + +-------------+-----------+---------------+----------------+ | vhost-net | Lost | 1170 Kpps | 1109 Kpps | 0 | +-----------+-------------+-----------+---------------+----------------+ [1] Link: https://lore.kernel.org/all/20250424085358.75d817ae@kernel.org/ Co-developed-by: Tim Gebauer Signed-off-by: Tim Gebauer Signed-off-by: Simon Schippers --- drivers/net/tun.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 71b6981d07d7..74d7fd09e9ba 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -1008,6 +1008,8 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, = struct net_device *dev) struct netdev_queue *queue; struct tun_file *tfile; int len =3D skb->len; + bool qdisc_present; + int ret; =20 rcu_read_lock(); tfile =3D rcu_dereference(tun->tfiles[txq]); @@ -1060,13 +1062,38 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb= , struct net_device *dev) =20 nf_reset_ct(skb); =20 - if (ptr_ring_produce(&tfile->tx_ring, skb)) { + queue =3D netdev_get_tx_queue(dev, txq); + qdisc_present =3D !qdisc_txq_has_no_queue(queue); + + spin_lock(&tfile->tx_ring.producer_lock); + ret =3D __ptr_ring_produce(&tfile->tx_ring, skb); + if (__ptr_ring_produce_peek(&tfile->tx_ring) && qdisc_present) { + netif_tx_stop_queue(queue); + /* Avoid races with queue wake-up in + * __{tun,tap}_ring_consume by waking if space is + * available in a re-check. + * The barrier makes sure that the stop is visible before + * we re-check. + */ + smp_mb__after_atomic(); + if (!__ptr_ring_produce_peek(&tfile->tx_ring)) + netif_tx_wake_queue(queue); + } + spin_unlock(&tfile->tx_ring.producer_lock); + + if (ret) { + /* If a qdisc is attached to our virtual device, + * returning NETDEV_TX_BUSY is allowed. + */ + if (qdisc_present) { + rcu_read_unlock(); + return NETDEV_TX_BUSY; + } drop_reason =3D SKB_DROP_REASON_FULL_RING; goto drop; } =20 /* dev->lltx requires to do our own update of trans_start */ - queue =3D netdev_get_tx_queue(dev, txq); txq_trans_cond_update(queue); =20 /* Notify and wake up reader process */ --=20 2.43.0