From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CBB3C31E46 for ; Wed, 12 Jun 2019 19:27:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2741D215EA for ; Wed, 12 Jun 2019 19:27:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=arista.com header.i=@arista.com header.b="keqEN34J" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388941AbfFLT1z (ORCPT ); Wed, 12 Jun 2019 15:27:55 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:46040 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728850AbfFLT04 (ORCPT ); Wed, 12 Jun 2019 15:26:56 -0400 Received: by mail-wr1-f68.google.com with SMTP id f9so18103677wre.12 for ; Wed, 12 Jun 2019 12:26:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KpgdAMMCHnPQrDnLWhMZsDPSXUfV6qkEmWfI7FzOlUQ=; b=keqEN34JtTtVA7NKymgP2DYb/nFo9R+KJ9voJ+K1mUldzJgJNdwdmfRFevkx3wbtpQ y+Z5h2YHYMTjI9RXifQ3Bz/iSKaJz0wuKmf4g60pyj6dJlt+qK+u3akwpE/HN+9l+TYH agKbQaN4gwFB5g65iMOmWJxyxwl+SkzNXpCmytzc5dC+miFRJod47pkvdP+0Ll45Js9Q vgPkatnJslN5IedjjLpV2IY6sRQx7GmPqakvVlcsc/bJiI7/w9A/Es0NteCl+SOVI79C lCsCAk3OdZhsklHsd9mn8zBQKbrgpreITVC+cJprWx9zdla4GgGN+E40qMK65hKT7kcX od/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KpgdAMMCHnPQrDnLWhMZsDPSXUfV6qkEmWfI7FzOlUQ=; b=WCzSQ9Ho/wIKyJaMGfZDTDLx/sA/7JCHYl3+oBJ9bJS3H1euviEuH37RFAxwzpSFdo MAIc8Tl4SNYVNlrxGJw1PLmAzbrdOMkBRCDjbDkSsbcKnTyGEZLIx53I9hDZHlBoh6jA CXh3WW4NAXrc8t6sJv9MU6DKqPgAlTE5M5T3IVuxM+nr1Erpp7NoujU5S5KNVS0wWcVd cZZtlPEEyonG6oPqQZGFs0Z8y4QGx+zHUlJ5xirgNIDH3R+r4KRJEbZ+hD9G2KehTpUM Hmw3TkO83/hbgp5P0k8iODda1xhJVK5hrBK0sRQxXrLKKkuC/NwBQnhb0XfRuLzPeg0O 6Utg== X-Gm-Message-State: APjAAAXxaCbfxX/NTX6QzhVDr4QQ7awSVZWLrGyXHtppIgSOV0aN4luQ PTy285b4PRylU+feyUIokckdGQIHcNs= X-Google-Smtp-Source: APXvYqw3oH/hLJ/mRBSulnvkir8RqsRXYTcoktY1T8F1V0GbvrLnAvvPebR0hcKcDXuF6mscRQCoHg== X-Received: by 2002:a5d:52cc:: with SMTP id r12mr16812548wrv.132.1560367614492; Wed, 12 Jun 2019 12:26:54 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id r5sm612526wrg.10.2019.06.12.12.26.53 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 12 Jun 2019 12:26:53 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov , Adrian Reber , Andrei Vagin , Andy Lutomirski , Arnd Bergmann , Christian Brauner , Cyrill Gorcunov , Dmitry Safonov <0x7f454c46@gmail.com>, "Eric W. Biederman" , "H. Peter Anvin" , Ingo Molnar , Jann Horn , Jeff Dike , Oleg Nesterov , Pavel Emelyanov , Shuah Khan , Thomas Gleixner , Vincenzo Frascino , containers@lists.linux-foundation.org, criu@openvz.org, linux-api@vger.kernel.org, x86@kernel.org, Andrei Vagin Subject: [PATCHv4 17/28] x86/vdso: Switch image on setns()/unshare()/clone() Date: Wed, 12 Jun 2019 20:26:16 +0100 Message-Id: <20190612192628.23797-18-dima@arista.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190612192628.23797-1-dima@arista.com> References: <20190612192628.23797-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As it has been discussed on timens RFC, adding a new conditional branch `if (inside_time_ns)` on VDSO for all processes is undesirable. It will add a penalty for everybody as branch predictor may mispredict the jump. Also there are instruction cache lines wasted on cmp/jmp. Those effects of introducing time namespace are very much unwanted having in mind how much work have been spent on micro-optimisation vdso code. Addressing those problems, there are two versions of VDSO's .so: for host tasks (without any penalty) and for processes inside of time namespace with clk_to_ns() that subtracts offsets from host's time. Whenever a user does setns()/unshare() or clone() with CLONE_TIMENS, change VDSO image in mm and zap existing VVAR/VDSO page tables. They will be re-faulted with corresponding image and VVAR offsets. Co-developed-by: Andrei Vagin Signed-off-by: Andrei Vagin Signed-off-by: Dmitry Safonov --- arch/x86/entry/vdso/vma.c | 28 ++++++++++++++++++++++++++++ arch/x86/include/asm/vdso.h | 1 + kernel/time_namespace.c | 11 +++++++++++ 3 files changed, 40 insertions(+) diff --git a/arch/x86/entry/vdso/vma.c b/arch/x86/entry/vdso/vma.c index cc06c6b70167..3ed5bf4932af 100644 --- a/arch/x86/entry/vdso/vma.c +++ b/arch/x86/entry/vdso/vma.c @@ -25,6 +25,7 @@ #include #include #include +#include #if defined(CONFIG_X86_64) unsigned int __read_mostly vdso64_enabled = 1; @@ -266,6 +267,33 @@ static const struct vm_special_mapping vvar_mapping = { .mremap = vvar_mremap, }; +#ifdef CONFIG_TIME_NS +int vdso_join_timens(struct task_struct *task) +{ + struct mm_struct *mm = task->mm; + struct vm_area_struct *vma; + + if (down_write_killable(&mm->mmap_sem)) + 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, &vvar_mapping) || + vma_is_special_mapping(vma, &vdso_mapping)) + zap_page_range(vma, vma->vm_start, size); + } + + up_write(&mm->mmap_sem); + return 0; +} +#else /* CONFIG_TIME_NS */ +int vdso_join_timens(struct task_struct *task) +{ + return -ENXIO; +} +#endif + /* * Add vdso and vvar mappings to current process. * @image - blob to map diff --git a/arch/x86/include/asm/vdso.h b/arch/x86/include/asm/vdso.h index 03f468c63a24..ccf89dedd04f 100644 --- a/arch/x86/include/asm/vdso.h +++ b/arch/x86/include/asm/vdso.h @@ -45,6 +45,7 @@ extern struct vdso_image vdso_image_32; extern void __init init_vdso_image(struct vdso_image *image); extern int map_vdso_once(const struct vdso_image *image, unsigned long addr); +extern int vdso_join_timens(struct task_struct *task); #endif /* __ASSEMBLER__ */ diff --git a/kernel/time_namespace.c b/kernel/time_namespace.c index b3cffdf2635c..2a2cab14ac29 100644 --- a/kernel/time_namespace.c +++ b/kernel/time_namespace.c @@ -14,6 +14,7 @@ #include #include #include +#include ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim, struct timens_offsets *ns_offsets) { @@ -182,11 +183,16 @@ static void timens_put(struct ns_common *ns) static int timens_install(struct nsproxy *nsproxy, struct ns_common *new) { struct time_namespace *ns = to_time_ns(new); + int ret; if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) || !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) return -EPERM; + ret = vdso_join_timens(current); + if (ret) + return ret; + get_time_ns(ns); get_time_ns(ns); put_time_ns(nsproxy->time_ns); @@ -201,10 +207,15 @@ int timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk) { struct ns_common *nsc = &nsproxy->time_ns_for_children->ns; struct time_namespace *ns = to_time_ns(nsc); + int ret; if (nsproxy->time_ns == nsproxy->time_ns_for_children) return 0; + ret = vdso_join_timens(tsk); + if (ret) + return ret; + get_time_ns(ns); put_time_ns(nsproxy->time_ns); nsproxy->time_ns = ns; -- 2.22.0