arm64: add the time namespace support
Subsystems (2)
v4 cover arm64: add the time namespace support
Allocate the time namespace page among VVAR pages and add the logic to handle faults on VVAR properly.
If a task belongs to a time namespace then the VVAR page which contains the system wide VDSO data is replaced with a namespace specific page which has the same layout as the VVAR page. That page has vdso_data->seq set to 1 to enforce the slow path and vdso_data->clock_mode set to VCLOCK_TIMENS to enforce the time namespace handling path.
The extra check in the case that vdso_data->seq is odd, e.g. a concurrent update of the VDSO data is in progress, is not really affecting regular tasks which are not part of a time namespace as the task is spin waiting for the update to finish and vdso_data->seq to become even again.
If a time namespace task hits that code path, it invokes the corresponding time getter function which retrieves the real VVAR page, reads host time and then adds the offset for the requested clock which is stored in the special VVAR page.
v2: Code cleanups suggested by Vincenzo.
v3: add a comment in __arch_get_timens_vdso_data.
v4: - fix an issue reported by the lkp robot.
- vvar has the same size with/without CONFIG_TIME_NAMESPACE, but the timens page isn't allocated on !CONFIG_TIME_NAMESPACE. This simplifies criu/vdso migration between different kernel configs.
v4 on github (if someone prefers git pull to git am):
https://github.com/avagin/linux-task-diag/tree/arm64/timens-v4
Andrei Vagin (6):
arm64/vdso: use the fault callback to map vvar pages arm64/vdso: Zap vvar pages when switching to a time namespace arm64/vdso: Add time namespace page
arm64/vdso: Handle faults on timens page arm64/vdso: Restrict splitting VVAR VMA
arm64: enable time namespace support
--
2.24.1
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v4 1/6 arm64/vdso: use the fault callback to map vvar pages
This is required to support time namespaces where a time namespace data page is different for each namespace.
--- arch/arm64/kernel/vdso.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
arch/arm64/kernel/vdso.c +15 -9
@@ -108,28 +108,32 @@ static int __vdso_init(enum vdso_abi abi)PAGE_SHIFT ;/* Allocate the vDSO pagelist, plus a page for the data. */ - vdso_pagelist =kcalloc (vdso_info[abi].vdso_pages +1 , + vdso_pagelist =kcalloc (vdso_info[abi].vdso_pages,sizeof (struct page *),GFP_KERNEL );if (vdso_pagelist ==NULL )return -ENOMEM ; -/* Grab the vDSO data page. */ - vdso_pagelist[0 ] =phys_to_page (__pa_symbol (vdso_data)); - -/* Grab the vDSO code pages. */ pfn =sym_to_pfn (vdso_info[abi].vdso_code_start);for (i =0 ; i < vdso_info[abi].vdso_pages; i++) - vdso_pagelist[i +1 ] =pfn_to_page (pfn + i); + vdso_pagelist[i] =pfn_to_page (pfn + i); - vdso_info[abi].dm->pages = &vdso_pagelist[0 ]; - vdso_info[abi].cm->pages = &vdso_pagelist[1 ]; + vdso_info[abi].cm->pages = vdso_pagelist;return 0 ; } +static vm_fault_t vvar_fault (const struct vm_special_mapping *sm, +struct vm_area_struct *vma,struct vm_fault *vmf) +{ +if (vmf->pgoff ==0 ) +return vmf_insert_pfn (vma, vmf->address, +sym_to_pfn (vdso_data)); +return VM_FAULT_SIGBUS ; +} + static int __setup_additional_pages(enum vdso_abi abi, struct mm_struct *mm, struct linux_binprm *bprm,
@@ -150,7 +154,7 @@ static int __setup_additional_pages(enum vdso_abi abi, } ret =_install_special_mapping (mm, vdso_base,PAGE_SIZE , -VM_READ |VM_MAYREAD , +VM_READ |VM_MAYREAD |VM_PFNMAP , vdso_info[abi].dm);if (IS_ERR (ret)) goto up_fail;
@@ -209,6 +213,7 @@ static struct vm_special_mapping aarch32_vdso_maps[] = { #ifdef CONFIG_COMPAT_VDSO [AA32_MAP_VVAR ] = { .name ="[vvar]" , + .fault = vvar_fault, }, [AA32_MAP_VDSO ] = { .name = "[vdso]",
@@ -376,6 +381,7 @@ enum aarch64_map {static struct vm_special_mapping aarch64_vdso_maps[]__ro_after_init = { [AA64_MAP_VVAR ] = { .name ="[vvar]" , + .fault = vvar_fault, }, [AA64_MAP_VDSO ] = { .name ="[vdso]" ,
Signature · 1 line
2.24.1
Mark Rutland
reviewerRe: [PATCH 1/6] arm64/vdso: use the fault callback to map vvar pages
Quoted 2 lines · Andrei Vagin
This is required to support time namespaces where a time namespace data page is different for each namespace.
Can you please give a bit more of an introduction to the changes here? As-is, this doesn't give reviewers the necessary context to understand the change, nor does it justify it.
Ideally, a commit message for this should look something like:
| Currently the vdso has no awareness of time namespaces, which may | apply distinct offsets to processes in different namespaces. To handle | this within the vdso, we'll need to expose a per-namespace data page. | | As a preparatory step, this patch separates the vdso data page from | the code pages, and has it faulted in via its own fault callback. | Subsquent patches will extend this to support distinct pages per time | namespace.
Otherwise, I have a few minor comments below.
Quoted 12 lines
Reviewed-by Vincenzo Frascino <[email protected]>Reviewed-by Dmitry Safonov <[email protected]>Signed-off-by Andrei Vagin <[email protected]>--- arch/arm64/kernel/vdso.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)arch/arm64/kernel/vdso.c +0
-0
arch/arm64/kernel/vdso.c static int __vdso_init(enum vdso_abi abi) · line 113
Quoted 7 lines
arch/arm64/kernel/vdso.c +1
-1@@ -108,28 +108,32 @@ static int __vdso_init(enum vdso_abi abi)PAGE_SHIFT ;/* Allocate the vDSO pagelist, plus a page for the data. */ - vdso_pagelist =kcalloc (vdso_info[abi].vdso_pages +1 , + vdso_pagelist =kcalloc (vdso_info[abi].vdso_pages,sizeof (struct page *),GFP_KERNEL );
The comment above is now stale. Can you please update it, or (event better) delete it entirely?
Quoted 19 lines
arch/arm64/kernel/vdso.c +2
-7if (vdso_pagelist ==NULL )return -ENOMEM ; -/* Grab the vDSO data page. */ - vdso_pagelist[0 ] =phys_to_page (__pa_symbol (vdso_data)); - -/* Grab the vDSO code pages. */ pfn =sym_to_pfn (vdso_info[abi].vdso_code_start);for (i =0 ; i < vdso_info[abi].vdso_pages; i++) - vdso_pagelist[i +1 ] =pfn_to_page (pfn + i); + vdso_pagelist[i] =pfn_to_page (pfn + i); - vdso_info[abi].dm->pages = &vdso_pagelist[0 ]; - vdso_info[abi].cm->pages = &vdso_pagelist[1 ]; + vdso_info[abi].cm->pages = vdso_pagelist;return 0 ;
arch/arm64/kernel/vdso.c static int __vdso_init(enum vdso_abi abi) · line 135
Quoted 10 lines
arch/arm64/kernel/vdso.c +8
-0} +static vm_fault_t vvar_fault (const struct vm_special_mapping *sm, +struct vm_area_struct *vma,struct vm_fault *vmf) +{ +if (vmf->pgoff ==0 ) +return vmf_insert_pfn (vma, vmf->address, +sym_to_pfn (vdso_data)); +return VM_FAULT_SIGBUS ; +}
This might look better as:
| if (vmf->pgoff != 0) | return VM_FAULT_SIGBUS; | | return vmf_insert_pfn(vma, vmf->address, sym_to_pfn(vdso_data));
arch/arm64/kernel/vdso.c static int __setup_additional_pages(enum vdso_abi abi, · line 157
Quoted 10 lines
arch/arm64/kernel/vdso.c +2
-1+ static int __setup_additional_pages(enum vdso_abi abi, struct mm_struct *mm, struct linux_binprm *bprm,@@ -150,7 +154,7 @@ static int __setup_additional_pages(enum vdso_abi abi, } ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE, - VM_READ|VM_MAYREAD, + VM_READ|VM_MAYREAD|VM_PFNMAP,
This change needs to be explained in the commit message. WHy is it necessary, and why only so for the data page?
Thanks,
Mark.
Quoted 22 lines
arch/arm64/kernel/vdso.c +2
-0vdso_info[abi].dm);if (IS_ERR (ret)) goto up_fail;@@ -209,6 +213,7 @@ static struct vm_special_mapping aarch32_vdso_maps[] = { #ifdef CONFIG_COMPAT_VDSO [AA32_MAP_VVAR ] = { .name ="[vvar]" , + .fault = vvar_fault, }, [AA32_MAP_VDSO ] = { .name = "[vdso]",@@ -376,6 +381,7 @@ enum aarch64_map {static struct vm_special_mapping aarch64_vdso_maps[]__ro_after_init = { [AA64_MAP_VVAR ] = { .name ="[vvar]" , + .fault = vvar_fault, }, [AA64_MAP_VDSO ] = { .name ="[vdso]" ,Signature · 1 line
2.24.1
Re: [PATCH 1/6] arm64/vdso: use the fault callback to map vvar pages
Quoted 9 lines · Mark Rutland
Quoted 2 lines · Andrei Vagin
This is required to support time namespaces where a time namespace data page is different for each namespace.
Can you please give a bit more of an introduction to the changes here? As-is, this doesn't give reviewers the necessary context to understand the change, nor does it justify it.
Ideally, a commit message for this should look something like:
Mark, thank you for the review. I will write more details message and fix this patch according with your other comments.
Quoted 26 lines
| Currently the vdso has no awareness of time namespaces, which may | apply distinct offsets to processes in different namespaces. To handle | this within the vdso, we'll need to expose a per-namespace data page. | | As a preparatory step, this patch separates the vdso data page from | the code pages, and has it faulted in via its own fault callback. | Subsquent patches will extend this to support distinct pages per time | namespace.Otherwise, I have a few minor comments below.
Quoted 14 lines
Reviewed-by Vincenzo Frascino <[email protected]>Reviewed-by Dmitry Safonov <[email protected]>Signed-off-by Andrei Vagin <[email protected]>--- arch/arm64/kernel/vdso.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)arch/arm64/kernel/vdso.c +0
-0@@ -108,28 +108,32 @@ static int __vdso_init(enum vdso_abi abi)PAGE_SHIFT ;
Quoted 8 lines
arch/arm64/kernel/vdso.c static int __vdso_init(enum vdso_abi abi) · line 113
Quoted 5 lines
arch/arm64/kernel/vdso.c +1
-1/* Allocate the vDSO pagelist, plus a page for the data. */ - vdso_pagelist =kcalloc (vdso_info[abi].vdso_pages +1 , + vdso_pagelist =kcalloc (vdso_info[abi].vdso_pages,sizeof (struct page *),GFP_KERNEL );The comment above is now stale. Can you please update it, or (event better) delete it entirely?
I will delete it.
Quoted 22 lines
Quoted 22 lines
arch/arm64/kernel/vdso.c +2
-7if (vdso_pagelist ==NULL )return -ENOMEM ; -/* Grab the vDSO data page. */ - vdso_pagelist[0 ] =phys_to_page (__pa_symbol (vdso_data)); - -/* Grab the vDSO code pages. */ pfn =sym_to_pfn (vdso_info[abi].vdso_code_start);for (i =0 ; i < vdso_info[abi].vdso_pages; i++) - vdso_pagelist[i +1 ] =pfn_to_page (pfn + i); + vdso_pagelist[i] =pfn_to_page (pfn + i); - vdso_info[abi].dm->pages = &vdso_pagelist[0 ]; - vdso_info[abi].cm->pages = &vdso_pagelist[1 ]; + vdso_info[abi].cm->pages = vdso_pagelist;return 0 ; }
Quoted 10 lines
arch/arm64/kernel/vdso.c static int __vdso_init(enum vdso_abi abi) · line 135
Quoted 8 lines
arch/arm64/kernel/vdso.c +8
-0+static vm_fault_t vvar_fault (const struct vm_special_mapping *sm, +struct vm_area_struct *vma,struct vm_fault *vmf) +{ +if (vmf->pgoff ==0 ) +return vmf_insert_pfn (vma, vmf->address, +sym_to_pfn (vdso_data)); +return VM_FAULT_SIGBUS ; +}This might look better as:
In the next patch, this function will handle more pages and this form will look better.
Quoted 13 lines
| if (vmf->pgoff != 0) | return VM_FAULT_SIGBUS; | | return vmf_insert_pfn(vma, vmf->address, sym_to_pfn(vdso_data));Quoted 7 lines
arch/arm64/kernel/vdso.c +1
-0+ static int __setup_additional_pages(enum vdso_abi abi, struct mm_struct *mm, struct linux_binprm *bprm,@@ -150,7 +154,7 @@ static int __setup_additional_pages(enum vdso_abi abi, }
Quoted 6 lines
arch/arm64/kernel/vdso.c static int __setup_additional_pages(enum vdso_abi abi, · line 157
Quoted 3 lines
arch/arm64/kernel/vdso.c +1
-1ret = _install_special_mapping(mm, vdso_base, PAGE_SIZE, - VM_READ|VM_MAYREAD, + VM_READ|VM_MAYREAD|VM_PFNMAP,This change needs to be explained in the commit message. WHy is it necessary, and why only so for the data page?
I will update the commit message. VM_PFNMAP is required to handle faults from a vma fault callback. In this case, it is vvar_fault.
Quoted 26 lines
Thanks,
Mark.Quoted 22 lines
arch/arm64/kernel/vdso.c +2
-0vdso_info[abi].dm);if (IS_ERR (ret)) goto up_fail;@@ -209,6 +213,7 @@ static struct vm_special_mapping aarch32_vdso_maps[] = { #ifdef CONFIG_COMPAT_VDSO [AA32_MAP_VVAR ] = { .name ="[vvar]" , + .fault = vvar_fault, }, [AA32_MAP_VDSO ] = { .name = "[vdso]",@@ -376,6 +381,7 @@ enum aarch64_map {static struct vm_special_mapping aarch64_vdso_maps[]__ro_after_init = { [AA64_MAP_VVAR ] = { .name ="[vvar]" , + .fault = vvar_fault, }, [AA64_MAP_VDSO ] = { .name ="[vdso]" ,Signature · 1 line
2.24.1
PATCH v4 2/6 arm64/vdso: Zap vvar pages when switching to a time namespace
In reply to Andrei VaginThe VVAR page layout depends on whether a task belongs to the root or non-root time namespace. Whenever a task changes its namespace, the VVAR page tables are cleared and then they will be re-faulted with a corresponding layout.
--- arch/arm64/kernel/vdso.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
arch/arm64/kernel/vdso.c +32 -0
@@ -125,6 +125,38 @@ static int __vdso_init(enum vdso_abi abi)return 0 ; } +#ifdef CONFIG_TIME_NS +/* +* The vvar page layout depends on whether a task belongs to the root or +* non-root time namespace. Whenever a task changes its namespace, the VVAR +* page tables are cleared and then they will re-faulted with a +* corresponding layout. +* See also the comment near timens_setup_vdso_data() for details. +*/ +int vdso_join_timens (struct task_struct *task,struct time_namespace *ns) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; + +if (mmap_write_lock_killable (mm)) +return -EINTR ; + +for (vma = mm->mmap; vma; vma = vma->vm_next) { +unsigned long size = vma->vm_end - vma->vm_start; + +if (vma_is_special_mapping (vma, vdso_info[VDSO_ABI_AA64 ].dm)) +zap_page_range (vma, vma->vm_start, size); +#ifdef CONFIG_COMPAT_VDSO +if (vma_is_special_mapping (vma, vdso_info[VDSO_ABI_AA32 ].dm)) +zap_page_range (vma, vma->vm_start, size); +#endif + } + +mmap_write_unlock (mm); +return 0 ; +} +#endif +static vm_fault_t vvar_fault (const struct vm_special_mapping *sm,struct vm_area_struct *vma,struct vm_fault *vmf) {
Signature · 1 line
2.24.1
Mark Rutland
reviewerRe: [PATCH 2/6] arm64/vdso: Zap vvar pages when switching to a time namespace
Quoted 2 lines · Andrei Vagin
The VVAR page layout depends on whether a task belongs to the root or non-root time namespace.
Please be more explicit as to what you mean by layout here, as that seems to be an overloaded term. For example, the comment above timens_setup_vdso_data() can be read to directly contradict this:
| A time namespace VVAR page has the same layout as the VVAR page which | contains the system wide VDSO data.
... I think you're trying to say is that when we add time namespace support, we'll have multiple VVAR pages, and their position in the address space depends on whether the task is part of a time namespace.
Quoted 3 lines
Whenever a task changes its namespace, the VVAR
page tables are cleared and then they will be re-faulted with a corresponding layout.
How does this work for multi-threaded applications? Are there any concerns w.r.t. atomicity of the change?
Quoted 13 lines
Reviewed-by Vincenzo Frascino <[email protected]>Reviewed-by Dmitry Safonov <[email protected]>Signed-off-by Andrei Vagin <[email protected]>--- arch/arm64/kernel/vdso.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)arch/arm64/kernel/vdso.c +0
-0@@ -125,6 +125,38 @@ static int __vdso_init(enum vdso_abi abi)return 0 ;
arch/arm64/kernel/vdso.c static int __vdso_init(enum vdso_abi abi) · line 135
Quoted 10 lines
arch/arm64/kernel/vdso.c +8
-0} +#ifdef CONFIG_TIME_NS +/* +* The vvar page layout depends on whether a task belongs to the root or +* non-root time namespace. Whenever a task changes its namespace, the VVAR +* page tables are cleared and then they will re-faulted with a +* corresponding layout. +* See also the comment near timens_setup_vdso_data() for details. +*/
As with the commit message, this is not very clear and can be read to contradict the comment it refers to, which is rather unhelpful.
How about:
/*
* The vvar mapping contains data for a specific time namespace, so when
* a task changes namespace we must unmap its vvar data for the old
* namespace. Subsequent faults will map in data for the new namespace. *
* For more details see timens_setup_vdso_data().
*/
Thanks,
Mark.
Quoted 30 lines
arch/arm64/kernel/vdso.c +24
-0+int vdso_join_timens (struct task_struct *task,struct time_namespace *ns) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; + +if (mmap_write_lock_killable (mm)) +return -EINTR ; + +for (vma = mm->mmap; vma; vma = vma->vm_next) { +unsigned long size = vma->vm_end - vma->vm_start; + +if (vma_is_special_mapping (vma, vdso_info[VDSO_ABI_AA64 ].dm)) +zap_page_range (vma, vma->vm_start, size); +#ifdef CONFIG_COMPAT_VDSO +if (vma_is_special_mapping (vma, vdso_info[VDSO_ABI_AA32 ].dm)) +zap_page_range (vma, vma->vm_start, size); +#endif + } + +mmap_write_unlock (mm); +return 0 ; +} +#endif +static vm_fault_t vvar_fault (const struct vm_special_mapping *sm,struct vm_area_struct *vma,struct vm_fault *vmf) {Signature · 1 line
2.24.1
Re: [PATCH 2/6] arm64/vdso: Zap vvar pages when switching to a time namespace
Hi Mark,
Quoted 1 line · Mark Rutland
On Tue, Jun 16, 2020 at 12:55:41AM -0700, Andrei Vagin wrote:
[..]
Quoted 6 lines
Quoted 3 lines
Whenever a task changes its namespace, the VVAR
page tables are cleared and then they will be re-faulted with a corresponding layout.How does this work for multi-threaded applications? Are there any concerns w.r.t. atomicity of the change?
Multi-threaded applications can't setns() for time namespace, timens_install():
: if (!current_is_single_threaded())
: return -EUSERS;
Thanks,
Dmitry
Re: [PATCH 2/6] arm64/vdso: Zap vvar pages when switching to a time namespace
In reply to Andrei VaginQuoted 25 lines · Andrei Vagin
The VVAR page layout depends on whether a task belongs to the root or non-root time namespace. Whenever a task changes its namespace, the VVAR page tables are cleared and then they will be re-faulted with a corresponding layout.
Reviewed-by Vincenzo Frascino <[email protected]>Reviewed-by Dmitry Safonov <[email protected]>Signed-off-by Andrei Vagin <[email protected]>--- arch/arm64/kernel/vdso.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)arch/arm64/kernel/vdso.c +5
-0@@ -125,6 +125,38 @@ static int __vdso_init(enum vdso_abi abi)return 0 ; } +#ifdef CONFIG_TIME_NS +/* +* The vvar page layout depends on whether a task belongs to the root or +* non-root time namespace. Whenever a task changes its namespace, the VVAR +* page tables are cleared and then they will re-faulted with a
arch/arm64/kernel/vdso.c static int __vdso_init(enum vdso_abi abi) · line 142
Quoted 10 lines
arch/arm64/kernel/vdso.c +10
-0+* corresponding layout. +* See also the comment near timens_setup_vdso_data() for details. +*/ +int vdso_join_timens (struct task_struct *task,struct time_namespace *ns) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; + +if (mmap_write_lock_killable (mm)) +return -EINTR ;
Hey,
Just a heads-up I'm about to plumb CLONE_NEWTIME support into setns() which would mean that vdso_join_timens() ould not be allowed to fail anymore to make it easy to switch to multiple namespaces atomically. So this would probably need to be changed to mmap_write_lock() which I've already brought up upstream:
https://lore.kernel.org/lkml/20200611110221.pgd3r5qkjrjmfqa2@wittgenstein/
(Assuming that people agree. I just sent the series and most people here are Cced.)
Thanks!
Christian
Re: [PATCH 2/6] arm64/vdso: Zap vvar pages when switching to a time namespace
Quoted 39 lines · Christian Brauner
Quoted 25 lines · Andrei Vagin
The VVAR page layout depends on whether a task belongs to the root or non-root time namespace. Whenever a task changes its namespace, the VVAR page tables are cleared and then they will be re-faulted with a corresponding layout.
Reviewed-by Vincenzo Frascino <[email protected]>Reviewed-by Dmitry Safonov <[email protected]>Signed-off-by Andrei Vagin <[email protected]>--- arch/arm64/kernel/vdso.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)arch/arm64/kernel/vdso.c +5
-0@@ -125,6 +125,38 @@ static int __vdso_init(enum vdso_abi abi)return 0 ; } +#ifdef CONFIG_TIME_NS +/* +* The vvar page layout depends on whether a task belongs to the root or +* non-root time namespace. Whenever a task changes its namespace, the VVAR +* page tables are cleared and then they will re-faulted with a arch/arm64/kernel/vdso.c static int __vdso_init(enum vdso_abi abi) · line 142
Quoted 10 lines
arch/arm64/kernel/vdso.c +10
-0+* corresponding layout. +* See also the comment near timens_setup_vdso_data() for details. +*/ +int vdso_join_timens (struct task_struct *task,struct time_namespace *ns) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; + +if (mmap_write_lock_killable (mm)) +return -EINTR ;Hey,
Quoted 1 line
Just a heads-up I'm about to plumb
CLONE_NEWTIMEsupport intosetns()
Hmm. I am not sure that I unserstand what you mean. I think setns(nsfd, CLONE_NEWTIME) works now. For example, we use it in tools/testing/selftests/timens/timens.c. Do you mean setns(pidfd, CLONE_NEWTIME | CLONE_something)?
Quoted 10 lines
which would mean that
vdso_join_timens()ould not be allowed to fail anymore to make it easy to switch to multiple namespaces atomically. So this would probably need to be changed tommap_write_lock()which I've already brought up upstream:
https://lore.kernel.org/lkml/20200611110221.pgd3r5qkjrjmfqa2@wittgenstein/
(Assuming that people agree. I just sent the series and most people here are Cced.)Thanks!
Christian
Re: [PATCH 2/6] arm64/vdso: Zap vvar pages when switching to a time namespace
Quoted 40 lines · Andrei Vagin
Quoted 39 lines · Christian Brauner
Quoted 25 lines · Andrei Vagin
The VVAR page layout depends on whether a task belongs to the root or non-root time namespace. Whenever a task changes its namespace, the VVAR page tables are cleared and then they will be re-faulted with a corresponding layout.
Reviewed-by Vincenzo Frascino <[email protected]>Reviewed-by Dmitry Safonov <[email protected]>Signed-off-by Andrei Vagin <[email protected]>--- arch/arm64/kernel/vdso.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)arch/arm64/kernel/vdso.c +5
-0@@ -125,6 +125,38 @@ static int __vdso_init(enum vdso_abi abi)return 0 ; } +#ifdef CONFIG_TIME_NS +/* +* The vvar page layout depends on whether a task belongs to the root or +* non-root time namespace. Whenever a task changes its namespace, the VVAR +* page tables are cleared and then they will re-faulted with a arch/arm64/kernel/vdso.c static int __vdso_init(enum vdso_abi abi) · line 142
Quoted 10 lines
arch/arm64/kernel/vdso.c +10
-0+* corresponding layout. +* See also the comment near timens_setup_vdso_data() for details. +*/ +int vdso_join_timens (struct task_struct *task,struct time_namespace *ns) +{ +struct mm_struct *mm = task->mm; +struct vm_area_struct *vma; + +if (mmap_write_lock_killable (mm)) +return -EINTR ;Hey,
Quoted 6 lines
Quoted 1 line
Just a heads-up I'm about to plumb
CLONE_NEWTIMEsupport intosetns()Hmm. I am not sure that I unserstand what you mean. I think
setns(nsfd, CLONE_NEWTIME)works now. For example, we use it intools/testing/selftests/timens/timens.c. Do you meansetns(pidfd, CLONE_NEWTIME | CLONE_something)?
Indeed, I'm talking about setns(pidfd, CLONE_NEWUSER | CLONE_NEWNS | CLONE_NEWTIME). But also in general, the setns infrastructure has been reworked and ideally all namespaces only perform permissions checks and install the namespace into the passed in new struct nsset (which was introduced this cycle) in their install handler (e.g. timens_install()) and don't make any task-visible changes yet but instead provide an install routine that does not fail which is then called from static void commit_nsset(struct nsset *nsset)
in kernel/nsproxy.c.
Quoted 11 lines
Quoted 11 lines
which would mean that
vdso_join_timens()ould not be allowed to fail anymore to make it easy to switch to multiple namespaces atomically. So this would probably need to be changed tommap_write_lock()which I've already brought up upstream:
https://lore.kernel.org/lkml/20200611110221.pgd3r5qkjrjmfqa2@wittgenstein/
(Assuming that people agree. I just sent the series and most people here are Cced.)Thanks!
Christian
PATCH v4 3/6 arm64/vdso: Add time namespace page
In reply to Andrei VaginAllocate the time namespace page among VVAR pages. Provide __arch_get_timens_vdso_data() helper for VDSO code to get the code-relative position of VVARs on that special page.
If a task belongs to a time namespace then the VVAR page which contains the system wide VDSO data is replaced with a namespace specific page which has the same layout as the VVAR page. That page has vdso_data->seq set to 1 to enforce the slow path and vdso_data->clock_mode set to VCLOCK_TIMENS to enforce the time namespace handling path.
The extra check in the case that vdso_data->seq is odd, e.g. a concurrent update of the VDSO data is in progress, is not really affecting regular tasks which are not part of a time namespace as the task is spin waiting for the update to finish and vdso_data->seq to become even again.
If a time namespace task hits that code path, it invokes the corresponding time getter function which retrieves the real VVAR page, reads host time and then adds the offset for the requested clock which is stored in the special VVAR page.
--- arch/arm64/include/asm/vdso.h | 2 ++ .../include/asm/vdso/compat_gettimeofday.h | 12 ++++++++++++ arch/arm64/include/asm/vdso/gettimeofday.h | 8 ++++++++ arch/arm64/kernel/vdso.c | 19 ++++++++++++++++--- arch/arm64/kernel/vdso/vdso.lds.S | 5 ++++- arch/arm64/kernel/vdso32/vdso.lds.S | 5 ++++- include/vdso/datapage.h | 1 + 7 files changed, 47 insertions(+), 5 deletions(-)
arch/arm64/include/asm/vdso.h +2 -0
@@ -12,6 +12,8 @@*/ #define VDSO_LBASE 0x0 +#define __VVAR_PAGES 2 +#ifndef __ASSEMBLY__#include <generated/vdso-offsets.h>
arch/arm64/include/asm/vdso/compat_gettimeofday.h +12 -0
@@ -152,6 +152,18 @@ static __always_inline const struct vdso_data *__arch_get_vdso_data(void)return ret; } +#ifdef CONFIG_TIME_NS +static __always_inline const struct vdso_data *__arch_get_timens_vdso_data(void) +{ +const struct vdso_data *ret; + +/* See __arch_get_vdso_data(). */ + asmvolatile ("mov %0, %1" :"=r" (ret) :"r" (_timens_data)); + +return ret; +} +#endif +#endif /* !__ASSEMBLY__ */ #endif /* __ASM_VDSO_GETTIMEOFDAY_H */
arch/arm64/include/asm/vdso/gettimeofday.h +8 -0
@@ -96,6 +96,14 @@ const struct vdso_data *__arch_get_vdso_data(void)return _vdso_data; } +#ifdef CONFIG_TIME_NS +static __always_inline +const struct vdso_data *__arch_get_timens_vdso_data(void) +{ +return _timens_data; +} +#endif +#endif /* !__ASSEMBLY__ */ #endif /* __ASM_VDSO_GETTIMEOFDAY_H */
arch/arm64/kernel/vdso.c +16 -3
@@ -40,6 +40,12 @@ enum vdso_abi {#endif /* CONFIG_COMPAT_VDSO */ }; +enum vvar_pages { +VVAR_DATA_PAGE_OFFSET , +VVAR_TIMENS_PAGE_OFFSET , +VVAR_NR_PAGES , +}; +struct vdso_abi_info {const char *name;const char *vdso_code_start;
@@ -126,6 +132,11 @@ static int __vdso_init(enum vdso_abi abi) }#ifdef CONFIG_TIME_NS +struct vdso_data *arch_get_vdso_data (void *vvar_page) +{ +return (struct vdso_data *)(vvar_page); +} +/* * The vvar page layout depends on whether a task belongs to the root or * non-root time namespace. Whenever a task changes its namespace, the VVAR
@@ -175,9 +186,11 @@ static int __setup_additional_pages(enum vdso_abi abi,unsigned long gp_flags =0 ;void *ret; +BUILD_BUG_ON (VVAR_NR_PAGES != __VVAR_PAGES); + vdso_text_len = vdso_info[abi].vdso_pages <<PAGE_SHIFT ;/* Be sure to map the data page */ - vdso_mapping_len = vdso_text_len +PAGE_SIZE ; + vdso_mapping_len = vdso_text_len +VVAR_NR_PAGES *PAGE_SIZE ; vdso_base =get_unmapped_area (NULL ,0 , vdso_mapping_len,0 ,0 );if (IS_ERR_VALUE (vdso_base)) {
@@ -185,7 +198,7 @@ static int __setup_additional_pages(enum vdso_abi abi, goto up_fail; } - ret =_install_special_mapping (mm, vdso_base,PAGE_SIZE , + ret =_install_special_mapping (mm, vdso_base,VVAR_NR_PAGES *PAGE_SIZE ,VM_READ |VM_MAYREAD |VM_PFNMAP , vdso_info[abi].dm);if (IS_ERR (ret))
@@ -194,7 +207,7 @@ static int __setup_additional_pages(enum vdso_abi abi,if (IS_ENABLED (CONFIG_ARM64_BTI_KERNEL ) &&system_supports_bti ()) gp_flags =VM_ARM64_BTI ; - vdso_base +=PAGE_SIZE ; + vdso_base +=VVAR_NR_PAGES *PAGE_SIZE ; mm->context.vdso = (void *)vdso_base; ret = _install_special_mapping(mm, vdso_base, vdso_text_len, VM_READ|VM_EXEC|gp_flags|
arch/arm64/kernel/vdso/vdso.lds.S +4 -1
@@ -17,7 +17,10 @@ OUTPUT_ARCH(aarch64) SECTIONS { - PROVIDE(_vdso_data = . - PAGE_SIZE); + PROVIDE(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE); +#ifdef CONFIG_TIME_NS + PROVIDE(_timens_data = _vdso_data + PAGE_SIZE); +#endif . = VDSO_LBASE + SIZEOF_HEADERS; .hash : { *(.hash) } :text
arch/arm64/kernel/vdso32/vdso.lds.S +4 -1
@@ -17,7 +17,10 @@ OUTPUT_ARCH(arm) SECTIONS { - PROVIDE_HIDDEN(_vdso_data = . - PAGE_SIZE); + PROVIDE_HIDDEN(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE); +#ifdef CONFIG_TIME_NS + PROVIDE_HIDDEN(_timens_data = _vdso_data + PAGE_SIZE); +#endif . = VDSO_LBASE + SIZEOF_HEADERS; .hash : { *(.hash) } :text
include/vdso/datapage.h +1 -0
@@ -109,6 +109,7 @@ struct vdso_data {* relocation, and this is what we need. */ extern struct vdso_data _vdso_data[CS_BASES] __attribute__((visibility("hidden"))); +extern struct vdso_data _timens_data[CS_BASES] __attribute__((visibility("hidden")));/* * The generic vDSO implementation requires that gettimeofday.h
Signature · 1 line
2.24.1
PATCH v4 4/6 arm64/vdso: Handle faults on timens page
In reply to Andrei VaginIf a task belongs to a time namespace then the VVAR page which contains the system wide VDSO data is replaced with a namespace specific page which has the same layout as the VVAR page.
--- arch/arm64/kernel/vdso.c | 57 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-)
arch/arm64/kernel/vdso.c +53 -4
@@ -18,6 +18,7 @@#include <linux/sched.h> #include <linux/signal.h> #include <linux/slab.h> +#include <linux/time_namespace.h> #include <linux/timekeeper_internal.h> #include <linux/vmalloc.h> #include <vdso/datapage.h>
@@ -166,15 +167,63 @@ int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)mmap_write_unlock (mm);return 0 ; } + +static struct page *find_timens_vvar_page (struct vm_area_struct *vma) +{ +if (likely (vma->vm_mm == current->mm)) +return current->nsproxy->time_ns->vvar_page; + +/* +* VM_PFNMAP | VM_IO protect .fault() handler from being called +* through interfaces like /proc/$pid/mem or +* process_vm_{readv,writev}() as long as there's no .access() +* in special_mapping_vmops(). +* For more details check_vma_flags() and __access_remote_vm() +*/ + +WARN (1 ,"vvar_page accessed remotely" ); + +return NULL ; +} +#else +static inline struct page *find_timens_vvar_page (struct vm_area_struct *vma) +{ +return NULL ; +}#endif static vm_fault_t vvar_fault (const struct vm_special_mapping *sm,struct vm_area_struct *vma,struct vm_fault *vmf) { -if (vmf->pgoff ==0 ) -return vmf_insert_pfn (vma, vmf->address, -sym_to_pfn (vdso_data)); -return VM_FAULT_SIGBUS ; +struct page *timens_page =find_timens_vvar_page (vma); +unsigned long pfn; + +switch (vmf->pgoff) { +case VVAR_DATA_PAGE_OFFSET : +if (timens_page) + pfn =page_to_pfn (timens_page); +else + pfn =sym_to_pfn (vdso_data); +break ; +#ifdef CONFIG_TIME_NS +case VVAR_TIMENS_PAGE_OFFSET : +/* +* If a task belongs to a time namespace then a namespace +* specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and +* the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET +* offset. +* See also the comment near timens_setup_vdso_data(). +*/ +if (!timens_page) +return VM_FAULT_SIGBUS ; + pfn =sym_to_pfn (vdso_data); +break ; +#endif /* CONFIG_TIME_NS */ +default : +return VM_FAULT_SIGBUS ; + } + +return vmf_insert_pfn (vma, vmf->address, pfn); } static int __setup_additional_pages(enum vdso_abi abi,
Signature · 1 line
2.24.1
PATCH v4 5/6 arm64/vdso: Restrict splitting VVAR VMA
In reply to Andrei VaginForbid splitting VVAR VMA resulting in a stricter ABI and reducing the amount of corner-cases to consider while working further on VDSO time namespace support.
As the offset from timens to VVAR page is computed compile-time, the pages in VVAR should stay together and not being partically mremap()'ed.
--- arch/arm64/kernel/vdso.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/arm64/kernel/vdso.c +13 -0
@@ -226,6 +226,17 @@ static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,return vmf_insert_pfn (vma, vmf->address, pfn); } +static int vvar_mremap (const struct vm_special_mapping *sm, +struct vm_area_struct *new_vma) +{ +unsigned long new_size = new_vma->vm_end - new_vma->vm_start; + +if (new_size !=VVAR_NR_PAGES *PAGE_SIZE ) +return -EINVAL ; + +return 0 ; +} + static int __setup_additional_pages(enum vdso_abi abi, struct mm_struct *mm, struct linux_binprm *bprm,
@@ -308,6 +319,7 @@ static struct vm_special_mapping aarch32_vdso_maps[] = { [AA32_MAP_VVAR] = { .name = "[vvar]", .fault = vvar_fault, + .mremap = vvar_mremap, }, [AA32_MAP_VDSO] = { .name = "[vdso]",
@@ -476,6 +488,7 @@ static struct vm_special_mapping aarch64_vdso_maps[] __ro_after_init = { [AA64_MAP_VVAR] = { .name = "[vvar]", .fault = vvar_fault, + .mremap = vvar_mremap, }, [AA64_MAP_VDSO] = { .name = "[vdso]",
Signature · 1 line
2.24.1
PATCH v4 6/6 arm64: enable time namespace support
In reply to Andrei VaginCONFIG_TIME_NS is dependes on GENERIC_VDSO_TIME_NS.
--- arch/arm64/Kconfig | 1 + 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +1 -0
@@ -118,6 +118,7 @@ config ARM64 select GENERIC_STRNLEN_USER select GENERIC_TIME_VSYSCALL select GENERIC_GETTIMEOFDAY + select GENERIC_VDSO_TIME_NS select HANDLE_DOMAIN_IRQ select HARDIRQS_SW_RESEND select HAVE_PCI
Signature · 1 line
2.24.1