From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 343A0C3DA6E for ; Wed, 10 Jan 2024 11:07:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA14D6B0096; Wed, 10 Jan 2024 06:07:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B50D76B009B; Wed, 10 Jan 2024 06:07:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F1B86B009C; Wed, 10 Jan 2024 06:07:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 8CA6D6B0096 for ; Wed, 10 Jan 2024 06:07:16 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 55882403F1 for ; Wed, 10 Jan 2024 11:07:16 +0000 (UTC) X-FDA: 81663124872.06.20B11D9 Received: from mail-vk1-f174.google.com (mail-vk1-f174.google.com [209.85.221.174]) by imf25.hostedemail.com (Postfix) with ESMTP id 8AF8EA0005 for ; Wed, 10 Jan 2024 11:07:14 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Wpz5OKgT; spf=pass (imf25.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704884834; a=rsa-sha256; cv=none; b=n+futUrQ2irYXpYJPOU9bzo0uEEwGwFKTtEV5rV+aKY16pv+zf9VCQuTbAQu2sxEcbKgr7 sWNAlEoLu2CR1POxft+WbQ4dv50AjusQRP8w8zM8V+h+AlKTwuHC43qWQJqYD4fYkv46LO nE6qGuyTmd98vOS13HJHSl7ZPvEbjlc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Wpz5OKgT; spf=pass (imf25.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.221.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704884834; h=from:from:sender: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:dkim-signature; bh=f05pa486KQz2zoeF0kfWjENZpLA8W1nr1C7qOoGIjBo=; b=QuySc3JOYUY01OT6GhYJInbIfM+mKfSz7JbiJ7BiSCObP/616rDeJBOu8xAlaDD/xkH6d/ 4sdPD08Qos/EzTl+xakLovoYwVUCaxoC4K+bscddWWBMG+vglPmdnYR05NxmQulogojv8C NQrjVimtiXB1QqJTe9AEY97nHRY3I6o= Received: by mail-vk1-f174.google.com with SMTP id 71dfb90a1353d-4b745fa9671so761534e0c.0 for ; Wed, 10 Jan 2024 03:07:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704884833; x=1705489633; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=f05pa486KQz2zoeF0kfWjENZpLA8W1nr1C7qOoGIjBo=; b=Wpz5OKgT/MWnP30VsGm5CqRKvMAQec2tsJ3pg0VDU9vpCU0M7m+c7mC0qMIgMdaOoQ tIavAGr9uUDgS6uoYmF2lAW1/ecPcMWD3uODcwDLzbHaPDNGuYfh1j0m4eApi0Y2gZ9Z ZRSmld5a14APgNl+EAYvfQuPuhd1s7jEoUCU+CHhFA77/SErbnoRnNXflnD4HPPzhtwT JbQr4+2DeufH9geW2XnGEmsoKOAIgBf49SL/QxxIYCnsTvr7JqcGD+JDKnHTuL2ri1Xn 46Cx3TLb58qR4e0Vr0JQzM5ULmEPjkepkciA2oopEwg6+5XbFIF/0pl/jnIBy1jttoRN tkPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704884833; x=1705489633; h=content-transfer-encoding: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=f05pa486KQz2zoeF0kfWjENZpLA8W1nr1C7qOoGIjBo=; b=K46v+vi9wbSTl44Vc3a2zim0ma9ZFVMw4qbg+7lfBskjJelQdlrLekq04yVax+uk9k Y1N0Trm5aE0lQnPGVw5Psz6KE0YDGVJxVSsXHH1n0j6LOSjZlfs0OzUF32AujUq+VaFs XXJATFMNadx3GWq0WjnFygXl7SjW8F+mhdzIl3W038lcmc/WIBhx5KwR3Ccn3s8rikTh Q/adNLBkZhnUGA2/X5K/VbkC7O9YyPGrzcsF5lvL02TVuI4qQhRGn8UBtyKeG32HWncn ysAWzu279fbSxfX7Db/+XI3XSM2G0pFN9Xs7+b6asWKauwDG9LQccHoCauqLfwHgHRaJ Bpng== X-Gm-Message-State: AOJu0YyZXZRP0Q7rml5b7Hmz0JS+CKLQSkMLGbCKdkrAL1vF2ycSCcUk hyf8/5hSIM12VsvEfHTM9hUGW27FiaJuDhsUdnjAKUgHKPbC+w== X-Google-Smtp-Source: AGHT+IGCXVGxPrYRMlyxGazZGvuC01LvBTV/MSEbc4YLw4OVH9FuCNgtH5OBhINL4AXS5zGF/CY2D06Y2MP9KQNYrjM= X-Received: by 2002:a05:6122:14ad:b0:4b7:3875:72d2 with SMTP id c13-20020a05612214ad00b004b7387572d2mr339546vkq.1.1704884833509; Wed, 10 Jan 2024 03:07:13 -0800 (PST) MIME-Version: 1.0 References: <20240102153828.1002295-1-ryan.roberts@arm.com> <4e7445a0-acc9-487f-999f-a2b6d03d265e@nvidia.com> <3bd5e4a3-9f67-4483-9a0e-9abb5eb783cd@arm.com> <94ebe62b-5f55-4be9-b464-4105b4692496@arm.com> <68d5ce7e-6587-47c6-bd0f-988adf5d92a4@arm.com> <714438db-b83b-41e7-bf73-b95267fa34c2@redhat.com> <8070266d-7cff-40c9-892c-2c7d1ca2e85d@arm.com> In-Reply-To: <8070266d-7cff-40c9-892c-2c7d1ca2e85d@arm.com> From: Barry Song <21cnbao@gmail.com> Date: Wed, 10 Jan 2024 19:07:01 +0800 Message-ID: Subject: Re: [RFC PATCH v1] tools/mm: Add thpmaps script to dump THP usage info To: Ryan Roberts Cc: David Hildenbrand , John Hubbard , Andrew Morton , Zenghui Yu , Matthew Wilcox , Kefeng Wang , Zi Yan , Alistair Popple , linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 8AF8EA0005 X-Stat-Signature: f4arkt5qfye3d9djeoze7869y1jo9ibi X-Rspam-User: X-HE-Tag: 1704884834-465629 X-HE-Meta: U2FsdGVkX181WjWyitT8omyzoI5f7bs7qCoQp6zBfWf7kfBcAC/ieVTV93aw4czoimptQtPE+Mp8ZGGRA9n0GgSwlOfTNyURETtU3DfSs1VETG8RH0I1EdjE9ZJ6boBvljlycp52oE0OdtTCzPeC9PolMaMTph7AquqR8FaVP2f08TplXWTudTbpLu256satX2G4QjKWstBEcVtJf3zIsJPDyTQohZW2CX5J9manm2ETK9Y3RnLFZOIhFUqHjwYrrdvRWOi2dcay0rPogViSZA1JdZPmcJ+2z3242WPQ70+UC2yguEFItKkAwxE4V929NbELp+ip7j36jfFWFKOu/MNvrcVM1kFiUsjroVusydqeIPWf5zDLxwwHm1/bUW3plc6I4G3oKU1Uwjo2O6pWms2O0YG5StYU3CeHfyRuy0WhJknkQ0NFcotCrEKdVRiCn/kvYZbNlC1h3zhfg9Q+SQySwghO5Qcujaomfas0sfTOP7RzZj/2NfBaHJkgG3sNLxh0LEijpOHTyfNIk/aEzY94ROkuY2o4nhbxGfQN1kxPLpkz6YunRSOL8mxu/zmWj1u/AXd/0zP+plPu7dREqDjOY2deyPJQXObnC+THQkGu5C2//nBTiMq+K3dFfxy4bkEUa1KQODkTwVOAUWiic4n2m0nWXbFmbMAtOUJxofLE5FKnZWSh/d5QCHYA+/QcvRnn751GNr0CiR8M7OYYh5gFNZmbdjzGXAWvBXmLmalERquD6BdKgKE9yfdH5R1B2kkG3iTdyCLXkZ4m8EDOe7NlCNR5ueks9zbwE4pl3ePvHzf7y9RIw9hOBxnhuFXPHEKFVrGpFWGSxHRm1LVSfisBRS4ZraCOX9bgST1ec3WwRN+uZrF351fLKA4FDxOtr3lSxzDmM3pph2TIUWzrzxSbkSCH+EHcbQt8JgHa6HP+le2I1tiKaRKypwarGTW94aB8OgLb1PdDERBl4gi MIUZkDk5 xadFNOiafExIpVFkin8h3zZRAoShNm9BKu0O9XiOHj+xM1GiiAC9H6e2bK1RJEn3Ad6dXUFYRZBzYAnfvQfSpTbvkh+YD3dWBYQsasK9+eOXSeY6cFBBeDTYOL7HqvFPHcxatC3YRx20IRIHHe5bVyp7imBox2+js+bWiW/WuBVJvu7Y2i0QwsSj9OwYyWpcSpgUo72w/99eGUVXU9Mi0bI3SKE1AZdZGSVimA/J502rNDkjKpDEwk74pD/F9+NnoDqaEIFvTcz+3KU9+6ZPdw/hKSBFpaqTgHZTOU65nSyRntY8HX+fUi3mwJ8bWU/jXBPKVl/AbtAF9cmsW81Af2FEiO7EVNlvCZV5/3XbpAgY2INWGM/x3y70/HpFue7fKxJaW+fHILq4u17Z/4yxp06JruYhaQUenw9ORUjUHhmnVGJ+Xh4k523QrWg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Jan 10, 2024 at 6:58=E2=80=AFPM Ryan Roberts = wrote: > > On 10/01/2024 10:54, David Hildenbrand wrote: > > On 10.01.24 11:48, Barry Song wrote: > >> On Wed, Jan 10, 2024 at 6:38=E2=80=AFPM Ryan Roberts wrote: > >>> > >>> On 10/01/2024 10:30, Barry Song wrote: > >>>> On Wed, Jan 10, 2024 at 6:23=E2=80=AFPM Ryan Roberts wrote: > >>>>> > >>>>> On 10/01/2024 09:09, Barry Song wrote: > >>>>>> On Wed, Jan 10, 2024 at 4:58=E2=80=AFPM Ryan Roberts wrote: > >>>>>>> > >>>>>>> On 10/01/2024 08:02, Barry Song wrote: > >>>>>>>> On Wed, Jan 10, 2024 at 12:16=E2=80=AFPM John Hubbard wrote: > >>>>>>>>> > >>>>>>>>> On 1/9/24 19:51, Barry Song wrote: > >>>>>>>>>> On Wed, Jan 10, 2024 at 11:35=E2=80=AFAM John Hubbard > >>>>>>>>>> wrote: > >>>>>>>>> ... > >>>>>>>>>>> Hi Ryan, > >>>>>>>>>>> > >>>>>>>>>>> One thing that immediately came up during some recent testing= of mTHP > >>>>>>>>>>> on arm64: the pid requirement is sometimes a little awkward. = I'm running > >>>>>>>>>>> tests on a machine at a time for now, inside various containe= rs and > >>>>>>>>>>> such, and it would be nice if there were an easy way to get s= ome numbers > >>>>>>>>>>> for the mTHPs across the whole machine. > >>>>>>> > >>>>>>> Just to confirm, you're expecting these "global" stats be truely = global > >>>>>>> and not > >>>>>>> per-container? (asking because you exploicitly mentioned being in= a > >>>>>>> container). > >>>>>>> If you want per-container, then you can probably just create the > >>>>>>> container in a > >>>>>>> cgroup? > >>>>>>> > >>>>>>>>>>> > >>>>>>>>>>> I'm not sure if that changes anything about thpmaps here. Pro= bably > >>>>>>>>>>> this is fine as-is. But I wanted to give some initial reactio= ns from > >>>>>>>>>>> just some quick runs: the global state would be convenient. > >>>>>>> > >>>>>>> Thanks for taking this for a spin! Appreciate the feedback. > >>>>>>> > >>>>>>>>>> > >>>>>>>>>> +1. but this seems to be impossible by scanning pagemap? > >>>>>>>>>> so may we add this statistics information in kernel just like > >>>>>>>>>> /proc/meminfo or a separate /proc/mthp_info? > >>>>>>>>>> > >>>>>>>>> > >>>>>>>>> Yes. From my perspective, it looks like the global stats are mo= re useful > >>>>>>>>> initially, and the more detailed per-pid or per-cgroup stats ar= e the > >>>>>>>>> next level of investigation. So feels odd to start with the mor= e > >>>>>>>>> detailed stats. > >>>>>>>>> > >>>>>>>> > >>>>>>>> probably because this can be done without the modification of th= e kernel. > >>>>>>> > >>>>>>> Yes indeed, as John said in an earlier thread, my previous attemp= ts to > >>>>>>> add stats > >>>>>>> directly in the kernel got pushback; DavidH was concerned that we= don't > >>>>>>> really > >>>>>>> know exectly how to account mTHPs yet > >>>>>>> (whole/partial/aligned/unaligned/per-size/etc) so didn't want to = end up > >>>>>>> adding > >>>>>>> the wrong ABI and having to maintain it forever. There has also b= een some > >>>>>>> pushback regarding adding more values to multi-value files in sys= fs, so > >>>>>>> David > >>>>>>> was suggesting coming up with a whole new scheme at some point (I= know > >>>>>>> /proc/meminfo isn't sysfs, but the equivalent files for NUMA node= s and > >>>>>>> cgroups > >>>>>>> do live in sysfs). > >>>>>>> > >>>>>>> Anyway, this script was my attempt to 1) provide a short term sol= ution to > >>>>>>> the > >>>>>>> "we need some stats" request and 2) provide a context in which to= explore > >>>>>>> what > >>>>>>> the right stats are - this script can evolve without the ABI prob= lem. > >>>>>>> > >>>>>>>> The detailed per-pid or per-cgroup is still quite useful to my c= ase in > >>>>>>>> which > >>>>>>>> we set mTHP enabled/disabled and allowed sizes according to vma = types, > >>>>>>>> eg. libc_malloc, java heaps etc. > >>>>>>>> > >>>>>>>> Different vma types can have different anon_name. So I can use t= he detailed > >>>>>>>> info to find out if specific VMAs have gotten mTHP properly and = how many > >>>>>>>> they have gotten. > >>>>>>>> > >>>>>>>>> However, Ryan did clearly say, above, "In future we may wish to > >>>>>>>>> introduce stats directly into the kernel (e.g. smaps or similar= )". And > >>>>>>>>> earlier he ran into some pushback on trying to set up /proc or = /sys > >>>>>>>>> values because this is still such an early feature. > >>>>>>>>> > >>>>>>>>> I wonder if we could put the global stats in debugfs for now? T= hat's > >>>>>>>>> specifically supposed to be a "we promise *not* to keep this AB= I stable" > >>>>>>>>> location. > >>>>>>> > >>>>>>> Now that I think about it, I wonder if we can add a --global mode= to the > >>>>>>> script > >>>>>>> (or just infer global when neither --pid nor --cgroup are provide= d). I > >>>>>>> think I > >>>>>>> should be able to determine all the physical memory ranges from /= proc/iomem, > >>>>>>> then grab all the info we need from /proc/kpageflags. We should t= hen be > >>>>>>> able to > >>>>>>> process it all in much the same way as for --pid/--cgroup and pro= vide the > >>>>>>> same > >>>>>>> stats, but it will apply globally. What do you think? > >>>>> > >>>>> Having now thought about this for a few mins (in the shower, if any= one > >>>>> wants the > >>>>> complete picture :) ), this won't quite work. This approach doesn't= have the > >>>>> virtual mapping information so the best it can do is tell us "how m= any of each > >>>>> size of THP are allocated?" - it doesn't tell us anything about whe= ther > >>>>> they are > >>>>> fully or partially mapped or what their alignment is (all necessary= if we want > >>>>> to know if they are contpte-mapped). So I don't think this approach= is > >>>>> going to > >>>>> be particularly useful. > >>>>> > >>>>> And this is also the big problem if we want to gather stats inside = the kernel; > >>>>> if we want something equivalant to /proc/meminfo's > >>>>> AnonHugePages/ShmemPmdMapped/FilePmdMapped, we need to consider not= just the > >>>>> allocation of the THP but also whether it is mapped. That's easy fo= r > >>>>> PMD-mappings, because there is only one entry to consider - when yo= u set > >>>>> it, you > >>>>> increment the number of PMD-mapped THPs, when you clear it, you dec= rement. But > >>>>> for PTE-mappings it's harder; you know the size when you are mappin= g so its > >>>>> easy > >>>>> to increment, but you can do a partial unmap, so you would need to = scan the > >>>>> PTEs > >>>>> to figure out if we are unmapping the first page of a previously > >>>>> fully-PTE-mapped THP, which is expensive. We would need a cheap mec= hanism to > >>>>> determine "is this folio fully and contiguously mapped in at least = one > >>>>> process?". > >>>> > >>>> as OPPO's approach I shared to you before is maintaining two mapcoun= t > >>>> 1. entire map > >>>> 2. subpage's map > >>>> 3. if 1 and 2 both exist, it is DoubleMapped. > >>>> > >>>> This isn't a problem for us. and everytime if we do a partial unmap, > >>>> we have an explicit > >>>> cont_pte split which will decrease the entire map and increase the > >>>> subpage's mapcount. > >>>> > >>>> but its downside is that we expose this info to mm-core. > >>> > >>> OK, but I think we have a slightly more generic situation going on wi= th the > >>> upstream; If I've understood correctly, you are using the PTE_CONT bi= t in the > >>> PTE to determne if its fully mapped? That works for your case where y= ou only > >>> have 1 size of THP that you care about (contpte-size). But for the up= stream, we > >>> have multi-size THP so we can't use the PTE_CONT bit to determine if = its fully > >>> mapped because we can only use that bit if the THP is at least 64K an= d aligned, > >>> and only on arm64. We would need a SW bit for this purpose, and the m= m would > >>> need to update that SW bit for every PTE one the full -> partial map = transition. > >> > >> My current implementation does use cont_pte but i don't think it is a = must-have. > >> we don't need a bit in PTE to know if we are partially unmapping a lar= ge folio > >> at all. > >> > >> as long as we are unmapping a part of a large folio, we do know what w= e are > >> doing. if a large folio is mapped entirely in a process, we get only > >> entire_map +1, > >> if we are unmapping a subpage of it, we get entire_map -1 and remained= subpage's > >> mapcount + 1. if we are only mapping a part of this large folio, we > >> only increase > >> its subpages' mapcount. > > > > That doesn't work as soon as you unmap a second subpage. Not to mention= that > > people ( :) ) are working on removing the subpage mapcounts. > > Yes, that was my point - Oppo's implementation relies on the bit in the P= TE to > tell the difference between unmapping the first subpage and unmapping the > others. We don't have that luxury here. right. The devil is in the details :-) > > > > > I'm going propose that as a topic for LSF/MM soon, once I get to it. > > >