Skip to content

Commit 99253de

Browse files
Alexei Starovoitovtehcaster
authored andcommitted
mm: Allow GFP_ACCOUNT to be used in alloc_pages_nolock().
Change alloc_pages_nolock() to default to __GFP_COMP when allocating pages, since upcoming reentrant alloc_slab_page() needs __GFP_COMP. Also allow __GFP_ACCOUNT flag to be specified, since most of BPF infra needs __GFP_ACCOUNT except BPF streams. Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev> Reviewed-by: Harry Yoo <harry.yoo@oracle.com> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
1 parent 4957089 commit 99253de

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

include/linux/gfp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ static inline struct page *alloc_page_vma_noprof(gfp_t gfp,
354354
}
355355
#define alloc_page_vma(...) alloc_hooks(alloc_page_vma_noprof(__VA_ARGS__))
356356

357-
struct page *alloc_pages_nolock_noprof(int nid, unsigned int order);
357+
struct page *alloc_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order);
358358
#define alloc_pages_nolock(...) alloc_hooks(alloc_pages_nolock_noprof(__VA_ARGS__))
359359

360360
extern unsigned long get_free_pages_noprof(gfp_t gfp_mask, unsigned int order);

kernel/bpf/stream.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static struct bpf_stream_page *bpf_stream_page_replace(void)
8383
struct bpf_stream_page *stream_page, *old_stream_page;
8484
struct page *page;
8585

86-
page = alloc_pages_nolock(NUMA_NO_NODE, 0);
86+
page = alloc_pages_nolock(/* Don't account */ 0, NUMA_NO_NODE, 0);
8787
if (!page)
8888
return NULL;
8989
stream_page = page_address(page);

kernel/bpf/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ static bool can_alloc_pages(void)
581581
static struct page *__bpf_alloc_page(int nid)
582582
{
583583
if (!can_alloc_pages())
584-
return alloc_pages_nolock(nid, 0);
584+
return alloc_pages_nolock(__GFP_ACCOUNT, nid, 0);
585585

586586
return alloc_pages_node(nid,
587587
GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT

mm/page_alloc.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7480,6 +7480,7 @@ static bool __free_unaccepted(struct page *page)
74807480

74817481
/**
74827482
* alloc_pages_nolock - opportunistic reentrant allocation from any context
7483+
* @gfp_flags: GFP flags. Only __GFP_ACCOUNT allowed.
74837484
* @nid: node to allocate from
74847485
* @order: allocation order size
74857486
*
@@ -7493,7 +7494,7 @@ static bool __free_unaccepted(struct page *page)
74937494
* Return: allocated page or NULL on failure. NULL does not mean EBUSY or EAGAIN.
74947495
* It means ENOMEM. There is no reason to call it again and expect !NULL.
74957496
*/
7496-
struct page *alloc_pages_nolock_noprof(int nid, unsigned int order)
7497+
struct page *alloc_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order)
74977498
{
74987499
/*
74997500
* Do not specify __GFP_DIRECT_RECLAIM, since direct claim is not allowed.
@@ -7515,12 +7516,13 @@ struct page *alloc_pages_nolock_noprof(int nid, unsigned int order)
75157516
* specify it here to highlight that alloc_pages_nolock()
75167517
* doesn't want to deplete reserves.
75177518
*/
7518-
gfp_t alloc_gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_NOMEMALLOC
7519-
| __GFP_ACCOUNT;
7519+
gfp_t alloc_gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_NOMEMALLOC | __GFP_COMP
7520+
| gfp_flags;
75207521
unsigned int alloc_flags = ALLOC_TRYLOCK;
75217522
struct alloc_context ac = { };
75227523
struct page *page;
75237524

7525+
VM_WARN_ON_ONCE(gfp_flags & ~__GFP_ACCOUNT);
75247526
/*
75257527
* In PREEMPT_RT spin_trylock() will call raw_spin_lock() which is
75267528
* unsafe in NMI. If spin_trylock() is called from hard IRQ the current
@@ -7558,7 +7560,7 @@ struct page *alloc_pages_nolock_noprof(int nid, unsigned int order)
75587560
if (page)
75597561
set_page_refcounted(page);
75607562

7561-
if (memcg_kmem_online() && page &&
7563+
if (memcg_kmem_online() && page && (gfp_flags & __GFP_ACCOUNT) &&
75627564
unlikely(__memcg_kmem_charge_page(page, alloc_gfp, order) != 0)) {
75637565
free_pages_nolock(page, order);
75647566
page = NULL;

0 commit comments

Comments
 (0)