From patchwork Mon Nov 7 18:47:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 16637 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2230623wru; Mon, 7 Nov 2022 10:54:32 -0800 (PST) X-Google-Smtp-Source: AMsMyM5Cwz9ItgY6vbyYD3FqpJ+8o4y733hg0gyqFDFOhpXi379rNBfpBFeBZup5/moFfUo45RLw X-Received: by 2002:a05:6402:360d:b0:459:5f40:5b0a with SMTP id el13-20020a056402360d00b004595f405b0amr50333734edb.168.1667847272637; Mon, 07 Nov 2022 10:54:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667847272; cv=none; d=google.com; s=arc-20160816; b=AWHRBjMW5PdQ9NU/vUEQ2/MYKER2XzuDO1JzBYTjBrkp7WcYFiDlvmJaeYPmD3ibFJ POEEqXOwxGx9ZQPuMV68MYkP5KRlubs/6r0xTLOE3a4wOo/pgFURoQqCdlwmGJnl6nB6 mab/Cq41KD1tuQt80WXkxjLd6kH9+I3RG/X3Skr8K0k8608fJNLjMwtZ85sGFaeNu2fw xhcgt8ZzuhoJS+iwelz8aOspHprAAfg18oSC5toNVj/TV1dFkrT3jaURPpDDr2xOTkvD NMlFAja+FEfecUVSezvzVG7k8Cbp13E/PD8VQFCHK8Z0H0U/XFouoaSs4nx5zZZVqQUg HAuw== 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:to:from:dkim-signature; bh=Lj2j2IowLRClh2CPK+1bgXAKaDYVWiVJ2biJrtBrjjs=; b=xzaPCJ6//MOGJtjkqFvqC0uwEt892S+KraAbmooB+gyWJX5jIYlEAOZtuAKPjjSWsS hqUI5tHwjICdaCk3V23IoiwLwCKGon3gjU+xloqUMv8GpxfqseREly5vQFMPpLXH3AG4 c3/3+D+UIBP75C/b9GpBgXbBy4mU2uJ3Tneep5AwWm4NvVbuxkqLNZc14cCiscmu5xpC dk5QU06xajU91WWxyUejJfXQqQ+vCGtdsQnumkIYW+RRr4B+UKuMqidKKyQd/CwsQn4n xHvZJ1xQ/m6UsJCl8za3RGpt1rpFxt+PUijXvGrvBeAKZzSXkZK8etIL8l0eVKlHD+W4 1AKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b=fAsxNpKq; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cr11-20020a170906d54b00b007acd09f5067si9915626ejc.417.2022.11.07.10.54.08; Mon, 07 Nov 2022 10:54:32 -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=@soleen.com header.s=google header.b=fAsxNpKq; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232564AbiKGSrY (ORCPT + 99 others); Mon, 7 Nov 2022 13:47:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232550AbiKGSrW (ORCPT ); Mon, 7 Nov 2022 13:47:22 -0500 Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 611B527158 for ; Mon, 7 Nov 2022 10:47:20 -0800 (PST) Received: by mail-qt1-x82b.google.com with SMTP id w4so7452086qts.0 for ; Mon, 07 Nov 2022 10:47:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=Lj2j2IowLRClh2CPK+1bgXAKaDYVWiVJ2biJrtBrjjs=; b=fAsxNpKqKfjlLu6qyaCUUsQ6rgzXrse5vGKTil+8bXk5cFvSQ6jD5lq6WuyaLZDydJ TrFSoxBtyaN6K68JJsrqntKcnCbm9BsA7xgOMU90mwKvLKC2oyLQ4p0Mi7teerV7EMWZ ZHsI0H6iaqFs63tPj46T4LVKPCzLMfx0GotpVLIO9u2f0jEGNMEpQ05AYjUEn37cWMnJ v8ptvlt23NbMCLTphQQi0fpn7DbbfscEsUxz0lEKrrW7q+6yi6ETrYqdEffF83R4Rid1 0qXLNdkQwo5ommzLS/5z0LfIKIjShEyNG9AV77dtGO4kW2exnkQNIHwC+E5Hx9vTFJ/9 /8FA== 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:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Lj2j2IowLRClh2CPK+1bgXAKaDYVWiVJ2biJrtBrjjs=; b=q1nItSiyT9x8fpgyW3YooZ/prlUznAMehRFEl+P44sIG+JOpiMELJqKzenl8Yw5WGU F/X8TGPJbcrWqJ7HuLRPmcAuyXVCJF+DVq7HRmEj+AhxuuwFUirrfuLkeFqFQdjSoQ+i tjyhZS2lTQv5EOENHjw1JLdNd7u1kEWwk7ceTwuYe5EoACwxUlTMV4O0R49xvGeJrpQ0 x0fQTMMzyzcYLoxA5WIXlf/+NuiNGFii8zeL4I1+/Z6ThDUMf1vVZzTAkbZ/3uVTMIu5 S5tfcCUJbzcI9iS9eSuPJNan1LZ/W2y8YoMDP2eKp5sIdyIqPn5xtFiijqQ28xBKxgMh jljQ== X-Gm-Message-State: ACrzQf1+Ox9PQCBpEjmzVClHha3C7j5Cd3ZIAuitQL1Xa8zIbeDzjc15 wAhmnm7xZoeYaqzeALVqa51ZAA== X-Received: by 2002:ac8:71c7:0:b0:3a5:4cb1:7baa with SMTP id i7-20020ac871c7000000b003a54cb17baamr21357905qtp.303.1667846839395; Mon, 07 Nov 2022 10:47:19 -0800 (PST) Received: from soleen.c.googlers.com.com (193.132.150.34.bc.googleusercontent.com. [34.150.132.193]) by smtp.gmail.com with ESMTPSA id t17-20020a05622a149100b0039ee562799csm6638167qtx.59.2022.11.07.10.47.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 10:47:18 -0800 (PST) From: Pasha Tatashin To: corbet@lwn.net, akpm@linux-foundation.org, hughd@google.com, hannes@cmpxchg.org, david@redhat.com, vincent.whitchurch@axis.com, seanjc@google.com, rppt@kernel.org, shy828301@gmail.com, pasha.tatashin@soleen.com, paul.gortmaker@windriver.com, peterx@redhat.com, vbabka@suse.cz, Liam.Howlett@Oracle.com, ccross@google.com, willy@infradead.org, arnd@arndb.de, cgel.zte@gmail.com, yuzhao@google.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, bagasdotme@gmail.com, kirill@shutemov.name Subject: [PATCH v2] mm: anonymous shared memory naming Date: Mon, 7 Nov 2022 18:47:15 +0000 Message-Id: <20221107184715.3950621-1-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS 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?1748623059840959641?= X-GMAIL-MSGID: =?utf-8?q?1748864621653527221?= Since: commit 9a10064f5625 ("mm: add a field to store names for private anonymous memory"), name for private anonymous memory, but not shared anonymous, can be set. However, naming shared anonymous memory just as useful for tracking purposes. Extend the functionality to be able to set names for shared anon. Sample output: /* Create shared anonymous segmenet */ anon_shmem = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); /* Name the segment: "MY-NAME" */ rv = prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, anon_shmem, SIZE, "MY-NAME"); cat /proc//maps (and smaps): 7fc8e2b4c000-7fc8f2b4c000 rw-s 00000000 00:01 1024 [anon_shmem:MY-NAME] Signed-off-by: Pasha Tatashin Acked-by: David Hildenbrand --- Documentation/filesystems/proc.rst | 8 +++++--- fs/proc/task_mmu.c | 14 ++++++++++---- include/linux/mm.h | 2 ++ include/linux/mm_types.h | 27 +++++++++++++-------------- mm/madvise.c | 7 ++----- mm/shmem.c | 20 ++++++++++++++++++-- 6 files changed, 50 insertions(+), 28 deletions(-) Changes since v1: https://lore.kernel.org/lkml/20221105025342.3130038-1-pasha.tatashin@soleen.com - removed "path" for user named anon shared memory - fixed a warning found by kernel test robot - fixed a warning reported by Bagas Sanjaya - simplified and improved the commit log diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst index 898c99eae8e4..b8f175ae4853 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -426,14 +426,16 @@ with the memory region, as the case would be with BSS (uninitialized data). The "pathname" shows the name associated file for this mapping. If the mapping is not associated with a file: - ============= ==================================== + =================== =========================================== [heap] the heap of the program [stack] the stack of the main process [vdso] the "virtual dynamic shared object", the kernel system call handler - [anon:] an anonymous mapping that has been + [anon:] a private anonymous mapping that has been named by userspace - ============= ==================================== + [anon_shmem:] an anonymous shared memory mapping that has + been named by userspace + =================== =========================================== or if empty, the mapping is anonymous. diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 8a74cdcc9af0..d22687d2e81e 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -277,6 +277,7 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma) struct mm_struct *mm = vma->vm_mm; struct file *file = vma->vm_file; vm_flags_t flags = vma->vm_flags; + struct anon_vma_name *anon_name; unsigned long ino = 0; unsigned long long pgoff = 0; unsigned long start, end; @@ -293,6 +294,7 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma) start = vma->vm_start; end = vma->vm_end; show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino); + anon_name = anon_vma_name(vma); /* * Print the dentry name for named mappings, and a @@ -300,7 +302,14 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma) */ if (file) { seq_pad(m, ' '); - seq_file_path(m, file, "\n"); + /* + * If user named this anon shared memory via + * prctl(PR_SET_VMA ..., use the provided name. + */ + if (anon_name) + seq_printf(m, "[anon_shmem:%s]", anon_name->name); + else + seq_file_path(m, file, "\n"); goto done; } @@ -312,8 +321,6 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma) name = arch_vma_name(vma); if (!name) { - struct anon_vma_name *anon_name; - if (!mm) { name = "[vdso]"; goto done; @@ -330,7 +337,6 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma) goto done; } - anon_name = anon_vma_name(vma); if (anon_name) { seq_pad(m, ' '); seq_printf(m, "[anon:%s]", anon_name->name); diff --git a/include/linux/mm.h b/include/linux/mm.h index 8bbcccbc5565..06b6fb3277ab 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -699,8 +699,10 @@ static inline unsigned long vma_iter_addr(struct vma_iterator *vmi) * paths in userfault. */ bool vma_is_shmem(struct vm_area_struct *vma); +bool vma_is_anon_shmem(struct vm_area_struct *vma); #else static inline bool vma_is_shmem(struct vm_area_struct *vma) { return false; } +static inline bool vma_is_anon_shmem(struct vm_area_struct *vma) { return false; } #endif int vma_is_stack_for_current(struct vm_area_struct *vma); diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 500e536796ca..08d8b973fb60 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -461,21 +461,11 @@ struct vm_area_struct { * For areas with an address space and backing store, * linkage into the address_space->i_mmap interval tree. * - * For private anonymous mappings, a pointer to a null terminated string - * containing the name given to the vma, or NULL if unnamed. */ - - union { - struct { - struct rb_node rb; - unsigned long rb_subtree_last; - } shared; - /* - * Serialized by mmap_sem. Never use directly because it is - * valid only when vm_file is NULL. Use anon_vma_name instead. - */ - struct anon_vma_name *anon_name; - }; + struct { + struct rb_node rb; + unsigned long rb_subtree_last; + } shared; /* * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma @@ -485,6 +475,7 @@ struct vm_area_struct { */ struct list_head anon_vma_chain; /* Serialized by mmap_lock & * page_table_lock */ + struct anon_vma *anon_vma; /* Serialized by page_table_lock */ /* Function pointers to deal with this struct. */ @@ -496,6 +487,14 @@ struct vm_area_struct { struct file * vm_file; /* File we map to (can be NULL). */ void * vm_private_data; /* was vm_pte (shared mem) */ +#ifdef CONFIG_ANON_VMA_NAME + /* + * For private and shared anonymous mappings, a pointer to a null + * terminated string containing the name given to the vma, or NULL if + * unnamed. Serialized by mmap_sem. Use anon_vma_name to access. + */ + struct anon_vma_name *anon_name; +#endif #ifdef CONFIG_SWAP atomic_long_t swap_readahead_info; #endif diff --git a/mm/madvise.c b/mm/madvise.c index c7105ec6d08c..255d5b485432 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -95,9 +95,6 @@ struct anon_vma_name *anon_vma_name(struct vm_area_struct *vma) { mmap_assert_locked(vma->vm_mm); - if (vma->vm_file) - return NULL; - return vma->anon_name; } @@ -183,7 +180,7 @@ static int madvise_update_vma(struct vm_area_struct *vma, * vm_flags is protected by the mmap_lock held in write mode. */ vma->vm_flags = new_flags; - if (!vma->vm_file) { + if (!vma->vm_file || vma_is_anon_shmem(vma)) { error = replace_anon_vma_name(vma, anon_name); if (error) return error; @@ -1273,7 +1270,7 @@ static int madvise_vma_anon_name(struct vm_area_struct *vma, int error; /* Only anonymous mappings can be named */ - if (vma->vm_file) + if (vma->vm_file && !vma_is_anon_shmem(vma)) return -EBADF; error = madvise_update_vma(vma, prev, start, end, vma->vm_flags, diff --git a/mm/shmem.c b/mm/shmem.c index c1d8b8a1aa3b..a6482cadda79 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -237,11 +237,17 @@ static const struct inode_operations shmem_inode_operations; static const struct inode_operations shmem_dir_inode_operations; static const struct inode_operations shmem_special_inode_operations; static const struct vm_operations_struct shmem_vm_ops; +static const struct vm_operations_struct shmem_anon_vm_ops; static struct file_system_type shmem_fs_type; +bool vma_is_anon_shmem(struct vm_area_struct *vma) +{ + return vma->vm_ops == &shmem_anon_vm_ops; +} + bool vma_is_shmem(struct vm_area_struct *vma) { - return vma->vm_ops == &shmem_vm_ops; + return vma_is_anon_shmem(vma) || vma->vm_ops == &shmem_vm_ops; } static LIST_HEAD(shmem_swaplist); @@ -3995,6 +4001,15 @@ static const struct vm_operations_struct shmem_vm_ops = { #endif }; +static const struct vm_operations_struct shmem_anon_vm_ops = { + .fault = shmem_fault, + .map_pages = filemap_map_pages, +#ifdef CONFIG_NUMA + .set_policy = shmem_set_policy, + .get_policy = shmem_get_policy, +#endif +}; + int shmem_init_fs_context(struct fs_context *fc) { struct shmem_options *ctx; @@ -4170,6 +4185,7 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) EXPORT_SYMBOL_GPL(shmem_truncate_range); #define shmem_vm_ops generic_file_vm_ops +#define shmem_anon_vm_ops generic_file_vm_ops #define shmem_file_operations ramfs_file_operations #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) #define shmem_acct_size(flags, size) 0 @@ -4275,7 +4291,7 @@ int shmem_zero_setup(struct vm_area_struct *vma) if (vma->vm_file) fput(vma->vm_file); vma->vm_file = file; - vma->vm_ops = &shmem_vm_ops; + vma->vm_ops = &shmem_anon_vm_ops; return 0; }