All of lore.kernel.org
 help / color / mirror / Atom feed
From: Muchun Song <songmuchun@bytedance.com>
To: Miaohe Lin <linmiaohe@huawei.com>
Cc: Matthew Wilcox <willy@infradead.org>,
	akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] filemap: obey mapping->invalidate_lock lock/unlock order
Date: Mon, 20 Jun 2022 17:47:28 +0800	[thread overview]
Message-ID: <YrBCMHLrOwo0XGAr@FVFYT0MHHV2J.usts.net> (raw)
In-Reply-To: <72315fc0-eee9-13c8-2d94-43c8c7045a91@huawei.com>

On Mon, Jun 20, 2022 at 02:35:30PM +0800, Miaohe Lin wrote:
> On 2022/6/20 12:47, Matthew Wilcox wrote:
> > On Mon, Jun 20, 2022 at 09:56:06AM +0800, Miaohe Lin wrote:
> >> On 2022/6/18 18:34, Matthew Wilcox wrote:
> >>> On Sat, Jun 18, 2022 at 04:38:20PM +0800, Miaohe Lin wrote:
> >>>> The invalidate_locks of two mappings should be unlocked in reverse order
> >>>> relative to the locking order in filemap_invalidate_lock_two(). Modifying
> >>>
> >>> Why?  It's perfectly valid to lock(A) lock(B) unlock(A) unlock(B).
> >>> If it weren't we'd have lockdep check it and complain.
> 
> It seems I misunderstand your word. I thought you said it must be at lock(A) lock(B) unlock(A) unlock(B)
> order... Sorry.
> 
> >>
> >> For spin_lock, they are lock(A) lock(B) unlock(B) unlock(A) e.g. in copy_huge_pud,
> > 
> > I think you need to spend some time thinking about the semantics of
> > locks and try to figure out why it would make any difference at all
> > which order locks (of any type) are _unlocked_ in,
> 
> IIUC, the lock orders are important to prevent possible deadlock. But unlock orders should be relaxed
> because they won't result in problem indeed. And what I advocate here is that making it at lock(A) lock(B)
> unlock(B) unlock(A) order should be a better program practice. Or unlock order shouldn't be obligatory
> at practice?
>

lock(A) lock(B) unlock(A) unlock(B) is fine. So it is better not to complicate the code.
 
> Thanks.
> 
> > 
> >> copy_huge_pmd, move_huge_pmd and so on:
> >> 	dst_ptl = pmd_lock(dst_mm, dst_pmd);
> >> 	src_ptl = pmd_lockptr(src_mm, src_pmd);
> >> 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
> >> 	...
> >> 	spin_unlock(src_ptl);
> >> 	spin_unlock(dst_ptl);
> >>
> >> For rw_semaphore, they are also lock(A) lock(B) unlock(B) unlock(A) e.g. in dup_mmap():
> >> 	mmap_write_lock_killable(oldmm)
> >> 	mmap_write_lock_nested(mm, SINGLE_DEPTH_NESTING);
> >> 	...
> >> 	mmap_write_unlock(mm);
> >> 	mmap_write_unlock(oldmm);
> >>
> >> and ntfs_extend_mft():
> >> 	down_write(&ni->file.run_lock);
> >> 	down_write_nested(&sbi->used.bitmap.rw_lock, BITMAP_MUTEX_CLUSTERS);
> >> 	...
> >> 	up_write(&sbi->used.bitmap.rw_lock);
> >> 	up_write(&ni->file.run_lock);
> >>
> >> But I see some lock(A) lock(B) unlock(A) unlock(B) examples in some fs codes. Could you
> >> please tell me the right lock/unlock order? I'm somewhat confused now...
> >>
> >> BTW: If lock(A) lock(B) unlock(A) unlock(B) is requested, filemap_invalidate_lock_two might
> >> still need to be changed to respect that order?
> >>
> >> Thanks!
> >>
> >>>
> >>> .
> >>>
> >>
> > 
> > .
> > 
> 
> 

  reply	other threads:[~2022-06-20  9:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-18  8:38 [PATCH] filemap: obey mapping->invalidate_lock lock/unlock order Miaohe Lin
2022-06-18 10:34 ` Matthew Wilcox
2022-06-20  1:56   ` Miaohe Lin
2022-06-20  4:47     ` Matthew Wilcox
2022-06-20  6:35       ` Miaohe Lin
2022-06-20  9:47         ` Muchun Song [this message]
2022-06-20 12:14           ` Miaohe Lin

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=YrBCMHLrOwo0XGAr@FVFYT0MHHV2J.usts.net \
    --to=songmuchun@bytedance.com \
    --cc=akpm@linux-foundation.org \
    --cc=linmiaohe@huawei.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.