2018-09-27 20:28:14

by Alex Xu (Hello71)

[permalink] [raw]
Subject: [PATCH] mm: fix z3fold warnings on CONFIG_SMP=n

Spinlocks are always lockable on UP systems, even if they were just
locked.

Cc: Dan Streetman <[email protected]>
Signed-off-by: Alex Xu (Hello71) <[email protected]>
---
mm/z3fold.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/mm/z3fold.c b/mm/z3fold.c
index 4b366d181..4e6ad2de4 100644
--- a/mm/z3fold.c
+++ b/mm/z3fold.c
@@ -202,6 +202,13 @@ static inline void z3fold_page_lock(struct z3fold_header *zhdr)
spin_lock(&zhdr->page_lock);
}

+static inline void z3fold_page_ensure_locked(struct z3fold_header *zhdr)
+{
+#ifdef CONFIG_SMP
+ WARN_ON(z3fold_page_trylock(zhdr));
+#endif
+}
+
/* Try to lock a z3fold page */
static inline int z3fold_page_trylock(struct z3fold_header *zhdr)
{
@@ -277,7 +284,7 @@ static void release_z3fold_page_locked(struct kref *ref)
{
struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
refcount);
- WARN_ON(z3fold_page_trylock(zhdr));
+ z3fold_page_ensure_locked(zhdr);
__release_z3fold_page(zhdr, true);
}

@@ -289,7 +296,7 @@ static void release_z3fold_page_locked_list(struct kref *ref)
list_del_init(&zhdr->buddy);
spin_unlock(&zhdr->pool->lock);

- WARN_ON(z3fold_page_trylock(zhdr));
+ z3fold_page_ensure_locked(zhdr);
__release_z3fold_page(zhdr, true);
}

@@ -403,7 +410,7 @@ static void do_compact_page(struct z3fold_header *zhdr, bool locked)

page = virt_to_page(zhdr);
if (locked)
- WARN_ON(z3fold_page_trylock(zhdr));
+ z3fold_page_ensure_locked(zhdr);
else
z3fold_page_lock(zhdr);
if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING, &page->private))) {
--
2.19.0



2018-09-27 20:43:14

by Dan Streetman

[permalink] [raw]
Subject: Re: [PATCH] mm: fix z3fold warnings on CONFIG_SMP=n

On Thu, Sep 27, 2018 at 4:27 PM Alex Xu (Hello71) <[email protected]> wrote:
>
> Spinlocks are always lockable on UP systems, even if they were just
> locked.

i think it would be much better to just use either
assert_spin_locked() or just spin_is_locked(), instead of an #ifdef.

>
> Cc: Dan Streetman <[email protected]>
> Signed-off-by: Alex Xu (Hello71) <[email protected]>
> ---
> mm/z3fold.c | 13 ++++++++++---
> 1 file changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/mm/z3fold.c b/mm/z3fold.c
> index 4b366d181..4e6ad2de4 100644
> --- a/mm/z3fold.c
> +++ b/mm/z3fold.c
> @@ -202,6 +202,13 @@ static inline void z3fold_page_lock(struct z3fold_header *zhdr)
> spin_lock(&zhdr->page_lock);
> }
>
> +static inline void z3fold_page_ensure_locked(struct z3fold_header *zhdr)
> +{
> +#ifdef CONFIG_SMP
> + WARN_ON(z3fold_page_trylock(zhdr));
> +#endif
> +}
> +
> /* Try to lock a z3fold page */
> static inline int z3fold_page_trylock(struct z3fold_header *zhdr)
> {
> @@ -277,7 +284,7 @@ static void release_z3fold_page_locked(struct kref *ref)
> {
> struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
> refcount);
> - WARN_ON(z3fold_page_trylock(zhdr));
> + z3fold_page_ensure_locked(zhdr);
> __release_z3fold_page(zhdr, true);
> }
>
> @@ -289,7 +296,7 @@ static void release_z3fold_page_locked_list(struct kref *ref)
> list_del_init(&zhdr->buddy);
> spin_unlock(&zhdr->pool->lock);
>
> - WARN_ON(z3fold_page_trylock(zhdr));
> + z3fold_page_ensure_locked(zhdr);
> __release_z3fold_page(zhdr, true);
> }
>
> @@ -403,7 +410,7 @@ static void do_compact_page(struct z3fold_header *zhdr, bool locked)
>
> page = virt_to_page(zhdr);
> if (locked)
> - WARN_ON(z3fold_page_trylock(zhdr));
> + z3fold_page_ensure_locked(zhdr);
> else
> z3fold_page_lock(zhdr);
> if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING, &page->private))) {
> --
> 2.19.0
>

2018-09-27 21:14:41

by Alex Xu (Hello71)

[permalink] [raw]
Subject: Re: [PATCH] mm: fix z3fold warnings on CONFIG_SMP=n

Quoting Dan Streetman (2018-09-27 20:41:21)
> On Thu, Sep 27, 2018 at 4:27 PM Alex Xu (Hello71) <[email protected]> wrote:
> >
> > Spinlocks are always lockable on UP systems, even if they were just
> > locked.
>
> i think it would be much better to just use either
> assert_spin_locked() or just spin_is_locked(), instead of an #ifdef.
>

I wrote a longer response and then learned about the WARN_ON_SMP macro,
so I'll just use that instead.

Original response below:

I thought about using assert_spin_locked, but I wanted to keep the
existing behavior, and it seems to make sense to try to lock the page if
we forgot to lock it earlier? Maybe not though; I don't understand this
code completely. I did write a version of z3fold_page_ensure_locked with
"if (assert_spin_locked(...))" but not only did that look even worse, it
doesn't even work, because assert_spin_locked is a statement on UP
systems, not an expression. It might be worth adding a
ensure_spin_locked function that does that though...

spin_is_locked currently still always returns 0 "on CONFIG_SMP=n builds
with CONFIG_DEBUG_SPINLOCK=n", so that would just return us to the same
problem of checking CONFIG_SMP.

2018-09-27 21:15:57

by Alex Xu (Hello71)

[permalink] [raw]
Subject: [PATCH v2] mm: fix z3fold warnings on CONFIG_SMP=n

Spinlocks are always lockable on UP systems, even if they were just
locked.

Cc: Dan Streetman <[email protected]>
Signed-off-by: Alex Xu (Hello71) <[email protected]>
---
mm/z3fold.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/z3fold.c b/mm/z3fold.c
index 4b366d181..2e8d268ac 100644
--- a/mm/z3fold.c
+++ b/mm/z3fold.c
@@ -277,7 +277,7 @@ static void release_z3fold_page_locked(struct kref *ref)
{
struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
refcount);
- WARN_ON(z3fold_page_trylock(zhdr));
+ WARN_ON_SMP(z3fold_page_trylock(zhdr));
__release_z3fold_page(zhdr, true);
}

@@ -289,7 +289,7 @@ static void release_z3fold_page_locked_list(struct kref *ref)
list_del_init(&zhdr->buddy);
spin_unlock(&zhdr->pool->lock);

- WARN_ON(z3fold_page_trylock(zhdr));
+ WARN_ON_SMP(z3fold_page_trylock(zhdr));
__release_z3fold_page(zhdr, true);
}

@@ -403,7 +403,7 @@ static void do_compact_page(struct z3fold_header *zhdr, bool locked)

page = virt_to_page(zhdr);
if (locked)
- WARN_ON(z3fold_page_trylock(zhdr));
+ WARN_ON_SMP(z3fold_page_trylock(zhdr));
else
z3fold_page_lock(zhdr);
if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING, &page->private))) {
--
2.19.0


2018-09-28 11:33:07

by Dan Streetman

[permalink] [raw]
Subject: Re: [PATCH v2] mm: fix z3fold warnings on CONFIG_SMP=n

On Thu, Sep 27, 2018 at 5:15 PM Alex Xu (Hello71) <[email protected]> wrote:
>
> Spinlocks are always lockable on UP systems, even if they were just
> locked.
>
> Cc: Dan Streetman <[email protected]>

I cc'ed Vitaly also, as this code is from him, but the change
certainly looks correct to me.

Acked-by: Dan Streetman <[email protected]>

> Signed-off-by: Alex Xu (Hello71) <[email protected]>
> ---
> mm/z3fold.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/mm/z3fold.c b/mm/z3fold.c
> index 4b366d181..2e8d268ac 100644
> --- a/mm/z3fold.c
> +++ b/mm/z3fold.c
> @@ -277,7 +277,7 @@ static void release_z3fold_page_locked(struct kref *ref)
> {
> struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
> refcount);
> - WARN_ON(z3fold_page_trylock(zhdr));
> + WARN_ON_SMP(z3fold_page_trylock(zhdr));
> __release_z3fold_page(zhdr, true);
> }
>
> @@ -289,7 +289,7 @@ static void release_z3fold_page_locked_list(struct kref *ref)
> list_del_init(&zhdr->buddy);
> spin_unlock(&zhdr->pool->lock);
>
> - WARN_ON(z3fold_page_trylock(zhdr));
> + WARN_ON_SMP(z3fold_page_trylock(zhdr));
> __release_z3fold_page(zhdr, true);
> }
>
> @@ -403,7 +403,7 @@ static void do_compact_page(struct z3fold_header *zhdr, bool locked)
>
> page = virt_to_page(zhdr);
> if (locked)
> - WARN_ON(z3fold_page_trylock(zhdr));
> + WARN_ON_SMP(z3fold_page_trylock(zhdr));
> else
> z3fold_page_lock(zhdr);
> if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING, &page->private))) {
> --
> 2.19.0
>