Use the newly-added function kunit_get_current_test() instead of
accessing current->kunit_test directly. This function uses a static key
to return more quickly when KUnit is enabled, but no tests are actively
running. There should therefore be a negligible performance impact to
enabling the slub KUnit tests.
Other than the performance improvement, this should be a no-op.
Cc: Oliver Glitta <[email protected]>
Cc: Hyeonggon Yoo <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Andrew Morton <[email protected]>
Signed-off-by: David Gow <[email protected]>
Acked-by: Vlastimil Babka <[email protected]>
---
This is intended as an example use of the new function. Other users
(such as KASAN) will be updated separately, as there would otherwise be
conflicts.
We'll take this whole series via the kselftest/kunit tree.
Changes since v2:
https://lore.kernel.org/all/[email protected]/
- Get rid of a redundant 'likely' (Thanks Vlastimil Babka)
- Use current->kunit_test directly when we already know a test is
running. (Thanks Vlastimil Babka)
- Add Vlastimil's Acked-by.
There was no v1 of this patch. v1 of the series can be found here:
https://lore.kernel.org/linux-kselftest/[email protected]/T/#u
Cheers,
-- David
---
lib/slub_kunit.c | 1 +
mm/slub.c | 3 ++-
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/lib/slub_kunit.c b/lib/slub_kunit.c
index 7a0564d7cb7a..8fd19c8301ad 100644
--- a/lib/slub_kunit.c
+++ b/lib/slub_kunit.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include <kunit/test.h>
+#include <kunit/test-bug.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/module.h>
diff --git a/mm/slub.c b/mm/slub.c
index 157527d7101b..1887996cb703 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -39,6 +39,7 @@
#include <linux/memcontrol.h>
#include <linux/random.h>
#include <kunit/test.h>
+#include <kunit/test-bug.h>
#include <linux/sort.h>
#include <linux/debugfs.h>
@@ -603,7 +604,7 @@ static bool slab_add_kunit_errors(void)
{
struct kunit_resource *resource;
- if (likely(!current->kunit_test))
+ if (!kunit_get_current_test())
return false;
resource = kunit_find_named_resource(current->kunit_test, "slab_errors");
--
2.38.1.584.g0f3c55d4c2-goog
On Sat, Nov 19, 2022 at 04:12:52PM +0800, David Gow wrote:
> Use the newly-added function kunit_get_current_test() instead of
> accessing current->kunit_test directly. This function uses a static key
> to return more quickly when KUnit is enabled, but no tests are actively
> running. There should therefore be a negligible performance impact to
> enabling the slub KUnit tests.
>
> Other than the performance improvement, this should be a no-op.
>
> Cc: Oliver Glitta <[email protected]>
> Cc: Hyeonggon Yoo <[email protected]>
> Cc: Christoph Lameter <[email protected]>
> Cc: Vlastimil Babka <[email protected]>
> Cc: David Rientjes <[email protected]>
> Cc: Andrew Morton <[email protected]>
> Signed-off-by: David Gow <[email protected]>
> Acked-by: Vlastimil Babka <[email protected]>
Acked-by: Hyeonggon Yoo <[email protected]>
with small comment:
> ---
>
> This is intended as an example use of the new function. Other users
> (such as KASAN) will be updated separately, as there would otherwise be
> conflicts.
>
> We'll take this whole series via the kselftest/kunit tree.
>
> Changes since v2:
> https://lore.kernel.org/all/[email protected]/
> - Get rid of a redundant 'likely' (Thanks Vlastimil Babka)
> - Use current->kunit_test directly when we already know a test is
> running. (Thanks Vlastimil Babka)
> - Add Vlastimil's Acked-by.
>
> There was no v1 of this patch. v1 of the series can be found here:
> https://lore.kernel.org/linux-kselftest/[email protected]/T/#u
>
> Cheers,
> -- David
>
> ---
> lib/slub_kunit.c | 1 +
> mm/slub.c | 3 ++-
> 2 files changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/lib/slub_kunit.c b/lib/slub_kunit.c
> index 7a0564d7cb7a..8fd19c8301ad 100644
> --- a/lib/slub_kunit.c
> +++ b/lib/slub_kunit.c
> @@ -1,5 +1,6 @@
> // SPDX-License-Identifier: GPL-2.0
> #include <kunit/test.h>
> +#include <kunit/test-bug.h>
Is this #include needed in slub_kunit.c?
> #include <linux/mm.h>
> #include <linux/slab.h>
> #include <linux/module.h>
> diff --git a/mm/slub.c b/mm/slub.c
> index 157527d7101b..1887996cb703 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -39,6 +39,7 @@
> #include <linux/memcontrol.h>
> #include <linux/random.h>
> #include <kunit/test.h>
> +#include <kunit/test-bug.h>
> #include <linux/sort.h>
>
> #include <linux/debugfs.h>
> @@ -603,7 +604,7 @@ static bool slab_add_kunit_errors(void)
> {
> struct kunit_resource *resource;
>
> - if (likely(!current->kunit_test))
> + if (!kunit_get_current_test())
> return false;
>
> resource = kunit_find_named_resource(current->kunit_test, "slab_errors");
> --
> 2.38.1.584.g0f3c55d4c2-goog
>
--
Thanks,
Hyeonggon
On Thu, Nov 24, 2022 at 6:43 PM Hyeonggon Yoo <[email protected]> wrote:
>
> On Sat, Nov 19, 2022 at 04:12:52PM +0800, David Gow wrote:
> > Use the newly-added function kunit_get_current_test() instead of
> > accessing current->kunit_test directly. This function uses a static key
> > to return more quickly when KUnit is enabled, but no tests are actively
> > running. There should therefore be a negligible performance impact to
> > enabling the slub KUnit tests.
> >
> > Other than the performance improvement, this should be a no-op.
> >
> > Cc: Oliver Glitta <[email protected]>
> > Cc: Hyeonggon Yoo <[email protected]>
> > Cc: Christoph Lameter <[email protected]>
> > Cc: Vlastimil Babka <[email protected]>
> > Cc: David Rientjes <[email protected]>
> > Cc: Andrew Morton <[email protected]>
> > Signed-off-by: David Gow <[email protected]>
> > Acked-by: Vlastimil Babka <[email protected]>
>
> Acked-by: Hyeonggon Yoo <[email protected]>
> with small comment:
>
Thanks very much!
> > ---
> >
> > This is intended as an example use of the new function. Other users
> > (such as KASAN) will be updated separately, as there would otherwise be
> > conflicts.
> >
> > We'll take this whole series via the kselftest/kunit tree.
> >
> > Changes since v2:
> > https://lore.kernel.org/all/[email protected]/
> > - Get rid of a redundant 'likely' (Thanks Vlastimil Babka)
> > - Use current->kunit_test directly when we already know a test is
> > running. (Thanks Vlastimil Babka)
> > - Add Vlastimil's Acked-by.
> >
> > There was no v1 of this patch. v1 of the series can be found here:
> > https://lore.kernel.org/linux-kselftest/[email protected]/T/#u
> >
> > Cheers,
> > -- David
> >
> > ---
> > lib/slub_kunit.c | 1 +
> > mm/slub.c | 3 ++-
> > 2 files changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/lib/slub_kunit.c b/lib/slub_kunit.c
> > index 7a0564d7cb7a..8fd19c8301ad 100644
> > --- a/lib/slub_kunit.c
> > +++ b/lib/slub_kunit.c
> > @@ -1,5 +1,6 @@
> > // SPDX-License-Identifier: GPL-2.0
> > #include <kunit/test.h>
> > +#include <kunit/test-bug.h>
>
> Is this #include needed in slub_kunit.c?
>
Yes: kunit_get_current_test() is defined in test-bug.h.
This header contains functions which are always available, even when
KUnit is not enabled.
(It's name isn't great: we may rename/refactor it down the line...)
Cheers,
-- David