Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp56484lqh; Wed, 27 Mar 2024 14:46:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWFcFvb3p4TxNLaYOgM0IFnIGhzjAxvoje/nVtVDOGE6SeAY8EFcV3d3edlIhvZg0xraFuwKMLO5/cH7mwGcJEFV8Mr0qLpvoqW6ebf4A== X-Google-Smtp-Source: AGHT+IE4wHmmUjSw+xrRDBtm5k7kOB9uaWXFa8s5vIjoB7ImCIAo8Wbfcb0n1aK1YAgj7sdugj+I X-Received: by 2002:a17:906:7252:b0:a46:a28d:2f49 with SMTP id n18-20020a170906725200b00a46a28d2f49mr457539ejk.32.1711575977802; Wed, 27 Mar 2024 14:46:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711575977; cv=pass; d=google.com; s=arc-20160816; b=UYUJRjM6fRmHc1rQrF3nqKI7c34TjDy+WmrdN0ScNSY9Ipl4ubZRGwYqv11kuZJOhf PicVRh6wT5WWrNA9MzSdqa7NcuaVDc/rMZPja0+YoVvrJ7HpoQK2OwLHqZfrFLaXTxY5 7QZq2WocLJVQqPT43sL3u4R4PlhxGWSYoqLwrbYvFJZxl44BLbP0b4Khk/aHGAqGQmCb BbeEaxRiTPMaC/5r9sKBdu4XkgGss4D7OzZs0+kycCjviISHau9HVmukyr7ljTySplmq zVqu/exkgsRSHFuyacc5xPnQYBOHGz4zc16HdEGFwtj6s6t4SzGY5MuQzV85nSmadC31 vpEA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=7nhCSN7ZlS5U7nzl7rNYNpzESUVtaxwKFd/9+3DX9HA=; fh=EE2fQTKBDwdgaY2bHYS8h1oigRhiqffygKLblBgJwiM=; b=gwSLqW1+4qdEqB4sTRk5vaLyx4a8J9Y/w0RWgfpWiX4MvKwv/O6nQxN39x766M0m2w pWarG3K2ybg2NByYasDrt5IZsXAC8f7Q3iNufaSMcYB9nyDFGL+Q8OhxrWvOP8TybcMS z4WO7rRofbu/APsDBqUqRS+vGc44ShHURcv/Y6K/JHDvqL+pWtgoXSB0xcTrSZb2pJbR sQ5Vxl1A+xvbq9+lSVHpPQ6YJkTGlqPWDYaI+xdoZX45PiReirEnFmZRFt73iXl1FR8P CY7WOvvFddST2nLUjnveVQMDAfE9AzDSqa/g5EfP8TT0T3R2tmVwrUMgbeycNRe1YFJA ymfQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=a8q6QeTZ; arc=pass (i=1 spf=pass spfdomain=flex--almasrymina.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-122050-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122050-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id q7-20020a17090622c700b00a4660b6aedfsi5044758eja.693.2024.03.27.14.46.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 14:46:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122050-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=a8q6QeTZ; arc=pass (i=1 spf=pass spfdomain=flex--almasrymina.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-122050-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122050-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5FDEE1F2FDB9 for ; Wed, 27 Mar 2024 21:46:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 516C3154435; Wed, 27 Mar 2024 21:45:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="a8q6QeTZ" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 5E0A5153BC9 for ; Wed, 27 Mar 2024 21:45:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711575934; cv=none; b=DMjHQ/XcCjexsPWcV8kUT+2avylDo9E6NaToF/gku5LliQ3DezSJ/HIPGje5e089wDVdPjIXYo7mODU3H8kJwb4zND4G+zrvtcFNJEL3QfXS0XvoPOxxB/h5zUSTh/SLiiwDN0Dl9MtYmKYlAgfTYtKxctXf5Re0/r9zUkEi7S0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711575934; c=relaxed/simple; bh=UFh9WIe9rm3vbiPkPQJfYCpA8I+9vkOzZY11SDb6fDU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fi7v4ywV2frelFMyTb0kCR/GEmzjywDB5EZi8BYYqWs0j7sfyNU2JnhLsopOL5+/k6mCAYR92S/L+uSddgVNe7AVNuHNzOc/2c7xQaTgBCtyQ2dcZOs4rudtjubjnPHP2DCe0BGje/CPAfUpq7uUE7bdVR8X2qPzAB82SsOCx/c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=a8q6QeTZ; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dced704f17cso454455276.1 for ; Wed, 27 Mar 2024 14:45:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711575931; x=1712180731; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7nhCSN7ZlS5U7nzl7rNYNpzESUVtaxwKFd/9+3DX9HA=; b=a8q6QeTZMtTOlnJPJ1w89kpOLnrz6gi3yy+FeNC1py2DaP+k/PRc6FnUcGCj3Vxv6P Zz4icwA6M5spcBSSjnm+xgK5ZTN/EaJ6MlKe1vPxTohAZIVsPHWKc84fHfc8Qk8uPmlC W1WWbGAZd/+P46GB7rkeIJciRKQDM8Jrh8TvSObSkDUSirF0M9V1rj+3BYJJmzTOuKiK F/xmXj/jG6vt0u5QZgQy+0P+BpaQrGY66f3PbhKZQ+oB3FRla+uEGr1rhLMe4qvq8lPc SZtwDe+5L5yW1OO7vFdK7wRmE6Svg4+X6nWxNj7b//WrDjVUBZl6vPokOK9vZlY9onet 8XGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711575931; x=1712180731; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7nhCSN7ZlS5U7nzl7rNYNpzESUVtaxwKFd/9+3DX9HA=; b=WNFrTaw7Mm9UJplfU4/C6BQJb+rcYNW5w3BlGoaRmqvClnhRcMr/g9TawptSRqxwLO 8EPZ8BbcsSzsORi3ijDL/Yd3nPk980EZnSDbzgB46gwNd3aaVvx3BlqElPuEYE/pNKnU FJrU53UuMTM+44qBTTh9lqGEYjje1UiOEHyCUCVSCNPkHUwNzHABFRiroBkKADrdy+n1 fudTPIJGIOHxsDkYibPg5PiwaHQKqPMn3s+xJWsTsa7yPIwj3H0czwYb7O7B6xN/GnXP Vfdf/uSd93qav0yiA99ZSacKa8JhkaFsbUrqSitfGHFkdCPO+jgM3FvHm1um3ykOWAiZ F3kw== X-Forwarded-Encrypted: i=1; AJvYcCWAkL3tOG4jDmAp+yS0RNxSqVIUgORGg74y0opYMB058C0Swep47ok2/4EYue0PvZt1rclIilfwxukjCE7Nat0fkO7NI4OlKfdLL/hB X-Gm-Message-State: AOJu0Yz8h54g4azSL3dVW6O5yAU4ng89ljzva6rCGjYPhRhyb43pVEH3 adwPQNkJbtKsnwF6obRDtSCB7CLiZeJxU/Ge9dIqBjcVwv6smGhEIs+PA/xDQtNCMvSOEillAw1 zre+4FTAOnIlCSsWDSunp6w== X-Received: from almasrymina.svl.corp.google.com ([2620:15c:2c4:200:b757:6e7b:2156:cabc]) (user=almasrymina job=sendgmr) by 2002:a05:6902:2311:b0:dbe:d0a9:2be8 with SMTP id do17-20020a056902231100b00dbed0a92be8mr118134ybb.0.1711575931557; Wed, 27 Mar 2024 14:45:31 -0700 (PDT) Date: Wed, 27 Mar 2024 14:45:20 -0700 In-Reply-To: <20240327214523.2182174-1-almasrymina@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240327214523.2182174-1-almasrymina@google.com> X-Mailer: git-send-email 2.44.0.396.g6e790dbe36-goog Message-ID: <20240327214523.2182174-3-almasrymina@google.com> Subject: [PATCH net-next v2 2/3] net: mirror skb frag ref/unref helpers From: Mina Almasry To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org Cc: Mina Almasry , Ayush Sawal , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Mirko Lindner , Stephen Hemminger , Tariq Toukan , Steffen Klassert , Herbert Xu , David Ahern , Boris Pismenny , John Fastabend , Dragos Tatulea Content-Type: text/plain; charset="UTF-8" Refactor some of the skb frag ref/unref helpers for improved clarity. Implement napi_pp_get_page() to be the mirror counterpart of napi_pp_put_page(). Implement skb_page_ref() to be the mirror of skb_page_unref(). Improve __skb_frag_ref() to become a mirror counterpart of __skb_frag_unref(). Previously unref could handle pp & non-pp pages, while the ref could only handle non-pp pages. Now both the ref & unref helpers can correctly handle both pp & non-pp pages. Now that __skb_frag_ref() can handle both pp & non-pp pages, remove skb_pp_frag_ref(), and use __skb_frag_ref() instead. This lets us remove pp specific handling from skb_try_coalesce. Signed-off-by: Mina Almasry Reviewed-by: Dragos Tatulea --- .../chelsio/inline_crypto/ch_ktls/chcr_ktls.c | 2 +- drivers/net/ethernet/sun/cassini.c | 4 +- include/linux/skbuff.h | 22 ++++++-- net/core/skbuff.c | 54 ++++++------------- 4 files changed, 38 insertions(+), 44 deletions(-) diff --git a/drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c b/drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c index 6482728794dd..f9b0a9533985 100644 --- a/drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c +++ b/drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c @@ -1658,7 +1658,7 @@ static void chcr_ktls_copy_record_in_skb(struct sk_buff *nskb, for (i = 0; i < record->num_frags; i++) { skb_shinfo(nskb)->frags[i] = record->frags[i]; /* increase the frag ref count */ - __skb_frag_ref(&skb_shinfo(nskb)->frags[i]); + __skb_frag_ref(&skb_shinfo(nskb)->frags[i], false); } skb_shinfo(nskb)->nr_frags = record->num_frags; diff --git a/drivers/net/ethernet/sun/cassini.c b/drivers/net/ethernet/sun/cassini.c index bfb903506367..fabba729e1b8 100644 --- a/drivers/net/ethernet/sun/cassini.c +++ b/drivers/net/ethernet/sun/cassini.c @@ -1999,7 +1999,7 @@ static int cas_rx_process_pkt(struct cas *cp, struct cas_rx_comp *rxc, skb->len += hlen - swivel; skb_frag_fill_page_desc(frag, page->buffer, off, hlen - swivel); - __skb_frag_ref(frag); + __skb_frag_ref(frag, false); /* any more data? */ if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) { @@ -2023,7 +2023,7 @@ static int cas_rx_process_pkt(struct cas *cp, struct cas_rx_comp *rxc, frag++; skb_frag_fill_page_desc(frag, page->buffer, 0, hlen); - __skb_frag_ref(frag); + __skb_frag_ref(frag, false); RX_USED_ADD(page, hlen + cp->crc_size); } diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index bafa5c9ff59a..058d72a2a250 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3494,15 +3494,29 @@ static inline struct page *skb_frag_page(const skb_frag_t *frag) return netmem_to_page(frag->netmem); } +bool napi_pp_get_page(struct page *page); + +static inline void skb_page_ref(struct page *page, bool recycle) +{ +#ifdef CONFIG_PAGE_POOL + if (recycle && napi_pp_get_page(page)) + return; +#endif + get_page(page); +} + /** * __skb_frag_ref - take an addition reference on a paged fragment. * @frag: the paged fragment + * @recycle: skb->pp_recycle param of the parent skb. False if no parent skb. * - * Takes an additional reference on the paged fragment @frag. + * Takes an additional reference on the paged fragment @frag. Obtains the + * correct reference count depending on whether skb->pp_recycle is set and + * whether the frag is a page pool frag. */ -static inline void __skb_frag_ref(skb_frag_t *frag) +static inline void __skb_frag_ref(skb_frag_t *frag, bool recycle) { - get_page(skb_frag_page(frag)); + skb_page_ref(skb_frag_page(frag), recycle); } /** @@ -3514,7 +3528,7 @@ static inline void __skb_frag_ref(skb_frag_t *frag) */ static inline void skb_frag_ref(struct sk_buff *skb, int f) { - __skb_frag_ref(&skb_shinfo(skb)->frags[f]); + __skb_frag_ref(&skb_shinfo(skb)->frags[f], skb->pp_recycle); } int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb, diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 17617c29be2d..5c86ecaceb6c 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -1005,6 +1005,19 @@ int skb_cow_data_for_xdp(struct page_pool *pool, struct sk_buff **pskb, EXPORT_SYMBOL(skb_cow_data_for_xdp); #if IS_ENABLED(CONFIG_PAGE_POOL) +bool napi_pp_get_page(struct page *page) +{ + + page = compound_head(page); + + if (!is_pp_page(page)) + return false; + + page_pool_ref_page(head_page); + return true; +} +EXPORT_SYMBOL(napi_pp_get_page); + bool napi_pp_put_page(struct page *page, bool napi_safe) { bool allow_direct = false; @@ -1057,37 +1070,6 @@ static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe) return napi_pp_put_page(virt_to_page(data), napi_safe); } -/** - * skb_pp_frag_ref() - Increase fragment references of a page pool aware skb - * @skb: page pool aware skb - * - * Increase the fragment reference count (pp_ref_count) of a skb. This is - * intended to gain fragment references only for page pool aware skbs, - * i.e. when skb->pp_recycle is true, and not for fragments in a - * non-pp-recycling skb. It has a fallback to increase references on normal - * pages, as page pool aware skbs may also have normal page fragments. - */ -static int skb_pp_frag_ref(struct sk_buff *skb) -{ - struct skb_shared_info *shinfo; - struct page *head_page; - int i; - - if (!skb->pp_recycle) - return -EINVAL; - - shinfo = skb_shinfo(skb); - - for (i = 0; i < shinfo->nr_frags; i++) { - head_page = compound_head(skb_frag_page(&shinfo->frags[i])); - if (likely(is_pp_page(head_page))) - page_pool_ref_page(head_page); - else - page_ref_inc(head_page); - } - return 0; -} - static void skb_kfree_head(void *head, unsigned int end_offset) { if (end_offset == SKB_SMALL_HEAD_HEADROOM) @@ -4196,7 +4178,7 @@ int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) to++; } else { - __skb_frag_ref(fragfrom); + __skb_frag_ref(fragfrom, skb->pp_recycle); skb_frag_page_copy(fragto, fragfrom); skb_frag_off_copy(fragto, fragfrom); skb_frag_size_set(fragto, todo); @@ -4846,7 +4828,7 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb, } *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; - __skb_frag_ref(nskb_frag); + __skb_frag_ref(nskb_frag, nskb->pp_recycle); size = skb_frag_size(nskb_frag); if (pos < offset) { @@ -5977,10 +5959,8 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, /* if the skb is not cloned this does nothing * since we set nr_frags to 0. */ - if (skb_pp_frag_ref(from)) { - for (i = 0; i < from_shinfo->nr_frags; i++) - __skb_frag_ref(&from_shinfo->frags[i]); - } + for (i = 0; i < from_shinfo->nr_frags; i++) + __skb_frag_ref(&from_shinfo->frags[i], from->pp_recycle); to->truesize += delta; to->len += len; -- 2.44.0.396.g6e790dbe36-goog