From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BAB48C433EF for ; Tue, 16 Nov 2021 14:30:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3FE8661BF5 for ; Tue, 16 Nov 2021 14:30:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3FE8661BF5 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 9570E6B0081; Tue, 16 Nov 2021 09:30:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 904506B0082; Tue, 16 Nov 2021 09:30:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CC176B0083; Tue, 16 Nov 2021 09:30:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0041.hostedemail.com [216.40.44.41]) by kanga.kvack.org (Postfix) with ESMTP id 6CBFB6B0081 for ; Tue, 16 Nov 2021 09:30:15 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 2131B8249980 for ; Tue, 16 Nov 2021 14:30:15 +0000 (UTC) X-FDA: 78815028390.04.AC6C675 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf15.hostedemail.com (Postfix) with ESMTP id 5563BD0004B6 for ; Tue, 16 Nov 2021 14:28:53 +0000 (UTC) Received: by mail-pf1-f177.google.com with SMTP id c4so18360693pfj.2 for ; Tue, 16 Nov 2021 06:29:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=bx+LOZiINaT1HDexzncKyUqsxvfo5/luW+zVzz6pEqU=; b=eEf8LRN9CrsaIGv34A5e3s1l9gevtvlUOtPEKUec3w+q5viXFPJ1FhM95YCrU7r40J BRWTNvnPRukNd4ArUtPaoqcP3nOFvtRuksXptQMxFWLwzeWfrBBUnPaJbYsLO5fcgJJu GiTU+pX71foKxYws906+9TVZtadnQNpcqSJ7VDcYzo8sWIJ9/5Rycwo2K/XJ5Ti8KCZf mVJQJeqFLQptZzav2VNl+OfQIDf0DbtOk0tLF6UYElmR8mFkFBZQ+Q/YatfKFZFr0umd 1DS4DzLYGIof/rReohkZF1Y5lDu+J1hu2DqPfhjuiJ2j7WN6xj4oyOOqoi1jBQaNaTK5 G+0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=bx+LOZiINaT1HDexzncKyUqsxvfo5/luW+zVzz6pEqU=; b=grd+VttjYT4xfCIsFndjDTXzVyjSSkDSSgTTf2EXX1dEA2yMMqbanqE9ai7FvXNF57 fj+hOJwCxHriidPMjPCiPqjY/o08s1TFGtmmB9RGlooEEzzq257mdqpOYCo1RshOL+b+ 6ADrG7J0Vq8Ks/zYcHFB9Tinu+L+9qzTVjoF7pJtLTHPaNvDq3mxwroKRsQdtJzb6H1G J1LQLgGRnLrLzVwbqQndSioyTHEDCyZmkrH1SvQv5hTF7M5vy2P/RMG6AQpNH9VVYpWe 0/p9mnGl75nB0Y3sUitdjEIh/hzmlWHx92d7e9YR5ARnXstsfZfRkuLNrE6AiAjpNNAn n/yA== X-Gm-Message-State: AOAM533WtUFBBvPBvPyKBqk+cJNGX3syEyS2CDYxXIBKs62mJyZ597oh Lu0SEPcpKd8iFaYvfjOQBld81ZWBokLU8K7dphlWdtu9 X-Google-Smtp-Source: ABdhPJztUxqXrd0srTV7/mQeglU5OrvO9gcDct2ABAZuqHPM69LI/C+axg15x2E3+1Q8xW/52v8G4IAr8Iqc5Bv0gJs= X-Received: by 2002:a05:6e02:1525:: with SMTP id i5mr4704714ilu.81.1637071359840; Tue, 16 Nov 2021 06:02:39 -0800 (PST) MIME-Version: 1.0 References: <20211116001628.24216-1-vbabka@suse.cz> <20211116001628.24216-22-vbabka@suse.cz> In-Reply-To: <20211116001628.24216-22-vbabka@suse.cz> From: Andrey Konovalov Date: Tue, 16 Nov 2021 15:02:29 +0100 Message-ID: Subject: Re: [RFC PATCH 21/32] mm: Convert struct page to struct slab in functions used by other subsystems To: Vlastimil Babka Cc: Matthew Wilcox , Linux Memory Management List , Christoph Lameter , David Rientjes , Joonsoo Kim , Pekka Enberg , Julia Lawall , Luis Chamberlain , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Marco Elver , Johannes Weiner , Michal Hocko , Vladimir Davydov , kasan-dev , cgroups@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 5563BD0004B6 X-Stat-Signature: cprgnxwk8oqfmo6bszs7j3a7gwm8npac Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=eEf8LRN9; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com X-HE-Tag: 1637072933-438476 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Nov 16, 2021 at 1:16 AM Vlastimil Babka wrote: > > KASAN, KFENCE and memcg interact with SLAB or SLUB internals through functions > nearest_obj(), obj_to_index() and objs_per_slab() that use struct page as > parameter. This patch converts it to struct slab including all callers, through > a coccinelle semantic patch. > > // Options: --include-headers --no-includes --smpl-spacing include/linux/slab_def.h include/linux/slub_def.h mm/slab.h mm/kasan/*.c mm/kfence/kfence_test.c mm/memcontrol.c mm/slab.c mm/slub.c > // Note: needs coccinelle 1.1.1 to avoid breaking whitespace > > @@ > @@ > > -objs_per_slab_page( > +objs_per_slab( > ... > ) > { ... } > > @@ > @@ > > -objs_per_slab_page( > +objs_per_slab( > ... > ) > > @@ > identifier fn =~ "obj_to_index|objs_per_slab"; > @@ > > fn(..., > - const struct page *page > + const struct slab *slab > ,...) > { > <... > ( > - page_address(page) > + slab_address(slab) > | > - page > + slab > ) > ...> > } > > @@ > identifier fn =~ "nearest_obj"; > @@ > > fn(..., > - struct page *page > + const struct slab *slab > ,...) > { > <... > ( > - page_address(page) > + slab_address(slab) > | > - page > + slab > ) > ...> > } > > @@ > identifier fn =~ "nearest_obj|obj_to_index|objs_per_slab"; > expression E; > @@ > > fn(..., > ( > - slab_page(E) > + E > | > - virt_to_page(E) > + virt_to_slab(E) > | > - virt_to_head_page(E) > + virt_to_slab(E) > | > - page > + page_slab(page) > ) > ,...) > > Signed-off-by: Vlastimil Babka > Cc: Julia Lawall > Cc: Luis Chamberlain > Cc: Andrey Ryabinin > Cc: Alexander Potapenko > Cc: Andrey Konovalov > Cc: Dmitry Vyukov > Cc: Marco Elver > Cc: Johannes Weiner > Cc: Michal Hocko > Cc: Vladimir Davydov > Cc: > Cc: > --- > include/linux/slab_def.h | 16 ++++++++-------- > include/linux/slub_def.h | 18 +++++++++--------- > mm/kasan/common.c | 4 ++-- > mm/kasan/generic.c | 2 +- > mm/kasan/report.c | 2 +- > mm/kasan/report_tags.c | 2 +- > mm/kfence/kfence_test.c | 4 ++-- > mm/memcontrol.c | 4 ++-- > mm/slab.c | 10 +++++----- > mm/slab.h | 4 ++-- > mm/slub.c | 2 +- > 11 files changed, 34 insertions(+), 34 deletions(-) > > diff --git a/include/linux/slab_def.h b/include/linux/slab_def.h > index 3aa5e1e73ab6..e24c9aff6fed 100644 > --- a/include/linux/slab_def.h > +++ b/include/linux/slab_def.h > @@ -87,11 +87,11 @@ struct kmem_cache { > struct kmem_cache_node *node[MAX_NUMNODES]; > }; > > -static inline void *nearest_obj(struct kmem_cache *cache, struct page *page, > +static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *slab, > void *x) > { > - void *object = x - (x - page->s_mem) % cache->size; > - void *last_object = page->s_mem + (cache->num - 1) * cache->size; > + void *object = x - (x - slab->s_mem) % cache->size; > + void *last_object = slab->s_mem + (cache->num - 1) * cache->size; > > if (unlikely(object > last_object)) > return last_object; > @@ -106,16 +106,16 @@ static inline void *nearest_obj(struct kmem_cache *cache, struct page *page, > * reciprocal_divide(offset, cache->reciprocal_buffer_size) > */ > static inline unsigned int obj_to_index(const struct kmem_cache *cache, > - const struct page *page, void *obj) > + const struct slab *slab, void *obj) > { > - u32 offset = (obj - page->s_mem); > + u32 offset = (obj - slab->s_mem); > return reciprocal_divide(offset, cache->reciprocal_buffer_size); > } > > -static inline int objs_per_slab_page(const struct kmem_cache *cache, > - const struct page *page) > +static inline int objs_per_slab(const struct kmem_cache *cache, > + const struct slab *slab) > { > - if (is_kfence_address(page_address(page))) > + if (is_kfence_address(slab_address(slab))) > return 1; > return cache->num; > } > diff --git a/include/linux/slub_def.h b/include/linux/slub_def.h > index 8a9c2876ca89..33c5c0e3bd8d 100644 > --- a/include/linux/slub_def.h > +++ b/include/linux/slub_def.h > @@ -158,11 +158,11 @@ static inline void sysfs_slab_release(struct kmem_cache *s) > > void *fixup_red_left(struct kmem_cache *s, void *p); > > -static inline void *nearest_obj(struct kmem_cache *cache, struct page *page, > +static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *slab, > void *x) { > - void *object = x - (x - page_address(page)) % cache->size; > - void *last_object = page_address(page) + > - (page->objects - 1) * cache->size; > + void *object = x - (x - slab_address(slab)) % cache->size; > + void *last_object = slab_address(slab) + > + (slab->objects - 1) * cache->size; > void *result = (unlikely(object > last_object)) ? last_object : object; > > result = fixup_red_left(cache, result); > @@ -178,16 +178,16 @@ static inline unsigned int __obj_to_index(const struct kmem_cache *cache, > } > > static inline unsigned int obj_to_index(const struct kmem_cache *cache, > - const struct page *page, void *obj) > + const struct slab *slab, void *obj) > { > if (is_kfence_address(obj)) > return 0; > - return __obj_to_index(cache, page_address(page), obj); > + return __obj_to_index(cache, slab_address(slab), obj); > } > > -static inline int objs_per_slab_page(const struct kmem_cache *cache, > - const struct page *page) > +static inline int objs_per_slab(const struct kmem_cache *cache, > + const struct slab *slab) > { > - return page->objects; > + return slab->objects; > } > #endif /* _LINUX_SLUB_DEF_H */ > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index 8428da2aaf17..6a1cd2d38bff 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -298,7 +298,7 @@ static inline u8 assign_tag(struct kmem_cache *cache, > /* For caches that either have a constructor or SLAB_TYPESAFE_BY_RCU: */ > #ifdef CONFIG_SLAB > /* For SLAB assign tags based on the object index in the freelist. */ > - return (u8)obj_to_index(cache, virt_to_head_page(object), (void *)object); > + return (u8)obj_to_index(cache, virt_to_slab(object), (void *)object); > #else > /* > * For SLUB assign a random tag during slab creation, otherwise reuse > @@ -341,7 +341,7 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, > if (is_kfence_address(object)) > return false; > > - if (unlikely(nearest_obj(cache, virt_to_head_page(object), object) != > + if (unlikely(nearest_obj(cache, virt_to_slab(object), object) != > object)) { > kasan_report_invalid_free(tagged_object, ip); > return true; > diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c > index 84a038b07c6f..5d0b79416c4e 100644 > --- a/mm/kasan/generic.c > +++ b/mm/kasan/generic.c > @@ -339,7 +339,7 @@ static void __kasan_record_aux_stack(void *addr, bool can_alloc) > return; > > cache = page->slab_cache; > - object = nearest_obj(cache, page, addr); > + object = nearest_obj(cache, page_slab(page), addr); > alloc_meta = kasan_get_alloc_meta(cache, object); > if (!alloc_meta) > return; > diff --git a/mm/kasan/report.c b/mm/kasan/report.c > index 0bc10f452f7e..e00999dc6499 100644 > --- a/mm/kasan/report.c > +++ b/mm/kasan/report.c > @@ -249,7 +249,7 @@ static void print_address_description(void *addr, u8 tag) > > if (page && PageSlab(page)) { > struct kmem_cache *cache = page->slab_cache; > - void *object = nearest_obj(cache, page, addr); > + void *object = nearest_obj(cache, page_slab(page), addr); The tab before addr should be a space. checkpatch should probably report this. > > describe_object(cache, object, addr, tag); > } > diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c > index 8a319fc16dab..06c21dd77493 100644 > --- a/mm/kasan/report_tags.c > +++ b/mm/kasan/report_tags.c > @@ -23,7 +23,7 @@ const char *kasan_get_bug_type(struct kasan_access_info *info) > page = kasan_addr_to_page(addr); > if (page && PageSlab(page)) { > cache = page->slab_cache; > - object = nearest_obj(cache, page, (void *)addr); > + object = nearest_obj(cache, page_slab(page), (void *)addr); > alloc_meta = kasan_get_alloc_meta(cache, object); > > if (alloc_meta) { > diff --git a/mm/kfence/kfence_test.c b/mm/kfence/kfence_test.c > index 695030c1fff8..f7276711d7b9 100644 > --- a/mm/kfence/kfence_test.c > +++ b/mm/kfence/kfence_test.c > @@ -291,8 +291,8 @@ static void *test_alloc(struct kunit *test, size_t size, gfp_t gfp, enum allocat > * even for KFENCE objects; these are required so that > * memcg accounting works correctly. > */ > - KUNIT_EXPECT_EQ(test, obj_to_index(s, page, alloc), 0U); > - KUNIT_EXPECT_EQ(test, objs_per_slab_page(s, page), 1); > + KUNIT_EXPECT_EQ(test, obj_to_index(s, page_slab(page), alloc), 0U); > + KUNIT_EXPECT_EQ(test, objs_per_slab(s, page_slab(page)), 1); > > if (policy == ALLOCATE_ANY) > return alloc; > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 781605e92015..c8b53ec074b4 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -2819,7 +2819,7 @@ static struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *objcg) > int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s, > gfp_t gfp, bool new_page) > { > - unsigned int objects = objs_per_slab_page(s, page); > + unsigned int objects = objs_per_slab(s, page_slab(page)); > unsigned long memcg_data; > void *vec; > > @@ -2881,7 +2881,7 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p) > struct obj_cgroup *objcg; > unsigned int off; > > - off = obj_to_index(page->slab_cache, page, p); > + off = obj_to_index(page->slab_cache, page_slab(page), p); > objcg = page_objcgs(page)[off]; > if (objcg) > return obj_cgroup_memcg(objcg); > diff --git a/mm/slab.c b/mm/slab.c > index 78ef4d94e3de..adf688d2da64 100644 > --- a/mm/slab.c > +++ b/mm/slab.c > @@ -1560,7 +1560,7 @@ static void check_poison_obj(struct kmem_cache *cachep, void *objp) > struct slab *slab = virt_to_slab(objp); > unsigned int objnr; > > - objnr = obj_to_index(cachep, slab_page(slab), objp); > + objnr = obj_to_index(cachep, slab, objp); > if (objnr) { > objp = index_to_obj(cachep, slab, objnr - 1); > realobj = (char *)objp + obj_offset(cachep); > @@ -2530,7 +2530,7 @@ static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slab) > static void slab_put_obj(struct kmem_cache *cachep, > struct slab *slab, void *objp) > { > - unsigned int objnr = obj_to_index(cachep, slab_page(slab), objp); > + unsigned int objnr = obj_to_index(cachep, slab, objp); > #if DEBUG > unsigned int i; > > @@ -2717,7 +2717,7 @@ static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp, > if (cachep->flags & SLAB_STORE_USER) > *dbg_userword(cachep, objp) = (void *)caller; > > - objnr = obj_to_index(cachep, slab_page(slab), objp); > + objnr = obj_to_index(cachep, slab, objp); > > BUG_ON(objnr >= cachep->num); > BUG_ON(objp != index_to_obj(cachep, slab, objnr)); > @@ -3663,7 +3663,7 @@ void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab) > objp = object - obj_offset(cachep); > kpp->kp_data_offset = obj_offset(cachep); > slab = virt_to_slab(objp); > - objnr = obj_to_index(cachep, slab_page(slab), objp); > + objnr = obj_to_index(cachep, slab, objp); > objp = index_to_obj(cachep, slab, objnr); > kpp->kp_objp = objp; > if (DEBUG && cachep->flags & SLAB_STORE_USER) > @@ -4182,7 +4182,7 @@ void __check_heap_object(const void *ptr, unsigned long n, > > /* Find and validate object. */ > cachep = slab->slab_cache; > - objnr = obj_to_index(cachep, slab_page(slab), (void *)ptr); > + objnr = obj_to_index(cachep, slab, (void *)ptr); > BUG_ON(objnr >= cachep->num); > > /* Find offset within object. */ > diff --git a/mm/slab.h b/mm/slab.h > index d6c993894c02..b07e842b5cfc 100644 > --- a/mm/slab.h > +++ b/mm/slab.h > @@ -483,7 +483,7 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s, > continue; > } > > - off = obj_to_index(s, page, p[i]); > + off = obj_to_index(s, page_slab(page), p[i]); > obj_cgroup_get(objcg); > page_objcgs(page)[off] = objcg; > mod_objcg_state(objcg, page_pgdat(page), > @@ -522,7 +522,7 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s_orig, > else > s = s_orig; > > - off = obj_to_index(s, page, p[i]); > + off = obj_to_index(s, page_slab(page), p[i]); > objcg = objcgs[off]; > if (!objcg) > continue; > diff --git a/mm/slub.c b/mm/slub.c > index 7759f3dde64b..981e40a88bab 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -4342,7 +4342,7 @@ void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab) > #else > objp = objp0; > #endif > - objnr = obj_to_index(s, slab_page(slab), objp); > + objnr = obj_to_index(s, slab, objp); > kpp->kp_data_offset = (unsigned long)((char *)objp0 - (char *)objp); > objp = base + s->size * objnr; > kpp->kp_objp = objp; > -- > 2.33.1 >