Re: [PATCH net-next v2 09/15] mm: page_frag: reuse MSB of 'size' field for pfmemalloc

From
Yunsheng Lin <hidden>
Date
2024-04-30 12:06:04
Also in
linux-mm, lkml
Reply to
Alexander Duyck, 2024-04-29
On 2024/4/29 22:49, Alexander Duyck wrote:

...
quoted
quoted
After considering a few different layouts for 'struct page_frag_cache',
it seems the below is more optimized:

struct page_frag_cache {
        /* page address & pfmemalloc & order */
        void *va;
I see. So basically just pack the much smaller bitfields in here.
quoted
#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32)
        u16 pagecnt_bias;
        u16 size;
#else
        u32 pagecnt_bias;
        u32 size;
#endif
}

The lower bits of 'va' is or'ed with the page order & pfmemalloc instead
of offset or pagecnt_bias, so that we don't have to add more checking
for handling the problem of not having enough space for offset or
pagecnt_bias for PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE and 32 bits system.
And page address & pfmemalloc & order is unchanged for the same page
in the same 'page_frag_cache' instance, it makes sense to fit them
together.

Also, it seems it is better to replace 'offset' with 'size', which indicates
the remaining size for the cache in a 'page_frag_cache' instance, and we
might be able to do a single 'size >= fragsz' checking for the case of cache
being enough, which should be the fast path if we ensure size is zoro when
'va' == NULL.
I'm not sure the rename to size is called for as it is going to be
confusing. Maybe something like "remaining"?
Yes, using 'size' for that is a bit confusing.
Beside the above 'remaining', by googling, it seems we may have below
options too:
'residual','unused', 'surplus'
quoted
Something like below:

#define PAGE_FRAG_CACHE_ORDER_MASK      GENMASK(1, 0)
#define PAGE_FRAG_CACHE_PFMEMALLOC_BIT  BIT(2)
The only downside is that it is ossifying things so that we can only
There is 12 bits that is always useful, we can always extend ORDER_MASK
to more bits if lager order number is needed.
ever do order 3 as the maximum cache size. It might be better to do a
full 8 bytes as on x86 it would just mean accessing the low end of a
16b register. Then you can have pfmemalloc at bit 8.
I am not sure I understand the above as it seems we may have below option:
1. Use somthing like the above ORDER_MASK and PFMEMALLOC_BIT.
2. Use bitfield as something like below:

unsigned long va:20;---or 52 for 64bit system
unsigned long pfmemalloc:1
unsigned long order:11;

Or is there a better idea in your mind?
quoted
struct page_frag_cache {
        /* page address & pfmemalloc & order */
        void *va;
When you start combining things like this we normally would convert va
to an unsigned long.
Ack.
quoted
#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) && (BITS_PER_LONG <= 32)
        u16 pagecnt_bias;
        u16 size;
#else
        u32 pagecnt_bias;
        u32 size;
#endif
};


static void *__page_frag_cache_refill(struct page_frag_cache *nc,
                                      unsigned int fragsz, gfp_t gfp_mask,
                                      unsigned int align_mask)
{
        gfp_t gfp = gfp_mask;
        struct page *page;
        void *va;

#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
        /* Ensure free_unref_page() can be used to free the page fragment */
        BUILD_BUG_ON(PAGE_FRAG_CACHE_MAX_ORDER > PAGE_ALLOC_COSTLY_ORDER);

        gfp_mask = (gfp_mask & ~__GFP_DIRECT_RECLAIM) |  __GFP_COMP |
                   __GFP_NOWARN | __GFP_NORETRY | __GFP_NOMEMALLOC;
        page = alloc_pages_node(NUMA_NO_NODE, gfp_mask,
                                PAGE_FRAG_CACHE_MAX_ORDER);
        if (likely(page)) {
                nc->size = PAGE_FRAG_CACHE_MAX_SIZE - fragsz;
I wouldn't pass fragsz here. Ideally we keep this from having to get
pulled directly into the allocator and can instead treat this as a
pristine page. We can do the subtraction further down in the actual
frag alloc call.
Yes for the maintanability point of view.
But for performance point of view, doesn't it make sense to do the
subtraction here, as doing the subtraction in the actual frag alloc
call may involve more load/store operation to do the subtraction?
quoted
                va = page_address(page);
                nc->va = (void *)((unsigned long)va |
                                  PAGE_FRAG_CACHE_MAX_ORDER |
                                  (page_is_pfmemalloc(page) ?
                                   PAGE_FRAG_CACHE_PFMEMALLOC_BIT : 0));
                page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE);
                nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE;
                return va;
        }
#endif
        page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
        if (likely(page)) {
                nc->size = PAGE_SIZE - fragsz;
                va = page_address(page);
                nc->va = (void *)((unsigned long)va |
                                  (page_is_pfmemalloc(page) ?
                                   PAGE_FRAG_CACHE_PFMEMALLOC_BIT : 0));
                page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE);
                nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE;
                return va;
        }

        nc->va = NULL;
        nc->size = 0;
        return NULL;
}

void *__page_frag_alloc_va_align(struct page_frag_cache *nc,
                                 unsigned int fragsz, gfp_t gfp_mask,
                                 unsigned int align_mask)
{
#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
        unsigned long page_order;
#endif
        unsigned long page_size;
        unsigned long size;
        struct page *page;
        void *va;

        size = nc->size & align_mask;
        va = nc->va;
#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
        page_order = (unsigned long)va & PAGE_FRAG_CACHE_ORDER_MASK;
        page_size = PAGE_SIZE << page_order;
#else
        page_size = PAGE_SIZE;
#endif
So I notice you got rid of the loops within the function. One of the
reasons for structuring it the way it was is to enable better code
caching. By unfolding the loops you are increasing the number of
instructions that have to be fetched and processed in order to
allocate the buffers.
I am not sure I understand what does 'the loops' means here, as there
is not 'while' or 'for' here. I suppose you are referring to the 'goto'
here?
Thread (42 messages) 42 messages, 5 authors, last reply 2024-05-06
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help