From: Yunsheng Lin <linyunsheng@huawei.com>
To: Bagas Sanjaya <bagasdotme@gmail.com>, <davem@davemloft.net>,
<kuba@kernel.org>, <pabeni@redhat.com>
Cc: <netdev@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
Alexander Duyck <alexander.duyck@gmail.com>,
Jonathan Corbet <corbet@lwn.net>,
Andrew Morton <akpm@linux-foundation.org>,
<linux-doc@vger.kernel.org>, <linux-mm@kvack.org>
Subject: Re: [PATCH net-next v22 13/14] mm: page_frag: update documentation for page_frag
Date: Mon, 21 Oct 2024 17:32:35 +0800 [thread overview]
Message-ID: <6f9840b3-66c4-485e-b6bb-baeaa641e720@huawei.com> (raw)
In-Reply-To: <ZxTVRRecKRpna6Aj@archie.me>
On 2024/10/20 18:02, Bagas Sanjaya wrote:
Thanks, will try my best to not miss any 'alloc' typo for doc patch
next version:(
> On Fri, Oct 18, 2024 at 06:53:50PM +0800, Yunsheng Lin wrote:
>> diff --git a/Documentation/mm/page_frags.rst b/Documentation/mm/page_frags.rst
>> index 503ca6cdb804..7fd9398aca4e 100644
>> --- a/Documentation/mm/page_frags.rst
>> +++ b/Documentation/mm/page_frags.rst
>> @@ -1,3 +1,5 @@
>> +.. SPDX-License-Identifier: GPL-2.0
>> +
>> ==============
>> Page fragments
>> ==============
>> @@ -40,4 +42,176 @@ page via a single call. The advantage to doing this is that it allows for
>> cleaning up the multiple references that were added to a page in order to
>> avoid calling get_page per allocation.
>>
>> -Alexander Duyck, Nov 29, 2016.
>> +
>> +Architecture overview
>> +=====================
>> +
>> +.. code-block:: none
>> +
>> + +----------------------+
>> + | page_frag API caller |
>> + +----------------------+
>> + |
>> + |
>> + v
>> + +------------------------------------------------------------------+
>> + | request page fragment |
>> + +------------------------------------------------------------------+
>> + | | |
>> + | | |
>> + | Cache not enough |
>> + | | |
>> + | +-----------------+ |
>> + | | reuse old cache |--Usable-->|
>> + | +-----------------+ |
>> + | | |
>> + | Not usable |
>> + | | |
>> + | v |
>> + Cache empty +-----------------+ |
>> + | | drain old cache | |
>> + | +-----------------+ |
>> + | | |
>> + v_________________________________v |
>> + | |
>> + | |
>> + _________________v_______________ |
>> + | | Cache is enough
>> + | | |
>> + PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE | |
>> + | | |
>> + | PAGE_SIZE >= PAGE_FRAG_CACHE_MAX_SIZE |
>> + v | |
>> + +----------------------------------+ | |
>> + | refill cache with order > 0 page | | |
>> + +----------------------------------+ | |
>> + | | | |
>> + | | | |
>> + | Refill failed | |
>> + | | | |
>> + | v v |
>> + | +------------------------------------+ |
>> + | | refill cache with order 0 page | |
>> + | +----------------------------------=-+ |
>> + | | |
>> + Refill succeed | |
>> + | Refill succeed |
>> + | | |
>> + v v v
>> + +------------------------------------------------------------------+
>> + | allocate fragment from cache |
>> + +------------------------------------------------------------------+
>> +
>> +API interface
>> +=============
>> +As the design and implementation of page_frag API implies, the allocation side
>> +does not allow concurrent calling. Instead it is assumed that the caller must
>> +ensure there is not concurrent alloc calling to the same page_frag_cache
>> +instance by using its own lock or rely on some lockless guarantee like NAPI
>> +softirq.
>> +
>> +Depending on different aligning requirement, the page_frag API caller may call
>> +page_frag_*_align*() to ensure the returned virtual address or offset of the
>> +page is aligned according to the 'align/alignment' parameter. Note the size of
>> +the allocated fragment is not aligned, the caller needs to provide an aligned
>> +fragsz if there is an alignment requirement for the size of the fragment.
>> +
>> +Depending on different use cases, callers expecting to deal with va, page or
>> +both va and page for them may call page_frag_alloc, page_frag_refill, or
>> +page_frag_alloc_refill API accordingly.
>> +
>> +There is also a use case that needs minimum memory in order for forward progress,
>> +but more performant if more memory is available. Using page_frag_*_prepare() and
>> +page_frag_commit*() related API, the caller requests the minimum memory it needs
>> +and the prepare API will return the maximum size of the fragment returned. The
>> +caller needs to either call the commit API to report how much memory it actually
>> +uses, or not do so if deciding to not use any memory.
>> +
>> +.. kernel-doc:: include/linux/page_frag_cache.h
>> + :identifiers: page_frag_cache_init page_frag_cache_is_pfmemalloc
>> + __page_frag_alloc_align page_frag_alloc_align page_frag_alloc
>> + __page_frag_refill_align page_frag_refill_align
>> + page_frag_refill __page_frag_refill_prepare_align
>> + page_frag_refill_prepare_align page_frag_refill_prepare
>> + __page_frag_alloc_refill_prepare_align
>> + page_frag_alloc_refill_prepare_align
>> + page_frag_alloc_refill_prepare page_frag_alloc_refill_probe
>> + page_frag_refill_probe page_frag_commit
>> + page_frag_commit_noref page_frag_alloc_abort
>> +
>> +.. kernel-doc:: mm/page_frag_cache.c
>> + :identifiers: page_frag_cache_drain page_frag_free
>> + __page_frag_alloc_refill_probe_align
>> +
>> +Coding examples
>> +===============
>> +
>> +Initialization and draining API
>> +-------------------------------
>> +
>> +.. code-block:: c
>> +
>> + page_frag_cache_init(nc);
>> + ...
>> + page_frag_cache_drain(nc);
>> +
>> +
>> +Allocation & freeing API
>> +------------------------
>> +
>> +.. code-block:: c
>> +
>> + void *va;
>> +
>> + va = page_frag_alloc_align(nc, size, gfp, align);
>> + if (!va)
>> + goto do_error;
>> +
>> + err = do_something(va, size);
>> + if (err) {
>> + page_frag_abort(nc, size);
>> + goto do_error;
>> + }
>> +
>> + ...
>> +
>> + page_frag_free(va);
>> +
>> +
>> +Preparation & committing API
>> +----------------------------
>> +
>> +.. code-block:: c
>> +
>> + struct page_frag page_frag, *pfrag;
>> + bool merge = true;
>> + void *va;
>> +
>> + pfrag = &page_frag;
>> + va = page_frag_alloc_refill_prepare(nc, 32U, pfrag, GFP_KERNEL);
>> + if (!va)
>> + goto wait_for_space;
>> +
>> + copy = min_t(unsigned int, copy, pfrag->size);
>> + if (!skb_can_coalesce(skb, i, pfrag->page, pfrag->offset)) {
>> + if (i >= max_skb_frags)
>> + goto new_segment;
>> +
>> + merge = false;
>> + }
>> +
>> + copy = mem_schedule(copy);
>> + if (!copy)
>> + goto wait_for_space;
>> +
>> + err = copy_from_iter_full_nocache(va, copy, iter);
>> + if (err)
>> + goto do_error;
>> +
>> + if (merge) {
>> + skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
>> + page_frag_commit_noref(nc, pfrag, copy);
>> + } else {
>> + skb_fill_page_desc(skb, i, pfrag->page, pfrag->offset, copy);
>> + page_frag_commit(nc, pfrag, copy);
>> + }
>
> Looks good.
>
>> +/**
>> + * page_frag_cache_is_pfmemalloc() - Check for pfmemalloc.
>> + * @nc: page_frag cache from which to check
>> + *
>> + * Used to check if the current page in page_frag cache is allocated from the
> "Check if ..."
>> + * pfmemalloc reserves. It has the same calling context expectation as the
>> + * allocation API.
>> + *
>> + * Return:
>> + * true if the current page in page_frag cache is allocated from the pfmemalloc
>> + * reserves, otherwise return false.
>> + */
>> <snipped>...
>> +/**
>> + * page_frag_alloc() - Allocate a page fragment.
>> + * @nc: page_frag cache from which to allocate
>> + * @fragsz: the requested fragment size
>> + * @gfp_mask: the allocation gfp to use when cache need to be refilled
>> + *
>> + * Alloc a page fragment from page_frag cache.
> "Allocate a page fragment ..."
>> + *
>> + * Return:
>> + * virtual address of the page fragment, otherwise return NULL.
>> + */
>> static inline void *page_frag_alloc(struct page_frag_cache *nc,
>> <snipped>...
>> +/**
>> + * __page_frag_refill_prepare_align() - Prepare refilling a page_frag with
>> + * aligning requirement.
>> + * @nc: page_frag cache from which to refill
>> + * @fragsz: the requested fragment size
>> + * @pfrag: the page_frag to be refilled.
>> + * @gfp_mask: the allocation gfp to use when cache need to be refilled
>> + * @align_mask: the requested aligning requirement for the fragment
>> + *
>> + * Prepare refill a page_frag from page_frag cache with aligning requirement.
> "Prepare refilling ..."
>> + *
>> + * Return:
>> + * True if prepare refilling succeeds, otherwise return false.
>> + */
>> <snipped>...
>> +/**
>> + * __page_frag_alloc_refill_probe_align() - Probe allocing a fragment and
>> + * refilling a page_frag with aligning requirement.
>> + * @nc: page_frag cache from which to allocate and refill
>> + * @fragsz: the requested fragment size
>> + * @pfrag: the page_frag to be refilled.
>> + * @align_mask: the requested aligning requirement for the fragment.
>> + *
>> + * Probe allocing a fragment and refilling a page_frag from page_frag cache with
> "Probe allocating..."
>> + * aligning requirement.
>> + *
>> + * Return:
>> + * virtual address of the page fragment, otherwise return NULL.
>> + */
>
> Thanks.
>
next prev parent reply other threads:[~2024-10-21 9:32 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20241018105351.1960345-1-linyunsheng@huawei.com>
2024-10-18 10:53 ` [PATCH net-next v22 01/14] mm: page_frag: add a test module " Yunsheng Lin
2024-10-18 10:53 ` [PATCH net-next v22 02/14] mm: move the page fragment allocator from page_alloc into its own file Yunsheng Lin
2024-10-18 10:53 ` [PATCH net-next v22 03/14] mm: page_frag: use initial zero offset for page_frag_alloc_align() Yunsheng Lin
2024-10-18 10:53 ` [PATCH net-next v22 04/14] mm: page_frag: avoid caller accessing 'page_frag_cache' directly Yunsheng Lin
2024-10-18 10:53 ` [PATCH net-next v22 06/14] mm: page_frag: reuse existing space for 'size' and 'pfmemalloc' Yunsheng Lin
2024-10-18 16:43 ` Alexander Duyck
2024-10-18 10:53 ` [PATCH net-next v22 07/14] mm: page_frag: some minor refactoring before adding new API Yunsheng Lin
2024-10-18 17:26 ` Alexander Duyck
2024-10-19 8:29 ` Yunsheng Lin
2024-10-20 15:45 ` Alexander Duyck
2024-10-21 9:34 ` Yunsheng Lin
2024-10-18 10:53 ` [PATCH net-next v22 08/14] mm: page_frag: use __alloc_pages() to replace alloc_pages_node() Yunsheng Lin
2024-10-18 10:53 ` [PATCH net-next v22 10/14] mm: page_frag: introduce prepare/probe/commit API Yunsheng Lin
2024-10-18 18:03 ` Alexander Duyck
2024-10-19 8:33 ` Yunsheng Lin
2024-10-20 16:04 ` Alexander Duyck
2024-10-21 9:36 ` Yunsheng Lin
2024-10-18 10:53 ` [PATCH net-next v22 11/14] mm: page_frag: add testing for the newly added prepare API Yunsheng Lin
2024-10-18 10:53 ` [PATCH net-next v22 13/14] mm: page_frag: update documentation for page_frag Yunsheng Lin
2024-10-20 10:02 ` Bagas Sanjaya
2024-10-21 9:32 ` Yunsheng Lin [this message]
[not found] ` <CAKgT0Uft5Ga0ub_Fj6nonV6E0hRYcej8x_axmGBBX_Nm_wZ_8w@mail.gmail.com>
[not found] ` <02d4971c-a906-44e8-b694-bd54a89cf671@gmail.com>
2024-10-24 9:05 ` [PATCH net-next v22 00/14] Replace page_frag with page_frag_cache for sk_page_frag() Paolo Abeni
2024-10-24 11:39 ` Yunsheng Lin
2024-10-27 3:42 ` Yunsheng Lin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6f9840b3-66c4-485e-b6bb-baeaa641e720@huawei.com \
--to=linyunsheng@huawei.com \
--cc=akpm@linux-foundation.org \
--cc=alexander.duyck@gmail.com \
--cc=bagasdotme@gmail.com \
--cc=corbet@lwn.net \
--cc=davem@davemloft.net \
--cc=kuba@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox