From patchwork Wed Nov 30 11:53:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 27762 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp879185wrr; Wed, 30 Nov 2022 04:00:51 -0800 (PST) X-Google-Smtp-Source: AA0mqf4/qnbN5JY1WjJAxCFBdNQmgMQefYzNV3F8gGUlNfMyLLIiLRio69Er5bg2HxKLOtSsK6Gq X-Received: by 2002:a17:906:1c8c:b0:7c0:a864:5b0f with SMTP id g12-20020a1709061c8c00b007c0a8645b0fmr87011ejh.105.1669809651476; Wed, 30 Nov 2022 04:00:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669809651; cv=none; d=google.com; s=arc-20160816; b=lM7Z7suQd1E8oPgp6qV4/B1gs2a8GcWh2bDvBrT+fTLk8clax0rWUQg9SHpTJbSDFI TonxfVajjR1qIiwJ9v2hrgPa5CTu4V/FRbapyaBlfrKLzhfkV2Ll6rKODx9JQQhwuK3+ +pDRLl1+HU0iKyhtxFDMnknDqZ1mgZDinPm3A0LmUvA04H19MbQtWkbhNeCtfoxbJJbV 0+xO5KwZSAHaRSycOKDmpXEygedCjScciG9QCtDXMa0thsOOZwqeUqnQdwf9z/1BFweY 9FHp2wQQF6ItpL9UArnk5bitb6jBxipByVNK8UwAiWCFNLRr0PXCFOJg9iocEy92FoLC qWCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=BJ0mV3lQZzVQnN0+KPldbno37+5NtwHZY0AFSgovrd4=; b=feaTd7TxyCWL6VBHsSAf8rbQCguhQorRzR0BldeFcHEImLl4SluXkyfEXrN/97A+P2 OlcAKXO8yi8RHEBz7uFpAoxJPL6jZgOG8aGGgKNujLaOZRg5PkpDK/uiMwWAXQrCG29d MpKVgZZX9SqrEQwvQBnTqYyBcjgs9tswV8evHaJSLsIuzWQ5ffV+hDbRYr186svvQV0V b5E+W9Wb6JlB8L3UYP4YpRvDUzGXid9hTYC2Wk/j2C6Oil98AOVy6keyrUx6NKyNU1cD SMkWM4IYwADUymAPjgEgnscTUgRstttonrg3YKDWqDleu3Ja9LYDOQUSUxkfll74DsSK l+/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=acIsg6LX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w21-20020a170906131500b0078dc5b2b6c4si887947ejb.666.2022.11.30.04.00.26; Wed, 30 Nov 2022 04:00:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=acIsg6LX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234800AbiK3LyN (ORCPT + 99 others); Wed, 30 Nov 2022 06:54:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231607AbiK3Lxc (ORCPT ); Wed, 30 Nov 2022 06:53:32 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2B9F69332 for ; Wed, 30 Nov 2022 03:53:30 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id w15so13490326wrl.9 for ; Wed, 30 Nov 2022 03:53:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=BJ0mV3lQZzVQnN0+KPldbno37+5NtwHZY0AFSgovrd4=; b=acIsg6LXeJ3grXrgT2fD7j0JfSNVoLIvbrRJLjdEzR6jry8EaXgyZjlwUOnNEhGASs 3vzwYUfLR3NchW0Oa1255fRrEPBgdA1aU1RIOqszr8HfabBBBR4KV7gW+CDGoqXlyXxw ySS3NttuppAdUl9Gu2mk3TCgj3zHkfCZY3a2ygan3tG4Ihb1R2yyJJm/VeEvAEv1FRZq 1IOgWFLjgw6ahoPqeCHrRE2trqgP1UDBMECJsTa0MMKdZf5Nw2mM/7P52Y8EwG1As9aM Fc4sFFXrrFHMD1Hwx8k+ficGAyDZv6ONpjJi5NND7E9VLUk3nud2vgCtiQzk0+uhR5cc 47ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=BJ0mV3lQZzVQnN0+KPldbno37+5NtwHZY0AFSgovrd4=; b=rykcdrkXtN/Uu91lwOQ3FbfdXMPt9pWZXceOnjqe3l999HMQqHl8CULuCTlV3tHXhA 8VxMZHZFbBkITvqjFg4EYgeGlBKhDazL7rfTuGRTB2NAeQwWxYleJyHYEJFrdW2i6YfE dPmLf6jN7oKxSG3UxESc5nwe/1K9H8OcEOrSNEjx/wnIF2p8B3iAqaGmYHkCv8qV3ww4 YxPJ+psn6HP7elYgFFayKTabALT6R36Y1DizTPawLFPPbRYVovoGClBwHsV9Umqv4HBM 0tVlLG6eTrKcmZleWfordzGriKLqMAQteQV+tdmJDgHmtsOS4MYHryCRDDMmCZ6bIYxg qO7A== X-Gm-Message-State: ANoB5pmMn/dwje57e/z8WY+HhkQ80+6riNjkV06+IAOC/Y2c+1YLAIwv 375D/Z1DVmPbAyLBT1KaWITMqQ== X-Received: by 2002:a5d:56d2:0:b0:236:cdf8:1e3f with SMTP id m18-20020a5d56d2000000b00236cdf81e3fmr29701721wrw.80.1669809209160; Wed, 30 Nov 2022 03:53:29 -0800 (PST) Received: from localhost ([2a00:79e0:9d:4:a4e3:afa8:e6c2:32d0]) by smtp.gmail.com with ESMTPSA id u15-20020a05600c210f00b003cf7292c553sm1686691wml.13.2022.11.30.03.53.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Nov 2022 03:53:28 -0800 (PST) From: Jann Horn To: Thomas Gleixner Cc: Andrei Vagin , linux-kernel@vger.kernel.org Subject: [PATCH] time/namespace: Refactor copy-pasted helper into one copy Date: Wed, 30 Nov 2022 12:53:20 +0100 Message-Id: <20221130115320.2918447-1-jannh@google.com> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog MIME-Version: 1.0 X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750922324907432473?= X-GMAIL-MSGID: =?utf-8?q?1750922324907432473?= find_timens_vvar_page() is not arch-specific, as can be seen from how all five per-architecture versions of it are the same. (arm64, powerpc and riscv are exactly the same; x86 and s390 have two characters difference inside a comment, less blank lines, and mark the !CONFIG_TIME_NS version as inline.) Refactor the five copies into a central copy in kernel/time/namespace.c. Signed-off-by: Jann Horn --- arch/arm64/kernel/vdso.c | 22 ---------------------- arch/powerpc/kernel/vdso.c | 22 ---------------------- arch/riscv/kernel/vdso.c | 22 ---------------------- arch/s390/kernel/vdso.c | 20 -------------------- arch/x86/entry/vdso/vma.c | 23 ----------------------- include/linux/time_namespace.h | 6 ++++++ kernel/time/namespace.c | 20 ++++++++++++++++++++ 7 files changed, 26 insertions(+), 109 deletions(-) base-commit: ca57f02295f188d6c65ec02202402979880fa6d8 diff --git a/arch/arm64/kernel/vdso.c b/arch/arm64/kernel/vdso.c index 99ae81ab91a74..e59a32aa0c49d 100644 --- a/arch/arm64/kernel/vdso.c +++ b/arch/arm64/kernel/vdso.c @@ -151,28 +151,6 @@ int vdso_join_timens(struct task_struct *task, struct time_namespace *ns) mmap_read_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 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, diff --git a/arch/powerpc/kernel/vdso.c b/arch/powerpc/kernel/vdso.c index 4abc019497020..507f8228f983b 100644 --- a/arch/powerpc/kernel/vdso.c +++ b/arch/powerpc/kernel/vdso.c @@ -129,28 +129,6 @@ int vdso_join_timens(struct task_struct *task, struct time_namespace *ns) 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 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, diff --git a/arch/riscv/kernel/vdso.c b/arch/riscv/kernel/vdso.c index 123d05255fcfa..e410275918ac4 100644 --- a/arch/riscv/kernel/vdso.c +++ b/arch/riscv/kernel/vdso.c @@ -137,28 +137,6 @@ int vdso_join_timens(struct task_struct *task, struct time_namespace *ns) mmap_read_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 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, diff --git a/arch/s390/kernel/vdso.c b/arch/s390/kernel/vdso.c index 3105ca5bd4701..d6df7169c01f2 100644 --- a/arch/s390/kernel/vdso.c +++ b/arch/s390/kernel/vdso.c @@ -44,21 +44,6 @@ struct vdso_data *arch_get_vdso_data(void *vvar_page) return (struct vdso_data *)(vvar_page); } -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; -} - /* * 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 @@ -84,11 +69,6 @@ int vdso_join_timens(struct task_struct *task, struct time_namespace *ns) mmap_read_unlock(mm); return 0; } -#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, diff --git a/arch/x86/entry/vdso/vma.c b/arch/x86/entry/vdso/vma.c index 311eae30e0894..6b36485054e8a 100644 --- a/arch/x86/entry/vdso/vma.c +++ b/arch/x86/entry/vdso/vma.c @@ -98,24 +98,6 @@ static int vdso_mremap(const struct vm_special_mapping *sm, } #ifdef CONFIG_TIME_NS -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; -} - /* * 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 @@ -140,11 +122,6 @@ int vdso_join_timens(struct task_struct *task, struct time_namespace *ns) return 0; } -#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, diff --git a/include/linux/time_namespace.h b/include/linux/time_namespace.h index 3146f1c056c98..bb9d3f5542f8e 100644 --- a/include/linux/time_namespace.h +++ b/include/linux/time_namespace.h @@ -45,6 +45,7 @@ struct time_namespace *copy_time_ns(unsigned long flags, void free_time_ns(struct time_namespace *ns); void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk); struct vdso_data *arch_get_vdso_data(void *vvar_page); +struct page *find_timens_vvar_page(struct vm_area_struct *vma); static inline void put_time_ns(struct time_namespace *ns) { @@ -141,6 +142,11 @@ static inline void timens_on_fork(struct nsproxy *nsproxy, return; } +static inline struct page *find_timens_vvar_page(struct vm_area_struct *vma) +{ + return NULL; +} + static inline void timens_add_monotonic(struct timespec64 *ts) { } static inline void timens_add_boottime(struct timespec64 *ts) { } diff --git a/kernel/time/namespace.c b/kernel/time/namespace.c index aec832801c26c..761c0ada5142a 100644 --- a/kernel/time/namespace.c +++ b/kernel/time/namespace.c @@ -192,6 +192,26 @@ static void timens_setup_vdso_data(struct vdso_data *vdata, offset[CLOCK_BOOTTIME_ALARM] = boottime; } +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; +} + + + /* * Protects possibly multiple offsets writers racing each other * and tasks entering the namespace.