Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1408192 > unrolled thread

[PATCH v1] [mm] Set page->slab_cache for every page allocated for a kmem_cache.

Started byAlexander Potapenko <glider@google.com>
First post2016-05-27 19:20 +0200
Last post2016-05-27 19:50 +0200
Articles 3 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH v1] [mm] Set page->slab_cache for every page allocated for a kmem_cache. Alexander Potapenko <glider@google.com> - 2016-05-27 19:20 +0200
    Re: [PATCH v1] [mm] Set page->slab_cache for every page allocated  for a kmem_cache. Christoph Lameter <cl@linux.com> - 2016-05-27 19:40 +0200
      Re: [PATCH v1] [mm] Set page->slab_cache for every page allocated for  a kmem_cache. Alexander Potapenko <glider@google.com> - 2016-05-27 19:50 +0200

#1408192 — [PATCH v1] [mm] Set page->slab_cache for every page allocated for a kmem_cache.

FromAlexander Potapenko <glider@google.com>
Date2016-05-27 19:20 +0200
Subject[PATCH v1] [mm] Set page->slab_cache for every page allocated for a kmem_cache.
Message-ID<rDtAt-6gF-7@gated-at.bofh.it>
It's reasonable to rely on the fact that for every page allocated for a
kmem_cache the |slab_cache| field points to that cache. Without that it's
hard to figure out which cache does an allocated object belong to.

Fixes: 55834c59098d0c5a97b0f324 ("mm: kasan: initial memory quarantine
implementation")
Signed-off-by: Alexander Potapenko <glider@google.com>
---
 mm/slab.c | 7 ++++++-
 mm/slub.c | 8 +++++---
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/mm/slab.c b/mm/slab.c
index cc8bbc1..ac6c251 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -2703,8 +2703,13 @@ static void slab_put_obj(struct kmem_cache *cachep,
 static void slab_map_pages(struct kmem_cache *cache, struct page *page,
 			   void *freelist)
 {
-	page->slab_cache = cache;
+	int i, nr_pages;
+	char *start = page_address(page);
+
 	page->freelist = freelist;
+	nr_pages = (1 << cache->gfporder);
+	for (i = 0; i < nr_pages; i++)
+		virt_to_page(start + PAGE_SIZE * i)->slab_cache = cache;
 }
 
 /*
diff --git a/mm/slub.c b/mm/slub.c
index 825ff45..fc75ddb 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -1411,7 +1411,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 	struct kmem_cache_order_objects oo = s->oo;
 	gfp_t alloc_gfp;
 	void *start, *p;
-	int idx, order;
+	int idx, order, i, pages;
 
 	flags &= gfp_allowed_mask;
 
@@ -1442,9 +1442,9 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 		stat(s, ORDER_FALLBACK);
 	}
 
+	pages = 1 << oo_order(oo);
 	if (kmemcheck_enabled &&
 	    !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
-		int pages = 1 << oo_order(oo);
 
 		kmemcheck_alloc_shadow(page, oo_order(oo), alloc_gfp, node);
 
@@ -1461,13 +1461,15 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 	page->objects = oo_objects(oo);
 
 	order = compound_order(page);
-	page->slab_cache = s;
 	__SetPageSlab(page);
 	if (page_is_pfmemalloc(page))
 		SetPageSlabPfmemalloc(page);
 
 	start = page_address(page);
 
+	for (i = 0; i < pages; i++)
+		virt_to_page(start + PAGE_SIZE * i)->slab_cache = s;
+
 	if (unlikely(s->flags & SLAB_POISON))
 		memset(start, POISON_INUSE, PAGE_SIZE << order);
 
-- 
2.8.0.rc3.226.g39d4020

[toc] | [next] | [standalone]


#1408213 — Re: [PATCH v1] [mm] Set page->slab_cache for every page allocated for a kmem_cache.

FromChristoph Lameter <cl@linux.com>
Date2016-05-27 19:40 +0200
SubjectRe: [PATCH v1] [mm] Set page->slab_cache for every page allocated for a kmem_cache.
Message-ID<rDtTQ-6nl-21@gated-at.bofh.it>
In reply to#1408192
On Fri, 27 May 2016, Alexander Potapenko wrote:

> It's reasonable to rely on the fact that for every page allocated for a
> kmem_cache the |slab_cache| field points to that cache. Without that it's
> hard to figure out which cache does an allocated object belong to.

The flags are set only in the head page of a coumpound page which is used
by SLAB. No need to do this. This would just mean unnecessarily dirtying
struct page cachelines on allocation.

[toc] | [prev] | [next] | [standalone]


#1408217 — Re: [PATCH v1] [mm] Set page->slab_cache for every page allocated for a kmem_cache.

FromAlexander Potapenko <glider@google.com>
Date2016-05-27 19:50 +0200
SubjectRe: [PATCH v1] [mm] Set page->slab_cache for every page allocated for a kmem_cache.
Message-ID<rDu3v-6qK-1@gated-at.bofh.it>
In reply to#1408213
On Fri, May 27, 2016 at 7:30 PM, Christoph Lameter <cl@linux.com> wrote:
> On Fri, 27 May 2016, Alexander Potapenko wrote:
>
>> It's reasonable to rely on the fact that for every page allocated for a
>> kmem_cache the |slab_cache| field points to that cache. Without that it's
>> hard to figure out which cache does an allocated object belong to.
>
> The flags are set only in the head page of a coumpound page which is used
> by SLAB. No need to do this. This would just mean unnecessarily dirtying
> struct page cachelines on allocation.
>

Got it, thank you.
Looks like I just need to make sure my code uses
virt_to_head_page()->page_slab to get the cache for an object.

-- 
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web