Skip to content

Commit 0ea7eee

Browse files
borkmanndavem330
authored andcommitted
mm, percpu: add support for __GFP_NOWARN flag
Add an option for pcpu_alloc() to support __GFP_NOWARN flag. Currently, we always throw a warning when size or alignment is unsupported (and also dump stack on failed allocation requests). The warning itself is harmless since we return NULL anyway for any failed request, which callers are required to handle anyway. However, it becomes harmful when panic_on_warn is set. The rationale for the WARN() in pcpu_alloc() is that it can be tracked when larger than supported allocation requests are made such that allocations limits can be tweaked if warranted. This makes sense for in-kernel users, however, there are users of pcpu allocator where allocation size is derived from user space requests, e.g. when creating BPF maps. In these cases, the requests should fail gracefully without throwing a splat. The current work-around was to check allocation size against the upper limit of PCPU_MIN_UNIT_SIZE from call-sites for bailing out prior to a call to pcpu_alloc() in order to avoid throwing the WARN(). This is bad in multiple ways since PCPU_MIN_UNIT_SIZE is an implementation detail, and having the checks on call-sites only complicates the code for no good reason. Thus, lets fix it generically by supporting the __GFP_NOWARN flag that users can then use with calling the __alloc_percpu_gfp() helper instead. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Cc: Tejun Heo <tj@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3fd3b03 commit 0ea7eee

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

‎mm/percpu.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,18 +1329,21 @@ static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
13291329
* @gfp: allocation flags
13301330
*
13311331
* Allocate percpu area of @size bytes aligned at @align. If @gfp doesn't
1332-
* contain %GFP_KERNEL, the allocation is atomic.
1332+
* contain %GFP_KERNEL, the allocation is atomic. If @gfp has __GFP_NOWARN
1333+
* then no warning will be triggered on invalid or failed allocation
1334+
* requests.
13331335
*
13341336
* RETURNS:
13351337
* Percpu pointer to the allocated area on success, NULL on failure.
13361338
*/
13371339
static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
13381340
gfp_t gfp)
13391341
{
1342+
bool is_atomic = (gfp & GFP_KERNEL) != GFP_KERNEL;
1343+
bool do_warn = !(gfp & __GFP_NOWARN);
13401344
static int warn_limit = 10;
13411345
struct pcpu_chunk *chunk;
13421346
const char *err;
1343-
bool is_atomic = (gfp & GFP_KERNEL) != GFP_KERNEL;
13441347
int slot, off, cpu, ret;
13451348
unsigned long flags;
13461349
void __percpu *ptr;
@@ -1361,7 +1364,7 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
13611364

13621365
if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE ||
13631366
!is_power_of_2(align))) {
1364-
WARN(true, "illegal size (%zu) or align (%zu) for percpu allocation\n",
1367+
WARN(do_warn, "illegal size (%zu) or align (%zu) for percpu allocation\n",
13651368
size, align);
13661369
return NULL;
13671370
}
@@ -1482,7 +1485,7 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
14821485
fail:
14831486
trace_percpu_alloc_percpu_fail(reserved, is_atomic, size, align);
14841487

1485-
if (!is_atomic && warn_limit) {
1488+
if (!is_atomic && do_warn && warn_limit) {
14861489
pr_warn("allocation failed, size=%zu align=%zu atomic=%d, %s\n",
14871490
size, align, is_atomic, err);
14881491
dump_stack();
@@ -1507,7 +1510,9 @@ static void __percpu *pcpu_alloc(size_t size, size_t align, bool reserved,
15071510
*
15081511
* Allocate zero-filled percpu area of @size bytes aligned at @align. If
15091512
* @gfp doesn't contain %GFP_KERNEL, the allocation doesn't block and can
1510-
* be called from any context but is a lot more likely to fail.
1513+
* be called from any context but is a lot more likely to fail. If @gfp
1514+
* has __GFP_NOWARN then no warning will be triggered on invalid or failed
1515+
* allocation requests.
15111516
*
15121517
* RETURNS:
15131518
* Percpu pointer to the allocated area on success, NULL on failure.

0 commit comments

Comments
 (0)