]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - mm/slab.c
ARM: dts: imx6ul: add support for Ka-Ro electronics TXUL mainboard
[karo-tx-linux.git] / mm / slab.c
index d5dffc806f8214b420a6c8f9acb34a0ef5b6ce35..f652b967e1da2693b8465d14527b58b21ffe571c 100644 (file)
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -168,12 +168,6 @@ typedef unsigned short freelist_idx_t;
 
 #define SLAB_OBJ_MAX_NUM ((1 << sizeof(freelist_idx_t) * BITS_PER_BYTE) - 1)
 
-/*
- * true if a page was allocated from pfmemalloc reserves for network-based
- * swap
- */
-static bool pfmemalloc_active __read_mostly;
-
 /*
  * struct array_cache
  *
@@ -195,10 +189,6 @@ struct array_cache {
                         * Must have this definition in here for the proper
                         * alignment of array_cache. Also simplifies accessing
                         * the entries.
-                        *
-                        * Entries should not be directly dereferenced as
-                        * entries belonging to slabs marked pfmemalloc will
-                        * have the lower bits set SLAB_OBJ_PFMEMALLOC
                         */
 };
 
@@ -207,23 +197,6 @@ struct alien_cache {
        struct array_cache ac;
 };
 
-#define SLAB_OBJ_PFMEMALLOC    1
-static inline bool is_obj_pfmemalloc(void *objp)
-{
-       return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
-}
-
-static inline void set_obj_pfmemalloc(void **objp)
-{
-       *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
-       return;
-}
-
-static inline void clear_obj_pfmemalloc(void **objp)
-{
-       *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
-}
-
 /*
  * Need this for bootstrapping a per node allocator.
  */
@@ -270,9 +243,10 @@ static void kmem_cache_node_init(struct kmem_cache_node *parent)
        MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid);  \
        } while (0)
 
+#define CFLGS_OBJFREELIST_SLAB (0x40000000UL)
 #define CFLGS_OFF_SLAB         (0x80000000UL)
+#define        OBJFREELIST_SLAB(x)     ((x)->flags & CFLGS_OBJFREELIST_SLAB)
 #define        OFF_SLAB(x)     ((x)->flags & CFLGS_OFF_SLAB)
-#define OFF_SLAB_MIN_SIZE (max_t(size_t, PAGE_SIZE >> 5, KMALLOC_MIN_SIZE + 1))
 
 #define BATCHREFILL_LIMIT      16
 /*
@@ -481,7 +455,7 @@ static void cache_estimate(unsigned long gfporder, size_t buffer_size,
         * the slabs are all pages aligned, the objects will be at the
         * correct alignment when allocated.
         */
-       if (flags & CFLGS_OFF_SLAB) {
+       if (flags & (CFLGS_OBJFREELIST_SLAB | CFLGS_OFF_SLAB)) {
                *num = slab_size / buffer_size;
                *left_over = slab_size % buffer_size;
        } else {
@@ -619,120 +593,24 @@ static struct array_cache *alloc_arraycache(int node, int entries,
        return ac;
 }
 
-static inline bool is_slab_pfmemalloc(struct page *page)
-{
-       return PageSlabPfmemalloc(page);
-}
-
-/* Clears pfmemalloc_active if no slabs have pfmalloc set */
-static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
-                                               struct array_cache *ac)
-{
-       struct kmem_cache_node *n = get_node(cachep, numa_mem_id());
-       struct page *page;
-       unsigned long flags;
-
-       if (!pfmemalloc_active)
-               return;
-
-       spin_lock_irqsave(&n->list_lock, flags);
-       list_for_each_entry(page, &n->slabs_full, lru)
-               if (is_slab_pfmemalloc(page))
-                       goto out;
-
-       list_for_each_entry(page, &n->slabs_partial, lru)
-               if (is_slab_pfmemalloc(page))
-                       goto out;
-
-       list_for_each_entry(page, &n->slabs_free, lru)
-               if (is_slab_pfmemalloc(page))
-                       goto out;
-
-       pfmemalloc_active = false;
-out:
-       spin_unlock_irqrestore(&n->list_lock, flags);
-}
-
-static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
-                                               gfp_t flags, bool force_refill)
+static noinline void cache_free_pfmemalloc(struct kmem_cache *cachep,
+                                       void *objp)
 {
-       int i;
-       void *objp = ac->entry[--ac->avail];
-
-       /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
-       if (unlikely(is_obj_pfmemalloc(objp))) {
-               struct kmem_cache_node *n;
-
-               if (gfp_pfmemalloc_allowed(flags)) {
-                       clear_obj_pfmemalloc(&objp);
-                       return objp;
-               }
+       struct page *page = virt_to_head_page(objp);
+       struct kmem_cache_node *n;
+       int page_node;
+       LIST_HEAD(list);
 
-               /* The caller cannot use PFMEMALLOC objects, find another one */
-               for (i = 0; i < ac->avail; i++) {
-                       /* If a !PFMEMALLOC object is found, swap them */
-                       if (!is_obj_pfmemalloc(ac->entry[i])) {
-                               objp = ac->entry[i];
-                               ac->entry[i] = ac->entry[ac->avail];
-                               ac->entry[ac->avail] = objp;
-                               return objp;
-                       }
-               }
+       if (unlikely(PageSlabPfmemalloc(page))) {
+               page_node = page_to_nid(page);
+               n = get_node(cachep, page_node);
 
-               /*
-                * If there are empty slabs on the slabs_free list and we are
-                * being forced to refill the cache, mark this one !pfmemalloc.
-                */
-               n = get_node(cachep, numa_mem_id());
-               if (!list_empty(&n->slabs_free) && force_refill) {
-                       struct page *page = virt_to_head_page(objp);
-                       ClearPageSlabPfmemalloc(page);
-                       clear_obj_pfmemalloc(&objp);
-                       recheck_pfmemalloc_active(cachep, ac);
-                       return objp;
-               }
+               spin_lock(&n->list_lock);
+               free_block(cachep, &objp, 1, page_node, &list);
+               spin_unlock(&n->list_lock);
 
-               /* No !PFMEMALLOC objects available */
-               ac->avail++;
-               objp = NULL;
+               slabs_destroy(cachep, &list);
        }
-
-       return objp;
-}
-
-static inline void *ac_get_obj(struct kmem_cache *cachep,
-                       struct array_cache *ac, gfp_t flags, bool force_refill)
-{
-       void *objp;
-
-       if (unlikely(sk_memalloc_socks()))
-               objp = __ac_get_obj(cachep, ac, flags, force_refill);
-       else
-               objp = ac->entry[--ac->avail];
-
-       return objp;
-}
-
-static noinline void *__ac_put_obj(struct kmem_cache *cachep,
-                       struct array_cache *ac, void *objp)
-{
-       if (unlikely(pfmemalloc_active)) {
-               /* Some pfmemalloc slabs exist, check if this is one */
-               struct page *page = virt_to_head_page(objp);
-               if (PageSlabPfmemalloc(page))
-                       set_obj_pfmemalloc(&objp);
-       }
-
-       return objp;
-}
-
-static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
-                                                               void *objp)
-{
-       if (unlikely(sk_memalloc_socks()))
-               objp = __ac_put_obj(cachep, ac, objp);
-
-       ac->entry[ac->avail++] = objp;
 }
 
 /*
@@ -935,7 +813,7 @@ static int __cache_free_alien(struct kmem_cache *cachep, void *objp,
                        STATS_INC_ACOVERFLOW(cachep);
                        __drain_alien_cache(cachep, ac, page_node, &list);
                }
-               ac_put_obj(cachep, ac, objp);
+               ac->entry[ac->avail++] = objp;
                spin_unlock(&alien->lock);
                slabs_destroy(cachep, &list);
        } else {
@@ -1536,10 +1414,6 @@ static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
                return NULL;
        }
 
-       /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
-       if (page_is_pfmemalloc(page))
-               pfmemalloc_active = true;
-
        nr_pages = (1 << cachep->gfporder);
        if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
                add_zone_page_state(page_zone(page),
@@ -1547,8 +1421,10 @@ static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
        else
                add_zone_page_state(page_zone(page),
                        NR_SLAB_UNRECLAIMABLE, nr_pages);
+
        __SetPageSlab(page);
-       if (page_is_pfmemalloc(page))
+       /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
+       if (sk_memalloc_socks() && page_is_pfmemalloc(page))
                SetPageSlabPfmemalloc(page);
 
        if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
@@ -1568,9 +1444,10 @@ static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
  */
 static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
 {
-       const unsigned long nr_freed = (1 << cachep->gfporder);
+       int order = cachep->gfporder;
+       unsigned long nr_freed = (1 << order);
 
-       kmemcheck_free_shadow(page, cachep->gfporder);
+       kmemcheck_free_shadow(page, order);
 
        if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
                sub_zone_page_state(page_zone(page),
@@ -1587,7 +1464,8 @@ static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
 
        if (current->reclaim_state)
                current->reclaim_state->reclaimed_slab += nr_freed;
-       __free_kmem_pages(page, cachep->gfporder);
+       memcg_uncharge_slab(page, order, cachep);
+       __free_pages(page, order);
 }
 
 static void kmem_rcu_free(struct rcu_head *head)
@@ -1802,6 +1680,12 @@ static void slab_destroy_debugcheck(struct kmem_cache *cachep,
                                                struct page *page)
 {
        int i;
+
+       if (OBJFREELIST_SLAB(cachep) && cachep->flags & SLAB_POISON) {
+               poison_obj(cachep, page->freelist - obj_offset(cachep),
+                       POISON_FREE);
+       }
+
        for (i = 0; i < cachep->num; i++) {
                void *objp = index_to_obj(cachep, page, i);
 
@@ -1879,7 +1763,6 @@ static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list)
 static size_t calculate_slab_order(struct kmem_cache *cachep,
                                size_t size, unsigned long flags)
 {
-       unsigned long offslab_limit;
        size_t left_over = 0;
        int gfporder;
 
@@ -1896,16 +1779,24 @@ static size_t calculate_slab_order(struct kmem_cache *cachep,
                        break;
 
                if (flags & CFLGS_OFF_SLAB) {
+                       struct kmem_cache *freelist_cache;
+                       size_t freelist_size;
+
+                       freelist_size = num * sizeof(freelist_idx_t);
+                       freelist_cache = kmalloc_slab(freelist_size, 0u);
+                       if (!freelist_cache)
+                               continue;
+
                        /*
-                        * Max number of objs-per-slab for caches which
-                        * use off-slab slabs. Needed to avoid a possible
-                        * looping condition in cache_grow().
+                        * Needed to avoid possible looping condition
+                        * in cache_grow()
                         */
-                       offslab_limit = size;
-                       offslab_limit /= sizeof(freelist_idx_t);
+                       if (OFF_SLAB(freelist_cache))
+                               continue;
 
-                       if (num > offslab_limit)
-                               break;
+                       /* check if off slab has enough benefit */
+                       if (freelist_cache->size > cachep->size / 2)
+                               continue;
                }
 
                /* Found something acceptable - save it away */
@@ -2023,6 +1914,79 @@ __kmem_cache_alias(const char *name, size_t size, size_t align,
        return cachep;
 }
 
+static bool set_objfreelist_slab_cache(struct kmem_cache *cachep,
+                       size_t size, unsigned long flags)
+{
+       size_t left;
+
+       cachep->num = 0;
+
+       if (cachep->ctor || flags & SLAB_DESTROY_BY_RCU)
+               return false;
+
+       left = calculate_slab_order(cachep, size,
+                       flags | CFLGS_OBJFREELIST_SLAB);
+       if (!cachep->num)
+               return false;
+
+       if (cachep->num * sizeof(freelist_idx_t) > cachep->object_size)
+               return false;
+
+       cachep->colour = left / cachep->colour_off;
+
+       return true;
+}
+
+static bool set_off_slab_cache(struct kmem_cache *cachep,
+                       size_t size, unsigned long flags)
+{
+       size_t left;
+
+       cachep->num = 0;
+
+       /*
+        * Always use on-slab management when SLAB_NOLEAKTRACE
+        * to avoid recursive calls into kmemleak.
+        */
+       if (flags & SLAB_NOLEAKTRACE)
+               return false;
+
+       /*
+        * Size is large, assume best to place the slab management obj
+        * off-slab (should allow better packing of objs).
+        */
+       left = calculate_slab_order(cachep, size, flags | CFLGS_OFF_SLAB);
+       if (!cachep->num)
+               return false;
+
+       /*
+        * If the slab has been placed off-slab, and we have enough space then
+        * move it on-slab. This is at the expense of any extra colouring.
+        */
+       if (left >= cachep->num * sizeof(freelist_idx_t))
+               return false;
+
+       cachep->colour = left / cachep->colour_off;
+
+       return true;
+}
+
+static bool set_on_slab_cache(struct kmem_cache *cachep,
+                       size_t size, unsigned long flags)
+{
+       size_t left;
+
+       cachep->num = 0;
+
+       left = calculate_slab_order(cachep, size, flags);
+       if (!cachep->num)
+               return false;
+
+       cachep->colour = left / cachep->colour_off;
+
+       return true;
+}
+
 /**
  * __kmem_cache_create - Create a cache.
  * @cachep: cache management descriptor
@@ -2047,7 +2011,6 @@ __kmem_cache_alias(const char *name, size_t size, size_t align,
 int
 __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 {
-       size_t left_over, freelist_size;
        size_t ralign = BYTES_PER_WORD;
        gfp_t gfp;
        int err;
@@ -2098,6 +2061,10 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
         * 4) Store it.
         */
        cachep->align = ralign;
+       cachep->colour_off = cache_line_size();
+       /* Offset must be a multiple of the alignment. */
+       if (cachep->colour_off < cachep->align)
+               cachep->colour_off = cachep->align;
 
        if (slab_is_available())
                gfp = GFP_KERNEL;
@@ -2144,51 +2111,37 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
         * sized slab is initialized in current slab initialization sequence.
         */
        if (debug_pagealloc_enabled() && (flags & SLAB_POISON) &&
-               !slab_early_init && size >= kmalloc_size(INDEX_NODE) &&
-               size >= 256 && cachep->object_size > cache_line_size() &&
-               size < PAGE_SIZE) {
-               cachep->obj_offset += PAGE_SIZE - size;
-               size = PAGE_SIZE;
+               size >= 256 && cachep->object_size > cache_line_size()) {
+               if (size < PAGE_SIZE || size % PAGE_SIZE == 0) {
+                       size_t tmp_size = ALIGN(size, PAGE_SIZE);
+
+                       if (set_off_slab_cache(cachep, tmp_size, flags)) {
+                               flags |= CFLGS_OFF_SLAB;
+                               cachep->obj_offset += tmp_size - size;
+                               size = tmp_size;
+                               goto done;
+                       }
+               }
        }
 #endif
 
-       /*
-        * Determine if the slab management is 'on' or 'off' slab.
-        * (bootstrapping cannot cope with offslab caches so don't do
-        * it too early on. Always use on-slab management when
-        * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
-        */
-       if (size >= OFF_SLAB_MIN_SIZE && !slab_early_init &&
-           !(flags & SLAB_NOLEAKTRACE)) {
-               /*
-                * Size is large, assume best to place the slab management obj
-                * off-slab (should allow better packing of objs).
-                */
-               flags |= CFLGS_OFF_SLAB;
+       if (set_objfreelist_slab_cache(cachep, size, flags)) {
+               flags |= CFLGS_OBJFREELIST_SLAB;
+               goto done;
        }
 
-       left_over = calculate_slab_order(cachep, size, flags);
-
-       if (!cachep->num)
-               return -E2BIG;
+       if (set_off_slab_cache(cachep, size, flags)) {
+               flags |= CFLGS_OFF_SLAB;
+               goto done;
+       }
 
-       freelist_size = cachep->num * sizeof(freelist_idx_t);
+       if (set_on_slab_cache(cachep, size, flags))
+               goto done;
 
-       /*
-        * If the slab has been placed off-slab, and we have enough space then
-        * move it on-slab. This is at the expense of any extra colouring.
-        */
-       if (flags & CFLGS_OFF_SLAB && left_over >= freelist_size) {
-               flags &= ~CFLGS_OFF_SLAB;
-               left_over -= freelist_size;
-       }
+       return -E2BIG;
 
-       cachep->colour_off = cache_line_size();
-       /* Offset must be a multiple of the alignment. */
-       if (cachep->colour_off < cachep->align)
-               cachep->colour_off = cachep->align;
-       cachep->colour = left_over / cachep->colour_off;
-       cachep->freelist_size = freelist_size;
+done:
+       cachep->freelist_size = cachep->num * sizeof(freelist_idx_t);
        cachep->flags = flags;
        cachep->allocflags = __GFP_COMP;
        if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
@@ -2209,15 +2162,8 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
 #endif
 
        if (OFF_SLAB(cachep)) {
-               cachep->freelist_cache = kmalloc_slab(freelist_size, 0u);
-               /*
-                * This is a possibility for one of the kmalloc_{dma,}_caches.
-                * But since we go off slab only for object size greater than
-                * OFF_SLAB_MIN_SIZE, and kmalloc_{dma,}_caches get created
-                * in ascending order,this should not happen at all.
-                * But leave a BUG_ON for some lucky dude.
-                */
-               BUG_ON(ZERO_OR_NULL_PTR(cachep->freelist_cache));
+               cachep->freelist_cache =
+                       kmalloc_slab(cachep->freelist_size, 0u);
        }
 
        err = setup_cpu_cache(cachep, gfp);
@@ -2401,7 +2347,9 @@ static void *alloc_slabmgmt(struct kmem_cache *cachep,
        page->s_mem = addr + colour_off;
        page->active = 0;
 
-       if (OFF_SLAB(cachep)) {
+       if (OBJFREELIST_SLAB(cachep))
+               freelist = NULL;
+       else if (OFF_SLAB(cachep)) {
                /* Slab management obj is off-slab. */
                freelist = kmem_cache_alloc_node(cachep->freelist_cache,
                                              local_flags, nodeid);
@@ -2427,14 +2375,14 @@ static inline void set_free_obj(struct page *page,
        ((freelist_idx_t *)(page->freelist))[idx] = val;
 }
 
-static void cache_init_objs(struct kmem_cache *cachep,
-                           struct page *page)
+static void cache_init_objs_debug(struct kmem_cache *cachep, struct page *page)
 {
+#if DEBUG
        int i;
 
        for (i = 0; i < cachep->num; i++) {
                void *objp = index_to_obj(cachep, page, i);
-#if DEBUG
+
                if (cachep->flags & SLAB_STORE_USER)
                        *dbg_userword(cachep, objp) = NULL;
 
@@ -2463,10 +2411,27 @@ static void cache_init_objs(struct kmem_cache *cachep,
                        poison_obj(cachep, objp, POISON_FREE);
                        slab_kernel_map(cachep, objp, 0, 0);
                }
-#else
-               if (cachep->ctor)
-                       cachep->ctor(objp);
+       }
 #endif
+}
+
+static void cache_init_objs(struct kmem_cache *cachep,
+                           struct page *page)
+{
+       int i;
+
+       cache_init_objs_debug(cachep, page);
+
+       if (OBJFREELIST_SLAB(cachep)) {
+               page->freelist = index_to_obj(cachep, page, cachep->num - 1) +
+                                               obj_offset(cachep);
+       }
+
+       for (i = 0; i < cachep->num; i++) {
+               /* constructor could break poison info */
+               if (DEBUG == 0 && cachep->ctor)
+                       cachep->ctor(index_to_obj(cachep, page, i));
+
                set_free_obj(page, i, i);
        }
 }
@@ -2513,6 +2478,9 @@ static void slab_put_obj(struct kmem_cache *cachep,
        }
 #endif
        page->active--;
+       if (!page->freelist)
+               page->freelist = objp + obj_offset(cachep);
+
        set_free_obj(page, page->active, objnr);
 }
 
@@ -2587,7 +2555,7 @@ static int cache_grow(struct kmem_cache *cachep,
        /* Get slab management. */
        freelist = alloc_slabmgmt(cachep, page, offset,
                        local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
-       if (!freelist)
+       if (OFF_SLAB(cachep) && !freelist)
                goto opps1;
 
        slab_map_pages(cachep, page, freelist);
@@ -2690,7 +2658,85 @@ static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
 #define cache_free_debugcheck(x,objp,z) (objp)
 #endif
 
-static struct page *get_first_slab(struct kmem_cache_node *n)
+static inline void fixup_objfreelist_debug(struct kmem_cache *cachep,
+                                               void **list)
+{
+#if DEBUG
+       void *next = *list;
+       void *objp;
+
+       while (next) {
+               objp = next - obj_offset(cachep);
+               next = *(void **)next;
+               poison_obj(cachep, objp, POISON_FREE);
+       }
+#endif
+}
+
+static inline void fixup_slab_list(struct kmem_cache *cachep,
+                               struct kmem_cache_node *n, struct page *page,
+                               void **list)
+{
+       /* move slabp to correct slabp list: */
+       list_del(&page->lru);
+       if (page->active == cachep->num) {
+               list_add(&page->lru, &n->slabs_full);
+               if (OBJFREELIST_SLAB(cachep)) {
+#if DEBUG
+                       /* Poisoning will be done without holding the lock */
+                       if (cachep->flags & SLAB_POISON) {
+                               void **objp = page->freelist;
+
+                               *objp = *list;
+                               *list = objp;
+                       }
+#endif
+                       page->freelist = NULL;
+               }
+       } else
+               list_add(&page->lru, &n->slabs_partial);
+}
+
+/* Try to find non-pfmemalloc slab if needed */
+static noinline struct page *get_valid_first_slab(struct kmem_cache_node *n,
+                                       struct page *page, bool pfmemalloc)
+{
+       if (!page)
+               return NULL;
+
+       if (pfmemalloc)
+               return page;
+
+       if (!PageSlabPfmemalloc(page))
+               return page;
+
+       /* No need to keep pfmemalloc slab if we have enough free objects */
+       if (n->free_objects > n->free_limit) {
+               ClearPageSlabPfmemalloc(page);
+               return page;
+       }
+
+       /* Move pfmemalloc slab to the end of list to speed up next search */
+       list_del(&page->lru);
+       if (!page->active)
+               list_add_tail(&page->lru, &n->slabs_free);
+       else
+               list_add_tail(&page->lru, &n->slabs_partial);
+
+       list_for_each_entry(page, &n->slabs_partial, lru) {
+               if (!PageSlabPfmemalloc(page))
+                       return page;
+       }
+
+       list_for_each_entry(page, &n->slabs_free, lru) {
+               if (!PageSlabPfmemalloc(page))
+                       return page;
+       }
+
+       return NULL;
+}
+
+static struct page *get_first_slab(struct kmem_cache_node *n, bool pfmemalloc)
 {
        struct page *page;
 
@@ -2702,21 +2748,55 @@ static struct page *get_first_slab(struct kmem_cache_node *n)
                                struct page, lru);
        }
 
+       if (sk_memalloc_socks())
+               return get_valid_first_slab(n, page, pfmemalloc);
+
        return page;
 }
 
-static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
-                                                       bool force_refill)
+static noinline void *cache_alloc_pfmemalloc(struct kmem_cache *cachep,
+                               struct kmem_cache_node *n, gfp_t flags)
+{
+       struct page *page;
+       void *obj;
+       void *list = NULL;
+
+       if (!gfp_pfmemalloc_allowed(flags))
+               return NULL;
+
+       /* Racy check if there is free objects */
+       if (!n->free_objects)
+               return NULL;
+
+       spin_lock(&n->list_lock);
+       page = get_first_slab(n, true);
+       if (!page) {
+               spin_unlock(&n->list_lock);
+               return NULL;
+       }
+
+       obj = slab_get_obj(cachep, page);
+       n->free_objects--;
+
+       fixup_slab_list(cachep, n, page, &list);
+
+       spin_unlock(&n->list_lock);
+       fixup_objfreelist_debug(cachep, &list);
+
+       return obj;
+}
+
+static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
 {
        int batchcount;
        struct kmem_cache_node *n;
        struct array_cache *ac;
        int node;
+       void *list = NULL;
 
        check_irq_off();
        node = numa_mem_id();
-       if (unlikely(force_refill))
-               goto force_grow;
+
 retry:
        ac = cpu_cache_get(cachep);
        batchcount = ac->batchcount;
@@ -2742,7 +2822,7 @@ retry:
        while (batchcount > 0) {
                struct page *page;
                /* Get slab alloc is to come from. */
-               page = get_first_slab(n);
+               page = get_first_slab(n, false);
                if (!page)
                        goto must_grow;
 
@@ -2760,25 +2840,29 @@ retry:
                        STATS_INC_ACTIVE(cachep);
                        STATS_SET_HIGH(cachep);
 
-                       ac_put_obj(cachep, ac, slab_get_obj(cachep, page));
+                       ac->entry[ac->avail++] = slab_get_obj(cachep, page);
                }
 
-               /* move slabp to correct slabp list: */
-               list_del(&page->lru);
-               if (page->active == cachep->num)
-                       list_add(&page->lru, &n->slabs_full);
-               else
-                       list_add(&page->lru, &n->slabs_partial);
+               fixup_slab_list(cachep, n, page, &list);
        }
 
 must_grow:
        n->free_objects -= ac->avail;
 alloc_done:
        spin_unlock(&n->list_lock);
+       fixup_objfreelist_debug(cachep, &list);
 
        if (unlikely(!ac->avail)) {
                int x;
-force_grow:
+
+               /* Check if we can use obj in pfmemalloc slab */
+               if (sk_memalloc_socks()) {
+                       void *obj = cache_alloc_pfmemalloc(cachep, n, flags);
+
+                       if (obj)
+                               return obj;
+               }
+
                x = cache_grow(cachep, gfp_exact_node(flags), node, NULL);
 
                /* cache_grow can reenable interrupts, then ac could change. */
@@ -2786,7 +2870,7 @@ force_grow:
                node = numa_mem_id();
 
                /* no objects in sight? abort */
-               if (!x && (ac->avail == 0 || force_refill))
+               if (!x && ac->avail == 0)
                        return NULL;
 
                if (!ac->avail)         /* objects refilled by interrupt? */
@@ -2794,7 +2878,7 @@ force_grow:
        }
        ac->touched = 1;
 
-       return ac_get_obj(cachep, ac, flags, force_refill);
+       return ac->entry[--ac->avail];
 }
 
 static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
@@ -2852,28 +2936,20 @@ static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
 {
        void *objp;
        struct array_cache *ac;
-       bool force_refill = false;
 
        check_irq_off();
 
        ac = cpu_cache_get(cachep);
        if (likely(ac->avail)) {
                ac->touched = 1;
-               objp = ac_get_obj(cachep, ac, flags, false);
+               objp = ac->entry[--ac->avail];
 
-               /*
-                * Allow for the possibility all avail objects are not allowed
-                * by the current flags
-                */
-               if (objp) {
-                       STATS_INC_ALLOCHIT(cachep);
-                       goto out;
-               }
-               force_refill = true;
+               STATS_INC_ALLOCHIT(cachep);
+               goto out;
        }
 
        STATS_INC_ALLOCMISS(cachep);
-       objp = cache_alloc_refill(cachep, flags, force_refill);
+       objp = cache_alloc_refill(cachep, flags);
        /*
         * the 'ac' may be updated by cache_alloc_refill(),
         * and kmemleak_erase() requires its correct value.
@@ -3011,6 +3087,7 @@ static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
        struct page *page;
        struct kmem_cache_node *n;
        void *obj;
+       void *list = NULL;
        int x;
 
        VM_BUG_ON(nodeid < 0 || nodeid >= MAX_NUMNODES);
@@ -3020,7 +3097,7 @@ static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
 retry:
        check_irq_off();
        spin_lock(&n->list_lock);
-       page = get_first_slab(n);
+       page = get_first_slab(n, false);
        if (!page)
                goto must_grow;
 
@@ -3034,15 +3111,11 @@ retry:
 
        obj = slab_get_obj(cachep, page);
        n->free_objects--;
-       /* move slabp to correct slabp list: */
-       list_del(&page->lru);
 
-       if (page->active == cachep->num)
-               list_add(&page->lru, &n->slabs_full);
-       else
-               list_add(&page->lru, &n->slabs_partial);
+       fixup_slab_list(cachep, n, page, &list);
 
        spin_unlock(&n->list_lock);
+       fixup_objfreelist_debug(cachep, &list);
        goto done;
 
 must_grow:
@@ -3177,7 +3250,6 @@ static void free_block(struct kmem_cache *cachep, void **objpp,
                void *objp;
                struct page *page;
 
-               clear_obj_pfmemalloc(&objpp[i]);
                objp = objpp[i];
 
                page = virt_to_head_page(objp);
@@ -3283,7 +3355,12 @@ static inline void __cache_free(struct kmem_cache *cachep, void *objp,
                cache_flusharray(cachep, ac);
        }
 
-       ac_put_obj(cachep, ac, objp);
+       if (sk_memalloc_socks()) {
+               cache_free_pfmemalloc(cachep, objp);
+               return;
+       }
+
+       ac->entry[ac->avail++] = objp;
 }
 
 /**