2020-11-10 22:15:06

by Andrey Konovalov

[permalink] [raw]
Subject: [PATCH v9 10/44] kasan: define KASAN_GRANULE_PAGE

Define KASAN_GRANULE_PAGE as (KASAN_GRANULE_SIZE << PAGE_SHIFT), which is
the same as (KASAN_GRANULE_SIZE * PAGE_SIZE), and use it across KASAN code
to simplify it.

Signed-off-by: Andrey Konovalov <[email protected]>
Signed-off-by: Vincenzo Frascino <[email protected]>
Reviewed-by: Marco Elver <[email protected]>
---
Change-Id: I0b627b24187d06c8b9bb2f1d04d94b3d06945e73
---
mm/kasan/init.c | 10 ++++------
mm/kasan/kasan.h | 1 +
mm/kasan/shadow.c | 16 +++++++---------
3 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/mm/kasan/init.c b/mm/kasan/init.c
index 1a71eaa8c5f9..26b2663b3a42 100644
--- a/mm/kasan/init.c
+++ b/mm/kasan/init.c
@@ -441,9 +441,8 @@ void kasan_remove_zero_shadow(void *start, unsigned long size)
addr = (unsigned long)kasan_mem_to_shadow(start);
end = addr + (size >> KASAN_SHADOW_SCALE_SHIFT);

- if (WARN_ON((unsigned long)start %
- (KASAN_GRANULE_SIZE * PAGE_SIZE)) ||
- WARN_ON(size % (KASAN_GRANULE_SIZE * PAGE_SIZE)))
+ if (WARN_ON((unsigned long)start % KASAN_GRANULE_PAGE) ||
+ WARN_ON(size % KASAN_GRANULE_PAGE))
return;

for (; addr < end; addr = next) {
@@ -476,9 +475,8 @@ int kasan_add_zero_shadow(void *start, unsigned long size)
shadow_start = kasan_mem_to_shadow(start);
shadow_end = shadow_start + (size >> KASAN_SHADOW_SCALE_SHIFT);

- if (WARN_ON((unsigned long)start %
- (KASAN_GRANULE_SIZE * PAGE_SIZE)) ||
- WARN_ON(size % (KASAN_GRANULE_SIZE * PAGE_SIZE)))
+ if (WARN_ON((unsigned long)start % KASAN_GRANULE_PAGE) ||
+ WARN_ON(size % KASAN_GRANULE_PAGE))
return -EINVAL;

ret = kasan_populate_early_shadow(shadow_start, shadow_end);
diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
index c31e2c739301..1865bb92d47a 100644
--- a/mm/kasan/kasan.h
+++ b/mm/kasan/kasan.h
@@ -7,6 +7,7 @@

#define KASAN_GRANULE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
#define KASAN_GRANULE_MASK (KASAN_GRANULE_SIZE - 1)
+#define KASAN_GRANULE_PAGE (KASAN_GRANULE_SIZE << PAGE_SHIFT)

#define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */
#define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */
diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c
index ca0cc4c31454..1fadd4930d54 100644
--- a/mm/kasan/shadow.c
+++ b/mm/kasan/shadow.c
@@ -161,7 +161,7 @@ static int __meminit kasan_mem_notifier(struct notifier_block *nb,
shadow_end = shadow_start + shadow_size;

if (WARN_ON(mem_data->nr_pages % KASAN_GRANULE_SIZE) ||
- WARN_ON(start_kaddr % (KASAN_GRANULE_SIZE << PAGE_SHIFT)))
+ WARN_ON(start_kaddr % KASAN_GRANULE_PAGE))
return NOTIFY_BAD;

switch (action) {
@@ -432,22 +432,20 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,
unsigned long region_start, region_end;
unsigned long size;

- region_start = ALIGN(start, PAGE_SIZE * KASAN_GRANULE_SIZE);
- region_end = ALIGN_DOWN(end, PAGE_SIZE * KASAN_GRANULE_SIZE);
+ region_start = ALIGN(start, KASAN_GRANULE_PAGE);
+ region_end = ALIGN_DOWN(end, KASAN_GRANULE_PAGE);

- free_region_start = ALIGN(free_region_start,
- PAGE_SIZE * KASAN_GRANULE_SIZE);
+ free_region_start = ALIGN(free_region_start, KASAN_GRANULE_PAGE);

if (start != region_start &&
free_region_start < region_start)
- region_start -= PAGE_SIZE * KASAN_GRANULE_SIZE;
+ region_start -= KASAN_GRANULE_PAGE;

- free_region_end = ALIGN_DOWN(free_region_end,
- PAGE_SIZE * KASAN_GRANULE_SIZE);
+ free_region_end = ALIGN_DOWN(free_region_end, KASAN_GRANULE_PAGE);

if (end != region_end &&
free_region_end > region_end)
- region_end += PAGE_SIZE * KASAN_GRANULE_SIZE;
+ region_end += KASAN_GRANULE_PAGE;

shadow_start = kasan_mem_to_shadow((void *)region_start);
shadow_end = kasan_mem_to_shadow((void *)region_end);
--
2.29.2.222.g5d2a92d10f8-goog


2020-11-11 14:15:50

by Alexander Potapenko

[permalink] [raw]
Subject: Re: [PATCH v9 10/44] kasan: define KASAN_GRANULE_PAGE

On Tue, Nov 10, 2020 at 11:11 PM Andrey Konovalov <[email protected]> wrote:
>
> Define KASAN_GRANULE_PAGE as (KASAN_GRANULE_SIZE << PAGE_SHIFT), which is
> the same as (KASAN_GRANULE_SIZE * PAGE_SIZE), and use it across KASAN code
> to simplify it.

What's the physical sense behind KASAN_GRANULE_PAGE? Is it something
more than just a product of two constants?
The name suggests it might be something page-sized, but in reality it is not.

>
> Signed-off-by: Andrey Konovalov <[email protected]>
> Signed-off-by: Vincenzo Frascino <[email protected]>
> Reviewed-by: Marco Elver <[email protected]>
> ---
> Change-Id: I0b627b24187d06c8b9bb2f1d04d94b3d06945e73
> ---
> mm/kasan/init.c | 10 ++++------
> mm/kasan/kasan.h | 1 +
> mm/kasan/shadow.c | 16 +++++++---------
> 3 files changed, 12 insertions(+), 15 deletions(-)
>
> diff --git a/mm/kasan/init.c b/mm/kasan/init.c
> index 1a71eaa8c5f9..26b2663b3a42 100644
> --- a/mm/kasan/init.c
> +++ b/mm/kasan/init.c
> @@ -441,9 +441,8 @@ void kasan_remove_zero_shadow(void *start, unsigned long size)
> addr = (unsigned long)kasan_mem_to_shadow(start);
> end = addr + (size >> KASAN_SHADOW_SCALE_SHIFT);
>
> - if (WARN_ON((unsigned long)start %
> - (KASAN_GRANULE_SIZE * PAGE_SIZE)) ||
> - WARN_ON(size % (KASAN_GRANULE_SIZE * PAGE_SIZE)))
> + if (WARN_ON((unsigned long)start % KASAN_GRANULE_PAGE) ||
> + WARN_ON(size % KASAN_GRANULE_PAGE))
> return;
>
> for (; addr < end; addr = next) {
> @@ -476,9 +475,8 @@ int kasan_add_zero_shadow(void *start, unsigned long size)
> shadow_start = kasan_mem_to_shadow(start);
> shadow_end = shadow_start + (size >> KASAN_SHADOW_SCALE_SHIFT);
>
> - if (WARN_ON((unsigned long)start %
> - (KASAN_GRANULE_SIZE * PAGE_SIZE)) ||
> - WARN_ON(size % (KASAN_GRANULE_SIZE * PAGE_SIZE)))
> + if (WARN_ON((unsigned long)start % KASAN_GRANULE_PAGE) ||
> + WARN_ON(size % KASAN_GRANULE_PAGE))
> return -EINVAL;
>
> ret = kasan_populate_early_shadow(shadow_start, shadow_end);
> diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h
> index c31e2c739301..1865bb92d47a 100644
> --- a/mm/kasan/kasan.h
> +++ b/mm/kasan/kasan.h
> @@ -7,6 +7,7 @@
>
> #define KASAN_GRANULE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT)
> #define KASAN_GRANULE_MASK (KASAN_GRANULE_SIZE - 1)
> +#define KASAN_GRANULE_PAGE (KASAN_GRANULE_SIZE << PAGE_SHIFT)
>
> #define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */
> #define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */
> diff --git a/mm/kasan/shadow.c b/mm/kasan/shadow.c
> index ca0cc4c31454..1fadd4930d54 100644
> --- a/mm/kasan/shadow.c
> +++ b/mm/kasan/shadow.c
> @@ -161,7 +161,7 @@ static int __meminit kasan_mem_notifier(struct notifier_block *nb,
> shadow_end = shadow_start + shadow_size;
>
> if (WARN_ON(mem_data->nr_pages % KASAN_GRANULE_SIZE) ||
> - WARN_ON(start_kaddr % (KASAN_GRANULE_SIZE << PAGE_SHIFT)))
> + WARN_ON(start_kaddr % KASAN_GRANULE_PAGE))
> return NOTIFY_BAD;
>
> switch (action) {
> @@ -432,22 +432,20 @@ void kasan_release_vmalloc(unsigned long start, unsigned long end,
> unsigned long region_start, region_end;
> unsigned long size;
>
> - region_start = ALIGN(start, PAGE_SIZE * KASAN_GRANULE_SIZE);
> - region_end = ALIGN_DOWN(end, PAGE_SIZE * KASAN_GRANULE_SIZE);
> + region_start = ALIGN(start, KASAN_GRANULE_PAGE);
> + region_end = ALIGN_DOWN(end, KASAN_GRANULE_PAGE);
>
> - free_region_start = ALIGN(free_region_start,
> - PAGE_SIZE * KASAN_GRANULE_SIZE);
> + free_region_start = ALIGN(free_region_start, KASAN_GRANULE_PAGE);
>
> if (start != region_start &&
> free_region_start < region_start)
> - region_start -= PAGE_SIZE * KASAN_GRANULE_SIZE;
> + region_start -= KASAN_GRANULE_PAGE;
>
> - free_region_end = ALIGN_DOWN(free_region_end,
> - PAGE_SIZE * KASAN_GRANULE_SIZE);
> + free_region_end = ALIGN_DOWN(free_region_end, KASAN_GRANULE_PAGE);
>
> if (end != region_end &&
> free_region_end > region_end)
> - region_end += PAGE_SIZE * KASAN_GRANULE_SIZE;
> + region_end += KASAN_GRANULE_PAGE;
>
> shadow_start = kasan_mem_to_shadow((void *)region_start);
> shadow_end = kasan_mem_to_shadow((void *)region_end);
> --
> 2.29.2.222.g5d2a92d10f8-goog
>


--
Alexander Potapenko
Software Engineer

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

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

2020-11-11 18:51:02

by Andrey Konovalov

[permalink] [raw]
Subject: Re: [PATCH v9 10/44] kasan: define KASAN_GRANULE_PAGE

On Wed, Nov 11, 2020 at 3:13 PM Alexander Potapenko <[email protected]> wrote:
>
> On Tue, Nov 10, 2020 at 11:11 PM Andrey Konovalov <[email protected]> wrote:
> >
> > Define KASAN_GRANULE_PAGE as (KASAN_GRANULE_SIZE << PAGE_SHIFT), which is
> > the same as (KASAN_GRANULE_SIZE * PAGE_SIZE), and use it across KASAN code
> > to simplify it.
>
> What's the physical sense behind KASAN_GRANULE_PAGE? Is it something
> more than just a product of two constants?

No, just a product.

> The name suggests it might be something page-sized, but in reality it is not.

What name would you prefer?

2020-11-11 19:08:15

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH v9 10/44] kasan: define KASAN_GRANULE_PAGE

On Wed, 11 Nov 2020 at 19:48, Andrey Konovalov <[email protected]> wrote:
>
> On Wed, Nov 11, 2020 at 3:13 PM Alexander Potapenko <[email protected]> wrote:
> >
> > On Tue, Nov 10, 2020 at 11:11 PM Andrey Konovalov <[email protected]> wrote:
> > >
> > > Define KASAN_GRANULE_PAGE as (KASAN_GRANULE_SIZE << PAGE_SHIFT), which is
> > > the same as (KASAN_GRANULE_SIZE * PAGE_SIZE), and use it across KASAN code
> > > to simplify it.
> >
> > What's the physical sense behind KASAN_GRANULE_PAGE? Is it something
> > more than just a product of two constants?
>
> No, just a product.
>
> > The name suggests it might be something page-sized, but in reality it is not.
>
> What name would you prefer?

Is it actually KASAN_GRANULES_PER_SHADOW_PAGE ? AFAIK we're trying
to calculate the granules that we can fit into a page of shadow
memory.

2020-11-12 01:42:18

by Andrey Konovalov

[permalink] [raw]
Subject: Re: [PATCH v9 10/44] kasan: define KASAN_GRANULE_PAGE

On Wed, Nov 11, 2020 at 8:05 PM Marco Elver <[email protected]> wrote:
>
> On Wed, 11 Nov 2020 at 19:48, Andrey Konovalov <[email protected]> wrote:
> >
> > On Wed, Nov 11, 2020 at 3:13 PM Alexander Potapenko <[email protected]> wrote:
> > >
> > > On Tue, Nov 10, 2020 at 11:11 PM Andrey Konovalov <[email protected]> wrote:
> > > >
> > > > Define KASAN_GRANULE_PAGE as (KASAN_GRANULE_SIZE << PAGE_SHIFT), which is
> > > > the same as (KASAN_GRANULE_SIZE * PAGE_SIZE), and use it across KASAN code
> > > > to simplify it.
> > >
> > > What's the physical sense behind KASAN_GRANULE_PAGE? Is it something
> > > more than just a product of two constants?
> >
> > No, just a product.
> >
> > > The name suggests it might be something page-sized, but in reality it is not.
> >
> > What name would you prefer?
>
> Is it actually KASAN_GRANULES_PER_SHADOW_PAGE ? AFAIK we're trying
> to calculate the granules that we can fit into a page of shadow
> memory.

Not exactly, it's the amount of memory, not the number of granules.

Will name it KASAN_MEMORY_PER_SHADOW_PAGE in v10.