From d2bf02ea754113713da7568e22d4ad080d7f78f0 Mon Sep 17 00:00:00 2001 From: Mohit Mishra Date: Thu, 5 Nov 2026 11:37:13 +0530 Subject: [PATCH] lib: Drop the unused page fragment allocator pagefrag_alloc.h has a use-after-free. pagefrag_free() unmaps a page once its last object is freed, but the allocating CPU still caches that page in page_frag_cur_page[] and keeps carving from it on its next allocation. Nothing in the tree uses the allocator. lib/sdt_alloc.bpf.c and lib/alloc/buddy.bpf.c cover the same need, so remove it instead of fixing it. Link: https://github.com/sched-ext/scx/issues/3510 Signed-off-by: Mohit Mishra --- scheds/include/lib/pagefrag_alloc.h | 61 ----------------------------- 1 file changed, 61 deletions(-) delete mode 100644 scheds/include/lib/pagefrag_alloc.h diff --git a/scheds/include/lib/pagefrag_alloc.h b/scheds/include/lib/pagefrag_alloc.h deleted file mode 100644 index 81f9d2f600..0000000000 --- a/scheds/include/lib/pagefrag_alloc.h +++ /dev/null @@ -1,61 +0,0 @@ -/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ -/* Copyright (c) 2024-2025 Meta Platforms, Inc. and affiliates. */ -#pragma once -#include -#include - -#ifndef __round_mask -#define __round_mask(x, y) ((__typeof__(x))((y)-1)) -#endif -#ifndef round_up -#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) -#endif - -static void __arena * __arena page_frag_cur_page[NR_CPUS]; -static int __arena page_frag_cur_offset[NR_CPUS]; - -/* Simple page_frag allocator */ -static inline void __arena* pagefrag_alloc(unsigned int size) -{ - __u64 __arena *obj_cnt; - __u32 cpu = bpf_get_smp_processor_id(); - void __arena *page = page_frag_cur_page[cpu]; - int __arena *cur_offset = &page_frag_cur_offset[cpu]; - int offset; - - size = round_up(size, 8); - if (size >= PAGE_SIZE - 8) - return NULL; - if (!page) { -refill: - page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0); - if (!page) - return NULL; - cast_kern(page); - page_frag_cur_page[cpu] = page; - *cur_offset = PAGE_SIZE - 8; - obj_cnt = page + PAGE_SIZE - 8; - *obj_cnt = 0; - } else { - cast_kern(page); - obj_cnt = page + PAGE_SIZE - 8; - } - - offset = *cur_offset - size; - if (offset < 0) - goto refill; - - (*obj_cnt)++; - *cur_offset = offset; - return page + offset; -} - -static inline void pagefrag_free(void __arena *addr) -{ - __u64 __arena *obj_cnt; - - addr = (void __arena *)(((long)addr) & ~(PAGE_SIZE - 1)); - obj_cnt = addr + PAGE_SIZE - 8; - if (--(*obj_cnt) == 0) - bpf_arena_free_pages(&arena, addr, 1); -}