diff mbox series

[mm,07/22] kasan: simplify kasan_update_kunit_status and call sites

Message ID dac26d811ae31856c3d7666de0b108a3735d962d.1646237226.git.andreyknvl@google.com (mailing list archive)
State New
Headers show
Series kasan: report clean-ups and improvements | expand

Commit Message

andrey.konovalov@linux.dev March 2, 2022, 4:36 p.m. UTC
From: Andrey Konovalov <andreyknvl@google.com>

- Rename kasan_update_kunit_status() to update_kunit_status()
  (the function is static).

- Move the IS_ENABLED(CONFIG_KUNIT) to the function's
  definition instead of duplicating it at call sites.

- Obtain and check current->kunit_test within the function.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 mm/kasan/report.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)

Comments

Alexander Potapenko March 2, 2022, 5:46 p.m. UTC | #1
On Wed, Mar 2, 2022 at 5:37 PM <andrey.konovalov@linux.dev> wrote:

> From: Andrey Konovalov <andreyknvl@google.com>
>
> - Rename kasan_update_kunit_status() to update_kunit_status()
>   (the function is static).
>
> - Move the IS_ENABLED(CONFIG_KUNIT) to the function's
>   definition instead of duplicating it at call sites.
>
> - Obtain and check current->kunit_test within the function.
>
> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
>

Reviewed-by: Alexander Potapenko <glider@google.com>


> ---
>  mm/kasan/report.c | 30 ++++++++++++++----------------
>  1 file changed, 14 insertions(+), 16 deletions(-)
>
> diff --git a/mm/kasan/report.c b/mm/kasan/report.c
> index 2d892ec050be..59db81211b8a 100644
> --- a/mm/kasan/report.c
> +++ b/mm/kasan/report.c
> @@ -357,24 +357,31 @@ static bool report_enabled(void)
>  }
>
>  #if IS_ENABLED(CONFIG_KUNIT)
> -static void kasan_update_kunit_status(struct kunit *cur_test, bool sync)
> +static void update_kunit_status(bool sync)
>  {
> +       struct kunit *test;
>         struct kunit_resource *resource;
>         struct kunit_kasan_status *status;
>
> -       resource = kunit_find_named_resource(cur_test, "kasan_status");
> +       test = current->kunit_test;
> +       if (!test)
> +               return;
>
> +       resource = kunit_find_named_resource(test, "kasan_status");
>         if (!resource) {
> -               kunit_set_failure(cur_test);
> +               kunit_set_failure(test);
>                 return;
>         }
>
>         status = (struct kunit_kasan_status *)resource->data;
>         WRITE_ONCE(status->report_found, true);
>         WRITE_ONCE(status->sync_fault, sync);
> +
>         kunit_put_resource(resource);
>  }
> -#endif /* IS_ENABLED(CONFIG_KUNIT) */
> +#else
> +static void update_kunit_status(bool sync) { }
> +#endif
>
>  void kasan_report_invalid_free(void *object, unsigned long ip)
>  {
> @@ -383,10 +390,7 @@ void kasan_report_invalid_free(void *object, unsigned
> long ip)
>
>         object = kasan_reset_tag(object);
>
> -#if IS_ENABLED(CONFIG_KUNIT)
> -       if (current->kunit_test)
> -               kasan_update_kunit_status(current->kunit_test, true);
> -#endif /* IS_ENABLED(CONFIG_KUNIT) */
> +       update_kunit_status(true);
>
>         start_report(&flags);
>         pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void
> *)ip);
> @@ -402,10 +406,7 @@ void kasan_report_async(void)
>  {
>         unsigned long flags;
>
> -#if IS_ENABLED(CONFIG_KUNIT)
> -       if (current->kunit_test)
> -               kasan_update_kunit_status(current->kunit_test, false);
> -#endif /* IS_ENABLED(CONFIG_KUNIT) */
> +       update_kunit_status(false);
>
>         start_report(&flags);
>         pr_err("BUG: KASAN: invalid-access\n");
> @@ -424,10 +425,7 @@ static void __kasan_report(unsigned long addr, size_t
> size, bool is_write,
>         void *untagged_addr;
>         unsigned long flags;
>
> -#if IS_ENABLED(CONFIG_KUNIT)
> -       if (current->kunit_test)
> -               kasan_update_kunit_status(current->kunit_test, true);
> -#endif /* IS_ENABLED(CONFIG_KUNIT) */
> +       update_kunit_status(true);
>
>         disable_trace_on_warning();
>
> --
> 2.25.1
>
> --
> You received this message because you are subscribed to the Google Groups
> "kasan-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to kasan-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/kasan-dev/dac26d811ae31856c3d7666de0b108a3735d962d.1646237226.git.andreyknvl%40google.com
> .
>
diff mbox series

Patch

diff --git a/mm/kasan/report.c b/mm/kasan/report.c
index 2d892ec050be..59db81211b8a 100644
--- a/mm/kasan/report.c
+++ b/mm/kasan/report.c
@@ -357,24 +357,31 @@  static bool report_enabled(void)
 }
 
 #if IS_ENABLED(CONFIG_KUNIT)
-static void kasan_update_kunit_status(struct kunit *cur_test, bool sync)
+static void update_kunit_status(bool sync)
 {
+	struct kunit *test;
 	struct kunit_resource *resource;
 	struct kunit_kasan_status *status;
 
-	resource = kunit_find_named_resource(cur_test, "kasan_status");
+	test = current->kunit_test;
+	if (!test)
+		return;
 
+	resource = kunit_find_named_resource(test, "kasan_status");
 	if (!resource) {
-		kunit_set_failure(cur_test);
+		kunit_set_failure(test);
 		return;
 	}
 
 	status = (struct kunit_kasan_status *)resource->data;
 	WRITE_ONCE(status->report_found, true);
 	WRITE_ONCE(status->sync_fault, sync);
+
 	kunit_put_resource(resource);
 }
-#endif /* IS_ENABLED(CONFIG_KUNIT) */
+#else
+static void update_kunit_status(bool sync) { }
+#endif
 
 void kasan_report_invalid_free(void *object, unsigned long ip)
 {
@@ -383,10 +390,7 @@  void kasan_report_invalid_free(void *object, unsigned long ip)
 
 	object = kasan_reset_tag(object);
 
-#if IS_ENABLED(CONFIG_KUNIT)
-	if (current->kunit_test)
-		kasan_update_kunit_status(current->kunit_test, true);
-#endif /* IS_ENABLED(CONFIG_KUNIT) */
+	update_kunit_status(true);
 
 	start_report(&flags);
 	pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
@@ -402,10 +406,7 @@  void kasan_report_async(void)
 {
 	unsigned long flags;
 
-#if IS_ENABLED(CONFIG_KUNIT)
-	if (current->kunit_test)
-		kasan_update_kunit_status(current->kunit_test, false);
-#endif /* IS_ENABLED(CONFIG_KUNIT) */
+	update_kunit_status(false);
 
 	start_report(&flags);
 	pr_err("BUG: KASAN: invalid-access\n");
@@ -424,10 +425,7 @@  static void __kasan_report(unsigned long addr, size_t size, bool is_write,
 	void *untagged_addr;
 	unsigned long flags;
 
-#if IS_ENABLED(CONFIG_KUNIT)
-	if (current->kunit_test)
-		kasan_update_kunit_status(current->kunit_test, true);
-#endif /* IS_ENABLED(CONFIG_KUNIT) */
+	update_kunit_status(true);
 
 	disable_trace_on_warning();