linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: "Liam R. Howlett" <Liam.Howlett@oracle.com>,
	SeongJae Park <sj@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"Matthew Wilcox (Oracle)" <willy@infradead.org>,
	David Hildenbrand <david@redhat.com>,
	Brendan Higgins <brendanhiggins@google.com>,
	David Gow <davidgow@google.com>,
	damon@lists.linux.dev, linux-mm@kvack.org,
	kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] mm/damon/tests/vaddr-kunit: don't use mas_lock for MM_MT_FLAGS-initialized maple tree
Date: Wed, 4 Sep 2024 12:56:33 -0700	[thread overview]
Message-ID: <274f253a-e44e-431b-9dd3-a499843be96f@roeck-us.net> (raw)
In-Reply-To: <gaouhgel2kmzkm7pofpkjkt5ya3a44rwsal74zsgzh76xzh4qo@ne6sqvzj5qor>

On 9/4/24 12:26, Liam R. Howlett wrote:
> * Guenter Roeck <linux@roeck-us.net> [240904 00:27]:
>> On 9/3/24 20:36, Liam R. Howlett wrote:
>>> * Guenter Roeck <linux@roeck-us.net> [240903 22:38]:
>>>> On 9/3/24 19:31, Liam R. Howlett wrote:
>>>>> * SeongJae Park <sj@kernel.org> [240903 21:18]:
>>>>>> On Tue,  3 Sep 2024 17:58:15 -0700 SeongJae Park <sj@kernel.org> wrote:
>>>>>>
>>>>>>> On Tue, 3 Sep 2024 20:48:53 -0400 "Liam R. Howlett" <Liam.Howlett@oracle.com> wrote:
>>>>>>>
>>>>>>>> * SeongJae Park <sj@kernel.org> [240903 20:45]:
>>>>>>>>> damon_test_three_regions_in_vmas() initializes a maple tree with
>>>>>>>>> MM_MT_FLAGS.  The flags contains MT_FLAGS_LOCK_EXTERN, which means
>>>>>>>>> mt_lock of the maple tree will not be used.  And therefore the maple
>>>>>>>>> tree initialization code skips initialization of the mt_lock.  However,
>>>>>>>>> __link_vmas(), which adds vmas for test to the maple tree, uses the
>>>>>>>>> mt_lock.  In other words, the uninitialized spinlock is used.  The
>>>>>>>>> problem becomes celar when spinlock debugging is turned on, since it
>>>>>>>>> reports spinlock bad magic bug.  Fix the issue by not using the mt_lock
>>>>>>>>> as promised.
>>>>>>>>
>>>>>>>> You can't do this, lockdep will tell you this is wrong.
>>>>>>>
>>>>>>> Hmm, but lockdep was silence on my setup?
>>>>>>>
>>>>>>>> We need a lock and to use the lock for writes.
>>>>>>>
>>>>>>> This code is executed by a single-thread test code.  Do we still need the lock?
>>>>>>>
>>>>>>>>
>>>>>>>> I'd suggest using different flags so the spinlock is used.
>>>>>>>
>>>>>>> The reporter mentioned simply dropping MT_FLAGS_LOCK_EXTERN from the flags
>>>>>>> causes suspicious RCU usage message.  May I ask if you have a suggestion of
>>>>>>> better flags?
>>>>>
>>>>> That would be the lockdep complaining, so that's good.
>>>>>
>>>>>>
>>>>>> I was actually thinking replacing the mt_init_flags() with mt_init(), which
>>>>>> same to mt_init_flags() with zero flag, like below.
>>>>>
>>>>> Yes.  This will use the spinlock which should fix your issue, but it
>>>>> will use a different style of maple tree.
>>>>>
>>>>> Perhaps use MT_FLAGS_ALLOC_RANGE to use the same type of maple tree, if
>>>>> you ever add threading you will want the rcu flag as well
>>>>> (MT_FLAGS_USE_RCU).
>>>>>
>>>>> I would recommend those two and just use the spinlock.
>>>>>
>>>>
>>>> I tried that (MT_FLAGS_ALLOC_RANGE | MT_FLAGS_USE_RCU). it also triggers
>>>> the suspicious RCU usage message.
>>>>
>>>
>>> I am running ./tools/testing/kunit/kunit.py run '*damon*' --arch x86_64 --raw
>>> with:
>>> CONFIG_LOCKDEP=y
>>> CONFIG_DEBUG_SPINLOCK=y
>>>
>>> and I don't have any issue with locking in the existing code.  How do I
>>> recreate this issue?
>>>
>>
>> I tested again, and I still see
>>
>>
>> [    6.233483] ok 4 damon
>> [    6.234190]     KTAP version 1
>> [    6.234263]     # Subtest: damon-operations
>> [    6.234335]     # module: vaddr
>> [    6.234384]     1..6
>> [    6.235726]
>> [    6.235931] =============================
>> [    6.236018] WARNING: suspicious RCU usage
>> [    6.236280] 6.11.0-rc6-00029-gda66250b210f-dirty #1 Tainted: G                 N
>> [    6.236398] -----------------------------
>> [    6.236474] lib/maple_tree.c:832 suspicious rcu_dereference_check() usage!
>> [    6.236579]
>> [    6.236579] other info that might help us debug this:
>> [    6.236579]
>> [    6.236738]
>> [    6.236738] rcu_scheduler_active = 2, debug_locks = 1
>> [    6.237039] no locks held by kunit_try_catch/208.
>> [    6.237166]
>> [    6.237166] stack backtrace:
>> [    6.237385] CPU: 0 UID: 0 PID: 208 Comm: kunit_try_catch Tainted: G                 N 6.11.0-rc6-00029-gda66250b210f-dirty #1
>> [    6.237629] Tainted: [N]=TEST
>> [    6.237714] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
>> [    6.238065] Call Trace:
>> [    6.238233]  <TASK>
>> [    6.238547]  dump_stack_lvl+0x9e/0xe0
>> [    6.239473]  lockdep_rcu_suspicious+0x145/0x1b0
>> [    6.239621]  mas_walk+0x19f/0x1d0
>> [    6.239765]  mas_find+0xb5/0x150
>> [    6.239873]  __damon_va_three_regions+0x7e/0x130
> 
> This function isn't taking the rcu read lock while iterating the tree.
> 
> Try this:
> 
> diff --git a/mm/damon/vaddr.c b/mm/damon/vaddr.c
> index b0e8b361891d..08cfd22b5249 100644
> --- a/mm/damon/vaddr.c
> +++ b/mm/damon/vaddr.c
> @@ -126,6 +126,7 @@ static int __damon_va_three_regions(struct mm_struct *mm,
>           * If this is too slow, it can be optimised to examine the maple
>           * tree gaps.
>           */
> +       rcu_read_lock();
>          for_each_vma(vmi, vma) {
>                  unsigned long gap;
>   
> @@ -146,6 +147,7 @@ static int __damon_va_three_regions(struct mm_struct *mm,
>   next:
>                  prev = vma;
>          }
> +       rcu_read_unlock();
>   
>          if (!sz_range(&second_gap) || !sz_range(&first_gap))
>                  return -EINVAL;
> 


Yes, that fixes the problem for me.

Thanks,
Guenter



  reply	other threads:[~2024-09-04 19:56 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-04  0:45 SeongJae Park
2024-09-04  0:48 ` Liam R. Howlett
2024-09-04  0:58   ` SeongJae Park
2024-09-04  1:18     ` SeongJae Park
2024-09-04  1:54       ` Guenter Roeck
2024-09-04  2:43         ` Liam R. Howlett
2024-09-04 17:36           ` SeongJae Park
2024-09-04  2:31       ` Liam R. Howlett
2024-09-04  2:38         ` Guenter Roeck
2024-09-04  2:46           ` Liam R. Howlett
2024-09-04  3:36           ` Liam R. Howlett
2024-09-04  4:27             ` Guenter Roeck
2024-09-04 19:26               ` Liam R. Howlett
2024-09-04 19:56                 ` Guenter Roeck [this message]
2024-09-05  0:19                   ` SeongJae Park
2024-09-04 16:58             ` Guenter Roeck
2024-09-04  1:28     ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=274f253a-e44e-431b-9dd3-a499843be96f@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=brendanhiggins@google.com \
    --cc=damon@lists.linux.dev \
    --cc=david@redhat.com \
    --cc=davidgow@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=sj@kernel.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox