Skip to content

Commit aa5e65d

Browse files
seehearfeelchenhuacai
authored andcommitted
LoongArch: Add support to clone a time namespace
We can see that "Time namespaces are not supported" on LoongArch: (1) clone3 test # cd tools/testing/selftests/clone3 && make && ./clone3 ... # Time namespaces are not supported ok 18 # SKIP Skipping clone3() with CLONE_NEWTIME # Totals: pass:17 fail:0 xfail:0 xpass:0 skip:1 error:0 (2) timens test # cd tools/testing/selftests/timens && make && ./timens ... 1..0 # SKIP Time namespaces are not supported On LoongArch the current kernel does not support CONFIG_TIME_NS which depends on GENERIC_VDSO_TIME_NS, select GENERIC_VDSO_TIME_NS to enable CONFIG_TIME_NS to build kernel/time/namespace.c. Additionally, it needs to define some arch-dependent functions for the timens, such as __arch_get_timens_vdso_data(), arch_get_vdso_data() and vdso_join_timens(). At the same time, modify the layout of vvar to use one page size for generic vdso data, expand another page size for timens vdso data and assign LOONGARCH_VDSO_DATA_SIZE (maybe exceeds a page size if expand in the future) for loongarch vdso data, at last add the callback function vvar_fault() and modify stack_top(). With this patch under CONFIG_TIME_NS: (1) clone3 test # cd tools/testing/selftests/clone3 && make && ./clone3 ... ok 18 [739] Result (0) matches expectation (0) # Totals: pass:18 fail:0 xfail:0 xpass:0 skip:0 error:0 (2) timens test # cd tools/testing/selftests/timens && make && ./timens ... # Totals: pass:10 fail:0 xfail:0 xpass:0 skip:0 error:0 Signed-off-by: Tiezhu Yang <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 65eea6b commit aa5e65d

File tree

7 files changed

+121
-24
lines changed

7 files changed

+121
-24
lines changed

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ config LOONGARCH
8282
select GENERIC_SCHED_CLOCK
8383
select GENERIC_SMP_IDLE_THREAD
8484
select GENERIC_TIME_VSYSCALL
85+
select GENERIC_VDSO_TIME_NS
8586
select GPIOLIB
8687
select HAS_IOPORT
8788
select HAVE_ARCH_AUDITSYSCALL

arch/loongarch/include/asm/page.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ typedef struct { unsigned long pgprot; } pgprot_t;
8181
#define __va(x) ((void *)((unsigned long)(x) + PAGE_OFFSET - PHYS_OFFSET))
8282

8383
#define pfn_to_kaddr(pfn) __va((pfn) << PAGE_SHIFT)
84+
#define sym_to_pfn(x) __phys_to_pfn(__pa_symbol(x))
8485

8586
#define virt_to_pfn(kaddr) PFN_DOWN(PHYSADDR(kaddr))
8687
#define virt_to_page(kaddr) pfn_to_page(virt_to_pfn(kaddr))

arch/loongarch/include/asm/vdso/gettimeofday.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,16 @@ static inline bool loongarch_vdso_hres_capable(void)
9191

9292
static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
9393
{
94-
return get_vdso_data();
94+
return (const struct vdso_data *)get_vdso_data();
9595
}
9696

97+
#ifdef CONFIG_TIME_NS
98+
static __always_inline
99+
const struct vdso_data *__arch_get_timens_vdso_data(const struct vdso_data *vd)
100+
{
101+
return (const struct vdso_data *)(get_vdso_data() + VVAR_TIMENS_PAGE_OFFSET * PAGE_SIZE);
102+
}
103+
#endif
97104
#endif /* !__ASSEMBLY__ */
98105

99106
#endif /* __ASM_VDSO_GETTIMEOFDAY_H */

arch/loongarch/include/asm/vdso/vdso.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,33 @@ struct vdso_pcpu_data {
1616

1717
struct loongarch_vdso_data {
1818
struct vdso_pcpu_data pdata[NR_CPUS];
19-
struct vdso_data data[CS_BASES]; /* Arch-independent data */
2019
};
2120

22-
#define VDSO_DATA_SIZE PAGE_ALIGN(sizeof(struct loongarch_vdso_data))
21+
/*
22+
* The layout of vvar:
23+
*
24+
* high
25+
* +---------------------+--------------------------+
26+
* | loongarch vdso data | LOONGARCH_VDSO_DATA_SIZE |
27+
* +---------------------+--------------------------+
28+
* | time-ns vdso data | PAGE_SIZE |
29+
* +---------------------+--------------------------+
30+
* | generic vdso data | PAGE_SIZE |
31+
* +---------------------+--------------------------+
32+
* low
33+
*/
34+
#define LOONGARCH_VDSO_DATA_SIZE PAGE_ALIGN(sizeof(struct loongarch_vdso_data))
35+
#define LOONGARCH_VDSO_DATA_PAGES (LOONGARCH_VDSO_DATA_SIZE >> PAGE_SHIFT)
36+
37+
enum vvar_pages {
38+
VVAR_GENERIC_PAGE_OFFSET,
39+
VVAR_TIMENS_PAGE_OFFSET,
40+
VVAR_LOONGARCH_PAGES_START,
41+
VVAR_LOONGARCH_PAGES_END = VVAR_LOONGARCH_PAGES_START + LOONGARCH_VDSO_DATA_PAGES - 1,
42+
VVAR_NR_PAGES,
43+
};
44+
45+
#define VVAR_SIZE (VVAR_NR_PAGES << PAGE_SHIFT)
2346

2447
static inline unsigned long get_vdso_base(void)
2548
{
@@ -34,10 +57,9 @@ static inline unsigned long get_vdso_base(void)
3457
return addr;
3558
}
3659

37-
static inline const struct vdso_data *get_vdso_data(void)
60+
static inline unsigned long get_vdso_data(void)
3861
{
39-
return (const struct vdso_data *)(get_vdso_base()
40-
- VDSO_DATA_SIZE + SMP_CACHE_BYTES * NR_CPUS);
62+
return get_vdso_base() - VVAR_SIZE;
4163
}
4264

4365
#endif /* __ASSEMBLY__ */

arch/loongarch/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ unsigned long stack_top(void)
285285

286286
/* Space for the VDSO & data page */
287287
top -= PAGE_ALIGN(current->thread.vdso->size);
288-
top -= PAGE_SIZE;
288+
top -= VVAR_SIZE;
289289

290290
/* Space to randomize the VDSO base */
291291
if (current->flags & PF_RANDOMIZE)

arch/loongarch/kernel/vdso.c

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/random.h>
1515
#include <linux/sched.h>
1616
#include <linux/slab.h>
17+
#include <linux/time_namespace.h>
1718
#include <linux/timekeeper_internal.h>
1819

1920
#include <asm/page.h>
@@ -26,12 +27,17 @@ extern char vdso_start[], vdso_end[];
2627

2728
/* Kernel-provided data used by the VDSO. */
2829
static union {
29-
u8 page[VDSO_DATA_SIZE];
30+
u8 page[PAGE_SIZE];
31+
struct vdso_data data[CS_BASES];
32+
} generic_vdso_data __page_aligned_data;
33+
34+
static union {
35+
u8 page[LOONGARCH_VDSO_DATA_SIZE];
3036
struct loongarch_vdso_data vdata;
3137
} loongarch_vdso_data __page_aligned_data;
3238

3339
static struct page *vdso_pages[] = { NULL };
34-
struct vdso_data *vdso_data = loongarch_vdso_data.vdata.data;
40+
struct vdso_data *vdso_data = generic_vdso_data.data;
3541
struct vdso_pcpu_data *vdso_pdata = loongarch_vdso_data.vdata.pdata;
3642

3743
static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
@@ -41,6 +47,43 @@ static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struc
4147
return 0;
4248
}
4349

50+
static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
51+
struct vm_area_struct *vma, struct vm_fault *vmf)
52+
{
53+
unsigned long pfn;
54+
struct page *timens_page = find_timens_vvar_page(vma);
55+
56+
switch (vmf->pgoff) {
57+
case VVAR_GENERIC_PAGE_OFFSET:
58+
if (!timens_page)
59+
pfn = sym_to_pfn(vdso_data);
60+
else
61+
pfn = page_to_pfn(timens_page);
62+
break;
63+
#ifdef CONFIG_TIME_NS
64+
case VVAR_TIMENS_PAGE_OFFSET:
65+
/*
66+
* If a task belongs to a time namespace then a namespace specific
67+
* VVAR is mapped with the VVAR_GENERIC_PAGE_OFFSET and the real
68+
* VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET offset.
69+
* See also the comment near timens_setup_vdso_data().
70+
*/
71+
if (!timens_page)
72+
return VM_FAULT_SIGBUS;
73+
else
74+
pfn = sym_to_pfn(vdso_data);
75+
break;
76+
#endif /* CONFIG_TIME_NS */
77+
case VVAR_LOONGARCH_PAGES_START ... VVAR_LOONGARCH_PAGES_END:
78+
pfn = sym_to_pfn(&loongarch_vdso_data) + vmf->pgoff - VVAR_LOONGARCH_PAGES_START;
79+
break;
80+
default:
81+
return VM_FAULT_SIGBUS;
82+
}
83+
84+
return vmf_insert_pfn(vma, vmf->address, pfn);
85+
}
86+
4487
struct loongarch_vdso_info vdso_info = {
4588
.vdso = vdso_start,
4689
.size = PAGE_SIZE,
@@ -51,6 +94,7 @@ struct loongarch_vdso_info vdso_info = {
5194
},
5295
.data_mapping = {
5396
.name = "[vvar]",
97+
.fault = vvar_fault,
5498
},
5599
.offset_sigreturn = vdso_offset_sigreturn,
56100
};
@@ -73,6 +117,37 @@ static int __init init_vdso(void)
73117
}
74118
subsys_initcall(init_vdso);
75119

120+
#ifdef CONFIG_TIME_NS
121+
struct vdso_data *arch_get_vdso_data(void *vvar_page)
122+
{
123+
return (struct vdso_data *)(vvar_page);
124+
}
125+
126+
/*
127+
* The vvar mapping contains data for a specific time namespace, so when a
128+
* task changes namespace we must unmap its vvar data for the old namespace.
129+
* Subsequent faults will map in data for the new namespace.
130+
*
131+
* For more details see timens_setup_vdso_data().
132+
*/
133+
int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
134+
{
135+
struct mm_struct *mm = task->mm;
136+
struct vm_area_struct *vma;
137+
138+
VMA_ITERATOR(vmi, mm, 0);
139+
140+
mmap_read_lock(mm);
141+
for_each_vma(vmi, vma) {
142+
if (vma_is_special_mapping(vma, &vdso_info.data_mapping))
143+
zap_vma_pages(vma);
144+
}
145+
mmap_read_unlock(mm);
146+
147+
return 0;
148+
}
149+
#endif
150+
76151
static unsigned long vdso_base(void)
77152
{
78153
unsigned long base = STACK_TOP;
@@ -88,7 +163,7 @@ static unsigned long vdso_base(void)
88163
int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
89164
{
90165
int ret;
91-
unsigned long vvar_size, size, data_addr, vdso_addr;
166+
unsigned long size, data_addr, vdso_addr;
92167
struct mm_struct *mm = current->mm;
93168
struct vm_area_struct *vma;
94169
struct loongarch_vdso_info *info = current->thread.vdso;
@@ -100,32 +175,23 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
100175
* Determine total area size. This includes the VDSO data itself
101176
* and the data pages.
102177
*/
103-
vvar_size = VDSO_DATA_SIZE;
104-
size = vvar_size + info->size;
178+
size = VVAR_SIZE + info->size;
105179

106180
data_addr = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
107181
if (IS_ERR_VALUE(data_addr)) {
108182
ret = data_addr;
109183
goto out;
110184
}
111-
vdso_addr = data_addr + VDSO_DATA_SIZE;
112185

113-
vma = _install_special_mapping(mm, data_addr, vvar_size,
114-
VM_READ | VM_MAYREAD,
186+
vma = _install_special_mapping(mm, data_addr, VVAR_SIZE,
187+
VM_READ | VM_MAYREAD | VM_PFNMAP,
115188
&info->data_mapping);
116189
if (IS_ERR(vma)) {
117190
ret = PTR_ERR(vma);
118191
goto out;
119192
}
120193

121-
/* Map VDSO data page. */
122-
ret = remap_pfn_range(vma, data_addr,
123-
virt_to_phys(&loongarch_vdso_data) >> PAGE_SHIFT,
124-
vvar_size, PAGE_READONLY);
125-
if (ret)
126-
goto out;
127-
128-
/* Map VDSO code page. */
194+
vdso_addr = data_addr + VVAR_SIZE;
129195
vma = _install_special_mapping(mm, vdso_addr, info->size,
130196
VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
131197
&info->code_mapping);

arch/loongarch/vdso/vgetcpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static __always_inline int read_cpu_id(void)
2121

2222
static __always_inline const struct vdso_pcpu_data *get_pcpu_data(void)
2323
{
24-
return (struct vdso_pcpu_data *)(get_vdso_base() - VDSO_DATA_SIZE);
24+
return (struct vdso_pcpu_data *)(get_vdso_data() + VVAR_LOONGARCH_PAGES_START * PAGE_SIZE);
2525
}
2626

2727
extern

0 commit comments

Comments
 (0)