Skip to content

Commit b84a35b

Browse files
Nick PigginLinus Torvalds
authored andcommitted
[PATCH] mempool: NOMEMALLOC and NORETRY
Mempools have 2 problems. The first is that mempool_alloc can possibly get stuck in __alloc_pages when they should opt to fail, and take an element from their reserved pool. The second is that it will happily eat emergency PF_MEMALLOC reserves instead of going to their reserved pools. Fix the first by passing __GFP_NORETRY in the allocation calls in mempool_alloc. Fix the second by introducing a __GFP_MEMPOOL flag which directs the page allocator not to allocate from the reserve pool. Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 8e30f27 commit b84a35b

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎include/linux/gfp.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ struct vm_area_struct;
3838
#define __GFP_NO_GROW 0x2000u /* Slab internal usage */
3939
#define __GFP_COMP 0x4000u /* Add compound page metadata */
4040
#define __GFP_ZERO 0x8000u /* Return zeroed page on success */
41+
#define __GFP_NOMEMALLOC 0x10000u /* Don't use emergency reserves */
4142

42-
#define __GFP_BITS_SHIFT 16 /* Room for 16 __GFP_FOO bits */
43+
#define __GFP_BITS_SHIFT 20 /* Room for 20 __GFP_FOO bits */
4344
#define __GFP_BITS_MASK ((1 << __GFP_BITS_SHIFT) - 1)
4445

4546
/* if you forget to add the bitmask here kernel will crash, period */
4647
#define GFP_LEVEL_MASK (__GFP_WAIT|__GFP_HIGH|__GFP_IO|__GFP_FS| \
4748
__GFP_COLD|__GFP_NOWARN|__GFP_REPEAT| \
48-
__GFP_NOFAIL|__GFP_NORETRY|__GFP_NO_GROW|__GFP_COMP)
49+
__GFP_NOFAIL|__GFP_NORETRY|__GFP_NO_GROW|__GFP_COMP| \
50+
__GFP_NOMEMALLOC)
4951

5052
#define GFP_ATOMIC (__GFP_HIGH)
5153
#define GFP_NOIO (__GFP_WAIT)

‎mm/mempool.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,16 @@ void * mempool_alloc(mempool_t *pool, unsigned int __nocast gfp_mask)
198198
void *element;
199199
unsigned long flags;
200200
DEFINE_WAIT(wait);
201-
int gfp_nowait = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
201+
int gfp_nowait;
202+
203+
gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */
204+
gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
205+
gfp_mask |= __GFP_NOWARN; /* failures are OK */
206+
gfp_nowait = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
202207

203208
might_sleep_if(gfp_mask & __GFP_WAIT);
204209
repeat_alloc:
205-
element = pool->alloc(gfp_nowait|__GFP_NOWARN, pool->pool_data);
210+
element = pool->alloc(gfp_nowait, pool->pool_data);
206211
if (likely(element != NULL))
207212
return element;
208213

‎mm/page_alloc.c‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -799,14 +799,18 @@ __alloc_pages(unsigned int __nocast gfp_mask, unsigned int order,
799799
}
800800

801801
/* This allocation should allow future memory freeing. */
802-
if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE))) && !in_interrupt()) {
803-
/* go through the zonelist yet again, ignoring mins */
804-
for (i = 0; (z = zones[i]) != NULL; i++) {
805-
if (!cpuset_zone_allowed(z))
806-
continue;
807-
page = buffered_rmqueue(z, order, gfp_mask);
808-
if (page)
809-
goto got_pg;
802+
803+
if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
804+
&& !in_interrupt()) {
805+
if (!(gfp_mask & __GFP_NOMEMALLOC)) {
806+
/* go through the zonelist yet again, ignoring mins */
807+
for (i = 0; (z = zones[i]) != NULL; i++) {
808+
if (!cpuset_zone_allowed(z))
809+
continue;
810+
page = buffered_rmqueue(z, order, gfp_mask);
811+
if (page)
812+
goto got_pg;
813+
}
810814
}
811815
goto nopage;
812816
}

0 commit comments

Comments
 (0)