2022-04-26 06:06:31

by Fabio M. De Francesco

[permalink] [raw]
Subject: [PATCH v2 0/4] Extend and reorganize Highmem's documentation

This series has the purpose to extend and reorganize Highmem's
documentation.

This is still a work in progress, because some information should still be
moved from highmem.rst to highmem.h and highmem-internal.h. Specifically
I'm talking about moving the "how to" information to the relevant headers,
as it as been suggested by Ira Weiny (Intel).

This series is composed by four patches gathered from a previous series
made of two, plus two more single patches. The subject of this cover has
changed and the layout of the changes across the four patches has
changed too. For this reason it is very hard to show a valid versions'
log. Therefore, I decided to start over and drop versions (Maintainers
of the previous patch have been told to drop them).

Changes from v1 to v2:

1/4 - Fix typos (Mike Rapoport); re-write the description of @page
because the original was wrong (Ira Weiny); add Ira's and
Mike's tags in the commit message.
2/4 - Add Ira's and Mike's tags in the commit message.
3/4 - Rework the subject to better summarize what this patch
changes; merge the section which was removed from highmem.rst
with the kdocs of highmem.h (suggested by Ira Weiny); add
Ira's tag in the commit message.
4/4 - Reformulate a sentence that was incomprehensible due to my
own mistakes in copying and pasting the text of another
paragraph (problem noted by Ira Weiny); refer to the kdocs
of kmap_local_page () to show how nested mappings should be
handled; fix grammar error; add Ira's tag in the commit
message.

Fabio M. De Francesco (4):
mm/highmem: Fix kernel-doc warnings in highmem*.h
Documentation/vm: Include kdocs into highmem.rst
Documentation/vm: Move section from highmem.rst to highmem.h
Documentation/vm: Rework "Temporary Virtual Mappings" section

Documentation/vm/highmem.rst | 103 +++++++++++++++++++------------
include/linux/highmem-internal.h | 14 ++++-
include/linux/highmem.h | 44 +++++++++----
3 files changed, 107 insertions(+), 54 deletions(-)

--
2.34.1


2022-04-26 09:45:30

by Fabio M. De Francesco

[permalink] [raw]
Subject: [PATCH v2 3/4] Documentation/vm: Move section from highmem.rst to highmem.h

The use of kmap_atomic() is new code is being deprecated in favor of
kmap_local_page(). For this reason the "Using kmap_atomic" section in
highmem.rst is obsolete and unnecessary, but it can still help developers
if it were moved to kdocs in highmem.h.

Therefore, move the relevant parts of this section from highmem.rst and
merge them with the kdocs in highmem.h.

Cc: Jonathan Corbet <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Suggested-by: Ira Weiny <[email protected]>
Signed-off-by: Fabio M. De Francesco <[email protected]>
---
Documentation/vm/highmem.rst | 35 -----------------------------------
include/linux/highmem.h | 31 +++++++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 35 deletions(-)

diff --git a/Documentation/vm/highmem.rst b/Documentation/vm/highmem.rst
index ccff08a8211d..e05bf5524174 100644
--- a/Documentation/vm/highmem.rst
+++ b/Documentation/vm/highmem.rst
@@ -72,41 +72,6 @@ The kernel contains several ways of creating temporary mappings:
It may be assumed that k[un]map_atomic() won't fail.


-Using kmap_atomic
-=================
-
-When and where to use kmap_atomic() is straightforward. It is used when code
-wants to access the contents of a page that might be allocated from high memory
-(see __GFP_HIGHMEM), for example a page in the pagecache. The API has two
-functions, and they can be used in a manner similar to the following::
-
- /* Find the page of interest. */
- struct page *page = find_get_page(mapping, offset);
-
- /* Gain access to the contents of that page. */
- void *vaddr = kmap_atomic(page);
-
- /* Do something to the contents of that page. */
- memset(vaddr, 0, PAGE_SIZE);
-
- /* Unmap that page. */
- kunmap_atomic(vaddr);
-
-Note that the kunmap_atomic() call takes the result of the kmap_atomic() call
-not the argument.
-
-If you need to map two pages because you want to copy from one page to
-another you need to keep the kmap_atomic calls strictly nested, like::
-
- vaddr1 = kmap_atomic(page1);
- vaddr2 = kmap_atomic(page2);
-
- memcpy(vaddr1, vaddr2, PAGE_SIZE);
-
- kunmap_atomic(vaddr2);
- kunmap_atomic(vaddr1);
-
-
Cost of Temporary Mappings
==========================

diff --git a/include/linux/highmem.h b/include/linux/highmem.h
index 3456dc1d38db..6b2d59e025c5 100644
--- a/include/linux/highmem.h
+++ b/include/linux/highmem.h
@@ -142,6 +142,37 @@ static inline void *kmap_local_folio(struct folio *folio, size_t offset);
* and preemption.
*
* Do not use in new code. Use kmap_local_page() instead.
+ *
+ * It is used in atomic context when code wants to access the contents of a
+ * page that might be allocated from high memory (see __GFP_HIGHMEM), for
+ * example a page in the pagecache. The API has two functions, and they
+ * can be used in a manner similar to the following:
+ *
+ * -- Find the page of interest. --
+ * struct page *page = find_get_page(mapping, offset);
+ *
+ * -- Gain access to the contents of that page. --
+ * void *vaddr = kmap_atomic(page);
+ *
+ * -- Do something to the contents of that page. --
+ * memset(vaddr, 0, PAGE_SIZE);
+ *
+ * -- Unmap that page. --
+ * kunmap_atomic(vaddr);
+ *
+ * Note that the kunmap_atomic() call takes the result of the kmap_atomic()
+ * call, not the argument.
+ *
+ * If you need to map two pages because you want to copy from one page to
+ * another you need to keep the kmap_atomic calls strictly nested, like:
+ *
+ * vaddr1 = kmap_atomic(page1);
+ * vaddr2 = kmap_atomic(page2);
+ *
+ * memcpy(vaddr1, vaddr2, PAGE_SIZE);
+ *
+ * kunmap_atomic(vaddr2);
+ * kunmap_atomic(vaddr1);
*/
static inline void *kmap_atomic(struct page *page);

--
2.34.1

2022-04-26 12:13:29

by Fabio M. De Francesco

[permalink] [raw]
Subject: [PATCH v2 4/4] Documentation/vm: Rework "Temporary Virtual Mappings" section

Extend and rework the "Temporary Virtual Mappings" section of the highmem.rst
documentation.

Despite the local kmaps were introduced by Thomas Gleixner in October 2020,
documentation was still missing information about them. These additions rely
largely on Gleixner's patches, Jonathan Corbet's LWN articles, comments by
Ira Weiny and Matthew Wilcox, and in-code comments from
./include/linux/highmem.h.

1) Add a paragraph to document kmap_local_page().
2) Reorder the list of functions by decreasing order of preference of
use.
3) Rework part of the kmap() entry in list.

Cc: Jonathan Corbet <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Suggested-by: Ira Weiny <[email protected]>
Signed-off-by: Fabio M. De Francesco <[email protected]>
---
Documentation/vm/highmem.rst | 73 ++++++++++++++++++++++++++++++------
1 file changed, 62 insertions(+), 11 deletions(-)

diff --git a/Documentation/vm/highmem.rst b/Documentation/vm/highmem.rst
index e05bf5524174..b09f1f9a81f2 100644
--- a/Documentation/vm/highmem.rst
+++ b/Documentation/vm/highmem.rst
@@ -50,26 +50,77 @@ space when they use mm context tags.
Temporary Virtual Mappings
==========================

-The kernel contains several ways of creating temporary mappings:
+The kernel contains several ways of creating temporary mappings. The following
+list shows them in order of preference of use.

-* vmap(). This can be used to make a long duration mapping of multiple
- physical pages into a contiguous virtual space. It needs global
- synchronization to unmap.
+* kmap_local_page(). This function is used to require short term mappings.
+ It can be invoked from any context (including interrupts) but the mappings
+ can only be used in the context which acquired them.
+
+ This function should be preferred, where feasible, over all the others.

-* kmap(). This permits a short duration mapping of a single page. It needs
- global synchronization, but is amortized somewhat. It is also prone to
- deadlocks when using in a nested fashion, and so it is not recommended for
- new code.
+ These mappings are per thread, CPU local (i.e., migration from one CPU to
+ another is disabled - this is why they are called "local"), but they don't
+ disable preemption. It's valid to take pagefaults in a local kmap region,
+ unless the context in which the local mapping is acquired does not allow
+ it for other reasons.
+
+ kmap_local_page() always returns a valid virtual address and it is assumed
+ that kunmap_local() will never fail.
+
+ If a task holding local kmaps is preempted, the maps are removed on context
+ switch and restored when the task comes back on the CPU. As the maps are
+ strictly CPU local, it is guaranteed that the task stays on the CPU and
+ that the CPU cannot be unplugged until the local kmaps are released.
+
+ Nesting kmap_local_page() and kmap_atomic() mappings is allowed to a certain
+ extent (up to KMAP_TYPE_NR) but their invocations have to be strictly ordered
+ because the map implementation is stack based. See kmap_local_page () kdocs
+ (included in the "Functions" section) for details on how to manage nested
+ mappings.

* kmap_atomic(). This permits a very short duration mapping of a single
page. Since the mapping is restricted to the CPU that issued it, it
performs well, but the issuing task is therefore required to stay on that
CPU until it has finished, lest some other task displace its mappings.

- kmap_atomic() may also be used by interrupt contexts, since it is does not
- sleep and the caller may not sleep until after kunmap_atomic() is called.
+ kmap_atomic() may also be used by interrupt contexts, since it does not
+ sleep and the callers too may not sleep until after kunmap_atomic() is
+ called.
+
+ Each call of kmap_atomic() in the kernel creates a non-preemptible section
+ and disable pagefaults. This could be a source of unwanted latency, so it
+ should be only used if it is absolutely required, otherwise kmap_local_page()
+ should be used where it is feasible.

- It may be assumed that k[un]map_atomic() won't fail.
+ It is assumed that k[un]map_atomic() won't fail.
+
+* kmap(). This should be used to make short duration mapping of a single
+ page with no restrictions on preemption or migration. It comes with an
+ overhead as mapping space is restricted and protected by a global lock
+ for synchronization. When mapping is no longer needed, the address that
+ the page was mapped to must be released with kunmap().
+
+ Mapping changes must be propagated across all the CPUs. kmap() also
+ requires global TLB invalidation when the kmap's pool wraps and it might
+ block when the mapping space is fully utilized until a slot becomes
+ available. Therefore, kmap() is only callable from preemptible context.
+
+ All the above work is necessary if a mapping must last for a relatively
+ long time but the bulk of high-memory mappings in the kernel are
+ short-lived and only used in one place. This means that the cost of
+ kmap() is mostly wasted in such cases. kmap() was not intended for long
+ term mappings but it has morphed in that direction and its use is
+ strongly discouraged in newer code and the set of the preceding functions
+ should be preferred.
+
+ On 64-bit systems, calls to kmap_local_page(), kmap_atomic() and kmap() have
+ no real work to do because a 64-bit address space is more than sufficient to
+ address all the physical memory whose pages are permanently mapped.
+
+* vmap(). This can be used to make a long duration mapping of multiple
+ physical pages into a contiguous virtual space. It needs global
+ synchronization to unmap.


Cost of Temporary Mappings
--
2.34.1

Subject: Re: [PATCH v2 4/4] Documentation/vm: Rework "Temporary Virtual Mappings" section

On 2022-04-25 18:24:00 [+0200], Fabio M. De Francesco wrote:
> index e05bf5524174..b09f1f9a81f2 100644
> --- a/Documentation/vm/highmem.rst
> +++ b/Documentation/vm/highmem.rst
> @@ -50,26 +50,77 @@ space when they use mm context tags.
> Temporary Virtual Mappings
> ==========================
>
> -The kernel contains several ways of creating temporary mappings:
> +The kernel contains several ways of creating temporary mappings. The following
> +list shows them in order of preference of use.
>
> -* vmap(). This can be used to make a long duration mapping of multiple
> - physical pages into a contiguous virtual space. It needs global
> - synchronization to unmap.
> +* kmap_local_page(). This function is used to require short term mappings.
> + It can be invoked from any context (including interrupts) but the mappings
> + can only be used in the context which acquired them.
> +
> + This function should be preferred, where feasible, over all the others.

feasible? It should always be used. I don't see a reason why using
kmap_local_page() would not be feasible.

> -* kmap(). This permits a short duration mapping of a single page. It needs
> - global synchronization, but is amortized somewhat. It is also prone to
> - deadlocks when using in a nested fashion, and so it is not recommended for
> - new code.
> + These mappings are per thread, CPU local (i.e., migration from one CPU to
> + another is disabled - this is why they are called "local"), but they don't
> + disable preemption. It's valid to take pagefaults in a local kmap region,
> + unless the context in which the local mapping is acquired does not allow
> + it for other reasons.
> +
> + kmap_local_page() always returns a valid virtual address and it is assumed
> + that kunmap_local() will never fail.
> +
> + If a task holding local kmaps is preempted, the maps are removed on context
> + switch and restored when the task comes back on the CPU. As the maps are
> + strictly CPU local, it is guaranteed that the task stays on the CPU and

Maybe "thread local" instead CPU local? Another thread on the same CPU
can not use this mapping.

> + that the CPU cannot be unplugged until the local kmaps are released.
> +
> + Nesting kmap_local_page() and kmap_atomic() mappings is allowed to a certain
> + extent (up to KMAP_TYPE_NR) but their invocations have to be strictly ordered
> + because the map implementation is stack based. See kmap_local_page () kdocs
^
> + (included in the "Functions" section) for details on how to manage nested
> + mappings.

While they can be nested I wouldn't encourage that.

> * kmap_atomic(). This permits a very short duration mapping of a single
> page. Since the mapping is restricted to the CPU that issued it, it
> performs well, but the issuing task is therefore required to stay on that
> CPU until it has finished, lest some other task displace its mappings.
>
> - kmap_atomic() may also be used by interrupt contexts, since it is does not
> - sleep and the caller may not sleep until after kunmap_atomic() is called.
> + kmap_atomic() may also be used by interrupt contexts, since it does not
> + sleep and the callers too may not sleep until after kunmap_atomic() is
> + called.
> +
> + Each call of kmap_atomic() in the kernel creates a non-preemptible section
> + and disable pagefaults. This could be a source of unwanted latency, so it
> + should be only used if it is absolutely required, otherwise kmap_local_page()
> + should be used where it is feasible.

Please add a note this function is deprecated and must not be used in
new code.

Sebastian

2022-04-26 18:18:36

by Fabio M. De Francesco

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] Documentation/vm: Rework "Temporary Virtual Mappings" section

On martedì 26 aprile 2022 09:17:06 CEST Sebastian Andrzej Siewior wrote:
> On 2022-04-25 18:24:00 [+0200], Fabio M. De Francesco wrote:
> > index e05bf5524174..b09f1f9a81f2 100644
> > --- a/Documentation/vm/highmem.rst
> > +++ b/Documentation/vm/highmem.rst
> > @@ -50,26 +50,77 @@ space when they use mm context tags.
> > Temporary Virtual Mappings
> > ==========================
> >
> > -The kernel contains several ways of creating temporary mappings:
> > +The kernel contains several ways of creating temporary mappings. The
following
> > +list shows them in order of preference of use.
> >
> > -* vmap(). This can be used to make a long duration mapping of
multiple
> > - physical pages into a contiguous virtual space. It needs global
> > - synchronization to unmap.
> > +* kmap_local_page(). This function is used to require short term
mappings.
> > + It can be invoked from any context (including interrupts) but the
mappings
> > + can only be used in the context which acquired them.
> > +
> > + This function should be preferred, where feasible, over all the
others.
>
> feasible? It should always be used.

No, it cannot always be used. Please read again few lines above this that
"The mapping can only be used in the context which acquired them". We
cannot do blind s/kmap/kmap_local_page/.

> I don't see a reason why using
> kmap_local_page() would not be feasible.

Ditto.

> > -* kmap(). This permits a short duration mapping of a single page. It
needs
> > - global synchronization, but is amortized somewhat. It is also prone
to
> > - deadlocks when using in a nested fashion, and so it is not
recommended for
> > - new code.
> > + These mappings are per thread, CPU local (i.e., migration from one
CPU to
> > + another is disabled - this is why they are called "local"), but they
don't
> > + disable preemption. It's valid to take pagefaults in a local kmap
region,
> > + unless the context in which the local mapping is acquired does not
allow
> > + it for other reasons.
> > +
> > + kmap_local_page() always returns a valid virtual address and it is
assumed
> > + that kunmap_local() will never fail.
> > +
> > + If a task holding local kmaps is preempted, the maps are removed on
context
> > + switch and restored when the task comes back on the CPU. As the maps
are
> > + strictly CPU local, it is guaranteed that the task stays on the CPU
and
>
> Maybe "thread local" instead CPU local? Another thread on the same CPU
> can not use this mapping.
>

Hmm, I might add "thread local" to convey that the local mappings should
stay in the same context that acquired them.

However, kmap_local_page() also disable migration. This is how Thomas
Gleixner talks about kmap_local_page() in his patch where it introduced
this function:

"The kmap_local.*() functions can be invoked from both preemptible and
atomic context. kmap local sections disable migration to keep the resulting
virtual mapping address correct, but disable neither pagefaults nor
preemption.".

Therefore, if it "disable migration" it is "CPU local". I mean that I might
also add "thread local" but I think (at least at this moment) that I won't
remove "CPU local".

@Ira: what about this proposal?

> > + that the CPU cannot be unplugged until the local kmaps are released.
> > +
> > + Nesting kmap_local_page() and kmap_atomic() mappings is allowed to a
certain
> > + extent (up to KMAP_TYPE_NR) but their invocations have to be
strictly ordered
> > + because the map implementation is stack based. See kmap_local_page
() kdocs
> ^
> > + (included in the "Functions" section) for details on how to manage
nested
> > + mappings.
>
> While they can be nested I wouldn't encourage that.

I'm not encouraging this kinds of usages. I'm only saying that "it is
allowed to a certain extent".

> > * kmap_atomic(). This permits a very short duration mapping of a
single
> > page. Since the mapping is restricted to the CPU that issued it, it
> > performs well, but the issuing task is therefore required to stay on
that
> > CPU until it has finished, lest some other task displace its
mappings.
> >
> > - kmap_atomic() may also be used by interrupt contexts, since it is
does not
> > - sleep and the caller may not sleep until after kunmap_atomic() is
called.
> > + kmap_atomic() may also be used by interrupt contexts, since it does
not
> > + sleep and the callers too may not sleep until after kunmap_atomic()
is
> > + called.
> > +
> > + Each call of kmap_atomic() in the kernel creates a non-preemptible
section
> > + and disable pagefaults. This could be a source of unwanted latency,
so it
> > + should be only used if it is absolutely required, otherwise
kmap_local_page()
> > + should be used where it is feasible.
>
> Please add a note this function is deprecated and must not be used in
> new code.

I have already responded (in my other reply for 1/4) about the possible
addition of a notice of deprecation. But, as said, I also need to take into
account what other people think about it.

However, I agree that, since we have that "deprecated!" in the kdocs of
kmap_atomic(), I should be consistent everywhere.

Please let me wait for more reviews before making further changes.

Thanks,

Fabio

>
> Sebastian
>




2022-04-27 10:38:22

by Fabio M. De Francesco

[permalink] [raw]
Subject: Re: [PATCH v2 4/4] Documentation/vm: Rework "Temporary Virtual Mappings" section

On martedì 26 aprile 2022 13:47:34 CEST Sebastian Andrzej Siewior wrote:
>
> Hmm. It is thread-local in the end. There are slots 0 … KM_MAX_IDX for
> the mappings. Slot 0 for task A can be different from slot 0 for task B
> while both run on CPU0. So the same address, that is returned from
> kmap_local(), will point to a different page for both tasks. Both tasks
> can't be migrated to another CPU while the mapping is active.
> "CPU local" sounds like something that is same to everyone on the same
> CPU which is what this_cpu_read() for instance does.
>

OK, I agree with you :)

I just got three notices from Greg K-H stating that he has applied three of
my driver / Android patches. The patches are some conversions from kmap()
and / or kmap_atomic() to kmap_local_page() (or wrappers around it):

https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

I had forgotten that I wrote the following sentence in all three commit
messages: "[] With kmap_local_page(), the mapping is per thread, CPU local
and not globally visible. []"

Therefore, I'll add "thread-local" or "per thread". I probably like your
wording more than mine: "thread-local" is more suitable.

For consistency (again) I like the other change you proposed, which is to
add "deprecated!" also in kunmap_atomic(), exactly as it is already in
kmap_atomic() kernel-docs.

However, I will wait one more day before sending v3, in case there are
other people who want to suggest further changes.

If I remember correctly, I'm overlooking nothing else. Do I overlook
something?

Thanks for your help,

Fabio


Subject: Re: [PATCH v2 4/4] Documentation/vm: Rework "Temporary Virtual Mappings" section

On 2022-04-26 12:45:12 [+0200], Fabio M. De Francesco wrote:
> > > +* kmap_local_page(). This function is used to require short term
> mappings.
> > > + It can be invoked from any context (including interrupts) but the
> mappings
> > > + can only be used in the context which acquired them.
> > > +
> > > + This function should be preferred, where feasible, over all the
> others.
> >
> > feasible? It should always be used.
>
> No, it cannot always be used. Please read again few lines above this that
> "The mapping can only be used in the context which acquired them". We
> cannot do blind s/kmap/kmap_local_page/.

I'm sorry it seems I slipped while reading and replying.
The kmap_atomic() part has "should be only used if it is absolutely
required" … " otherwise kmap_local_page()". The kmap_atomic() should be
used in new code. The alternative kmap() and kmap_local*() should be
enough.

> > Maybe "thread local" instead CPU local? Another thread on the same CPU
> > can not use this mapping.
> >
>
> Hmm, I might add "thread local" to convey that the local mappings should
> stay in the same context that acquired them.
>
> However, kmap_local_page() also disable migration. This is how Thomas
> Gleixner talks about kmap_local_page() in his patch where it introduced
> this function:
>
> "The kmap_local.*() functions can be invoked from both preemptible and
> atomic context. kmap local sections disable migration to keep the resulting
> virtual mapping address correct, but disable neither pagefaults nor
> preemption.".
>
> Therefore, if it "disable migration" it is "CPU local". I mean that I might
> also add "thread local" but I think (at least at this moment) that I won't
> remove "CPU local".

Hmm. It is thread-local in the end. There are slots 0 … KM_MAX_IDX for
the mappings. Slot 0 for task A can be different from slot 0 for task B
while both run on CPU0. So the same address, that is returned from
kmap_local(), will point to a different page for both tasks. Both tasks
can't be migrated to another CPU while the mapping is active.
"CPU local" sounds like something that is same to everyone on the same
CPU which is what this_cpu_read() for instance does.

> @Ira: what about this proposal?
>
> Thanks,
>
> Fabio

Sebastian