Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp355190pxb; Fri, 15 Oct 2021 06:55:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyL1hc/4yFtZC4b5xjyQGp2Xnjxu6gvJ4hix7rQGjB1sESp+ypbLFWYvwRXuJuBDDmeNQrK X-Received: by 2002:a17:90b:1d04:: with SMTP id on4mr13656145pjb.68.1634306126832; Fri, 15 Oct 2021 06:55:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634306126; cv=none; d=google.com; s=arc-20160816; b=a7UdL8ZP0gLYovv7+/3ekefU5V4p7OgMZhVoXP9wI6mifo6kjS+Q/z1h81ryAjnr+6 mYgQLRPPYlPuSkd6BNezrctuDALhCrHCX5tSSuD5e6vaU48AelIXzNIHGLEh3xtj22dU M9+wbeyE4sXOpVyOVKzjPRipasFQ3mlQ7dARcrrnZ8HwCqBBC5TDdbNTrajgIeqQ6jPM 4AB7Pu0/YZ35iqNp+EBHHMIr6Z5/rI4xo9ce/rLc6Svb1dVVkJFwyU4Ivf3fhlqKzxID zcZYRe+Kqod9k7OcyrnTXSlcAXq6g+WG/aZQQxiqmWaCn3GXn5FylwkfB3IpHLTBFAE0 0E6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=ggZGR9DaIVOeY+FRltem0wB6ymI3HDlfEdwv9GyNtfI=; b=rXPwX5ZDRFRCWcwaUcggIY8IyFIu2BoBqj4ogYg+U1NT68Mgt/L09aJ4pIREqs9CaU Rk6OERE6GkYk4VYgxwUV2wrSgGk1FAIIDpEkg0wLBitvY7IKPvvt3ki5c+f/Vano1qVG I25DFex+SxucLOHfHzj1OKHqr9pAzJGuFQdZ4Hffe3Z1DmTqyJHNRHvrUasD9RrChHUU lntAdl+Gt9juD3lu9Qp5NoeUoI/+bR41rOOM5AVcBUnLQEQbdX0yV5KoBvu7Foq4HEwE VfYmgkGJrn9cJnvFvH+feIMeQovbQ8KuX3bV3k1sHtAmYJeJZzYTYctkz1impFYmfabb rS1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VS12Lp+e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u70si7356069pgd.535.2021.10.15.06.55.14; Fri, 15 Oct 2021 06:55:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VS12Lp+e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236601AbhJOIG1 (ORCPT + 99 others); Fri, 15 Oct 2021 04:06:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:38085 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236599AbhJOIGF (ORCPT ); Fri, 15 Oct 2021 04:06:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634285036; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ggZGR9DaIVOeY+FRltem0wB6ymI3HDlfEdwv9GyNtfI=; b=VS12Lp+eR3OB29gXCpt+rsrElp11aBZLw5uBsaSFIQya7Vcfnc50VU4dX5xqYg4iRotghy eDjPxi3Jp0r/KoYyYeDqQ+t2km+XGLS9pdKin1pVQv6T34wYleKqenGQGztUi76F5sN/UQ rh6vxK5s0Y7X4QP8o734mD6/y1O5F8c= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-283-4mYH-bOiN822pbEgkQ8cMQ-1; Fri, 15 Oct 2021 04:03:55 -0400 X-MC-Unique: 4mYH-bOiN822pbEgkQ8cMQ-1 Received: by mail-wm1-f71.google.com with SMTP id k6-20020a7bc306000000b0030d92a6bdc7so600629wmj.3 for ; Fri, 15 Oct 2021 01:03:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=ggZGR9DaIVOeY+FRltem0wB6ymI3HDlfEdwv9GyNtfI=; b=vd7UmQsyPt04HLO8EzfAzsaTHXaDG2Dkv/DDxH34zv46lAQSSCG6Zc7YeGYRw01Ijc ZcFxG3kxlqo9+aOEXNtXb0y2VtqBT6Ez6DqGSON6Ma0j0qimSAppXp9ScMkGDW9YjAjf 2+bIc2bVvZSIv4NADjji7TSKvO7ZvSgrkwWwOffbXVHFROkgxzXsIzeVkDU3JoLYWLMq 0Slt1H0YCIPiSbb/DYdxykKi+eZ/fcKskb4fdNAuMNaFE3L48yN5ugbFjTjssZu05QcO EOnU29SYzg6TRvTN0MNB3SlKDT/oLKJ4rhsM+3V11BWJWVWCEBtcIzAs3PmhYVQmkdad Pe3Q== X-Gm-Message-State: AOAM530ETuJYB8a+YP0LyIvr2bXdpIG2+qsFQ+DOJdIr0MTflzm7/t9i f0SKMoVAjYEe6u+WuaMn8vM5SEsMq8suBkeMbxf/X1cSbmkWEam/WedLE2Ha8ZpCCL2V72rgGhl nWLBBDeTnh2SAbuZU5rZ7ithK X-Received: by 2002:a05:600c:1d1f:: with SMTP id l31mr24174311wms.44.1634285034171; Fri, 15 Oct 2021 01:03:54 -0700 (PDT) X-Received: by 2002:a05:600c:1d1f:: with SMTP id l31mr24174227wms.44.1634285033755; Fri, 15 Oct 2021 01:03:53 -0700 (PDT) Received: from [192.168.3.132] (p5b0c6a01.dip0.t-ipconnect.de. [91.12.106.1]) by smtp.gmail.com with ESMTPSA id p18sm4349286wrt.54.2021.10.15.01.03.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 15 Oct 2021 01:03:53 -0700 (PDT) Message-ID: Date: Fri, 15 Oct 2021 10:03:50 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [PATCH v10 3/3] mm: add anonymous vma name refcounting Content-Language: en-US To: Suren Baghdasaryan Cc: Michal Hocko , Kees Cook , Pavel Machek , Rasmus Villemoes , John Hubbard , Andrew Morton , Colin Cross , Sumit Semwal , Dave Hansen , Matthew Wilcox , "Kirill A . Shutemov" , Vlastimil Babka , Johannes Weiner , Jonathan Corbet , Al Viro , Randy Dunlap , Kalesh Singh , Peter Xu , rppt@kernel.org, Peter Zijlstra , Catalin Marinas , vincenzo.frascino@arm.com, =?UTF-8?B?Q2hpbndlbiBDaGFuZyAo5by16Yym5paHKQ==?= , Axel Rasmussen , Andrea Arcangeli , Jann Horn , apopple@nvidia.com, Yu Zhao , Will Deacon , fenghua.yu@intel.com, thunder.leizhen@huawei.com, Hugh Dickins , feng.tang@intel.com, Jason Gunthorpe , Roman Gushchin , Thomas Gleixner , krisman@collabora.com, Chris Hyser , Peter Collingbourne , "Eric W. Biederman" , Jens Axboe , legion@kernel.org, Rolf Eike Beer , Cyrill Gorcunov , Muchun Song , Viresh Kumar , Thomas Cedeno , sashal@kernel.org, cxfcosmos@gmail.com, LKML , linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm , kernel-team References: <92cbfe3b-f3d1-a8e1-7eb9-bab735e782f6@rasmusvillemoes.dk> <20211007101527.GA26288@duo.ucw.cz> <202110071111.DF87B4EE3@keescook> <202110081344.FE6A7A82@keescook> <26f9db1e-69e9-1a54-6d49-45c0c180067c@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 14.10.21 22:16, Suren Baghdasaryan wrote: > On Tue, Oct 12, 2021 at 10:01 AM Suren Baghdasaryan wrote: >> >> On Tue, Oct 12, 2021 at 12:44 AM David Hildenbrand wrote: >>> >>>> I'm still evaluating the proposal to use memfds but I'm not sure if >>>> the issue that David Hildenbrand mentioned about additional memory >>>> consumed in pagecache (which has to be addressed) is the only one we >>>> will encounter with this approach. If anyone knows of any potential >>>> issues with using memfds as named anonymous memory, I would really >>>> appreciate your feedback before I go too far in that direction. >>> >>> [MAP_PRIVATE memfd only behave that way with 4k, not with huge pages, so >>> I think it just has to be fixed. It doesn't make any sense to allocate a >>> page for the pagecache ("populate the file") when accessing via a >>> private mapping that's supposed to leave the file untouched] >>> >>> My gut feeling is if you really need a string as identifier, then try >>> going with memfds. Yes, we might hit some road blocks to be sorted out, >>> but it just logically makes sense to me: Files have names. These names >>> exist before mapping and after mapping. They "name" the content. >> >> I'm investigating this direction. I don't have much background with >> memfds, so I'll need to digest the code first. > > I've done some investigation into the possibility of using memfds to > name anonymous VMAs. Here are my findings: Thanks for exploring the alternatives! > > 1. Forking a process with anonymous vmas named using memfd is 5-15% > slower than with prctl (depends on the number of VMAs in the process > being forked). Profiling shows that i_mmap_lock_write() dominates > dup_mmap(). Exit path is also slower by roughly 9% with > free_pgtables() and fput() dominating exit_mmap(). Fork performance is > important for Android because almost all processes are forked from > zygote, therefore this limitation already makes this approach > prohibitive. Interesting, naturally I wonder if that can be optimized. > > 2. mremap() usage to grow the mapping has an issue when used with memfds: > > fd = memfd_create(name, MFD_ALLOW_SEALING); > ftruncate(fd, size_bytes); > ptr = mmap(NULL, size_bytes, prot, MAP_PRIVATE, fd, 0); > close(fd); > ptr = mremap(ptr, size_bytes, size_bytes * 2, MREMAP_MAYMOVE); > touch_mem(ptr, size_bytes * 2); > > This would generate a SIGBUS in touch_mem(). I believe it's because > ftruncate() specified the size to be size_bytes and we are accessing > more than that after remapping. prctl() does not have this limitation > and we do have a usecase for growing a named VMA. Can't you simply size the memfd much larger? I mean, it doesn't really cost much, does it? > > 3. Leaves an fd exposed, even briefly, which may lead to unexpected > flaws (e.g. anything using mmap MAP_SHARED could allow exposures or > overwrites). Even MAP_PRIVATE, if an attacker writes into the file > after ftruncate() and before mmap(), can cause private memory to be > initialized with unexpected data. I don't quite follow. Can you elaborate what exactly the issue here is? We use a temporary fd, yes, but how is that a problem? Any attacker can just write any random memory memory in the address space, so I don't see the issue. > > 4. There is a usecase in the Android userspace where vma naming > happens after memory was allocated. Bionic linker does in-memory > relocations and then names some relocated sections. Would renaming a memfd be an option or is that "too late" ? > > In the light of these findings, could the current patchset be reconsidered? > Thanks, > Suren. > -- Thanks, David / dhildenb