mm/highmem: Add notes about conversions from kmap{,_atomic}()
Subsystems (3)
PATCH v2 1/1 mm/highmem: Add notes about conversions from kmap{,_atomic}()
kmap() and kmap_atomic() have been deprecated. kmap_local_page() should always be used in new code and the call sites of the two deprecated functions should be converted. This latter task can lead to errors if it is not carried out with the necessary attention to the context around and between the maps and unmaps.
Therefore, add further information to the Highmem's documentation for the purpose to make it clearer that (1) kmap() and kmap_atomic() must not any longer be called in new code and (2) developers doing conversions from kmap() amd kmap_atomic() are expected to take care of the context around and between the maps and unmaps, in order to not break the code.
Relevant parts of this patch have been taken from messages exchanged privately with Ira Weiny (thanks!).
---
v1->v2: Reworked part of the "Notes" paragraph in the kmap_atomic() section in order to leave less room for misinterpretation. I've made these changes by merging some suggestions from Sebastian (thanks!), although I'm not yet entirely sure I've achieved crystal clear exposition.
Documentation/mm/highmem.rst | 41 +++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 10 deletions(-)
Documentation/mm/highmem.rst +31 -10
@@ -57,7 +57,8 @@ list shows them in order of preference of use. 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. + This function should always be used. kmap_atomic() and kmap() have been + deprecated. These mappings are thread-local and CPU-local, meaning that the mapping can only be accessed from within this thread and the thread is bound to the
@@ -100,10 +101,21 @@ list shows them in order of preference of use. (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(). This function has been deprecated; use kmap_local_page(). + + NOTE: Conversions to kmap_local_page() must take care to follow the mapping + restrictions imposed on kmap_local_page(). Furthermore, the code between + calls to kmap_atomic() and kunmap_atomic() may implicitly depend on the side + effects of atomic mappings, i.e. disabling page faults or preemption, or both. + In that case, explicit calls to pagefault_disable() or preempt_disable() or + both must be made in conjunction with the use of kmap_local_page(). + + [Legacy documentation] + + 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 does not sleep and the callers too may not sleep until after kunmap_atomic() is
@@ -115,11 +127,20 @@ list shows them in order of preference of use. 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(). +* kmap(). This function has been deprecated; use kmap_local_page(). + + NOTE: Conversions to kmap_local_page() must take care to follow the mapping + restrictions imposed on kmap_local_page(). In particular, it is necessary to + make sure that the kernel virtual memory pointer is only valid in the thread + that obtained it. + + [Legacy documentation] + + 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
Signature · 1 line
2.38.1
Documentation/mm/highmem.rst list shows them in order of preference of use. · line 61
Quoted 11 lines · Fabio M. De Francesco
Documentation/mm/highmem.rst +2
-1@@ -57,7 +57,8 @@ list shows them in order of preference of use. 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. + This function should always be used. kmap_atomic() and kmap() have been + deprecated.
"... always be used, whereas ..."
Documentation/mm/highmem.rst
Quoted 13 lines
Documentation/mm/highmem.rst +7
-0+ NOTE: Conversions to kmap_local_page() must take care to follow the mapping + restrictions imposed on kmap_local_page(). Furthermore, the code between + calls to kmap_atomic() and kunmap_atomic() may implicitly depend on the side + effects of atomic mappings, i.e. disabling page faults or preemption, or both. + In that case, explicit calls to pagefault_disable() or preempt_disable() or + both must be made in conjunction with the use of kmap_local_page(). +<snipped>...
Documentation/mm/highmem.rst +5
-0+ NOTE: Conversions to kmap_local_page() must take care to follow the mapping + restrictions imposed on kmap_local_page(). In particular, it is necessary to + make sure that the kernel virtual memory pointer is only valid in the thread + that obtained it. +
What about using note block to signify conversion notes above?
---- >8 ----
Documentation/mm/highmem.rst +13 -10
@@ -103,12 +103,14 @@ list shows them in order of preference of use. * kmap_atomic(). This function has been deprecated; use kmap_local_page(). - NOTE: Conversions to kmap_local_page() must take care to follow the mapping - restrictions imposed on kmap_local_page(). Furthermore, the code between - calls to kmap_atomic() and kunmap_atomic() may implicitly depend on the side - effects of atomic mappings, i.e. disabling page faults or preemption, or both. - In that case, explicit calls to pagefault_disable() or preempt_disable() or - both must be made in conjunction with the use of kmap_local_page(). + .. note:: + Conversions to kmap_local_page() must take care to follow the mapping + restrictions imposed on kmap_local_page(). Furthermore, the code between + calls to kmap_atomic() and kunmap_atomic() may implicitly depend on the + side effects of atomic mappings, i.e. disabling page faults or preemption, + or both. In that case, explicit calls to pagefault_disable() or + preempt_disable() or both must be made in conjunction with the use of + kmap_local_page(). [Legacy documentation]
@@ -129,10 +131,11 @@ list shows them in order of preference of use. * kmap(). This function has been deprecated; use kmap_local_page(). - NOTE: Conversions to kmap_local_page() must take care to follow the mapping - restrictions imposed on kmap_local_page(). In particular, it is necessary to - make sure that the kernel virtual memory pointer is only valid in the thread - that obtained it. + .. note:: + Conversions to kmap_local_page() must take care to follow the mapping + restrictions imposed on kmap_local_page(). In particular, it is necessary + to make sure that the kernel virtual memory pointer is only valid in the + thread that obtained it. [Legacy documentation]
Thanks.
Signature · 1 line
An old man doll... just what I always wanted! - Clara
Quoted 11 lines · On giovedì 8 dicembre 2022 05:22:37 CET Bagas Sanjaya wrote:
Quoted 10 lines · Fabio M. De Francesco
Documentation/mm/highmem.rst +0
-0@@ -57,7 +57,8 @@ list shows them in order of preference of use. It can be invoked from any context (including interrupts) but the mappings can only be used in the context which acquired them.
Quoted 8 lines
Documentation/mm/highmem.rst list shows them in order of preference of use. · line 63
Quoted 3 lines
Documentation/mm/highmem.rst +2
-1- This function should be preferred, where feasible, over all the others. + This function should always be used. kmap_atomic() and kmap() have been + deprecated."... always be used, whereas ..."
Quoted 2 lines
Documentation/mm/highmem.rst +1
-0+ NOTE: Conversions to kmap_local_page() must take care to follow themapping + restrictions imposed on
kmap_local_page(). Furthermore, the
code
Quoted 1 line
Quoted 1 line
between + calls to
kmap_atomic()andkunmap_atomic()may implicitly
depend
Quoted 2 lines
Quoted 2 lines
on the side + effects of atomic mappings, i.e. disabling page faults or preemption, or both. + In that case, explicit calls to
pagefault_disable()
Quoted 1 line
Quoted 1 line
or
preempt_disable()or + both must be made in conjunction with the use
of
Quoted 17 lines
Quoted 7 lines
kmap_local_page(). +
<snipped>...
+ NOTE: Conversions tokmap_local_page()must take care to follow the mapping + restrictions imposed onkmap_local_page(). In particular, it is necessary to + make sure that the kernel virtual memory pointer is only valid in the thread + that obtained it.
+What about using note block to signify conversion notes above?
---- >8 ----
Documentation/mm/highmem.rst +0
-0
Documentation/mm/highmem.rst list shows them in order of preference of use. · line 106
Quoted 4 lines
Documentation/mm/highmem.rst +0
-1@@ -103,12 +103,14 @@ list shows them in order of preference of use. * kmap_atomic(). This function has been deprecated; use kmap_local_page(). - NOTE: Conversions to kmap_local_page() must take care to follow the
mapping
Documentation/mm/highmem.rst list shows them in order of preference of use. · line 107
Quoted 1 line
Documentation/mm/highmem.rst +0
-1- restrictions imposed on kmap_local_page(). Furthermore, the code between
-
Documentation/mm/highmem.rst list shows them in order of preference of use. · line 106
Quoted 1 line
Documentation/mm/highmem.rst +0
-0calls to kmap_atomic() and kunmap_atomic() may implicitly depend on the
side
Documentation/mm/highmem.rst
Quoted 6 lines
Documentation/mm/highmem.rst +0
-1- effects of atomic mappings, i.e. disabling page faults or preemption, orboth. - In that case, explicit calls to
pagefault_disable()orpreempt_disable()or - both must be made in conjunction with the use ofkmap_local_page(). + .. note::Documentation/mm/highmem.rst +2
-0+ Conversions to kmap_local_page() must take care to follow the mapping + restrictions imposed on kmap_local_page(). Furthermore, the code
between
Quoted 1 line
+ calls to
kmap_atomic()andkunmap_atomic()may implicitly depend on
the
Documentation/mm/highmem.rst list shows them in order of preference of use. · line 132
Quoted 12 lines
+ side effects of atomic mappings, i.e. disabling page faults or
preemption, + or both. In that case, explicit calls topagefault_disable()or +preempt_disable()or both must be made in
conjunction with the use of +kmap_local_page().[Legacy documentation]
Documentation/mm/highmem.rst +0
-1@@ -129,10 +131,11 @@ list shows them in order of preference of use. * kmap(). This function has been deprecated; use kmap_local_page(). - NOTE: Conversions to kmap_local_page() must take care to follow the
mapping
Quoted 9 lines
Documentation/mm/highmem.rst +0
-1- restrictions imposed on kmap_local_page(). In particular, it is necessaryto - make sure that the kernel virtual memory pointer is only valid in the thread - that obtained it.
Documentation/mm/highmem.rst +3
-0+ .. note:: + Conversions to kmap_local_page() must take care to follow the mapping + restrictions imposed on kmap_local_page(). In particular, it isnecessary + to make sure that the kernel virtual memory pointer is only
valid in the + thread that obtained it.
Quoted 6 lines
[Legacy documentation]
Thanks.
--
An old man doll... just what I always wanted! - Clara
You provided valid suggestions, thanks!
However, immediately after submitting this v2 patch, Andrew added it to the - mm mm-unstable branch.
I'll do the changes that you suggested in a subsequent patch which will build on this.
Again thanks,
Fabio
Quoted 6 lines · Fabio M. De Francesco
You provided valid suggestions, thanks!
However, immediately after submitting this v2 patch, Andrew added it to the - mm mm-unstable branch.
I'll do the changes that you suggested in a subsequent patch which will build on this.
OK, thanks!
Signature · 1 line
An old man doll... just what I always wanted! - Clara