[PATCH net-next 1/7] net: Add skb_dst_reset and skb_dst_restore

Stanislav Fomichev posted 7 patches 1 month, 3 weeks ago
[PATCH net-next 1/7] net: Add skb_dst_reset and skb_dst_restore
Posted by Stanislav Fomichev 1 month, 3 weeks ago
Going forward skb_dst_set will assert that skb dst_entry
is empty during skb_dst_set to prevent potential leaks. There
are few places that still manually manage dst_entry not using
the helpers. Convert them to the following new helpers:
- skb_dst_reset that resets dst_entry and returns previous dst_entry
  value
- skb_dst_restore that restores dst_entry previously reset via skb_dst_restore

Signed-off-by: Stanislav Fomichev <sdf@fomichev.me>
---
 include/linux/skbuff.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 14b923ddb6df..8240e0826204 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -1159,6 +1159,37 @@ static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
 	return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
 }
 
+/**
+ * skb_dst_reset() - return current dst_entry value and clear it
+ * @skb: buffer
+ *
+ * Resets skb dst_entry without adjusting its reference count. Useful in
+ * cases where dst_entry needs to be temporarily reset and restored.
+ * Note that the returned value cannot be used directly because it
+ * might contain SKB_DST_NOREF bit.
+ *
+ * When in doubt, prefer skb_dst_drop() over skb_dst_reset() to correctly
+ * handle dst_entry reference counting.
+ *
+ * Returns: original skb dst_entry.
+ */
+static inline unsigned long skb_dst_reset(struct sk_buff *skb)
+{
+	unsigned long refdst = skb->_skb_refdst;
+
+	skb->_skb_refdst = 0;
+	return refdst;
+}
+
+/**
+ * skb_dst_restore() - restore skb dst_entry saved via skb_dst_reset
+ * @skb: buffer
+ */
+static inline void skb_dst_restore(struct sk_buff *skb, unsigned long refdst)
+{
+	skb->_skb_refdst = refdst;
+}
+
 /**
  * skb_dst_set - sets skb dst
  * @skb: buffer
-- 
2.50.1
Re: [PATCH net-next 1/7] net: Add skb_dst_reset and skb_dst_restore
Posted by Jakub Kicinski 1 month, 3 weeks ago
On Tue, 12 Aug 2025 08:52:39 -0700 Stanislav Fomichev wrote:
> +/**
> + * skb_dst_reset() - return current dst_entry value and clear it
> + * @skb: buffer
> + *
> + * Resets skb dst_entry without adjusting its reference count. Useful in
> + * cases where dst_entry needs to be temporarily reset and restored.
> + * Note that the returned value cannot be used directly because it
> + * might contain SKB_DST_NOREF bit.
> + *
> + * When in doubt, prefer skb_dst_drop() over skb_dst_reset() to correctly
> + * handle dst_entry reference counting.

thoughts on prefixing these two new helpers with __ to hint that
they are low level and best avoided?

> + *
> + * Returns: original skb dst_entry.
> + */
> +static inline unsigned long skb_dst_reset(struct sk_buff *skb)
> +{
> +	unsigned long refdst = skb->_skb_refdst;
> +
> +	skb->_skb_refdst = 0;
> +	return refdst;
> +}
> +
> +/**
> + * skb_dst_restore() - restore skb dst_entry saved via skb_dst_reset

saved -> removed ?
Also I think for better kdoc linking it's good to add () after function
names

> + * @skb: buffer

kdoc missing for refdst

> + */
> +static inline void skb_dst_restore(struct sk_buff *skb, unsigned long refdst)
> +{
> +	skb->_skb_refdst = refdst;
Re: [PATCH net-next 1/7] net: Add skb_dst_reset and skb_dst_restore
Posted by Jakub Kicinski 1 month, 3 weeks ago
On Wed, 13 Aug 2025 17:57:40 -0700 Jakub Kicinski wrote:
> On Tue, 12 Aug 2025 08:52:39 -0700 Stanislav Fomichev wrote:
> > +/**
> > + * skb_dst_reset() - return current dst_entry value and clear it
> > + * @skb: buffer
> > + *
> > + * Resets skb dst_entry without adjusting its reference count. Useful in
> > + * cases where dst_entry needs to be temporarily reset and restored.
> > + * Note that the returned value cannot be used directly because it
> > + * might contain SKB_DST_NOREF bit.
> > + *
> > + * When in doubt, prefer skb_dst_drop() over skb_dst_reset() to correctly
> > + * handle dst_entry reference counting.  
> 
> thoughts on prefixing these two new helpers with __ to hint that
> they are low level and best avoided?

Looking at the uses -- maybe skb_dstref_steal() or skb_steal_dstref()
would be a better name? We have skb_steal_sock() (et.al.) already,
same semantics.
Re: [PATCH net-next 1/7] net: Add skb_dst_reset and skb_dst_restore
Posted by Stanislav Fomichev 1 month, 3 weeks ago
On 08/13, Jakub Kicinski wrote:
> On Wed, 13 Aug 2025 17:57:40 -0700 Jakub Kicinski wrote:
> > On Tue, 12 Aug 2025 08:52:39 -0700 Stanislav Fomichev wrote:
> > > +/**
> > > + * skb_dst_reset() - return current dst_entry value and clear it
> > > + * @skb: buffer
> > > + *
> > > + * Resets skb dst_entry without adjusting its reference count. Useful in
> > > + * cases where dst_entry needs to be temporarily reset and restored.
> > > + * Note that the returned value cannot be used directly because it
> > > + * might contain SKB_DST_NOREF bit.
> > > + *
> > > + * When in doubt, prefer skb_dst_drop() over skb_dst_reset() to correctly
> > > + * handle dst_entry reference counting.  
> > 
> > thoughts on prefixing these two new helpers with __ to hint that
> > they are low level and best avoided?
> 
> Looking at the uses -- maybe skb_dstref_steal() or skb_steal_dstref()
> would be a better name? We have skb_steal_sock() (et.al.) already,
> same semantics.

Sure, will rename and address the rest of your feedback. Thanks for
the review!