Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6317035rwb; Mon, 14 Nov 2022 18:24:24 -0800 (PST) X-Google-Smtp-Source: AA0mqf7+Rp66b5RL2TraACCAi7TKW7nDczmncpFjITqXIycJJfuVRuFyRHcPwlxo4T3CJKN7O8Zv X-Received: by 2002:a17:903:11c6:b0:184:fa22:8b67 with SMTP id q6-20020a17090311c600b00184fa228b67mr1869347plh.149.1668479063998; Mon, 14 Nov 2022 18:24:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668479063; cv=none; d=google.com; s=arc-20160816; b=dc0mjpRMtdtuvl6N3omyyqPznxpR0gBaK8BQ+ZTz0sneL0IIc9RNvC7AIh9kQRO9l1 E6/emf93vv9f8OxFrvbsca1e4CDUdEznLiL7AWTnMRli13NYQp1UlRjjVI4lcEzszSTY py4MbeBWl22Df8Kf/0/MOUQLwO/Plki5XU9EkP8L2ttLA6mdcpuzRBa61pkNFwVyQQpp 3zo227t2cAnPKrOZ9fSUUnHbeLhyAEUdMPBITQJ1mddKOF+AKWn08wLhn4d6BukVH73x vIMFpSaKi04h/fRZASJczRTTi9LSMiS+nN9nh6sz1z1zvpAmDl+Wc1AW1asty7SqatWr dj6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=JGEQys0q+vn6kmmzyVuQOW7GVEeqdKy9lN8OIJWvOQs=; b=Nww2L2ssLoHaX1h6Hg30+RsRCcDq66GyKCX6a/RlDWyHCxRDk1KwbHcEnnMrHqxepS fpBnwSzXEjxSQwHAR9uJNoVaSKofV1i/ft6zn/xiFwghVCctTOgdnouWMbi0pXxDCl1J oeAiBIE56iZpmXKOlPg41RXYroNUJOe1ciu3eoRg6OADgoqXNFr0BO/Q1ZHvrKhM1ZfM mP56POj9mDNb7DlVgg5Jg2mNoqgJbu6WPkFAVczqaUvhs3sZDRIKyO3lkg4zzBoCHnxV bAKAKzJGKE/ix3Nw4FWpLaTqq1/t+Jin92LYIcpi4RhzbHAGAja1UpLnlmwlLtV1r7U9 sr2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b="PSrB/jmA"; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i2-20020a636d02000000b00463dbc5cebfsi11742582pgc.67.2022.11.14.18.24.10; Mon, 14 Nov 2022 18:24:23 -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="PSrB/jmA"; 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 S232149AbiKOBgq (ORCPT + 88 others); Mon, 14 Nov 2022 20:36:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230131AbiKOBgm (ORCPT ); Mon, 14 Nov 2022 20:36:42 -0500 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69E9114032 for ; Mon, 14 Nov 2022 17:36:41 -0800 (PST) Received: by mail-ej1-x635.google.com with SMTP id f5so32657305ejc.5 for ; Mon, 14 Nov 2022 17:36:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=JGEQys0q+vn6kmmzyVuQOW7GVEeqdKy9lN8OIJWvOQs=; b=PSrB/jmAglnUp28dEPhbOBEDVHtOyET03hzbsVCqEwMkZ54OJlNDXfWBCGnL86RVTW gfwfg54OIdAw8qy5SXee3IO1bQ2tBYPOE4X13S4R8eKXqwvTeMzddt8p3/rSyBVx6Fks a+4Q9XuWs3mL/JuLZKGdyZMYN38qX34Rk8UYLpFh/ox1Kxns5WRODVykYo6Iye4QVKYy XorRzqemDyQsEh+6YJ9/InCSyoknBRZ3egXW4380pxN5HZnUVzggu3nNxPtlV5FD5zed maf0oNgfFm4NREuJsoWujUue1x98cStxRmhb4jPVJrzgy+qpWewB3siMSOCHmom7MuwF a6SQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=JGEQys0q+vn6kmmzyVuQOW7GVEeqdKy9lN8OIJWvOQs=; b=VP7Jkb0+fpwuzDcfNkG7aeTNNXq97zz/087MkEJBb4pwi474UZWddCzwYxcFnTyUhi yGEgbJYoLwIDsMfApQJIf5TkXbJ++MI9lg/L/ZdrG/t6eViCCUZcnUneUpVAwftnIbpZ I6WV7A8VPEQq2IXkgJe3dW/9KXFdnlvJvr+yLqNGufYavo+WX1r5QqgTkf2EXtQUODaZ BMbj/jjSw6B19qGMFvVcCMPs/XzdJda++8dAWjCt+1ldLv9N+y8x+xO2OuwHroNkQIA7 yiIuU828awIJbKf4mCebotufABKu7GTHOOhjZopSKqtJzwjhV80dEz+Paq/d1rhGITKP g9tA== X-Gm-Message-State: ANoB5pnHVyldijvCm8XQ+rti4A7rDlrK3dgq5xVQUjQweWUoCFK72gfT P/tgD4xHZsIBz9syISXb4cj7igT67NImh/wJP2GOCw== X-Received: by 2002:a17:906:b10d:b0:7a3:fbfa:32e5 with SMTP id u13-20020a170906b10d00b007a3fbfa32e5mr12420307ejy.7.1668476199923; Mon, 14 Nov 2022 17:36:39 -0800 (PST) MIME-Version: 1.0 References: <20221107184715.3950621-1-pasha.tatashin@soleen.com> <70a8541b-6066-45ca-e2bc-3b7ecc0e7bb2@redhat.com> In-Reply-To: <70a8541b-6066-45ca-e2bc-3b7ecc0e7bb2@redhat.com> From: Pasha Tatashin Date: Mon, 14 Nov 2022 20:36:03 -0500 Message-ID: Subject: Re: [PATCH v2] mm: anonymous shared memory naming To: David Hildenbrand Cc: corbet@lwn.net, akpm@linux-foundation.org, hughd@google.com, hannes@cmpxchg.org, vincent.whitchurch@axis.com, seanjc@google.com, rppt@kernel.org, shy828301@gmail.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 Content-Type: text/plain; charset="UTF-8" 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 On Wed, Nov 9, 2022 at 5:11 AM David Hildenbrand wrote: > > >> > >>> 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] > >> > >> What would it have looked like before? Just no additional information? > > > > Before: > > > > 7fc8e2b4c000-7fc8f2b4c000 rw-s 00000000 00:01 1024 /dev/zero (deleted) > > Can we add that to the patch description? > > >> > >>> > >>> Signed-off-by: Pasha Tatashin > >>> --- > >> > >> > >> [...] > >> > >>> 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; > >>> > >> > >> So that effectively grows the size of vm_area_struct. Hm. I'd really > >> prefer to keep this specific to actual anonymous memory, not extending > >> it to anonymous files. > > > > It grows only when CONFIG_ANON_VMA_NAME=y, otherwise it stays the same > > as before. Are you suggesting adding another config specifically for > > shared memory? I wonder if we could add a union for some other part of > > vm_area_struct where anon and file cannot be used together. > > In practice, all distributions will enable CONFIG_ANON_VMA_NAME in the > long term I guess. So if we could avoid increasing the VMA size, that > would be great. > > > > >> Do we have any *actual* users where we don't have an alternative? I > >> doubt that this is really required. > >> > >> The simplest approach seems to be to use memfd instead of MAP_SHARED | > >> MAP_ANONYMOUS. __NR_memfd_create can be passed a name and you get what > >> you propose here effectively already. Or does anything speak against it? > > > > For our use case the above does not work. We are working on highly > > paravirtualized virtual machines. The VMM maps VM memory as anonymous > > shared memory (not private because VMM is sandboxed and drivers are > > running in their own processes). However, the VM tells back to the VMM > > how parts of the memory are actually used by the guest, how each of > > the segments should be backed (i.e. 4K pages, 2M pages), and some > > other information about the segments. The naming allows us to monitor > > the effective memory footprint for each of these segments from the > > host without looking inside the guest. > > That's a reasonable use case, although naive me would worry about #VMA > limits etc. > > Can you add some condensed use-case explanation to the patch > description? (IOW, memfd cannot be used because parts of the memfd are > required to receive distinct names) > > I'd appreciate if we could avoid increasing the VMA size; but in any case I've explored ways not to increase VMA size, but there are no obvious solutions here. Let's keep it as is for now, and in the future if there we are going to be adding some fields that are only used by anonymous memory, we can explore of adding a union for this field. > > Acked-by: David Hildenbrand Thank you. I will soon send a new version with support for memfd anon memory as well. Pasha