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 2CB59C54ED1 for ; Tue, 27 May 2025 08:14:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA8F76B0088; Tue, 27 May 2025 04:14:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B59C76B008A; Tue, 27 May 2025 04:14:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A48826B008C; Tue, 27 May 2025 04:14:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 852EB6B0088 for ; Tue, 27 May 2025 04:14:30 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E981658267 for ; Tue, 27 May 2025 08:14:29 +0000 (UTC) X-FDA: 83487975858.19.5F584F8 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) by imf01.hostedemail.com (Postfix) with ESMTP id 0BE854000D for ; Tue, 27 May 2025 08:14:27 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZpHUxWYc; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.222.173 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=1748333668; 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=KaNQTaEyUd3zGpDeyMxKJHVSUTBrFb5+VNEBkzCYBC0=; b=6nFkA1cWMWUphWHqstKxD3LyoOI1Sfl02tGM5KtV9KTafdUao9UzLYj26eIEcrtgtEWWda tmmQm/HcSVnt6pDYYGm1vVJerPM1YypV68z4gkcjm+uJaL3WQILa2XeVnx+Hvll9huwcJO QNXf2QDdcMzr36zVSex/rNhAvbynMIA= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZpHUxWYc; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.222.173 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=1748333668; a=rsa-sha256; cv=none; b=8D0UllS9n/FbLfPt7yUCDuacl0EPleL4r9aBgZ+Dm19Lcn3CQydWFHweyhAty25JU2MXQM WfUj9oMrEpE6P4CrhfczpPtH3m4tr99NMc36Gr7//i4+TMCwb1CgCqLBX6TP/UkbUTX51n avUzmPh+ALKqgdOSgiDb9IRNaqsNvCg= Received: by mail-qk1-f173.google.com with SMTP id af79cd13be357-7c5b8d13f73so311293185a.0 for ; Tue, 27 May 2025 01:14:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1748333667; x=1748938467; 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=KaNQTaEyUd3zGpDeyMxKJHVSUTBrFb5+VNEBkzCYBC0=; b=ZpHUxWYcvgKL03GKS+p9rmKBEoPw0ywnqhosbqO0saHaSNZR42pdlXbOAQeEQfaIbl /vceobkCdQp7fr77hws6hV5xS91DaSZmPzpANGi+osqBWBg1N6lb07RMVlJozEnuykih z8itf6NAuLu9dWRkxbgud7+X4tk6o2t//qZn/wseMCzbQU12InKPYk9KO2WwPQPo0V86 teBkz2YYRATGxTCCfbqtYDfloUU/79wqbcQajDTaTjIoH9NobPrYm6i+SQhh6U1q+zso 19Bq9ZUX57YLYwR5QvRfKOoh7+ewX2SruWoakznzXJuWgeTugf7ZTlxMGwt/KBpPQVkr zK1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748333667; x=1748938467; 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=KaNQTaEyUd3zGpDeyMxKJHVSUTBrFb5+VNEBkzCYBC0=; b=pKyK2as8X9mMjMl31onAZJk2qNBUaR5zCUMNz8/FZu5zx4y0hu/g+H5QsojgDSNrqf gS5mVCGFzhMGdFNbcyyzPSDuWM1KLeF92mboxbWC2qqYShn0Y34X8Ozpk1w1ga8d/XjG 6O1DWeop8JmDqMDyI25L8XOlfRsOcpWze69Nw1ouNR97Vzo+bJCW7WaAVml4Tle3pv7R h+a6Z97R1qUqycHU4RRkTZl8yy/KxHCDEV1MQJJz73tdzlBZIn2Jx+XaQBQZe2nUEFX9 DyItbcOQ4oAxEIrUQWrZEAxj6jOw7aiPP+M75BZfrlFXfAgmhkdYjC95A0J6HFZKqphT iZ9A== X-Forwarded-Encrypted: i=1; AJvYcCVAlM7QUxwgYcnYwFQEh5yzZYlViRjmSi9r5rPWqfbs1EzsIxHSZ2VFa0WbdJpLKs4l0ZTH7vSwCw==@kvack.org X-Gm-Message-State: AOJu0YwizL1o4+I4RL9N9p+StWZrj4Ha+YuqtIk2pauYOU15s4OFGOk9 jMYBLOPyrgpBeRRyiZc7r21U1apIuACDiRdgGA8tHEMvwIl36XfcLfa+KiGUmdRxZQ+j4Tkian7 FwOmxUjNV/OSAjbvm7dNyYZvmvIzlrJM= X-Gm-Gg: ASbGnctD9rpm/r2lNBk7CzSGuWQ3VivMd3bZNoACLVq5bYfIZcu0hBKLjKgDD4r4qw5 M3H8bJu7YlUQrm89mrr4DuaLMmDN0+Ba42H7Bpxwdle+u9NL33sSuS7fsqdYJSDhUKOX2hh4Fui KIY84aksxl5yyfLuLueE79GlWRFERm62N+Og== X-Google-Smtp-Source: AGHT+IF6UnFIGNe0BPCY8Q18oxqWJXZOD7gK9UnA0fz8D/U6DdNI3gV5KMfQ980OtKU+zyTPjxjZ5DIsXeoV3uwV0sw= X-Received: by 2002:a05:6214:5c4:b0:6fa:9d5a:ae6e with SMTP id 6a1803df08f44-6fa9d5ab650mr108180316d6.6.1748333667026; Tue, 27 May 2025 01:14:27 -0700 (PDT) MIME-Version: 1.0 References: <20250520060504.20251-1-laoar.shao@gmail.com> <7d8a9a5c-e0ef-4e36-9e1d-1ef8e853aed4@redhat.com> In-Reply-To: From: Yafang Shao Date: Tue, 27 May 2025 16:13:50 +0800 X-Gm-Features: AX0GCFsz_Jg5WjvVW8_sdkCN2L2PMZnv99cPfGyCRD3M5sN8ZyrxgdPDk13oj7Y 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-Stat-Signature: 5y76xupfayjqs1nbwbh3d7qix7zuqpb9 X-Rspamd-Queue-Id: 0BE854000D X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1748333667-812028 X-HE-Meta: U2FsdGVkX18xO2Q7Xws7qGpim96+tJxA7SQJDRvgl4zvsdGvMynB/iKfsDAECsroFi+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piPAXW/QaGwqERJVXvdTJH1zsHxwo6lzSSaYYopq1FTxF0gLpuc9A3+ppd8CBPheCnaOVXS+DUpfn1xFlSM5u8xa2tBrFSsaKKSim/GjjgOuVQRvjgfZscCM03MP8Ga1oxDaTE98jD6A+XJFah0SEf5FU4IWp3WPf0vm6fT8UVQpzBGjzjqkTNvWRjcnZPeo7oiaOt9YP3PmxlZmud2OfALBzNBYiJQdw== 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 3:58=E2=80=AFPM David Hildenbrand wrote: > > On 27.05.25 07:46, Yafang Shao wrote: > > On Mon, May 26, 2025 at 6:49=E2=80=AFPM David Hildenbrand wrote: > >> > >> On 26.05.25 11:37, Yafang Shao wrote: > >>> On Mon, May 26, 2025 at 4:14=E2=80=AFPM David Hildenbrand wrote: > >>>> > >>>>> Hi all, > >>>>> > >>>>> Let=E2=80=99s summarize the current state of the discussion and ide= ntify how > >>>>> to move forward. > >>>>> > >>>>> - Global-Only Control is Not Viable > >>>>> We all seem to agree that a global-only control for THP is unwise. = In > >>>>> practice, some workloads benefit from THP while others do not, so a > >>>>> one-size-fits-all approach doesn=E2=80=99t work. > >>>>> > >>>>> - Should We Use "Always" or "Madvise"? > >>>>> I suspect no one would choose 'always' in its current state. ;) > >>>> > >>>> IIRC, RHEL9 has the default set to "always" for a long time. > >>> > >>> good to know. > >>> > >>>> > >>>> I guess it really depends on how different the workloads are that yo= u > >>>> are running on the same machine. > >>> > >>> Correct. If we want to enable THP for specific workloads without > >>> modifying the kernel, we must isolate them on dedicated servers. > >>> However, this approach wastes resources and is not an acceptable > >>> solution. > >>> > >>>> > >>>> > Both Lorenzo and David propose relying on the madvise mode. How= ever,> > >>>> since madvise is an unprivileged userspace mechanism, any user can > >>>>> freely adjust their THP policy. This makes fine-grained control > >>>>> impossible without breaking userspace compatibility=E2=80=94an unde= sirable > >>>>> tradeoff. > >>>> > >>>> If required, we could look into a "sealing" mechanism, that would > >>>> essentially lock modification attempts performed by the process (i.e= ., > >>>> MADV_HUGEPAGE). > >>> > >>> If we don=E2=80=99t introduce a new THP mode and instead rely solely = on > >>> madvise, the "sealing" mechanism could either violate the intended > >>> semantics of madvise(), or simply break madvise() entirely, right? > >> > >> We would have to be a bit careful, yes. > >> > >> Errors from MADV_HUGEPAGE/MADV_NOHUGEPAGE are often ignored, because > >> these options also fail with -EINVAL on kernels without THP support. > >> > >> Ignoring MADV_NOHUGEPAGE can be problematic with userfaultfd. > >> > >> What you likely really want to do is seal when you configured > >> MADV_NOHUGEPAGE to be the default, and fail MADV_HUGEPAGE later. > >> > >>>> > >>>> The could be added on top of the current proposals that are flying > >>>> around, and could be done e.g., per-process. > >>> > >>> How about introducing a dedicated "process" mode? This would allow > >>> each process to use different THP modes=E2=80=94some in "always," oth= ers in > >>> "madvise," and the rest in "never." Future THP modes could also be > >>> added to this framework. > >> > >> We have to be really careful about not creating even more mess with mo= re > >> modes. > >> > >> How would that design look like in detail (how would we set it per > >> process etc?)? > > > > I have a preliminary idea to implement this using BPF. > > 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()/.../ raised 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. > > > We could define > > the API as follows: > > > > struct bpf_thp_ops { > > /** > > * @task_thp_mode: Get the THP mode for a specific task > > * > > * 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 BPF > > program based on the task's attributes. We would place the BPF hook in > > appropriate kernel functions. Note that this setting wouldn't be > > inherited during fork/exec - the BPF program would make the decision > > 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 dynamically > 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 adjust > the THP policy of processes that coarsely sounds ... very wrong long-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. > > > > As Liam pointed out in another thread, naming is challenging 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 might be > used by multiple processes ... I consistently use 'thread' for the latter case. 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. --=20 Regards Yafang