Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp683303pxb; Thu, 30 Sep 2021 15:00:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwYGZOaPg84E1AE9Wr1S4m+4vslKqySGIzHN2NVsTp7VDI1dRCMDdyDXbXRqghEfBms1Thx X-Received: by 2002:a63:4614:: with SMTP id t20mr6832559pga.372.1633039203010; Thu, 30 Sep 2021 15:00:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633039203; cv=none; d=google.com; s=arc-20160816; b=RUz9uRFcpyuhO3k6JtT4wl5Y6+8PzCjitLUD8n7XS8D6BRoSPOTOH7mIoUgPs2b85/ rEmChMARHvtiPdKNRwzM83EYDqc5a6z/bWThz4QQmozUl+slE0mD3Fbky1X4e3FAEkSA htP37ZmjAodBi0G/qXeQRvSsqdF5w4U5Ermvvmd1ktes4fnR8WdBV+MGWoG/yDlaXxE8 O2wFiGiFBQibQA5hm798rNsP9LixYDo7ayKjbWxhtTRNR6u0+6obUxEYANavzBcY3F1t CS+7TO5PyW4eXCTZiE/09u1gczcA8NsoPFLyyaC0qEMSvAIJcScXUhr1JodVWZfDwB/E l05g== 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=kvnWJDkfYff63dedYnGJTZjKEKtttIPMWbyKWd7jcys=; b=lE24VfPdLOnMloAB7JzjLCUgzeOdW0aW9vKEtQGjEhxBqbsaW76sZ2wnBHBLXoKzkc IdUUsTQCL9QyoegwT+SErwLESta/u8IvDvo6mJ2j4kH4ZCQlnQ0qRy4zn7GvEa7phYb9 NDIY2h9Ucw1YlrzI2b3h+rmEIJKC8dLDr+8humbg0IPswGbGm1Py+2W75iGMVpgNKvBc f8Qm6d81zZMvr1I3rUmFVcN/K45ZPIyybBkmnlTtODztlbJJ3W4f7z90InoL6/B6TC6B NSCV+LVnxWNbtBBJCLEk8JmOtT+oT5AnzxncP0aW8Y3FiHPUS6rfhi6BODNewdfUrwIb 9rmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=MszGudlz; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 37si5183437pgn.589.2021.09.30.14.59.49; Thu, 30 Sep 2021 15:00:02 -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=@google.com header.s=20210112 header.b=MszGudlz; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245702AbhI3S6I (ORCPT + 99 others); Thu, 30 Sep 2021 14:58:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236957AbhI3S6H (ORCPT ); Thu, 30 Sep 2021 14:58:07 -0400 Received: from mail-yb1-xb29.google.com (mail-yb1-xb29.google.com [IPv6:2607:f8b0:4864:20::b29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD314C06176A for ; Thu, 30 Sep 2021 11:56:24 -0700 (PDT) Received: by mail-yb1-xb29.google.com with SMTP id m132so15462622ybf.8 for ; Thu, 30 Sep 2021 11:56:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kvnWJDkfYff63dedYnGJTZjKEKtttIPMWbyKWd7jcys=; b=MszGudlzOc7y2MG5Azj1ge3Zo3H/LqnWaymyDtHDYs/NteFQibW/dGtvn9pUd/GVmz dXUSVvumzvTjZUnyRrk7AfBsc1+y7CD7RPdT7eJYe7EZ+pqt+g32fvP/vdEt8S6U4W5I JAx/jsNK74i/jP2lFaJFwh+YlQEcOEGR+Opiqs5pd32JCMWAYLBHKoW6Eti0j3vvQims OJBrZ7kdlHUALRmeGKU5vDZW+FCjkSkvraAmOeaP5Ep89KwGxX1QnW9y3lMRNf6Yg/iB r89sVxo6dhS5lweD4Z77UKiVnKYPID+Xuen3WRgMapVd0WDWAyazW8zmJacKqS6AjA5U 2XZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kvnWJDkfYff63dedYnGJTZjKEKtttIPMWbyKWd7jcys=; b=m3T1KJlCj10t6RFSdfH0CR1rGg4jDpzkdYoLqR33QLewb73ZeYMFUTx4a6aIvcCaC+ zCHXvHUqYpS2QE732dlpz63lQZp5gPvdBLPm5UswEazG3MBBRcehHAPuNoONf7n+RzXe FSBD95mfTtZDM4lH+ncZUi/DpDv/4fVAW5EuC9a3j3c56Sf5zQE8sqMstiwnPNi6sZ1G gKienyMM49Duc2F0M4UoD0+Q8bNZLIo71ZFgRKuEAeeNW+AgY7niTBwYBojYoOAIwbuT tdM0t1fWeOgnpDzhzpRIjVjz8Ea0fblBJ29RxyBcprxYgxPabCerQZ6MgI+d1atQrvlb pTPw== X-Gm-Message-State: AOAM5314B5chAOGKuUI0C/MnbZW1+Va0UWNm5Ne/RRYm5++RIcD3X9I2 ZXX2EtfiCByFf5wE77UnF4K7y830ZdDDCDIet257Gw== X-Received: by 2002:a05:6902:124f:: with SMTP id t15mr1118645ybu.161.1633028183843; Thu, 30 Sep 2021 11:56:23 -0700 (PDT) MIME-Version: 1.0 References: <20210902231813.3597709-1-surenb@google.com> <20210902231813.3597709-2-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Thu, 30 Sep 2021 11:56:12 -0700 Message-ID: Subject: Re: [PATCH v9 2/3] mm: add a field to store names for private anonymous memory To: Matthew Wilcox Cc: Andrew Morton , Colin Cross , Sumit Semwal , Michal Hocko , Dave Hansen , Kees Cook , "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, John Hubbard , 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@oracle.com, 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, Rasmus Villemoes , LKML , linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm , kernel-team Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 8, 2021 at 9:05 PM Suren Baghdasaryan wrote: > > On Mon, Sep 6, 2021 at 9:57 AM Matthew Wilcox wrote: > > > > On Thu, Sep 02, 2021 at 04:18:12PM -0700, Suren Baghdasaryan wrote: > > > On Android we heavily use a set of tools that use an extended version of > > > the logic covered in Documentation/vm/pagemap.txt to walk all pages mapped > > > in userspace and slice their usage by process, shared (COW) vs. unique > > > mappings, backing, etc. This can account for real physical memory usage > > > even in cases like fork without exec (which Android uses heavily to share > > > as many private COW pages as possible between processes), Kernel SamePage > > > Merging, and clean zero pages. It produces a measurement of the pages > > > that only exist in that process (USS, for unique), and a measurement of > > > the physical memory usage of that process with the cost of shared pages > > > being evenly split between processes that share them (PSS). > > > > > > If all anonymous memory is indistinguishable then figuring out the real > > > physical memory usage (PSS) of each heap requires either a pagemap walking > > > tool that can understand the heap debugging of every layer, or for every > > > layer's heap debugging tools to implement the pagemap walking logic, in > > > which case it is hard to get a consistent view of memory across the whole > > > system. > > > > > > Tracking the information in userspace leads to all sorts of problems. > > > It either needs to be stored inside the process, which means every > > > process has to have an API to export its current heap information upon > > > request, or it has to be stored externally in a filesystem that > > > somebody needs to clean up on crashes. It needs to be readable while > > > the process is still running, so it has to have some sort of > > > synchronization with every layer of userspace. Efficiently tracking > > > the ranges requires reimplementing something like the kernel vma > > > trees, and linking to it from every layer of userspace. It requires > > > more memory, more syscalls, more runtime cost, and more complexity to > > > separately track regions that the kernel is already tracking. > > > > I understand that the information is currently incoherent, but why is > > this the right way to make it coherent? It would seem more useful to > > use something like one of the tracing mechanisms (eg ftrace, LTTng, > > whatever the current hotness is in userspace tracing) for the malloc > > library to log all the useful information, instead of injecting a subset > > of it into the kernel for userspace to read out again. > > Sorry, for the delay with the response. I'm travelling and my internet > access is very patchy. > > Just to clarify, your suggestion is to require userspace to log any > allocation using ftrace or a similar mechanism and then for the system > to parse these logs to calculate the memory usage for each process? > I didn't think much in this direction but I guess logging each > allocation in the system and periodically collecting that data would > be quite expensive both from memory usage and performance POV. I'll > need to think a bit more but these are to me the obvious downsides of > this approach. Sorry for the delay again. Now that I'm back there should not be any more of them. I thought more about these alternative suggestions for userspace to record allocations but that would introduce considerable complexity into userspace. Userspace would have to collect and consolidate this data by some daemon, all users would have to query it for the data (IPC or something similar), in case this daemon crashes the data would need to be somehow recovered. So, in short, it's possible but makes things much more complex compared to proposed in-kernel implementation. OTOH, the only downside of the current implementation is the additional memory required to store anon vma names. I checked the memory consumption on the latest Android with these patches and because we share vma names during fork, the actual memory required to store vma names is no more than 600kB. Even on older phones like Pixel 3 with 4GB RAM, this is less than 0.015% of total memory. IMHO, this is an acceptable price to pay.