From: Andrei Vagin <[email protected]>

Introduce offsets for time namespace. They will contain adjustment
needed to convert clocks to/from host's.

Allocate one page for each time namespace that will be premapped into
userspace with vvar pages.

Signed-off-by: Andrei Vagin <[email protected]>
Signed-off-by: Dmitry Safonov <[email protected]>
---
 include/linux/time_namespace.h |  2 ++
 include/linux/timens_offsets.h |  8 ++++++++
 kernel/time_namespace.c        | 14 ++++++++++++--
 3 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/timens_offsets.h

diff --git a/include/linux/time_namespace.h b/include/linux/time_namespace.h
index bf98f35efe07..4960c54f1b33 100644
--- a/include/linux/time_namespace.h
+++ b/include/linux/time_namespace.h
@@ -8,6 +8,7 @@
 #include <linux/nsproxy.h>
 #include <linux/ns_common.h>
 #include <linux/err.h>
+#include <linux/timens_offsets.h>
 
 struct user_namespace;
 extern struct user_namespace init_user_ns;
@@ -17,6 +18,7 @@ struct time_namespace {
        struct user_namespace *user_ns;
        struct ucounts *ucounts;
        struct ns_common ns;
+       struct timens_offsets *offsets;
 } __randomize_layout;
 extern struct time_namespace init_time_ns;
 
diff --git a/include/linux/timens_offsets.h b/include/linux/timens_offsets.h
new file mode 100644
index 000000000000..7d7cb68ea778
--- /dev/null
+++ b/include/linux/timens_offsets.h
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_TIME_OFFSETS_H
+#define _LINUX_TIME_OFFSETS_H
+
+struct timens_offsets {
+};
+
+#endif
diff --git a/kernel/time_namespace.c b/kernel/time_namespace.c
index 902cd9c22159..a985529754b4 100644
--- a/kernel/time_namespace.c
+++ b/kernel/time_namespace.c
@@ -13,6 +13,7 @@
 #include <linux/user_namespace.h>
 #include <linux/proc_ns.h>
 #include <linux/sched/task.h>
+#include <linux/mm.h>
 
 static struct ucounts *inc_time_namespaces(struct user_namespace *ns)
 {
@@ -44,6 +45,7 @@ static struct time_namespace *clone_time_ns(struct 
user_namespace *user_ns,
 {
        struct time_namespace *ns;
        struct ucounts *ucounts;
+       struct page *page;
        int err;
 
        err = -ENOSPC;
@@ -56,15 +58,22 @@ static struct time_namespace *clone_time_ns(struct 
user_namespace *user_ns,
        if (!ns)
                goto fail_dec;
 
+       page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+       if (!page)
+               goto fail_free;
+       ns->offsets = page_address(page);
+       BUILD_BUG_ON(sizeof(*ns->offsets) > PAGE_SIZE);
+
        err = ns_alloc_inum(&ns->ns);
        if (err)
-               goto fail_free;
+               goto fail_page;
 
        ns->ucounts = ucounts;
        ns->ns.ops = &timens_operations;
        ns->user_ns = get_user_ns(user_ns);
        return ns;
-
+fail_page:
+       free_page((unsigned long)ns->offsets);
 fail_free:
        kfree(ns);
 fail_dec:
@@ -93,6 +102,7 @@ void free_time_ns(struct kref *kref)
        struct time_namespace *ns;
 
        ns = container_of(kref, struct time_namespace, kref);
+       free_page((unsigned long)ns->offsets);
        dec_time_namespaces(ns->ucounts);
        put_user_ns(ns->user_ns);
        ns_free_inum(&ns->ns);
-- 
2.13.6

Reply via email to