Documentation: update path-lookup.md for parallel lookups
Subsystems (2)
PATCH 1/1 Documentation: update path-lookup.md for parallel lookups
Since this document was written, i_mutex has been replace with i_rwsem, and shared locks are utilized to allow lookups in the one directory to happen in parallel.
So replace i_mutex with i_rwsem, and explain how this is used for parallel lookups.
---
Just FYI
$ git grep -w i_mutex | wc -l 262
it was 276 before this patch... so a small improvement.
NeilBrown
Documentation/filesystems/path-lookup.md | 85 ++++++++++++++++++------ 1 file changed, 66 insertions(+), 19 deletions(-)
Documentation/filesystems/path-lookup.md +66 -19
@@ -12,6 +12,10 @@ This write-up is based on three articles published at lwn.net: - <https://lwn.net/Articles/650786/> A walk among the symlinks Written by Neil Brown with help from Al Viro and Jon Corbet. +It has subsequently been updated to reflect changes in the kernel +including: + +- per-directory parallel name lookup. Introduction ------------
@@ -231,37 +235,80 @@ renamed. If `d_lookup` finds that a rename happened while it unsuccessfully scanned a chain in the hash table, it simply tries again. -### inode->i_mutex ### +### inode->i_rwsem ### -`i_mutex` is a mutex that serializes all changes to a particular +`i_rwsem` is a read/write semaphore that serializes all changes to a particular directory. This ensures that, for example, an `unlink()` and a `rename()` cannot both happen at the same time. It also keeps the directory stable while the filesystem is asked to look up a name that is not -currently in the dcache. +currently in the dcache or, optionally, when the list of entries in a +directory is being retrieved with `readdir()`. -This has a complementary role to that of `d_lock`: `i_mutex` on a +This has a complementary role to that of `d_lock`: `i_rwsem` on a directory protects all of the names in that directory, while `d_lock` on a name protects just one name in a directory. Most changes to the -dcache hold `i_mutex` on the relevant directory inode and briefly take +dcache hold `i_rwsem` on the relevant directory inode and briefly take `d_lock` on one or more the dentries while the change happens. One exception is when idle dentries are removed from the dcache due to -memory pressure. This uses `d_lock`, but `i_mutex` plays no role. +memory pressure. This uses `d_lock`, but `i_rwsem` plays no role. -The mutex affects pathname lookup in two distinct ways. Firstly it -serializes lookup of a name in a directory. `walk_component()` uses +The semaphore affects pathname lookup in two distinct ways. Firstly it +prevents changes during lookup of a name in a directory. `walk_component()` uses `lookup_fast()` first which, in turn, checks to see if the name is in the cache, using only `d_lock` locking. If the name isn't found, then `walk_component()` -falls back to `lookup_slow()` which takes `i_mutex`, checks again that +falls back to `lookup_slow()` which takes a shared lock on `i_rwsem`, checks again that the name isn't in the cache, and then calls in to the filesystem to get a definitive answer. A new dentry will be added to the cache regardless of the result. Secondly, when pathname lookup reaches the final component, it will -sometimes need to take `i_mutex` before performing the last lookup so +sometimes need to take an exclusive lock on `i_rwsem` before performing the last lookup so that the required exclusion can be achieved. How path lookup chooses -to take, or not take, `i_mutex` is one of the +to take, or not take, `i_rwsem` is one of the issues addressed in a subsequent section. +If two threads attempt to look up the same name at the same time - a +name that is not yet in the dcache - the shared lock on `i_rwsem` will +not prevent them both adding new dentries with the same name. As this +would result in confusion an extra level of interlocking is used, +based around a secondary hash table (`in_lookup_hashtable`) and a +per-dentry flag bit (`DCACHE_PAR_LOOKUP`). + +To add a new dentry to the cache while only holding a shared lock on +`i_rwsem`, a thread must call `d_alloc_parallel()`. This allocates a +dentry, stores the required name and parent in it, checks if there +is already a matching dentry in the primary or secondary hash +tables, and if not, stores the newly allocated dentry in the secondary +hash table, with `DCACHE_PAR_LOOKUP` set. + +If a matching dentry was found in the primary hash table then that is +returned and the caller can know that it lost a race with some other +thread adding the entry. If no matching dentry is found in either +cache, the newly allocated dentry is returned and the caller can +detect this from the presence of `DCACHE_PAR_LOOKUP`. In this case it +knows that it has won any race and now is responsible for asking the +filesystem to perform the lookup and find the matching inode. When +the lookup is complete, it must call `d_lookup_done()` which clears +the flag and does some other house keeping, including removing the +dentry from the secondary hash table - it will normally have been +added to the primary hash table already. Note that a `struct +waitqueue_head` is passed to `d_alloc_parallel()`, and +`d_lookup_done()` must be called while this `waitqueue_head` is still +in scope. + +If a matching dentry is found in the secondary hash table, +`d_alloc_parallel()` has a little more work to do. It first waits for +`DCACHE_PAR_LOOKUP` to be cleared, using a wait_queue that was passed +to the instance of `d_alloc_parallel()` that won the race and that +will be woken by the call to `d_lookup_done()`. It then checks to see +if the dentry has now been added to the primary hash table. If it +has, the dentry is returned and the caller just sees that it lost any +race. If it hasn't been added to the primary hash table, the most +likely explanation is that some other dentry was added instead using +`d_splice_alias()`. In any case, `d_alloc_parallel()` repeats all the +look ups from the start and will normally return something from the +primary hash table. + ### mnt->mnt_count ### `mnt_count` is a per-CPU reference counter on "`mount`" structures.
@@ -376,7 +423,7 @@ described. If it finds a `LAST_NORM` component it first calls "`lookup_fast()`" which only looks in the dcache, but will ask the filesystem to revalidate the result if it is that sort of filesystem. If that doesn't get a good result, it calls "`lookup_slow()`" which -takes the `i_mutex`, rechecks the cache, and then asks the filesystem +takes `i_rwsem`, rechecks the cache, and then asks the filesystem to find a definitive answer. Each of these will call `follow_managed()` (as described below) to handle any mount points.
@@ -408,7 +455,7 @@ of housekeeping around `link_path_walk()` and returns the parent directory and final component to the caller. The caller will be either aiming to create a name (via `filename_create()`) or remove or rename a name (in which case `user_path_parent()` is used). They will use -`i_mutex` to exclude other changes while they validate and then +`i_rwsem` to exclude other changes while they validate and then perform their operation. `path_lookupat()` is nearly as simple - it is used when an existing
@@ -429,7 +476,7 @@ complexity needed to handle the different subtleties of O_CREAT (with or without O_EXCL), final "`/`" characters, and trailing symbolic links. We will revisit this in the final part of this series, which focuses on those symbolic links. "`do_last()`" will sometimes, but -not always, take `i_mutex`, depending on what it finds. +not always, take `i_rwsem`, depending on what it finds. Each of these, or the functions which call them, need to be alert to the possibility that the final component is not `LAST_NORM`. If the
@@ -728,12 +775,12 @@ checking the `seq` number of the old exactly mirrors the process of getting a counted reference to the new dentry before dropping that for the old dentry which we saw in REF-walk. -### No `inode->i_mutex` or even `rename_lock` ### +### No `inode->i_rwsem` or even `rename_lock` ### -A mutex is a fairly heavyweight lock that can only be taken when it is +A semaphore is a fairly heavyweight lock that can only be taken when it is permissible to sleep. As `rcu_read_lock()` forbids sleeping, -`inode->i_mutex` plays no role in RCU-walk. If some other thread does -take `i_mutex` and modifies the directory in a way that RCU-walk needs +`inode->i_rwsem` plays no role in RCU-walk. If some other thread does +take `i_rwsem` and modifies the directory in a way that RCU-walk needs to notice, the result will be either that RCU-walk fails to find the dentry that it is looking for, or it will find a dentry which `read_seqretry()` won't validate. In either case it will drop down to
@@ -1134,7 +1181,7 @@ and `do_last()`, each of which use the same convention as to be followed. Of these, `do_last()` is the most interesting as it is used for -opening a file. Part of `do_last()` runs with `i_mutex` held and this +opening a file. Part of `do_last()` runs with `i_rwsem` held and this part is in a separate function: `lookup_open()`. Explaining `do_last()` completely is beyond the scope of this article,
Signature · 1 line
2.19.0.216.g2d3b1c576c85
Quoted 8 lines · On Mon, 19 Nov 2018 11:55:46 +1100 NeilBrown <[email protected]> wrote:
Since this document was written,
i_mutexhas been replace withi_rwsem, and shared locks are utilized to allow lookups in the one directory to happen in parallel.So replace
i_mutexwithi_rwsem, and explain how this is used for parallel lookups.Signed-off-by NeilBrown <[email protected]>
I've applied this, thanks. I do have a couple of small comments, though...
#1: what are the chances of getting this document converted to RST and tied into our documentation tree?
#2: ...
Quoted 18 lines
Just FYI
$ git grep -w i_mutex | wc -l 262it was 276 before this patch... so a small improvement.
NeilBrown
Documentation/filesystems/path-lookup.md | 85 ++++++++++++++++++------ 1 file changed, 66 insertions(+), 19 deletions(-)Documentation/filesystems/path-lookup.md +0
-0
Documentation/filesystems/path-lookup.md This write-up is based on three articles published at lwn.net: · line 18
Quoted 7 lines
Documentation/filesystems/path-lookup.md +4
-0@@ -12,6 +12,10 @@ This write-up is based on three articles published at lwn.net: - <https://lwn.net/Articles/650786/> A walk among the symlinks Written by Neil Brown with help from Al Viro and Jon Corbet. +It has subsequently been updated to reflect changes in the kernel +including: + +- per-directory parallel name lookup.
I'm unconvinced about the value of putting changelogs into the docs themselves; that's what we keep git around for. (I *do* think there's some value of having something in a document giving a hint of how current it is, though).
Thanks,
jon
PATCH 1/2 Documentation: convert path-lookup from markdown to resturctured text
In reply to NeilBrownThis allows the document to be integrated with the main documentation tree.
Changes include:
- rename from .md to .rst
- use `` for code, not single `
- use correct sub-section marking
- fix indented blocks, both code and non-code
- fix external-link markup
---
Documentation/filesystems/index.rst | 2 +
.../{path-lookup.md => path-lookup.rst} | 875 +++++++++++----------
2 files changed, 448 insertions(+), 429 deletions(-)
rename Documentation/filesystems/{path-lookup.md => path-lookup.rst} (59%)Documentation/filesystems/index.rst +2 -0
@@ -359,3 +359,5 @@ encryption of files and directories. :maxdepth: 2 fscrypt + +.. include:: path-lookup.rst
Documentation/filesystems/path-lookup.rst renamed from Documentation/filesystems/path-lookup.md +446 -429
1,539 lines not on this page · raw message
Signature · 1 line
2.14.0.rc0.dirty
PATCH 2/2 Documentation: path-lookup - update externel refs
As gmane is no longer reliable, use lkml.org
Section numbers used by the open group seem to have changed!
--- Documentation/filesystems/path-lookup.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/filesystems/path-lookup.rst +3 -3
@@ -68,7 +68,7 @@ pathname that is just slashes have a final component. If it does exist, it could be "``.``" or "``..``" which are handled quite differently from other components. -.. _POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_12 +.. _POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13 If a pathname ends with a slash, such as "``/tmp/foo/``" it might be tempting to consider that to have an empty final component. In many
@@ -964,7 +964,7 @@ successfully - the error ``ELOOP`` must be returned. Loops can be detected without imposing limits, but limits are the simplest solution and, given the second reason for restriction, quite sufficient. -.. _outlined recently: http://thread.gmane.org/gmane.linux.kernel/1934390/focus=1934550 +.. _outlined recently: https://lkml.org/lkml/2015/4/20/644 The second reason was `outlined recently`_ by Linus:
@@ -1249,7 +1249,7 @@ Symlinks are different it seems. Both reading a symlink (with ``readlink()``) and looking up a symlink on the way to some other destination can update the atime on that symlink. -.. _clearest statement: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_08 +.. _clearest statement: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_09 It is not clear why this is the case; POSIX has little to say on the subject. The `clearest statement`_ is that, if a particular implementation
Signature · 1 line
2.14.0.rc0.dirty
Jonathan Corbet
maintainerRe: [PATCH 1/2] Documentation: convert path-lookup from markdown to resturctured text
In reply to NeilBrownQuoted 10 lines · On Wed, 05 Dec 2018 10:02:51 +1100 NeilBrown <[email protected]> wrote:
This allows the document to be integrated with the main documentation tree.
Changes include:
- rename from .md to .rst
- use `` for code, not single `
- use correct sub-section marking
- fix indented blocks, both code and non-code
- fix external-link markupSigned-off-by NeilBrown <[email protected]>
Looks good at a first glance, but I have one question...
Quoted 10 lines
Documentation/filesystems/index.rst | 2 + .../{path-lookup.md => path-lookup.rst} | 875 +++++++++++---------- 2 files changed, 448 insertions(+), 429 deletions(-) rename Documentation/filesystems/{path-lookup.md => path-lookup.rst} (59%)Documentation/filesystems/index.rst +0
-0
Documentation/filesystems/index.rst encryption of files and directories. · line 363
Quoted 5 lines
Documentation/filesystems/index.rst +2
-0@@ -359,3 +359,5 @@ encryption of files and directories. :maxdepth: 2 fscrypt + +.. include:: path-lookup.rst
Is there a reason you used "include" rather than just adding it to the TOC tree with the rest?
Thanks,
jon
Re: [PATCH 1/2] Documentation: convert path-lookup from markdown to resturctured text
Quoted 28 lines · Jonathan Corbet
Quoted 10 lines · On Wed, 05 Dec 2018 10:02:51 +1100 NeilBrown <[email protected]> wrote:
This allows the document to be integrated with the main documentation tree.
Changes include:
- rename from .md to .rst
- use `` for code, not single `
- use correct sub-section marking
- fix indented blocks, both code and non-code
- fix external-link markupSigned-off-by NeilBrown <[email protected]>Looks good at a first glance, but I have one question...
Quoted 12 lines
Documentation/filesystems/index.rst | 2 + .../{path-lookup.md => path-lookup.rst} | 875 +++++++++++---------- 2 files changed, 448 insertions(+), 429 deletions(-) rename Documentation/filesystems/{path-lookup.md => path-lookup.rst} (59%)Documentation/filesystems/index.rst +0
-0@@ -359,3 +359,5 @@ encryption of files and directories. :maxdepth: 2
Quoted 6 lines
Documentation/filesystems/index.rst encryption of files and directories. · line 363
Quoted 3 lines
Documentation/filesystems/index.rst +2
-0fscrypt + +.. include:: path-lookup.rstIs there a reason you used "include" rather than just adding it to the TOC tree with the rest?
Simple. I didn't know what I was doing, and so randomly made changes until the result seemed acceptable - then I stopped. If there is a better way, feel free to tweak that when applying.
Thanks,
NeilBrown
Jonathan Corbet
maintainerRe: [PATCH 1/2] Documentation: convert path-lookup from markdown to resturctured text
Quoted 6 lines · On Thu, 06 Dec 2018 08:00:58 +1100 NeilBrown <[email protected]> wrote:
Quoted 2 lines
Is there a reason you used "include" rather than just adding it to the TOC tree with the rest?
Simple. I didn't know what I was doing, and so randomly made changes until the result seemed acceptable - then I stopped. If there is a better way, feel free to tweak that when applying.
OK, tweaked and applied, thanks.
jon
Re: [PATCH 1/2] Documentation: convert path-lookup from markdown to resturctured text
Quoted 6 lines · Jonathan Corbet
Quoted 3 lines · On Thu, 06 Dec 2018 08:00:58 +1100 NeilBrown <[email protected]> wrote:
Quoted 2 lines
Is there a reason you used "include" rather than just adding it to the TOC tree with the rest?
Quoted 7 lines
Quoted 3 lines
Simple. I didn't know what I was doing, and so randomly made changes until the result seemed acceptable - then I stopped. If there is a better way, feel free to tweak that when applying.
OK, tweaked and applied, thanks.
jon
Thanks...
However we now have a stub section "Pathname lookup" which serves only to introduce another stub sectoin "Pathname lookup in Linux", which does little more than introduce the rest of the section.
This seems inelegant, and is probably why I went for the "include" approach.
What would you think of something more like the following?
Thanks,
NeilBrown
Documentation/filesystems/index.rst +12 -2
@@ -363,8 +363,18 @@ encryption of files and directories. Pathname lookup =============== -Pathname lookup in Linux is a complex beast; the document linked below -provides a comprehensive summary for those looking for the details. + +This write-up is based on three articles published at lwn.net: + +- <https://lwn.net/Articles/649115/> Pathname lookup in Linux +- <https://lwn.net/Articles/649729/> RCU-walk: faster pathname lookup in Linux +- <https://lwn.net/Articles/650786/> A walk among the symlinks + +Written by Neil Brown with help from Al Viro and Jon Corbet. +It has subsequently been updated to reflect changes in the kernel +including: + +- per-directory parallel name lookup. .. toctree:: :maxdepth: 2
Documentation/filesystems/path-lookup.rst +0 -15
@@ -1,18 +1,3 @@ -======================== -Pathname lookup in Linux -======================== - -This write-up is based on three articles published at lwn.net: - -- <https://lwn.net/Articles/649115/> Pathname lookup in Linux -- <https://lwn.net/Articles/649729/> RCU-walk: faster pathname lookup in Linux -- <https://lwn.net/Articles/650786/> A walk among the symlinks - -Written by Neil Brown with help from Al Viro and Jon Corbet. -It has subsequently been updated to reflect changes in the kernel -including: - -- per-directory parallel name lookup. Introduction to pathname lookup ===============================
Jonathan Corbet
maintainerRe: [PATCH 1/2] Documentation: convert path-lookup from markdown to resturctured text
Quoted 7 lines · On Mon, 10 Dec 2018 09:58:37 +1100 NeilBrown <[email protected]> wrote:
However we now have a stub section "Pathname lookup" which serves only to introduce another stub sectoin "Pathname lookup in Linux", which does little more than introduce the rest of the section.
This seems inelegant, and is probably why I went for the "include" approach.What would you think of something more like the following?
Sorry for the delay ... $EXCUSES ...
This makes sense; I went ahead and applied it with your SOB on it; let me know if that's not what you wanted.
Thanks,
jon
Re: [PATCH 1/2] Documentation: convert path-lookup from markdown to resturctured text
Quoted 9 lines · Jonathan Corbet
Quoted 6 lines · On Mon, 10 Dec 2018 09:58:37 +1100 NeilBrown <[email protected]> wrote:
However we now have a stub section "Pathname lookup" which serves only to introduce another stub sectoin "Pathname lookup in Linux", which does little more than introduce the rest of the section.
This seems inelegant, and is probably why I went for the "include" approach.
Quoted 3 lines
Quoted 1 line
What would you think of something more like the following?
Sorry for the delay ... $EXCUSES ...
'tis the season for $EXCUSES
Quoted 3 lines
This makes sense; I went ahead and applied it with your SOB on it; let me know if that's not what you wanted.
Perfect, thanks.
NeilBrown
Quoted 4 lines
Thanks,
jon