From nobody Thu Jan 30 18:51:57 2025 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (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 147D01442F2; Mon, 27 Jan 2025 03:04:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947100; cv=none; b=Q+O4MfOfLa7vjSJBvlfcCBmjE7mkAMpcKG6fR+HyOMXK66sngyU+hIv4Nh6n4j1pmkgIeNxV+pQKr4jmyzfG2i3iEM7E7McyKxRcHRwsHO/1SWJ087SwYV1uEg2DZ8h9wbB0NQSgWoMORJ4IL1KnYot035H66OImYL7b19hNjwk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947100; c=relaxed/simple; bh=TbJDnKkOKwPGi4I8v4q7sAbSCUPNDrw0zvJqnGtWerY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fEBzC8FmN1R/oVkD3y/j9oWIObDQVVTOu66R1yrPByn4v9fxEaR1nx9fGZwXs5NZwo3BsKMDm+8Edn3USw5p/otaX2uZ6zE0KffhupucnpfAy0RGXgaksx4WLV784ecLh4F/ktrNfAA5lvMuTr42+EZOTAyz5vzL+U7LGJgLH9E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4YhCqf1TZHz11TB4; Mon, 27 Jan 2025 11:01:34 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 2F3481402CF; Mon, 27 Jan 2025 11:04:49 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 27 Jan 2025 11:04:48 +0800 From: Yunsheng Lin To: , , CC: , , , Yunsheng Lin , Wei Fang , Shenwei Wang , Clark Wang , Andrew Lunn , Eric Dumazet , Jeroen de Borst , Praveen Kaligineedi , Shailend Chand , Tony Nguyen , Przemek Kitszel , Alexander Lobakin , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Saeed Mahameed , Leon Romanovsky , Tariq Toukan , Felix Fietkau , Lorenzo Bianconi , Ryder Lee , Shayne Chen , Sean Wang , Kalle Valo , Matthias Brugger , AngeloGioacchino Del Regno , Simon Horman , Ilias Apalodimas , , , , , , , , , Subject: [RFC v8 1/5] page_pool: introduce page_pool_get_pp() API Date: Mon, 27 Jan 2025 10:57:30 +0800 Message-ID: <20250127025734.3406167-2-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250127025734.3406167-1-linyunsheng@huawei.com> References: <20250127025734.3406167-1-linyunsheng@huawei.com> 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 X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemf200006.china.huawei.com (7.185.36.61) Content-Type: text/plain; charset="utf-8" introduce page_pool_get_pp() API to avoid caller accessing page->pp directly. Signed-off-by: Yunsheng Lin --- drivers/net/ethernet/freescale/fec_main.c | 8 +++++--- .../net/ethernet/google/gve/gve_buffer_mgmt_dqo.c | 2 +- drivers/net/ethernet/intel/iavf/iavf_txrx.c | 6 ++++-- drivers/net/ethernet/intel/idpf/idpf_txrx.c | 14 +++++++++----- drivers/net/ethernet/intel/libeth/rx.c | 2 +- drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c | 3 ++- drivers/net/netdevsim/netdev.c | 6 ++++-- drivers/net/wireless/mediatek/mt76/mt76.h | 2 +- include/net/libeth/rx.h | 3 ++- include/net/page_pool/helpers.h | 5 +++++ net/core/xdp.c | 3 ++- 11 files changed, 36 insertions(+), 18 deletions(-) diff --git a/drivers/net/ethernet/freescale/fec_main.c b/drivers/net/ethern= et/freescale/fec_main.c index 1b55047c0237..8709426ae6d9 100644 --- a/drivers/net/ethernet/freescale/fec_main.c +++ b/drivers/net/ethernet/freescale/fec_main.c @@ -1009,7 +1009,8 @@ static void fec_enet_bd_init(struct net_device *dev) struct page *page =3D txq->tx_buf[i].buf_p; =20 if (page) - page_pool_put_page(page->pp, page, 0, false); + page_pool_put_page(page_pool_get_pp(page), + page, 0, false); } =20 txq->tx_buf[i].buf_p =3D NULL; @@ -1549,7 +1550,7 @@ fec_enet_tx_queue(struct net_device *ndev, u16 queue_= id, int budget) xdp_return_frame_rx_napi(xdpf); } else { /* recycle pages of XDP_TX frames */ /* The dma_sync_size =3D 0 as XDP_TX has already synced DMA for_device = */ - page_pool_put_page(page->pp, page, 0, true); + page_pool_put_page(page_pool_get_pp(page), page, 0, true); } =20 txq->tx_buf[index].buf_p =3D NULL; @@ -3311,7 +3312,8 @@ static void fec_enet_free_buffers(struct net_device *= ndev) } else { struct page *page =3D txq->tx_buf[i].buf_p; =20 - page_pool_put_page(page->pp, page, 0, false); + page_pool_put_page(page_pool_get_pp(page), + page, 0, false); } =20 txq->tx_buf[i].buf_p =3D NULL; diff --git a/drivers/net/ethernet/google/gve/gve_buffer_mgmt_dqo.c b/driver= s/net/ethernet/google/gve/gve_buffer_mgmt_dqo.c index 403f0f335ba6..87422b8828ff 100644 --- a/drivers/net/ethernet/google/gve/gve_buffer_mgmt_dqo.c +++ b/drivers/net/ethernet/google/gve/gve_buffer_mgmt_dqo.c @@ -210,7 +210,7 @@ void gve_free_to_page_pool(struct gve_rx_ring *rx, if (!page) return; =20 - page_pool_put_full_page(page->pp, page, allow_direct); + page_pool_put_full_page(page_pool_get_pp(page), page, allow_direct); buf_state->page_info.page =3D NULL; } =20 diff --git a/drivers/net/ethernet/intel/iavf/iavf_txrx.c b/drivers/net/ethe= rnet/intel/iavf/iavf_txrx.c index 26b424fd6718..e1bf5554f6e3 100644 --- a/drivers/net/ethernet/intel/iavf/iavf_txrx.c +++ b/drivers/net/ethernet/intel/iavf/iavf_txrx.c @@ -1050,7 +1050,8 @@ static void iavf_add_rx_frag(struct sk_buff *skb, const struct libeth_fqe *rx_buffer, unsigned int size) { - u32 hr =3D rx_buffer->page->pp->p.offset; + struct page_pool *pool =3D page_pool_get_pp(rx_buffer->page); + u32 hr =3D pool->p.offset; =20 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page, rx_buffer->offset + hr, size, rx_buffer->truesize); @@ -1067,7 +1068,8 @@ static void iavf_add_rx_frag(struct sk_buff *skb, static struct sk_buff *iavf_build_skb(const struct libeth_fqe *rx_buffer, unsigned int size) { - u32 hr =3D rx_buffer->page->pp->p.offset; + struct page_pool *pool =3D page_pool_get_pp(rx_buffer->page); + u32 hr =3D pool->p.offset; struct sk_buff *skb; void *va; =20 diff --git a/drivers/net/ethernet/intel/idpf/idpf_txrx.c b/drivers/net/ethe= rnet/intel/idpf/idpf_txrx.c index 2fa9c36e33c9..04f2347716ca 100644 --- a/drivers/net/ethernet/intel/idpf/idpf_txrx.c +++ b/drivers/net/ethernet/intel/idpf/idpf_txrx.c @@ -385,7 +385,8 @@ static void idpf_rx_page_rel(struct libeth_fqe *rx_buf) if (unlikely(!rx_buf->page)) return; =20 - page_pool_put_full_page(rx_buf->page->pp, rx_buf->page, false); + page_pool_put_full_page(page_pool_get_pp(rx_buf->page), rx_buf->page, + false); =20 rx_buf->page =3D NULL; rx_buf->offset =3D 0; @@ -3098,7 +3099,8 @@ idpf_rx_process_skb_fields(struct idpf_rx_queue *rxq,= struct sk_buff *skb, void idpf_rx_add_frag(struct idpf_rx_buf *rx_buf, struct sk_buff *skb, unsigned int size) { - u32 hr =3D rx_buf->page->pp->p.offset; + struct page_pool *pool =3D page_pool_get_pp(rx_buf->page); + u32 hr =3D pool->p.offset; =20 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buf->page, rx_buf->offset + hr, size, rx_buf->truesize); @@ -3130,8 +3132,10 @@ static u32 idpf_rx_hsplit_wa(const struct libeth_fqe= *hdr, if (!libeth_rx_sync_for_cpu(buf, copy)) return 0; =20 - dst =3D page_address(hdr->page) + hdr->offset + hdr->page->pp->p.offset; - src =3D page_address(buf->page) + buf->offset + buf->page->pp->p.offset; + dst =3D page_address(hdr->page) + hdr->offset + + page_pool_get_pp(hdr->page)->p.offset; + src =3D page_address(buf->page) + buf->offset + + page_pool_get_pp(buf->page)->p.offset; memcpy(dst, src, LARGEST_ALIGN(copy)); =20 buf->offset +=3D copy; @@ -3149,7 +3153,7 @@ static u32 idpf_rx_hsplit_wa(const struct libeth_fqe = *hdr, */ struct sk_buff *idpf_rx_build_skb(const struct libeth_fqe *buf, u32 size) { - u32 hr =3D buf->page->pp->p.offset; + u32 hr =3D page_pool_get_pp(buf->page)->p.offset; struct sk_buff *skb; void *va; =20 diff --git a/drivers/net/ethernet/intel/libeth/rx.c b/drivers/net/ethernet/= intel/libeth/rx.c index 66d1d23b8ad2..8de0c3a3b146 100644 --- a/drivers/net/ethernet/intel/libeth/rx.c +++ b/drivers/net/ethernet/intel/libeth/rx.c @@ -207,7 +207,7 @@ EXPORT_SYMBOL_NS_GPL(libeth_rx_fq_destroy, "LIBETH"); */ void libeth_rx_recycle_slow(struct page *page) { - page_pool_recycle_direct(page->pp, page); + page_pool_recycle_direct(page_pool_get_pp(page), page); } EXPORT_SYMBOL_NS_GPL(libeth_rx_recycle_slow, "LIBETH"); =20 diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c b/drivers/net= /ethernet/mellanox/mlx5/core/en/xdp.c index 94b291662087..30baca49c71e 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c @@ -716,7 +716,8 @@ static void mlx5e_free_xdpsq_desc(struct mlx5e_xdpsq *s= q, /* No need to check ((page->pp_magic & ~0x3UL) =3D=3D PP_SIGNATURE) * as we know this is a page_pool page. */ - page_pool_recycle_direct(page->pp, page); + page_pool_recycle_direct(page_pool_get_pp(page), + page); } while (++n < num); =20 break; diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index e068a9761c09..e583415f9088 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -632,7 +632,8 @@ nsim_pp_hold_write(struct file *file, const char __user= *data, if (!ns->page) ret =3D -ENOMEM; } else { - page_pool_put_full_page(ns->page->pp, ns->page, false); + page_pool_put_full_page(page_pool_get_pp(ns->page), ns->page, + false); ns->page =3D NULL; } =20 @@ -831,7 +832,8 @@ void nsim_destroy(struct netdevsim *ns) =20 /* Put this intentionally late to exercise the orphaning path */ if (ns->page) { - page_pool_put_full_page(ns->page->pp, ns->page, false); + page_pool_put_full_page(page_pool_get_pp(ns->page), ns->page, + false); ns->page =3D NULL; } =20 diff --git a/drivers/net/wireless/mediatek/mt76/mt76.h b/drivers/net/wirele= ss/mediatek/mt76/mt76.h index 0b75a45ad2e8..c3f79686f3b0 100644 --- a/drivers/net/wireless/mediatek/mt76/mt76.h +++ b/drivers/net/wireless/mediatek/mt76/mt76.h @@ -1688,7 +1688,7 @@ static inline void mt76_put_page_pool_buf(void *buf, = bool allow_direct) { struct page *page =3D virt_to_head_page(buf); =20 - page_pool_put_full_page(page->pp, page, allow_direct); + page_pool_put_full_page(page_pool_get_pp(page), page, allow_direct); } =20 static inline void * diff --git a/include/net/libeth/rx.h b/include/net/libeth/rx.h index 43574bd6612f..f4ae75f9cc1b 100644 --- a/include/net/libeth/rx.h +++ b/include/net/libeth/rx.h @@ -137,7 +137,8 @@ static inline bool libeth_rx_sync_for_cpu(const struct = libeth_fqe *fqe, return false; } =20 - page_pool_dma_sync_for_cpu(page->pp, page, fqe->offset, len); + page_pool_dma_sync_for_cpu(page_pool_get_pp(page), page, fqe->offset, + len); =20 return true; } diff --git a/include/net/page_pool/helpers.h b/include/net/page_pool/helper= s.h index 793e6fd78bc5..d48e17f571bc 100644 --- a/include/net/page_pool/helpers.h +++ b/include/net/page_pool/helpers.h @@ -83,6 +83,11 @@ static inline u64 *page_pool_ethtool_stats_get(u64 *data= , const void *stats) } #endif =20 +static inline struct page_pool *page_pool_get_pp(struct page *page) +{ + return page->pp; +} + /** * page_pool_dev_alloc_pages() - allocate a page. * @pool: pool from which to allocate diff --git a/net/core/xdp.c b/net/core/xdp.c index bcc5551c6424..73576192710b 100644 --- a/net/core/xdp.c +++ b/net/core/xdp.c @@ -384,7 +384,8 @@ void __xdp_return(void *data, struct xdp_mem_info *mem,= bool napi_direct, /* No need to check ((page->pp_magic & ~0x3UL) =3D=3D PP_SIGNATURE) * as mem->type knows this a page_pool page */ - page_pool_put_full_page(page->pp, page, napi_direct); + page_pool_put_full_page(page_pool_get_pp(page), page, + napi_direct); break; case MEM_TYPE_PAGE_SHARED: page_frag_free(data); --=20 2.33.0 From nobody Thu Jan 30 18:51:57 2025 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (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 9B05B7E792; Mon, 27 Jan 2025 03:04:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947100; cv=none; b=UnfyoDROFkKB2sWuTUTQhjUA/aP5QVBOOwlijuOaNadQRsnDClMLrtabolg0+bqGAu0pCToFBhG2zGqOwin+nuVJcSix2438/NK2qGxm0ecibnYnDw6F52GssbWsbpGvk5HL6Px9SvJJ6syKTGWR/d6W925NsnqhQuY8vlTmfNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947100; c=relaxed/simple; bh=HofMslkLzU27lEYsgNYZYxtDL4pv/CesiUGNib6YiCY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XbVapJxo6SLaxbDjC0QHLzF1CF9+z2iNmhZlVAf2zBuMQDuBA8Zrn01hueIZQlui4vcLcVLSrqiLHnxJiZRXHLN0ZxQcFvtW40xH9yhfktfYPKQlEsVU9hZQd29gpWQFR6bFZBdHLU/eBBNK13tFgXlOSCN0Rj1PUamFtdSnKuk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4YhCqg4rYMz11TB5; Mon, 27 Jan 2025 11:01:35 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id A6F78180101; Mon, 27 Jan 2025 11:04:50 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 27 Jan 2025 11:04:50 +0800 From: Yunsheng Lin To: , , CC: , , , Yunsheng Lin , Alexander Lobakin , Xuan Zhuo , Jesper Dangaard Brouer , Ilias Apalodimas , Eric Dumazet , Simon Horman , , Subject: [RFC v8 2/5] page_pool: fix timing for checking and disabling napi_local Date: Mon, 27 Jan 2025 10:57:31 +0800 Message-ID: <20250127025734.3406167-3-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250127025734.3406167-1-linyunsheng@huawei.com> References: <20250127025734.3406167-1-linyunsheng@huawei.com> 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 X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemf200006.china.huawei.com (7.185.36.61) Content-Type: text/plain; charset="utf-8" page_pool page may be freed from skb_defer_free_flush() in softirq context without binding to any specific napi, it may cause use-after-free problem due to the below time window, as below, CPU1 may still access napi->list_owner after CPU0 free the napi memory: CPU 0 CPU1 page_pool_destroy() skb_defer_free_flush() . . . napi =3D READ_ONCE(pool->p.napi); . . page_pool_disable_direct_recycling() . driver free napi memory . . . . napi && READ_ONCE(napi->list_owner) =3D=3D cpuid . . Use rcu mechanism to avoid the above problem. Note, the above was found during code reviewing on how to fix the problem in [1]. As the following IOMMU fix patch depends on rcu synchronization added in this patch and the time window is so small that it doesn't seem to be an urgent fix, so target the net-next as the IOMMU fix patch does. 1. https://lore.kernel.org/lkml/8067f204-1380-4d37-8ffd-007fc6f26738@kernel= .org/T/ Fixes: dd64b232deb8 ("page_pool: unlink from napi during destroy") Signed-off-by: Yunsheng Lin CC: Alexander Lobakin Reviewed-by: Xuan Zhuo --- net/core/page_pool.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/net/core/page_pool.c b/net/core/page_pool.c index f89cf93f6eb4..713502e8e8c9 100644 --- a/net/core/page_pool.c +++ b/net/core/page_pool.c @@ -810,6 +810,11 @@ static bool page_pool_napi_local(const struct page_poo= l *pool) if (READ_ONCE(pool->cpuid) =3D=3D cpuid) return true; =20 + /* The in_softirq() checking above should ensure RCU-bh read-side + * critical section, which is paired with the rcu sync in + * page_pool_destroy(). + */ + DEBUG_NET_WARN_ON_ONCE(!rcu_read_lock_bh_held()); napi =3D READ_ONCE(pool->p.napi); =20 return napi && READ_ONCE(napi->list_owner) =3D=3D cpuid; @@ -1126,6 +1131,12 @@ void page_pool_destroy(struct page_pool *pool) if (!page_pool_release(pool)) return; =20 + /* Paired with RCU-bh read-side critical section to enable clearing + * of pool->p.napi in page_pool_disable_direct_recycling() is seen + * before returning to driver to free the napi instance. + */ + synchronize_net(); + page_pool_detached(pool); pool->defer_start =3D jiffies; pool->defer_warn =3D jiffies + DEFER_WARN_INTERVAL; --=20 2.33.0 From nobody Thu Jan 30 18:51:57 2025 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (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 0AA0834545; Mon, 27 Jan 2025 03:04:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947099; cv=none; b=PTWqR2dERyrNh/32w3LwXs6coqkCsSn9d36L9t2RDsnWlypBlqQbeOvbtLXL4ww5x0bKoFHNXHzAWSNhdZYBF0RzSGBjEW/+sFJYi82P5BcvFDjQTOuQJh5WX92zvTndJ8V2zDN8GlrFXuNa7kRqKgfPMkBLtpJt4nu5qVVg54g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947099; c=relaxed/simple; bh=UnY0Jup8jqqW6ffVZeOiwt1wmK4Fwoyq/fS3dALc4ow=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Vyp8sC3310I9NrRxDKIMijyXg1z/NDkN3jXwdxYLmzOcMd4pdDOMN9J3T4/u8lmSpnY9xj8iE+FV8f393bEGqTTqvoB+3WhQv7K+vPAbq8AVT/UgDIOB6PJVCmdbOJkJ+0FED1R4+zqDpdTGJHLb1TsFU9Q1g8bjYwLtaKdKcNk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4YhCqn1zvczcm2p; Mon, 27 Jan 2025 11:01:41 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id AC5C7180101; Mon, 27 Jan 2025 11:04:54 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 27 Jan 2025 11:04:54 +0800 From: Yunsheng Lin To: , , CC: , , , Yunsheng Lin , Robin Murphy , Alexander Duyck , IOMMU , Andrew Morton , Eric Dumazet , Simon Horman , Jesper Dangaard Brouer , Ilias Apalodimas , , , Subject: [RFC v8 3/5] page_pool: fix IOMMU crash when driver has already unbound Date: Mon, 27 Jan 2025 10:57:32 +0800 Message-ID: <20250127025734.3406167-4-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250127025734.3406167-1-linyunsheng@huawei.com> References: <20250127025734.3406167-1-linyunsheng@huawei.com> 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 X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemf200006.china.huawei.com (7.185.36.61) Content-Type: text/plain; charset="utf-8" Networking driver with page_pool support may hand over page still with dma mapping to network stack and try to reuse that page after network stack is done with it and passes it back to page_pool to avoid the penalty of dma mapping/unmapping. With all the caching in the network stack, some pages may be held in the network stack without returning to the page_pool soon enough, and with VF disable causing the driver unbound, the page_pool does not stop the driver from doing it's unbounding work, instead page_pool uses workqueue to check if there is some pages coming back from the network stack periodically, if there is any, it will do the dma unmmapping related cleanup work. As mentioned in [1], attempting DMA unmaps after the driver has already unbound may leak resources or at worst corrupt memory. Fundamentally, the page pool code cannot allow DMA mappings to outlive the driver they belong to. Currently it seems there are at least two cases that the page is not released fast enough causing dma unmmapping done after driver has already unbound: 1. ipv4 packet defragmentation timeout: this seems to cause delay up to 30 secs. 2. skb_defer_free_flush(): this may cause infinite delay if there is no triggering for net_rx_action(). In order not to call DMA APIs to do DMA unmmapping after driver has already unbound and stall the unloading of the networking driver, use some pre-allocated item blocks to record inflight pages including the ones which are handed over to network stack, so the page_pool can do the DMA unmmapping for those pages when page_pool_destroy() is called. As the pre-allocated item blocks need to be large enough to avoid performance degradation, add a 'item_fast_empty' stat to indicate the unavailability of the pre-allocated item blocks. The overhead of tracking of inflight pages is about 10ns~20ns, which causes about 10% performance degradation for the test case of time_bench_page_pool03_slow() in [2]. Note, the devmem patchset seems to make the bug harder to fix, and may make backporting harder too. As there is no actual user for the devmem and the fixing for devmem is unclear for now, this patch does not consider fixing the case for devmem yet. 1. https://lore.kernel.org/lkml/8067f204-1380-4d37-8ffd-007fc6f26738@kernel= .org/T/ 2. https://github.com/netoptimizer/prototype-kernel CC: Robin Murphy CC: Alexander Duyck CC: IOMMU Fixes: f71fec47c2df ("page_pool: make sure struct device is stable") Signed-off-by: Yunsheng Lin Tested-by: Yonglong Liu --- include/linux/mm_types.h | 2 +- include/linux/skbuff.h | 1 + include/net/netmem.h | 22 ++- include/net/page_pool/helpers.h | 12 +- include/net/page_pool/types.h | 36 +++- net/core/devmem.c | 4 +- net/core/netmem_priv.h | 5 +- net/core/page_pool.c | 280 ++++++++++++++++++++++++++++---- net/core/page_pool_priv.h | 10 +- 9 files changed, 324 insertions(+), 48 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 332cee285662..97d32a2a3b77 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -120,7 +120,7 @@ struct page { * page_pool allocated pages. */ unsigned long pp_magic; - struct page_pool *pp; + struct page_pool_item *pp_item; unsigned long _pp_mapping_pad; unsigned long dma_addr; atomic_long_t pp_ref_count; diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 58009fa66102..0cff0806731a 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -39,6 +39,7 @@ #include #include #include +#include =20 /** * DOC: skb checksums diff --git a/include/net/netmem.h b/include/net/netmem.h index 8a6e20be4b9d..31257323ae1d 100644 --- a/include/net/netmem.h +++ b/include/net/netmem.h @@ -23,7 +23,7 @@ DECLARE_STATIC_KEY_FALSE(page_pool_mem_providers); struct net_iov { unsigned long __unused_padding; unsigned long pp_magic; - struct page_pool *pp; + struct page_pool_item *pp_item; struct dmabuf_genpool_chunk_owner *owner; unsigned long dma_addr; atomic_long_t pp_ref_count; @@ -33,7 +33,7 @@ struct net_iov { * * struct { * unsigned long pp_magic; - * struct page_pool *pp; + * struct page_pool_item *pp_item; * unsigned long _pp_mapping_pad; * unsigned long dma_addr; * atomic_long_t pp_ref_count; @@ -49,7 +49,7 @@ struct net_iov { static_assert(offsetof(struct page, pg) =3D=3D \ offsetof(struct net_iov, iov)) NET_IOV_ASSERT_OFFSET(pp_magic, pp_magic); -NET_IOV_ASSERT_OFFSET(pp, pp); +NET_IOV_ASSERT_OFFSET(pp_item, pp_item); NET_IOV_ASSERT_OFFSET(dma_addr, dma_addr); NET_IOV_ASSERT_OFFSET(pp_ref_count, pp_ref_count); #undef NET_IOV_ASSERT_OFFSET @@ -67,6 +67,11 @@ NET_IOV_ASSERT_OFFSET(pp_ref_count, pp_ref_count); */ typedef unsigned long __bitwise netmem_ref; =20 +/* Mirror page_pool_item_block, see include/net/page_pool/types.h */ +struct netmem_item_block { + struct page_pool *pp; +}; + static inline bool netmem_is_net_iov(const netmem_ref netmem) { return (__force unsigned long)netmem & NET_IOV; @@ -127,9 +132,18 @@ static inline struct net_iov *__netmem_clear_lsb(netme= m_ref netmem) return (struct net_iov *)((__force unsigned long)netmem & ~NET_IOV); } =20 +static inline struct page_pool_item *netmem_get_pp_item(netmem_ref netmem) +{ + return __netmem_clear_lsb(netmem)->pp_item; +} + static inline struct page_pool *netmem_get_pp(netmem_ref netmem) { - return __netmem_clear_lsb(netmem)->pp; + struct page_pool_item *item =3D netmem_get_pp_item(netmem); + struct netmem_item_block *block; + + block =3D (struct netmem_item_block *)((unsigned long)item & PAGE_MASK); + return block->pp; } =20 static inline atomic_long_t *netmem_get_pp_ref_count_ref(netmem_ref netmem) diff --git a/include/net/page_pool/helpers.h b/include/net/page_pool/helper= s.h index d48e17f571bc..41dcff1a35ad 100644 --- a/include/net/page_pool/helpers.h +++ b/include/net/page_pool/helpers.h @@ -83,9 +83,19 @@ static inline u64 *page_pool_ethtool_stats_get(u64 *data= , const void *stats) } #endif =20 +static inline struct page_pool_item_block * +page_pool_item_to_block(struct page_pool_item *item) +{ + return (struct page_pool_item_block *)((unsigned long)item & PAGE_MASK); +} + static inline struct page_pool *page_pool_get_pp(struct page *page) { - return page->pp; + /* The size of item_block is always PAGE_SIZE, the address of item_block + * for a specific item can be calculated using 'item & PAGE_MASK', so + * that we can find the page_pool object it belongs to. + */ + return page_pool_item_to_block(page->pp_item)->pp; } =20 /** diff --git a/include/net/page_pool/types.h b/include/net/page_pool/types.h index c022c410abe3..e7f6c8f1a778 100644 --- a/include/net/page_pool/types.h +++ b/include/net/page_pool/types.h @@ -102,6 +102,7 @@ struct page_pool_params { * @refill: an allocation which triggered a refill of the cache * @waive: pages obtained from the ptr ring that cannot be added to * the cache due to a NUMA mismatch + * @item_fast_empty: pre-allocated item cache is empty */ struct page_pool_alloc_stats { u64 fast; @@ -110,6 +111,7 @@ struct page_pool_alloc_stats { u64 empty; u64 refill; u64 waive; + u64 item_fast_empty; }; =20 /** @@ -142,6 +144,30 @@ struct page_pool_stats { }; #endif =20 +struct page_pool_item { + unsigned long state; + + union { + netmem_ref pp_netmem; + struct llist_node lentry; + }; +}; + +/* The size of item_block is always PAGE_SIZE, so that the address of item= _block + * for a specific item can be calculated using 'item & PAGE_MASK' + */ +struct page_pool_item_block { + struct page_pool *pp; + struct list_head list; + struct page_pool_item items[]; +}; + +/* Ensure the offset of 'pp' field for both 'page_pool_item_block' and + * 'netmem_item_block' are the same. + */ +static_assert(offsetof(struct page_pool_item_block, pp) =3D=3D \ + offsetof(struct netmem_item_block, pp)); + /* The whole frag API block must stay within one cacheline. On 32-bit syst= ems, * sizeof(long) =3D=3D sizeof(int), so that the block size is ``3 * sizeof= (long)``. * On 64-bit systems, the actual size is ``2 * sizeof(long) + sizeof(int)`= `. @@ -161,6 +187,7 @@ struct page_pool { =20 int cpuid; u32 pages_state_hold_cnt; + struct llist_head hold_items; =20 bool has_init_callback:1; /* slow::init_callback is set */ bool dma_map:1; /* Perform DMA mapping */ @@ -222,13 +249,20 @@ struct page_pool { #endif atomic_t pages_state_release_cnt; =20 + /* Synchronizate dma unmapping operation in page_pool_return_page() with + * page_pool_destory() when destroy_cnt is non-zero. + */ + spinlock_t item_lock; + struct list_head item_blocks; + struct llist_head release_items; + /* A page_pool is strictly tied to a single RX-queue being * protected by NAPI, due to above pp_alloc_cache. This * refcnt serves purpose is to simplify drivers error handling. */ refcount_t user_cnt; =20 - u64 destroy_cnt; + unsigned long destroy_cnt; =20 /* Slow/Control-path information follows */ struct page_pool_params_slow slow; diff --git a/net/core/devmem.c b/net/core/devmem.c index 11b91c12ee11..09c5aa83f12a 100644 --- a/net/core/devmem.c +++ b/net/core/devmem.c @@ -85,7 +85,7 @@ net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding = *binding) niov =3D &owner->niovs[index]; =20 niov->pp_magic =3D 0; - niov->pp =3D NULL; + niov->pp_item =3D NULL; atomic_long_set(&niov->pp_ref_count, 0); =20 return niov; @@ -380,7 +380,7 @@ bool mp_dmabuf_devmem_release_page(struct page_pool *po= ol, netmem_ref netmem) if (WARN_ON_ONCE(refcount !=3D 1)) return false; =20 - page_pool_clear_pp_info(netmem); + page_pool_clear_pp_info(pool, netmem); =20 net_devmem_free_dmabuf(netmem_to_net_iov(netmem)); =20 diff --git a/net/core/netmem_priv.h b/net/core/netmem_priv.h index 7eadb8393e00..3173f6070cf7 100644 --- a/net/core/netmem_priv.h +++ b/net/core/netmem_priv.h @@ -18,9 +18,10 @@ static inline void netmem_clear_pp_magic(netmem_ref netm= em) __netmem_clear_lsb(netmem)->pp_magic =3D 0; } =20 -static inline void netmem_set_pp(netmem_ref netmem, struct page_pool *pool) +static inline void netmem_set_pp_item(netmem_ref netmem, + struct page_pool_item *item) { - __netmem_clear_lsb(netmem)->pp =3D pool; + __netmem_clear_lsb(netmem)->pp_item =3D item; } =20 static inline void netmem_set_dma_addr(netmem_ref netmem, diff --git a/net/core/page_pool.c b/net/core/page_pool.c index 713502e8e8c9..dfce02623afa 100644 --- a/net/core/page_pool.c +++ b/net/core/page_pool.c @@ -61,6 +61,7 @@ static const char pp_stats[][ETH_GSTRING_LEN] =3D { "rx_pp_alloc_empty", "rx_pp_alloc_refill", "rx_pp_alloc_waive", + "rx_pp_alloc_item_fast_empty", "rx_pp_recycle_cached", "rx_pp_recycle_cache_full", "rx_pp_recycle_ring", @@ -94,6 +95,7 @@ bool page_pool_get_stats(const struct page_pool *pool, stats->alloc_stats.empty +=3D pool->alloc_stats.empty; stats->alloc_stats.refill +=3D pool->alloc_stats.refill; stats->alloc_stats.waive +=3D pool->alloc_stats.waive; + stats->alloc_stats.item_fast_empty +=3D pool->alloc_stats.item_fast_empty; =20 for_each_possible_cpu(cpu) { const struct page_pool_recycle_stats *pcpu =3D @@ -139,6 +141,7 @@ u64 *page_pool_ethtool_stats_get(u64 *data, const void = *stats) *data++ =3D pool_stats->alloc_stats.empty; *data++ =3D pool_stats->alloc_stats.refill; *data++ =3D pool_stats->alloc_stats.waive; + *data++ =3D pool_stats->alloc_stats.item_fast_empty; *data++ =3D pool_stats->recycle_stats.cached; *data++ =3D pool_stats->recycle_stats.cache_full; *data++ =3D pool_stats->recycle_stats.ring; @@ -321,6 +324,209 @@ static void page_pool_uninit(struct page_pool *pool) #endif } =20 +#define PAGE_POOL_ITEM_USED 0 +#define PAGE_POOL_ITEM_MAPPED 1 + +#define ITEMS_PER_PAGE ((PAGE_SIZE - \ + offsetof(struct page_pool_item_block, items)) / \ + sizeof(struct page_pool_item)) + +#if defined(CONFIG_DEBUG_NET) +#define page_pool_item_set_used(item) \ + __set_bit(PAGE_POOL_ITEM_USED, &(item)->state) + +#define page_pool_item_clear_used(item) \ + __clear_bit(PAGE_POOL_ITEM_USED, &(item)->state) + +#define page_pool_item_is_used(item) \ + test_bit(PAGE_POOL_ITEM_USED, &(item)->state) +#else +#define page_pool_item_set_used(item) +#define page_pool_item_clear_used(item) +#define page_pool_item_is_used(item) false +#endif + +#define page_pool_item_set_mapped(item) \ + __set_bit(PAGE_POOL_ITEM_MAPPED, &(item)->state) + +/* Only clear_mapped and is_mapped need to be atomic as they can be + * called concurrently. + */ +#define page_pool_item_clear_mapped(item) \ + clear_bit(PAGE_POOL_ITEM_MAPPED, &(item)->state) + +#define page_pool_item_is_mapped(item) \ + test_bit(PAGE_POOL_ITEM_MAPPED, &(item)->state) + +static __always_inline void __page_pool_release_page_dma(struct page_pool = *pool, + netmem_ref netmem) +{ + struct page_pool_item *item; + bool destroyed; + dma_addr_t dma; + + if (!pool->dma_map) + /* Always account for inflight pages, even if we didn't + * map them + */ + return; + + /* Paired with the rcu synchronization in page_pool_destroy() to ensure + * synchronize dma unmapping operation between page_pool_destroy() and + * page being released to page_pool from networking by using a spinlock + * when pool->destroy_cnt is non-zero. + */ + rcu_read_lock(); + destroyed =3D !!READ_ONCE(pool->destroy_cnt); + item =3D netmem_get_pp_item(netmem); + + /* To catch the case of item state not setting up correctly as dma + * unmapping is always needed when page_pool_destory() is not called + * yet. + */ + DEBUG_NET_WARN_ON_ONCE(!destroyed && + !page_pool_item_is_mapped(item)); + if (unlikely(destroyed)) { + spin_lock_bh(&pool->item_lock); + + if (!page_pool_item_is_mapped(item)) + goto out_unlock; + } + + dma =3D page_pool_get_dma_addr_netmem(netmem); + + /* When page is unmapped, it cannot be returned to our pool */ + dma_unmap_page_attrs(pool->p.dev, dma, + PAGE_SIZE << pool->p.order, pool->p.dma_dir, + DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING); + page_pool_set_dma_addr_netmem(netmem, 0); + page_pool_item_clear_mapped(item); + +out_unlock: + if (unlikely(destroyed)) + spin_unlock_bh(&pool->item_lock); + + rcu_read_unlock(); +} + +static void page_pool_item_uninit(struct page_pool *pool) +{ + while (!list_empty(&pool->item_blocks)) { + struct page_pool_item_block *block; + + block =3D list_first_entry(&pool->item_blocks, + struct page_pool_item_block, + list); + list_del(&block->list); + put_page(virt_to_page(block)); + } +} + +static int page_pool_item_init(struct page_pool *pool) +{ +#define PAGE_POOL_MIN_INFLIGHT_ITEMS 512 + struct page_pool_item_block *block; + int item_cnt; + + INIT_LIST_HEAD(&pool->item_blocks); + spin_lock_init(&pool->item_lock); + init_llist_head(&pool->hold_items); + init_llist_head(&pool->release_items); + + item_cnt =3D pool->p.pool_size * 2 + PP_ALLOC_CACHE_SIZE + + PAGE_POOL_MIN_INFLIGHT_ITEMS; + for (; item_cnt > 0; item_cnt -=3D ITEMS_PER_PAGE) { + struct page *page; + unsigned int i; + + page =3D alloc_pages_node(pool->p.nid, GFP_KERNEL | __GFP_ZERO, + 0); + if (!page) { + page_pool_item_uninit(pool); + return -ENOMEM; + } + + block =3D page_address(page); + block->pp =3D pool; + list_add(&block->list, &pool->item_blocks); + + for (i =3D 0; i < ITEMS_PER_PAGE; i++) + __llist_add(&block->items[i].lentry, &pool->hold_items); + } + + return 0; +} + +static void page_pool_item_unmap(struct page_pool *pool) +{ + struct page_pool_item_block *block; + + if (!pool->dma_map || pool->mp_priv) + return; + + list_for_each_entry(block, &pool->item_blocks, list) { + struct page_pool_item *items =3D block->items; + int i; + + for (i =3D 0; i < ITEMS_PER_PAGE; i++) { + struct page_pool_item *item =3D &items[i]; + + if (!page_pool_item_is_mapped(item)) + continue; + + __page_pool_release_page_dma(pool, item->pp_netmem); + } + } +} + +static struct page_pool_item *page_pool_fast_item_alloc(struct page_pool *= pool) +{ + struct llist_node *first =3D pool->hold_items.first; + + if (unlikely(!first)) { + first =3D llist_del_all(&pool->release_items); + + if (unlikely(!first)) { + alloc_stat_inc(pool, item_fast_empty); + return NULL; + } + } + + pool->hold_items.first =3D first->next; + return llist_entry(first, struct page_pool_item, lentry); +} + +static bool page_pool_set_item_info(struct page_pool *pool, netmem_ref net= mem) +{ + struct page_pool_item *item =3D page_pool_fast_item_alloc(pool); + + if (likely(item)) { + item->pp_netmem =3D netmem; + page_pool_item_set_used(item); + netmem_set_pp_item(netmem, item); + } + + return !!item; +} + +static void page_pool_fast_item_free(struct page_pool *pool, + struct page_pool_item *item) +{ + llist_add(&item->lentry, &pool->release_items); +} + +static void page_pool_clear_item_info(struct page_pool *pool, netmem_ref n= etmem) +{ + struct page_pool_item *item =3D netmem_get_pp_item(netmem); + + DEBUG_NET_WARN_ON_ONCE(item->pp_netmem !=3D netmem); + DEBUG_NET_WARN_ON_ONCE(page_pool_item_is_mapped(item)); + DEBUG_NET_WARN_ON_ONCE(!page_pool_item_is_used(item)); + page_pool_item_clear_used(item); + netmem_set_pp_item(netmem, NULL); + page_pool_fast_item_free(pool, item); +} + /** * page_pool_create_percpu() - create a page pool for a given cpu. * @params: parameters, see struct page_pool_params @@ -340,12 +546,18 @@ page_pool_create_percpu(const struct page_pool_params= *params, int cpuid) if (err < 0) goto err_free; =20 - err =3D page_pool_list(pool); + err =3D page_pool_item_init(pool); if (err) goto err_uninit; =20 + err =3D page_pool_list(pool); + if (err) + goto err_item_uninit; + return pool; =20 +err_item_uninit: + page_pool_item_uninit(pool); err_uninit: page_pool_uninit(pool); err_free: @@ -460,6 +672,7 @@ page_pool_dma_sync_for_device(const struct page_pool *p= ool, =20 static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem) { + struct page_pool_item *item; dma_addr_t dma; =20 /* Setup DMA mapping: use 'struct page' area for storing DMA-addr @@ -477,6 +690,9 @@ static bool page_pool_dma_map(struct page_pool *pool, n= etmem_ref netmem) if (page_pool_set_dma_addr_netmem(netmem, dma)) goto unmap_failed; =20 + item =3D netmem_get_pp_item(netmem); + DEBUG_NET_WARN_ON_ONCE(page_pool_item_is_mapped(item)); + page_pool_item_set_mapped(item); page_pool_dma_sync_for_device(pool, netmem, pool->p.max_len); =20 return true; @@ -499,19 +715,24 @@ static struct page *__page_pool_alloc_page_order(stru= ct page_pool *pool, if (unlikely(!page)) return NULL; =20 - if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(pag= e)))) { - put_page(page); - return NULL; - } + if (unlikely(!page_pool_set_pp_info(pool, page_to_netmem(page)))) + goto err_alloc; + + if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(pag= e)))) + goto err_set_info; =20 alloc_stat_inc(pool, slow_high_order); - page_pool_set_pp_info(pool, page_to_netmem(page)); =20 /* Track how many pages are held 'in-flight' */ pool->pages_state_hold_cnt++; trace_page_pool_state_hold(pool, page_to_netmem(page), pool->pages_state_hold_cnt); return page; +err_set_info: + page_pool_clear_pp_info(pool, page_to_netmem(page)); +err_alloc: + put_page(page); + return NULL; } =20 /* slow path */ @@ -546,12 +767,18 @@ static noinline netmem_ref __page_pool_alloc_pages_sl= ow(struct page_pool *pool, */ for (i =3D 0; i < nr_pages; i++) { netmem =3D pool->alloc.cache[i]; + + if (unlikely(!page_pool_set_pp_info(pool, netmem))) { + put_page(netmem_to_page(netmem)); + continue; + } + if (dma_map && unlikely(!page_pool_dma_map(pool, netmem))) { + page_pool_clear_pp_info(pool, netmem); put_page(netmem_to_page(netmem)); continue; } =20 - page_pool_set_pp_info(pool, netmem); pool->alloc.cache[pool->alloc.count++] =3D netmem; /* Track how many pages are held 'in-flight' */ pool->pages_state_hold_cnt++; @@ -623,9 +850,11 @@ s32 page_pool_inflight(const struct page_pool *pool, b= ool strict) return inflight; } =20 -void page_pool_set_pp_info(struct page_pool *pool, netmem_ref netmem) +bool page_pool_set_pp_info(struct page_pool *pool, netmem_ref netmem) { - netmem_set_pp(netmem, pool); + if (unlikely(!page_pool_set_item_info(pool, netmem))) + return false; + netmem_or_pp_magic(netmem, PP_SIGNATURE); =20 /* Ensuring all pages have been split into one fragment initially: @@ -637,32 +866,14 @@ void page_pool_set_pp_info(struct page_pool *pool, ne= tmem_ref netmem) page_pool_fragment_netmem(netmem, 1); if (pool->has_init_callback) pool->slow.init_callback(netmem, pool->slow.init_arg); -} =20 -void page_pool_clear_pp_info(netmem_ref netmem) -{ - netmem_clear_pp_magic(netmem); - netmem_set_pp(netmem, NULL); + return true; } =20 -static __always_inline void __page_pool_release_page_dma(struct page_pool = *pool, - netmem_ref netmem) +void page_pool_clear_pp_info(struct page_pool *pool, netmem_ref netmem) { - dma_addr_t dma; - - if (!pool->dma_map) - /* Always account for inflight pages, even if we didn't - * map them - */ - return; - - dma =3D page_pool_get_dma_addr_netmem(netmem); - - /* When page is unmapped, it cannot be returned to our pool */ - dma_unmap_page_attrs(pool->p.dev, dma, - PAGE_SIZE << pool->p.order, pool->p.dma_dir, - DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING); - page_pool_set_dma_addr_netmem(netmem, 0); + netmem_clear_pp_magic(netmem); + page_pool_clear_item_info(pool, netmem); } =20 /* Disconnects a page (from a page_pool). API users can have a need @@ -688,7 +899,7 @@ void page_pool_return_page(struct page_pool *pool, netm= em_ref netmem) trace_page_pool_state_release(pool, netmem, count); =20 if (put) { - page_pool_clear_pp_info(netmem); + page_pool_clear_pp_info(pool, netmem); put_page(netmem_to_page(netmem)); } /* An optimization would be to call __free_pages(page, pool->p.order) @@ -1012,6 +1223,7 @@ static void __page_pool_destroy(struct page_pool *poo= l) if (pool->disconnect) pool->disconnect(pool); =20 + page_pool_item_uninit(pool); page_pool_unlist(pool); page_pool_uninit(pool); =20 @@ -1043,7 +1255,7 @@ static void page_pool_empty_alloc_cache_once(struct p= age_pool *pool) static void page_pool_scrub(struct page_pool *pool) { page_pool_empty_alloc_cache_once(pool); - pool->destroy_cnt++; + WRITE_ONCE(pool->destroy_cnt, pool->destroy_cnt + 1); =20 /* No more consumers should exist, but producers could still * be in-flight. @@ -1137,6 +1349,8 @@ void page_pool_destroy(struct page_pool *pool) */ synchronize_net(); =20 + page_pool_item_unmap(pool); + page_pool_detached(pool); pool->defer_start =3D jiffies; pool->defer_warn =3D jiffies + DEFER_WARN_INTERVAL; diff --git a/net/core/page_pool_priv.h b/net/core/page_pool_priv.h index 57439787b9c2..5d85f862a30a 100644 --- a/net/core/page_pool_priv.h +++ b/net/core/page_pool_priv.h @@ -36,16 +36,18 @@ static inline bool page_pool_set_dma_addr(struct page *= page, dma_addr_t addr) } =20 #if defined(CONFIG_PAGE_POOL) -void page_pool_set_pp_info(struct page_pool *pool, netmem_ref netmem); -void page_pool_clear_pp_info(netmem_ref netmem); +bool page_pool_set_pp_info(struct page_pool *pool, netmem_ref netmem); +void page_pool_clear_pp_info(struct page_pool *pool, netmem_ref netmem); int page_pool_check_memory_provider(struct net_device *dev, struct netdev_rx_queue *rxq); #else -static inline void page_pool_set_pp_info(struct page_pool *pool, +static inline bool page_pool_set_pp_info(struct page_pool *pool, netmem_ref netmem) { + return true; } -static inline void page_pool_clear_pp_info(netmem_ref netmem) +static inline void page_pool_clear_pp_info(struct page_pool *pool, + netmem_ref netmem) { } static inline int page_pool_check_memory_provider(struct net_device *dev, --=20 2.33.0 From nobody Thu Jan 30 18:51:57 2025 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) (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 CD9A61FCFC0; Mon, 27 Jan 2025 03:05:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947108; cv=none; b=l3xXcoPtNgnKA0rAufzalT9Knql62/9SCgx1SgNQ/J9Ub4u8BhQ0rppqZdUb3mf/wB5mLskFg8G6S4JGHuSndhX2co+I3liYwMrYDpGMF63slBwovZkycA93WAzGqS6jtxCt7eh8toJcBNBxzpc1ojaQ37f3X7Piol/k5KK4aF8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947108; c=relaxed/simple; bh=a3slRYUOH75ANQAss5H386VAN8u1Ki+dwG+HDBEi4L0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WHvgPTzyX6HWkKNY5RhB++00KpHpOopRtBMMvcjzjt5cCEROneN+vPLBk9PGf3OwETUhReFpOUqyVQRU/2aQRDrNKIXAxn0cBMG6+pes4xepkY1Gt8WjCcuRKNRutFP0w3cS9urK/9rRm7Y3bTZM84UPLwIGg3QSlnJxJmrMWkg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.48]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4YhCrY0XpkzRm2B; Mon, 27 Jan 2025 11:02:21 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 8243F1802E1; Mon, 27 Jan 2025 11:04:56 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 27 Jan 2025 11:04:56 +0800 From: Yunsheng Lin To: , , CC: , , , Yunsheng Lin , Robin Murphy , Alexander Duyck , IOMMU , Jesper Dangaard Brouer , Ilias Apalodimas , Eric Dumazet , Simon Horman , , Subject: [RFC v8 4/5] page_pool: support unlimited number of inflight pages Date: Mon, 27 Jan 2025 10:57:33 +0800 Message-ID: <20250127025734.3406167-5-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250127025734.3406167-1-linyunsheng@huawei.com> References: <20250127025734.3406167-1-linyunsheng@huawei.com> 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 X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemf200006.china.huawei.com (7.185.36.61) Content-Type: text/plain; charset="utf-8" Currently a fixed size of pre-allocated memory is used to keep track of the inflight pages, in order to use the DMA API correctly. As mentioned [1], the number of inflight pages can be up to 73203 depending on the use cases. Allocate memory dynamically to keep track of the inflight pages when pre-allocated memory runs out. The overhead of using dynamic memory allocation is about 10ns~ 20ns, which causes 5%~10% performance degradation for the test case of time_bench_page_pool03_slow() in [2]. 1. https://lore.kernel.org/all/b8b7818a-e44b-45f5-91c2-d5eceaa5dd5b@kernel.= org/ 2. https://github.com/netoptimizer/prototype-kernel CC: Robin Murphy CC: Alexander Duyck CC: IOMMU Fixes: f71fec47c2df ("page_pool: make sure struct device is stable") Signed-off-by: Yunsheng Lin --- include/net/page_pool/types.h | 10 +++++ net/core/page_pool.c | 80 ++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/include/net/page_pool/types.h b/include/net/page_pool/types.h index e7f6c8f1a778..ea393e8b7d65 100644 --- a/include/net/page_pool/types.h +++ b/include/net/page_pool/types.h @@ -103,6 +103,7 @@ struct page_pool_params { * @waive: pages obtained from the ptr ring that cannot be added to * the cache due to a NUMA mismatch * @item_fast_empty: pre-allocated item cache is empty + * @item_slow_failed: failed to allocate memory for item_block */ struct page_pool_alloc_stats { u64 fast; @@ -112,6 +113,7 @@ struct page_pool_alloc_stats { u64 refill; u64 waive; u64 item_fast_empty; + u64 item_slow_failed; }; =20 /** @@ -159,9 +161,16 @@ struct page_pool_item { struct page_pool_item_block { struct page_pool *pp; struct list_head list; + unsigned int flags; + refcount_t ref; struct page_pool_item items[]; }; =20 +struct page_pool_slow_item { + struct page_pool_item_block *block; + unsigned int next_to_use; +}; + /* Ensure the offset of 'pp' field for both 'page_pool_item_block' and * 'netmem_item_block' are the same. */ @@ -188,6 +197,7 @@ struct page_pool { int cpuid; u32 pages_state_hold_cnt; struct llist_head hold_items; + struct page_pool_slow_item slow_items; =20 bool has_init_callback:1; /* slow::init_callback is set */ bool dma_map:1; /* Perform DMA mapping */ diff --git a/net/core/page_pool.c b/net/core/page_pool.c index dfce02623afa..470c8ccde4ef 100644 --- a/net/core/page_pool.c +++ b/net/core/page_pool.c @@ -62,6 +62,7 @@ static const char pp_stats[][ETH_GSTRING_LEN] =3D { "rx_pp_alloc_refill", "rx_pp_alloc_waive", "rx_pp_alloc_item_fast_empty", + "rx_pp_alloc_item_slow_failed", "rx_pp_recycle_cached", "rx_pp_recycle_cache_full", "rx_pp_recycle_ring", @@ -96,6 +97,7 @@ bool page_pool_get_stats(const struct page_pool *pool, stats->alloc_stats.refill +=3D pool->alloc_stats.refill; stats->alloc_stats.waive +=3D pool->alloc_stats.waive; stats->alloc_stats.item_fast_empty +=3D pool->alloc_stats.item_fast_empty; + stats->alloc_stats.item_slow_failed +=3D pool->alloc_stats.item_slow_fail= ed; =20 for_each_possible_cpu(cpu) { const struct page_pool_recycle_stats *pcpu =3D @@ -142,6 +144,7 @@ u64 *page_pool_ethtool_stats_get(u64 *data, const void = *stats) *data++ =3D pool_stats->alloc_stats.refill; *data++ =3D pool_stats->alloc_stats.waive; *data++ =3D pool_stats->alloc_stats.item_fast_empty; + *data++ =3D pool_stats->alloc_stats.item_slow_failed; *data++ =3D pool_stats->recycle_stats.cached; *data++ =3D pool_stats->recycle_stats.cache_full; *data++ =3D pool_stats->recycle_stats.ring; @@ -418,6 +421,7 @@ static void page_pool_item_uninit(struct page_pool *poo= l) struct page_pool_item_block, list); list_del(&block->list); + WARN_ON(refcount_read(&block->ref)); put_page(virt_to_page(block)); } } @@ -496,10 +500,43 @@ static struct page_pool_item *page_pool_fast_item_all= oc(struct page_pool *pool) return llist_entry(first, struct page_pool_item, lentry); } =20 +#define PAGE_POOL_SLOW_ITEM_BLOCK_BIT BIT(0) +static struct page_pool_item *page_pool_slow_item_alloc(struct page_pool *= pool) +{ + if (unlikely(!pool->slow_items.block || + pool->slow_items.next_to_use >=3D ITEMS_PER_PAGE)) { + struct page_pool_item_block *block; + struct page *page; + + page =3D alloc_pages_node(pool->p.nid, GFP_ATOMIC | __GFP_NOWARN | + __GFP_ZERO, 0); + if (!page) { + alloc_stat_inc(pool, item_slow_failed); + return NULL; + } + + block =3D page_address(page); + block->pp =3D pool; + block->flags |=3D PAGE_POOL_SLOW_ITEM_BLOCK_BIT; + refcount_set(&block->ref, ITEMS_PER_PAGE); + pool->slow_items.block =3D block; + pool->slow_items.next_to_use =3D 0; + + spin_lock_bh(&pool->item_lock); + list_add(&block->list, &pool->item_blocks); + spin_unlock_bh(&pool->item_lock); + } + + return &pool->slow_items.block->items[pool->slow_items.next_to_use++]; +} + static bool page_pool_set_item_info(struct page_pool *pool, netmem_ref net= mem) { struct page_pool_item *item =3D page_pool_fast_item_alloc(pool); =20 + if (unlikely(!item)) + item =3D page_pool_slow_item_alloc(pool); + if (likely(item)) { item->pp_netmem =3D netmem; page_pool_item_set_used(item); @@ -509,6 +546,37 @@ static bool page_pool_set_item_info(struct page_pool *= pool, netmem_ref netmem) return !!item; } =20 +static void __page_pool_slow_item_free(struct page_pool *pool, + struct page_pool_item_block *block) +{ + spin_lock_bh(&pool->item_lock); + list_del(&block->list); + spin_unlock_bh(&pool->item_lock); + + put_page(virt_to_page(block)); +} + +static void page_pool_slow_item_drain(struct page_pool *pool) +{ + struct page_pool_item_block *block =3D pool->slow_items.block; + + if (!block || pool->slow_items.next_to_use >=3D ITEMS_PER_PAGE) + return; + + if (refcount_sub_and_test(ITEMS_PER_PAGE - pool->slow_items.next_to_use, + &block->ref)) + __page_pool_slow_item_free(pool, block); +} + +static void page_pool_slow_item_free(struct page_pool *pool, + struct page_pool_item_block *block) +{ + if (likely(!refcount_dec_and_test(&block->ref))) + return; + + __page_pool_slow_item_free(pool, block); +} + static void page_pool_fast_item_free(struct page_pool *pool, struct page_pool_item *item) { @@ -518,13 +586,22 @@ static void page_pool_fast_item_free(struct page_pool= *pool, static void page_pool_clear_item_info(struct page_pool *pool, netmem_ref n= etmem) { struct page_pool_item *item =3D netmem_get_pp_item(netmem); + struct page_pool_item_block *block; =20 DEBUG_NET_WARN_ON_ONCE(item->pp_netmem !=3D netmem); DEBUG_NET_WARN_ON_ONCE(page_pool_item_is_mapped(item)); DEBUG_NET_WARN_ON_ONCE(!page_pool_item_is_used(item)); page_pool_item_clear_used(item); netmem_set_pp_item(netmem, NULL); - page_pool_fast_item_free(pool, item); + + block =3D page_pool_item_to_block(item); + if (likely(!(block->flags & PAGE_POOL_SLOW_ITEM_BLOCK_BIT))) { + DEBUG_NET_WARN_ON_ONCE(refcount_read(&block->ref)); + page_pool_fast_item_free(pool, item); + return; + } + + page_pool_slow_item_free(pool, block); } =20 /** @@ -1339,6 +1416,7 @@ void page_pool_destroy(struct page_pool *pool) =20 page_pool_disable_direct_recycling(pool); page_pool_free_frag(pool); + page_pool_slow_item_drain(pool); =20 if (!page_pool_release(pool)) return; --=20 2.33.0 From nobody Thu Jan 30 18:51:57 2025 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (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 2D99B1FBEB8; Mon, 27 Jan 2025 03:04:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947102; cv=none; b=iGmreLzS/EpBL3U8g9/A7qap7KFO1rUSGe2gF0rKOsxycuu1M9vrygEcCHnEf64DYPLCaygVd0zRXHz3Cs89ubQ3Sb5UxrvFsGhXg40jCJU/QGDszOOEcT4dYUvvw3zkPUJgmdNDyvNkEHJYjI+DFsZ5klH4M/2CWZLv7pCDIGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737947102; c=relaxed/simple; bh=jv0DufX4C9BC88MPMm+snIWtTCqONcf9tEYOqFM8GcQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dm9rEeDbtxX1T3PCZFmex8QAlK48ttFReHaYw7VTKrdS6L2GtIrJCrVZ81MYvG4X/VJZCAZWyiNxg2jzEjgtWNS0kXJ4xz5A39VtvnIp2ossF3bMtu/28PR5HZD7RgjNLpeuIl8dLxh5vCtgvoqfN2hviQDcNIY1KJjUgR8oRjs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4YhCqd03bxz1l0Bg; Mon, 27 Jan 2025 11:01:33 +0800 (CST) Received: from dggpemf200006.china.huawei.com (unknown [7.185.36.61]) by mail.maildlp.com (Postfix) with ESMTPS id 03940180042; Mon, 27 Jan 2025 11:04:58 +0800 (CST) Received: from localhost.localdomain (10.90.30.45) by dggpemf200006.china.huawei.com (7.185.36.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 27 Jan 2025 11:04:57 +0800 From: Yunsheng Lin To: , , CC: , , , Yunsheng Lin , Robin Murphy , Alexander Duyck , IOMMU , Jesper Dangaard Brouer , Ilias Apalodimas , Eric Dumazet , Simon Horman , , Subject: [RFC v8 5/5] page_pool: skip dma sync operation for inflight pages Date: Mon, 27 Jan 2025 10:57:34 +0800 Message-ID: <20250127025734.3406167-6-linyunsheng@huawei.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250127025734.3406167-1-linyunsheng@huawei.com> References: <20250127025734.3406167-1-linyunsheng@huawei.com> 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 X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemf200006.china.huawei.com (7.185.36.61) Content-Type: text/plain; charset="utf-8" Skip dma sync operation for inflight pages before the page_pool_destroy() returns to the driver as DMA API expects to be called with a valid device bound to a driver as mentioned in [1]. After page_pool_destroy() is called, the page is not expected to be recycled back to pool->alloc cache and dma sync operation is not needed when the page is not recyclable or pool->ring is full, so only skip the dma sync operation for the infilght pages by clearing the pool->dma_sync, as rcu sync operation in page_pool_destroy() is paired with rcu lock in page_pool_recycle_in_ring() to ensure that there is no dma sync operation called after rcu sync operation. 1. https://lore.kernel.org/all/caf31b5e-0e8f-4844-b7ba-ef59ed13b74e@arm.com/ CC: Robin Murphy CC: Alexander Duyck CC: IOMMU Fixes: f71fec47c2df ("page_pool: make sure struct device is stable") Signed-off-by: Yunsheng Lin --- net/core/page_pool.c | 56 +++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/net/core/page_pool.c b/net/core/page_pool.c index 470c8ccde4ef..1e93415ee50c 100644 --- a/net/core/page_pool.c +++ b/net/core/page_pool.c @@ -278,9 +278,6 @@ static int page_pool_init(struct page_pool *pool, /* Driver calling page_pool_create() also call page_pool_destroy() */ refcount_set(&pool->user_cnt, 1); =20 - if (pool->dma_map) - get_device(pool->p.dev); - if (pool->slow.flags & PP_FLAG_ALLOW_UNREADABLE_NETMEM) { /* We rely on rtnl_lock()ing to make sure netdev_rx_queue * configuration doesn't change while we're initializing @@ -318,9 +315,6 @@ static void page_pool_uninit(struct page_pool *pool) { ptr_ring_cleanup(&pool->ring, NULL); =20 - if (pool->dma_map) - put_device(pool->p.dev); - #ifdef CONFIG_PAGE_POOL_STATS if (!pool->system) free_percpu(pool->recycle_stats); @@ -747,6 +741,25 @@ page_pool_dma_sync_for_device(const struct page_pool *= pool, __page_pool_dma_sync_for_device(pool, netmem, dma_sync_size); } =20 +static __always_inline void +page_pool_dma_sync_for_device_rcu(const struct page_pool *pool, + netmem_ref netmem, + u32 dma_sync_size) +{ + if (!pool->dma_sync || !dma_dev_need_sync(pool->p.dev)) + return; + + rcu_read_lock(); + + /* Recheck the dma_sync under rcu lock to pair with rcu sync operation + * in page_pool_destroy(). + */ + if (pool->dma_sync) + __page_pool_dma_sync_for_device(pool, netmem, dma_sync_size); + + rcu_read_unlock(); +} + static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem) { struct page_pool_item *item; @@ -985,7 +998,8 @@ void page_pool_return_page(struct page_pool *pool, netm= em_ref netmem) */ } =20 -static bool page_pool_recycle_in_ring(struct page_pool *pool, netmem_ref n= etmem) +static bool page_pool_recycle_in_ring(struct page_pool *pool, netmem_ref n= etmem, + unsigned int dma_sync_size) { int ret; /* BH protection not needed if current is softirq */ @@ -994,12 +1008,12 @@ static bool page_pool_recycle_in_ring(struct page_po= ol *pool, netmem_ref netmem) else ret =3D ptr_ring_produce_bh(&pool->ring, (__force void *)netmem); =20 - if (!ret) { + if (likely(!ret)) { + page_pool_dma_sync_for_device_rcu(pool, netmem, dma_sync_size); recycle_stat_inc(pool, ring); - return true; } =20 - return false; + return !ret; } =20 /* Only allow direct recycling in special circumstances, into the @@ -1052,10 +1066,10 @@ __page_pool_put_page(struct page_pool *pool, netmem= _ref netmem, if (likely(__page_pool_page_can_be_recycled(netmem))) { /* Read barrier done in page_ref_count / READ_ONCE */ =20 - page_pool_dma_sync_for_device(pool, netmem, dma_sync_size); - - if (allow_direct && page_pool_recycle_in_cache(netmem, pool)) + if (allow_direct && page_pool_recycle_in_cache(netmem, pool)) { + page_pool_dma_sync_for_device(pool, netmem, dma_sync_size); return 0; + } =20 /* Page found as candidate for recycling */ return netmem; @@ -1116,7 +1130,7 @@ void page_pool_put_unrefed_netmem(struct page_pool *p= ool, netmem_ref netmem, =20 netmem =3D __page_pool_put_page(pool, netmem, dma_sync_size, allow_direct); - if (netmem && !page_pool_recycle_in_ring(pool, netmem)) { + if (netmem && !page_pool_recycle_in_ring(pool, netmem, dma_sync_size)) { /* Cache full, fallback to free pages */ recycle_stat_inc(pool, ring_full); page_pool_return_page(pool, netmem); @@ -1174,13 +1188,17 @@ void page_pool_put_page_bulk(struct page_pool *pool= , void **data, =20 /* Bulk producer into ptr_ring page_pool cache */ in_softirq =3D page_pool_producer_lock(pool); + rcu_read_lock(); for (i =3D 0; i < bulk_len; i++) { if (__ptr_ring_produce(&pool->ring, data[i])) { /* ring full */ recycle_stat_inc(pool, ring_full); break; } + page_pool_dma_sync_for_device(pool, (__force netmem_ref)data[i], + -1); } + rcu_read_unlock(); recycle_stat_add(pool, ring, i); page_pool_producer_unlock(pool, in_softirq); =20 @@ -1421,6 +1439,16 @@ void page_pool_destroy(struct page_pool *pool) if (!page_pool_release(pool)) return; =20 + /* After page_pool_destroy() is called, the page is not expected to be + * recycled back to pool->alloc cache and dma sync operation is not + * needed when the page is not recyclable or pool->ring is full, skip + * the dma sync operation for the infilght pages by clearing the + * pool->dma_sync, and the below synchronize_net() is paired with rcu + * lock when page is recycled back into ptr_ring to ensure that there is + * no dma sync operation called after rcu sync operation. + */ + pool->dma_sync =3D false; + /* Paired with RCU-bh read-side critical section to enable clearing * of pool->p.napi in page_pool_disable_direct_recycling() is seen * before returning to driver to free the napi instance. --=20 2.33.0