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 66550C5AD49 for ; Wed, 28 May 2025 02:05:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F422C6B007B; Tue, 27 May 2025 22:05:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF2C26B0082; Tue, 27 May 2025 22:05:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE31F6B0083; Tue, 27 May 2025 22:05:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BE0A76B007B for ; Tue, 27 May 2025 22:05:39 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5B406161D41 for ; Wed, 28 May 2025 02:05:39 +0000 (UTC) X-FDA: 83490675198.18.FFA3054 Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) by imf05.hostedemail.com (Postfix) with ESMTP id 7A37310000F for ; Wed, 28 May 2025 02:05:37 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mPFMZPOL; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.219.47 as permitted sender) smtp.mailfrom=laoar.shao@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=1748397937; 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=cew7xGCjKJyTF9UQgzRcATvY1dIa35IHDV0I0x7sUac=; b=pOY5p0r3oF2erOVhIGBt9VuV4aMxceUxO+f0O8vyGxQNC0U12M5a/l2cKvOURHkpF7HvZJ ehC9xcnE1e9Z7MOXDzp+/OtRxVRR6R2nnPNUVoaZtN8Yzrb+CnLKXn7rGUH8hBsDA4wWfv ppIOgX0+v3O28Np3Prxhi/IRbJmeI1c= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mPFMZPOL; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.219.47 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1748397937; a=rsa-sha256; cv=none; b=yxvgKjdybmsJlqTIpNYNad24r1Q1pzGIuEbORtGALbaEUFAcdP5bq0hJMxFIWMqLd6i+NP D3jbY/UsYWeHLFNB7XJdNH6zb2i14zPMR14PVYd0iV6U8yAfksEiXzGht69zA6UUZXNT16 H3FrsgJLFZ0xGrVVOQhVrwXVykz4N00= Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-6f2b04a6169so42638226d6.1 for ; Tue, 27 May 2025 19:05:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748397936; x=1749002736; 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=cew7xGCjKJyTF9UQgzRcATvY1dIa35IHDV0I0x7sUac=; b=mPFMZPOLlN+rkzk7830p9/V7YcZuGSAnUvIKtniTXRbJwSbCqDuuxwKMSo54sdS+8H sVuB+qhwtohd7TQpeA5i0SuEPCPB4PrB1ugFPiojEgXM7jRBQpcKKrwD3pjO9Bu1wcBn OxbkyFJ0lQerRFTc+FuAzs/kz8gMfJvy0M5EozPBUAEelnIjHFi3qVCc7f39b9Xd4Pl7 9f7CV0fnEweVJ3ZPmHMo7+5y7PSIJeeYt+gr22d7j/qoC5+b2LXuoY+wWREtd8X/ooSJ VBAOk87oupVXRrgiPZ4aFhqkFxbcO1b0aPg/bo4amoUAufkbK4cx1NzWMkDXtx4L6PHC ZOpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748397936; x=1749002736; 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=cew7xGCjKJyTF9UQgzRcATvY1dIa35IHDV0I0x7sUac=; b=Hal50Th6Nxw3CBSDCYyJm6XH1TS8VhjfrMe23zXKYnnpdKmYWw2CxVZ4FxGgwOiQUx p6rO2DyJvdwqbqHoyun4iZRyIekZPPpB+DRIIhkddDjFFwQxR6BGoEa20mRcz7UOSMr0 dEnRo5nMBev8M7LVI2RwXh32ZY+VDOdS8sv4mql2IVS0vulRHKFmGgPi/LHtwuf0cFBV XapwoJYT4WmIsJ1vkRoBdnVumkqXrOzknCEXLIF5y+iKxdqPqw+lvFxHYlluHn9p2Sy0 Bia1C+H6Y/W6T9Z4xIgXRXPFTnbvBWaFfSDy/nz4W/rKhyLeM9C7uwUwbyUjkCMnUstQ dX2Q== X-Forwarded-Encrypted: i=1; AJvYcCXLiwfr+onKcMrUWeUKVbER/37xPVg/7ctFhds2q13Uh/Mr96+4AWo7MkEOv4/cLHSrU9aVC3is1g==@kvack.org X-Gm-Message-State: AOJu0YzDIAVkFdy2Rg7yzAs83O8dWa8MP96L6r7TlolCDZ/3N74GoTNQ 87sPFsEDIK8hlcG8JY7xW20CBw4tVK8k+a4uvid72Vi8U4KwJtR9Y6I1u7b/90rRi7U16N5WkMi JqECC5f0f+NSOz166wTvFQrJ3dgMqxnc= X-Gm-Gg: ASbGnctccQdQiBhQVK2yTR80hX8lwMkJSeh5P2dW3FMsUhP3xbJ5tRHVVYCNDtxCqzg 3HIjzGpUlzyb2SpCpWv9QjGhs9ph7jetEircpCBtYXb8QWOzVIUWcxKEcIg50OjvK6mM5szBK0U p3DdyQ46jZIT0Pw+DdYGXpj1EakCkRk5G4A0f39jIdZNfl X-Google-Smtp-Source: AGHT+IGy9AW7RTT4Jh6tlgVPViqs58QoT+47agRa9FVD8kOyFA4Mvk23OuiwU4jifYLb00X2c8boUK2mylQSqQF3MyI= X-Received: by 2002:ad4:5ec9:0:b0:6e8:f3ec:5406 with SMTP id 6a1803df08f44-6fa9d13624dmr263259046d6.19.1748397936285; Tue, 27 May 2025 19:05:36 -0700 (PDT) MIME-Version: 1.0 References: <20250520060504.20251-1-laoar.shao@gmail.com> <7d8a9a5c-e0ef-4e36-9e1d-1ef8e853aed4@redhat.com> <5f0aadb1-28a8-4be0-bad9-16b738840e57@redhat.com> <5d48d0c3-89a3-44da-bc1a-9a4601f146a4@redhat.com> In-Reply-To: From: Yafang Shao Date: Wed, 28 May 2025 10:04:59 +0800 X-Gm-Features: AX0GCFtA_GfBosUVJr0x5-sgy2b7fijVdN8EBjj7HnWXkYTnkE3TdA3OmWFSgPg Message-ID: Subject: Re: [RFC PATCH v2 0/5] mm, bpf: BPF based THP adjustment To: David Hildenbrand Cc: akpm@linux-foundation.org, ziy@nvidia.com, baolin.wang@linux.alibaba.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, hannes@cmpxchg.org, usamaarif642@gmail.com, gutierrez.asier@huawei-partners.com, willy@infradead.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Stat-Signature: 9hxirshzmgtsmya8i6s541ui6gnuou7z X-Rspamd-Queue-Id: 7A37310000F X-Rspamd-Server: rspam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bgNx+lhX/SO9Pi5tah8+ztDyCqyvnwzBBuIszSE6RUwi5F/vQzvaIXFSTobF2VWch7VTuu7jOzn1OC6wLsF5TSfu/p27AZ9+5LjlLkSiqbWFqagEHFOiO/XjmpXT3W4u3Vr+4tUMd2xOQWN3jHK3AppkQkWElp1DizMMzSW8iZsFUCyS/oTLi4Yah10CS8BrVEyxaQPe3nfOT+ptOdaldpa7HyGvCwu2g86QmzBSIZ76MoN4gdV+7s98V8SDi9H0uftpqLuKHzzUM5FQWE6DeR+zW1GvVYxxcj6nTuuSACg8TKwx79jClnCtfGGAcMJLrjlS3xRXMfjZ+RobVzYBqaOG2qU+y20W67d0cO8xALu2NIVoZaYljygWPEw== 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 Tue, May 27, 2025 at 8:19=E2=80=AFPM David Hildenbrand wrote: > > On 27.05.25 11:43, Yafang Shao wrote: > > On Tue, May 27, 2025 at 5:27=E2=80=AFPM David Hildenbrand wrote: > >> > >> On 27.05.25 10:40, Yafang Shao wrote: > >>> On Tue, May 27, 2025 at 4:30=E2=80=AFPM David Hildenbrand wrote: > >>>> > >>>>>> I don't think we want to add such a mechanism (new mode) where the > >>>>>> primary configuration mechanism is through bpf. > >>>>>> > >>>>>> Maybe bpf could be used as an alternative, but we should look into= a > >>>>>> reasonable alternative first, like the discussed mctrl()/.../ rais= ed in > >>>>>> the process_madvise() series. > >>>>>> > >>>>>> No "bpf" mode in disguise, please :) > >>>>> > >>>>> This goal can be readily achieved using a BPF program. In any case,= it > >>>>> is a feasible solution. > >>>> > >>>> No BPF-only solution. > >>>> > >>>>> > >>>>>> > >>>>>>> We could define > >>>>>>> the API as follows: > >>>>>>> > >>>>>>> struct bpf_thp_ops { > >>>>>>> /** > >>>>>>> * @task_thp_mode: Get the THP mode for a specific tas= k > >>>>>>> * > >>>>>>> * Return: > >>>>>>> * - TASK_THP_ALWAYS: "always" mode > >>>>>>> * - TASK_THP_MADVISE: "madvise" mode > >>>>>>> * - TASK_THP_NEVER: "never" mode > >>>>>>> * Future modes can also be added. > >>>>>>> */ > >>>>>>> int (*task_thp_mode)(struct task_struct *p); > >>>>>>> }; > >>>>>>> > >>>>>>> For observability, we could add a "THP mode" field to > >>>>>>> /proc/[pid]/status. For example: > >>>>>>> > >>>>>>> $ grep "THP mode" /proc/123/status > >>>>>>> always > >>>>>>> $ grep "THP mode" /proc/456/status > >>>>>>> madvise > >>>>>>> $ grep "THP mode" /proc/789/status > >>>>>>> never > >>>>>>> > >>>>>>> The THP mode for each task would be determined by the attached BP= F > >>>>>>> program based on the task's attributes. We would place the BPF ho= ok in > >>>>>>> appropriate kernel functions. Note that this setting wouldn't be > >>>>>>> inherited during fork/exec - the BPF program would make the decis= ion > >>>>>>> dynamically for each task. > >>>>>> > >>>>>> What would be the mode (default) when the bpf program would not be= active? > >>>>>> > >>>>>>> This approach also enables runtime adjustments to THP modes based= on > >>>>>>> system-wide conditions, such as memory fragmentation or other > >>>>>>> performance overheads. The BPF program could adapt policies > >>>>>>> dynamically, optimizing THP behavior in response to changing > >>>>>>> workloads. > >>>>>> > >>>>>> I am not sure that is the proper way to handle these scenarios: I = never > >>>>>> heard that people would be adjusting the system-wide policy dynami= cally > >>>>>> in that way either. > >>>>>> > >>>>>> Whatever we do, we have to make sure that what we add won't > >>>>>> over-complicate things in the future. Having tooling dynamically a= djust > >>>>>> the THP policy of processes that coarsely sounds ... very wrong lo= ng-term. > >>>>> > >>>>> This is just an example demonstrating how BPF can be used to adjust > >>>>> its flexibility. Notably, all these policies can be implemented > >>>>> without modifying the kernel. > >>>> > >>>> See below on "policy". > >>>> > >>>>> > >>>>>> > >>>>>> > > As Liam pointed out in another thread, naming is challengi= ng here - > >>>>>>> "process" might not be the most accurate term for this context. > >>>>>> > >>>>>> No, it's not even a per-process thing. It is per MM, and a MM migh= t be > >>>>>> used by multiple processes ... > >>>>> > >>>>> I consistently use 'thread' for the latter case. > >>>> > >>>> You can use CLONE_VM without CLONE_THREAD ... > >>> > >>> If I understand correctly, this can only occur for shared THP but not > >>> anonymous THP. For instance, if either process allocates an anonymous > >>> THP, it would trigger the creation of a new MM. Please correct me if > >>> I'm mistaken. > >> > >> What clone(CLONE_VM) will do is essentially create a new process, that > >> shares the MM with the original process. Similar to a thread, just tha= t > >> the new process will show up in /proc/ as ... a new process, not as a > >> thread under /prod/$pid/tasks of the original process. > >> > >> Both processes will operate on the shared MM struct as if they were > >> ordinary threads. No Copy-on-Write involved. > >> > >> One example use case I've been involved in is async teardown in QEMU [= 1]. > >> > >> [1] https://kvm-forum.qemu.org/2022/ibm_async_destroy.pdf > > > > I understand what you mean, but what I'm really confused about is how > > this relates to allocating anonymous THP. If either one allocates > > anon THP, it will definitely create a new MM, right ? > > No. They work on the same address space - same MM. Either can allocate a > new anon THP and the other one would be able to modify it. No fork/CoW. > > I only bring it up because it's two "processes" sharing the same MM. And > the THP mode in your proposal would actually be per-MM and not per proces= s. > > It's confusing ... :) Thanks for the explanation. > > > > >> > >>> > >>>> > >>>> Additionally, this > >>>>> can be implemented per-MM without kernel code modifications. > >>>>> With a well-designed API, users can even implement custom THP > >>>>> policies=E2=80=94all without altering kernel code. > >>>> > >>>> You can switch between modes, that' all you can do. I wouldn't reall= y > >>>> call that "custom policy" as it is extremely limited. > >>>> > >>>> And that's exactly my point: it's basic switching between modes ... = a > >>>> reasonable policy in the future will make placement decisions and no= t > >>>> just state "always/never/madvise". > >>> > >>> Could you please elaborate further on 'make placement decisions'? As > >>> previously mentioned, we (including the broader community) really nee= d > >>> the user input to determine whether THP allocation is appropriate in = a > >>> given case. > >> > >> The glorious future were we make smarter decisions where to actually > >> place THPs even in the "always" mode. > >> > >> E.g., just because we enable "always" for a process does not mean that > >> we really want a THP everywhere; quite the opposite. > > > > So 'always' simply means "the system doesn't guarantee THP allocation > > will succeed" ? > > I mean, with THPs, there are no guarantees, ever :( > > > If that's the case, we should revisit RFC v1 [0], > > where we proposed rejecting THP allocations in certain scenarios for > > specific tasks. > > Hooking into actual page allocation during page faults (e.g., THP size, > khugepaged collapse decisions) is IMHO a much better application of ebpf > than setting a THP mode per process (or MM ... ) using epbf. > > So yes, you could drive the system in "always" mode and decide to not > allocate THPs during page faults / khugepaged for specific processes. > > IMHO that also does not contradict the VM_HUGEPAGE / VM_NOHUGEPAGE > default setting proposal: VM_HUGEPAGE could feed into the epbf program > as yet another parameter to make a decision. That seems like a viable solution. Thank you for your help. --=20 Regards Yafang