Skip to content

Commit afaa7b5

Browse files
0x7f454c46KAGA-KOKO
authored andcommitted
time: Allocate per-timens vvar page
VDSO support for Time namespace needs to set up a page with the same layout as VVAR. That timens page will be placed on position of VVAR page inside namespace. That page contains time namespace clock offsets and it 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. Allocate the timens page during namespace creation. Setup the offsets when the first task enters the ns and freeze them to guarantee the pace of monotonic/boottime clocks and to avoid breakage of applications. The design decision is to have a global offset_lock which is used during namespace offsets setup and to freeze offsets when the first task joins the new time namespace. That is better in terms of memory usage compared to having a per namespace mutex that's used only during the setup period. Suggested-by: Andy Lutomirski <luto@kernel.org> Based-on-work-by: Thomas Gleixner <tglx@linutronix.de> Co-developed-by: Andrei Vagin <avagin@gmail.com> Signed-off-by: Andrei Vagin <avagin@gmail.com> Signed-off-by: Dmitry Safonov <dima@arista.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Link: https://lore.kernel.org/r/20191112012724.250792-24-dima@arista.com
1 parent 550a77a commit afaa7b5

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎include/linux/time_namespace.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ struct time_namespace {
2323
struct ucounts *ucounts;
2424
struct ns_common ns;
2525
struct timens_offsets offsets;
26+
struct page *vvar_page;
27+
/* If set prevents changing offsets after any task joined namespace. */
28+
bool frozen_offsets;
2629
} __randomize_layout;
2730

2831
extern struct time_namespace init_time_ns;

‎kernel/time/namespace.c‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <linux/err.h>
1717
#include <linux/mm.h>
1818

19+
#include <vdso/datapage.h>
20+
1921
ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
2022
struct timens_offsets *ns_offsets)
2123
{
@@ -90,16 +92,23 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
9092

9193
kref_init(&ns->kref);
9294

95+
ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
96+
if (!ns->vvar_page)
97+
goto fail_free;
98+
9399
err = ns_alloc_inum(&ns->ns);
94100
if (err)
95-
goto fail_free;
101+
goto fail_free_page;
96102

97103
ns->ucounts = ucounts;
98104
ns->ns.ops = &timens_operations;
99105
ns->user_ns = get_user_ns(user_ns);
100106
ns->offsets = old_ns->offsets;
107+
ns->frozen_offsets = false;
101108
return ns;
102109

110+
fail_free_page:
111+
__free_page(ns->vvar_page);
103112
fail_free:
104113
kfree(ns);
105114
fail_dec:
@@ -128,6 +137,93 @@ struct time_namespace *copy_time_ns(unsigned long flags,
128137
return clone_time_ns(user_ns, old_ns);
129138
}
130139

140+
static struct timens_offset offset_from_ts(struct timespec64 off)
141+
{
142+
struct timens_offset ret;
143+
144+
ret.sec = off.tv_sec;
145+
ret.nsec = off.tv_nsec;
146+
147+
return ret;
148+
}
149+
150+
/*
151+
* A time namespace VVAR page has the same layout as the VVAR page which
152+
* contains the system wide VDSO data.
153+
*
154+
* For a normal task the VVAR pages are installed in the normal ordering:
155+
* VVAR
156+
* PVCLOCK
157+
* HVCLOCK
158+
* TIMENS <- Not really required
159+
*
160+
* Now for a timens task the pages are installed in the following order:
161+
* TIMENS
162+
* PVCLOCK
163+
* HVCLOCK
164+
* VVAR
165+
*
166+
* The check for vdso_data->clock_mode is in the unlikely path of
167+
* the seq begin magic. So for the non-timens case most of the time
168+
* 'seq' is even, so the branch is not taken.
169+
*
170+
* If 'seq' is odd, i.e. a concurrent update is in progress, the extra check
171+
* for vdso_data->clock_mode is a non-issue. The task is spin waiting for the
172+
* update to finish and for 'seq' to become even anyway.
173+
*
174+
* Timens page has vdso_data->clock_mode set to VCLOCK_TIMENS which enforces
175+
* the time namespace handling path.
176+
*/
177+
static void timens_setup_vdso_data(struct vdso_data *vdata,
178+
struct time_namespace *ns)
179+
{
180+
struct timens_offset *offset = vdata->offset;
181+
struct timens_offset monotonic = offset_from_ts(ns->offsets.monotonic);
182+
struct timens_offset boottime = offset_from_ts(ns->offsets.boottime);
183+
184+
vdata->seq = 1;
185+
vdata->clock_mode = VCLOCK_TIMENS;
186+
offset[CLOCK_MONOTONIC] = monotonic;
187+
offset[CLOCK_MONOTONIC_RAW] = monotonic;
188+
offset[CLOCK_MONOTONIC_COARSE] = monotonic;
189+
offset[CLOCK_BOOTTIME] = boottime;
190+
offset[CLOCK_BOOTTIME_ALARM] = boottime;
191+
}
192+
193+
/*
194+
* Protects possibly multiple offsets writers racing each other
195+
* and tasks entering the namespace.
196+
*/
197+
static DEFINE_MUTEX(offset_lock);
198+
199+
static void timens_set_vvar_page(struct task_struct *task,
200+
struct time_namespace *ns)
201+
{
202+
struct vdso_data *vdata;
203+
unsigned int i;
204+
205+
if (ns == &init_time_ns)
206+
return;
207+
208+
/* Fast-path, taken by every task in namespace except the first. */
209+
if (likely(ns->frozen_offsets))
210+
return;
211+
212+
mutex_lock(&offset_lock);
213+
/* Nothing to-do: vvar_page has been already initialized. */
214+
if (ns->frozen_offsets)
215+
goto out;
216+
217+
ns->frozen_offsets = true;
218+
vdata = arch_get_vdso_data(page_address(ns->vvar_page));
219+
220+
for (i = 0; i < CS_BASES; i++)
221+
timens_setup_vdso_data(&vdata[i], ns);
222+
223+
out:
224+
mutex_unlock(&offset_lock);
225+
}
226+
131227
void free_time_ns(struct kref *kref)
132228
{
133229
struct time_namespace *ns;
@@ -136,6 +232,7 @@ void free_time_ns(struct kref *kref)
136232
dec_time_namespaces(ns->ucounts);
137233
put_user_ns(ns->user_ns);
138234
ns_free_inum(&ns->ns);
235+
__free_page(ns->vvar_page);
139236
kfree(ns);
140237
}
141238

@@ -192,6 +289,8 @@ static int timens_install(struct nsproxy *nsproxy, struct ns_common *new)
192289
!ns_capable(current_user_ns(), CAP_SYS_ADMIN))
193290
return -EPERM;
194291

292+
timens_set_vvar_page(current, ns);
293+
195294
get_time_ns(ns);
196295
put_time_ns(nsproxy->time_ns);
197296
nsproxy->time_ns = ns;
@@ -211,6 +310,8 @@ int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk)
211310
if (nsproxy->time_ns == nsproxy->time_ns_for_children)
212311
return 0;
213312

313+
timens_set_vvar_page(tsk, ns);
314+
214315
get_time_ns(ns);
215316
put_time_ns(nsproxy->time_ns);
216317
nsproxy->time_ns = ns;
@@ -246,6 +347,7 @@ struct time_namespace init_time_ns = {
246347
.user_ns = &init_user_ns,
247348
.ns.inum = PROC_TIME_INIT_INO,
248349
.ns.ops = &timens_operations,
350+
.frozen_offsets = true,
249351
};
250352

251353
static int __init time_ns_init(void)

0 commit comments

Comments
 (0)