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 CAEAAC47422 for ; Fri, 26 Jan 2024 10:16:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 23EBD6B0087; Fri, 26 Jan 2024 05:16:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C8596B0089; Fri, 26 Jan 2024 05:16:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 019EE6B0099; Fri, 26 Jan 2024 05:16:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DF8566B0096 for ; Fri, 26 Jan 2024 05:16:05 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9CCBD120EE8 for ; Fri, 26 Jan 2024 10:16:05 +0000 (UTC) X-FDA: 81721056690.19.C9245F2 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) by imf10.hostedemail.com (Postfix) with ESMTP id 01CCFC0016 for ; Fri, 26 Jan 2024 10:16:03 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JUUKEgQq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.128.170 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706264164; 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=a7xDsoHGA/u32CDMqKiVNSmBCLWpWQL3KC549th7KB4=; b=kLZTjzPnUKU7GMIv7GwUrJyFtLh+C+Rdq9ozZdngf8ocEwPo5+t8g+7SyB828d5aVXfiJB Wc4rk/pjwm/zZ74xglxkITKrVsuTFa/7JytGX+DU2ZNHrg/bGw5/OJbj0/Pl4MKwQz8IOn NZZ/LCzP1/hMF81ax3EIxm1ivKZVsT8= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JUUKEgQq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.128.170 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706264164; a=rsa-sha256; cv=none; b=IO350HGxvfHT9DmpZlSqiwQIsdgEJnLmSqr3FI40ZXkaLGq/q4bVeeKCKyeeRr4Kop6Vxv /0yAEq5U5tiioSgWfWVSghXXNdcv5GmdZ30H09ID/mcwrXBKjvMMajZGN/Oz/BO4QfqJ1m FVEp7kjq97p2FL4I2ntQizw1zmqHSjg= Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-6001449a2beso1449527b3.3 for ; Fri, 26 Jan 2024 02:16:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706264163; x=1706868963; 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=a7xDsoHGA/u32CDMqKiVNSmBCLWpWQL3KC549th7KB4=; b=JUUKEgQqfenQbtD1vpoIiQMmZXD+PGp3Wz+IE0zRwKlLDq5J3cXmOxqpFlTj1pGsY+ S6N2NVE9UwhcPrYzuwo1wzhrbEDLdgjz81fwGuuGTM3io0umGHhAbrOwW7VBHEgAbwgh +X7ToL8/AHTtjzjApOyDiucNU+A59f+ad4gcRH0Sotl7MXwgr1DjaCN3gDU+q0vBm4+0 3TQloHmqq7K4IgMmFye1WdATcRMZg0mZaoKSf17HDrdcFlmocQ9Uy2DemWwnAEsGOwxL lH0Dua9zI+CeBw/h++COPhjEn28/dC8/E9ZVP2P/KCAA9FHNAqvTXMHQ9LSGLY0ctsv5 IW3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706264163; x=1706868963; 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=a7xDsoHGA/u32CDMqKiVNSmBCLWpWQL3KC549th7KB4=; b=HJ04YzfRHgWgr2+aajGdzzRyJl3x6QTh4lTCGnysbg/vIKlqpfXq+qC/KraKsD2hKI B2y6gO2cFpKEDdvsuZN+R8Q0Oi37q9MSzeFVvFGi+erKHEheRrxt2rR5YnTQ1rpgoqjT y5VvrxIgO4nxLdotHzV6Lhxj6npwVJSd1HM/AGqdtgNYyA0t/dmfhfJOMuvsdksdECLj 41a9yr20+pUoy1CkA/hNzJK/G9nvfX70uX4oL/fKI0WhWxAFzwtbqaVzl71sFMFTIR2/ 8yrROUY3pXYUz6C6LVDcDiBX7DoHOAQFl6pGSDDDC6+n3nQS5U6jqKYo6X0Y5Ab2STiq 35GQ== X-Gm-Message-State: AOJu0YzPgo/K1Fm/+WojWxXmA+pGzr6x0vU5TyWIMeGk/fnhD2+7k0XO QMkqeGGfGirAB5IYY0H5vy8lDV07zXkTAX997M+eVCY3CZOJD8Fs3CBlp2TS7EVML5/3niNzqzL l8aSD7XLZvlDtef4iOaD6MIZ3e8g= X-Google-Smtp-Source: AGHT+IG0Xnky0HjoJIF+EbsD16Da47nzSyT1OwOgbIW/hacu6rg3KU+v8c3Dpc2+8iF6FZWdCG3ojfdRGLucw01s+pw= X-Received: by 2002:a81:b60a:0:b0:5ff:44e8:6df5 with SMTP id u10-20020a81b60a000000b005ff44e86df5mr1022570ywh.18.1706264162875; Fri, 26 Jan 2024 02:16:02 -0800 (PST) MIME-Version: 1.0 References: <20240118120347.61817-1-ioworker0@gmail.com> In-Reply-To: From: Lance Yang Date: Fri, 26 Jan 2024 18:15:51 +0800 Message-ID: Subject: Re: [PATCH v2 1/1] mm/madvise: add MADV_F_COLLAPSE_LIGHT to process_madvise() To: akpm@linux-foundation.org, Michal Hocko , "Zach O'Keefe" , Yang Shi , David Hildenbrand Cc: songmuchun@bytedance.com, peterx@redhat.com, mknyszek@google.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 01CCFC0016 X-Stat-Signature: ip4e66qz7qqaa9rx7uimiu9krp1kguhb X-Rspam-User: X-HE-Tag: 1706264163-347306 X-HE-Meta: U2FsdGVkX1/YCeNInKukMYZQK2EnxEaNd6/t1yWvKDZ6OGCuramb+R/eSNYAUROzlhp88ir5A9qjYkW8RKbDfxEQMN76ZMtUgQTGEm0DoTTDUw1lCYjLGL2RB4HzffYrhLXNrcHdO3KFTFnYDkX9g8kF9YMFTM95QlbcC4pHM6PuMmOhwd23/JhoM5OmYUM5HYQmPO6xUpUXhUqW4hR7MI8GZBrkwQWYGmxAH6Eo2vZbIBvfG699BkeOoLHbPULuyxNXGrZCEaVc2aAM0tBxFaI70NL9V/hxdRwTYVMu7sq9SioqivQKMC7Z6uyuxfwfvtcrritqPL1wmr9+Xa8a4u8nKfemgHjHLYe8RTQRHGNr1UcdVAwuTJ7kGphYJz3URjwkyuFECT3r3tl6r+3QH+6bdnnf2GhSKhc56sw0XsLil4+iuMUR3MFf4cicot7UT7ziT+gpNSiMyvkpOSzq9ol37r3ZNC6tInKiQGdI+ucJe3KUNcFTOabTpc8Vdzp+/sq9Dii32J7GJx6Ax9Dj5GdkPlMzdF2wKvSH8bIKtaV9asMaln4kaildY3rgyFQuRS5z/zdqOtkOqv/5JDzfpNPlnxk8yaFxLtQ1MH6cKsT3SbeLa2V0sa/yWrTSaHkobc2wZNqKh73Z9MiUJVPEj11NUqkK8+j072Ua5wmE8zx+Y4ZZbE3+G/1INdJUMyyKX2rj/iyc+jkbRSeKC8cR/uqkfqYifja1pXFmZG7hHHn2T11d+8cWgFP3se29chPAsTwVBBs/W5yZGt1p7cAIT5y9ojBY9F3oboVC3EZrfc7HgvHuW1U+qN3Q5nifqVawPPwQ4OFItHB8NbruvkMky4NqI4Yr3XMMeHtb6OIqnCD21uY6r3d20lumCHRPzGPZA+kIkURd6eT2qt9ymXRW0BayPnU7XvlkDIDmLkAOp3HLAWukOUtPYtQWy7WFs0qjw7PlOiEXIhncEIgKXtf dih/9vAt LD5ljtC/oXcw7/KrXVStkxpQbULjTTR6AWFdCN/+C4DyfchdZ0VoWJZjQDXyJV0tN9ToWDIy+yc6VmIGtA7EeK+Dchg== 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: I would like to correct the information provided in my previous email and also provide some additional information. On Fri, Jan 26, 2024 at 2:16=E2=80=AFPM Lance Yang wr= ote: > > I=E2=80=99d like to add another real use case. > > In our company, we deploy applications using offline-online > hybrid deployment. This approach leverages the distinctive > resource utilization patterns of online services, utilizing idle > resources during various time periods by filling them with > offline jobs. This helps reduce the growing cost expenditures > for the enterprise. > > Whether for online services or offline jobs, their requirements > for THP can be roughly categorized into three types: > > * The first type aims to use huge pages as much as possible > and tolerates unpredictable stalls caused by direct reclaim > and/or compaction. > * The second type attempts to use huge pages but is relatively > latency-sensitive and cannot tolerate unpredictable stalls. > * The third type prefers not to use huge pages at all and is > extremely latency-sensitive. > > After careful consideration, we decided to prioritize the > requirements of the first type and modify the THP settings > as follows: > > echo madvise >/sys/kernel/mm/transparent_hugepage/enabled > echo defer >/sys/kernel/mm/transparent_hugepage/defrag > > With the introduction of MADV_COLLAPSE into the kernel, > it is no longer dependent on any sysfs setting under > /sys/kernel/mm/transparent_hugepage. MADV_COLLAPSE > offers the potential for fine-grained synchronous control over > the huge page allocation mechanism, marking a significant > enhancement for THP. > > If the kernel supports a more relaxed (opportunistic) > MADV_COLLAPSE, we will modify the THP settings as follows: > > echo madvise >/sys/kernel/mm/transparent_hugepage/enabled > echo madvise >/sys/kernel/mm/transparent_hugepage/defrag The correct THP settings should be: echo always >/sys/kernel/mm/transparent_hugepage/enabled echo madvise >/sys/kernel/mm/transparent_hugepage/defrag > > Then, we will use process_madvise(MADV_COLLAPSE, xx_relaxed_flag) > to address the requirements of the second type. > > Why don't we favor madvise(MADV_COLLAPSE) for the first type > of requirements? > The main reason is that these requirements are typically for offline > jobs in the Hadoop ecosystem, such as MapReduce and Spark, > which run primarily on the JVM. IIRC, the JVM currently does not > support madvise(MADV_COLLAPSE). The second type of To add, there are also some offline jobs that rely on PyTorch for machine learning model training tasks. IIRC, PyTorch also does not support madvise(MADV_COLLAPSE). Thanks, Lance > requirements is all for our in-house developed online services. > For us, integrating a more relaxed (opportunistic) > MADV_COLLAPSE into our online services is relatively > straightforward. > > By introducing various flags to MADV_COLLAPSE, we can offer > multiple synchronous allocation strategies for applications. This > fine-grained control may be more suitable for cloud-native > environments than the widespread settings under > /sys/kernel/mm/transparent_hugepage in sysfs. > > Thanks for your time! > Lance > > On Sun, Jan 21, 2024 at 11:12=E2=80=AFAM Lance Yang = wrote: > > > > Hello Everyone, > > > > For applications actively utilizing THP, the defrag mode may > > not be a very user-friendly design. Here are the reasons: > > 1. Before marking the address space with > > MADV_HUGEPAGE,it is necessary to check if > > the current configuration of the defrag mode aligns with > > their preferences. > > 2. Once the defrag mode configuration changes, these > > applications may face the risk of unpredictable stalls. > > > > THP is an important feature of the Linux kernel that can > > significantly enhance memory access performance. > > However, due to the lack of fine-grained control over > > the huge page allocation strategy, many applications > > default to not using huge pages and even recommend > > users to disable THP. This situation is regrettable. > > > > With the introduction of MADV_COLLAPSE into the kernel, > > it is not affected by the defrag mode. > > MADV_COLLAPSE offers the potential for > > fine-grained synchronous control over the huge page > > allocation mechanism, marking a significant enhancement > > for THP. > > > > By adding flags to MADV_COLLAPSE, different > > synchronous allocation strategies can be provided to > > applications. This can instill confidence in them, allowing > > them to reconsider using THP and allocate huge pages > > according to their desired synchronous allocation strategy, > > without worrying about the defrag mode configuration. > > > > BR, > > Lance > > > > > > On Thu, Jan 18, 2024 at 8:03=E2=80=AFPM Lance Yang wrote: > > > > > > This idea was inspired by MADV_COLLAPSE introduced by Zach O'Keefe[1]= . > > > > > > Allow MADV_F_COLLAPSE_LIGHT behavior for process_madvise(2) if the ca= ller > > > has CAP_SYS_ADMIN or is requesting the collapse of its own memory. > > > > > > The semantics of MADV_F_COLLAPSE_LIGHT are similar to MADV_COLLAPSE, = but > > > it avoids direct reclaim and/or compaction, quickly failing on alloc= ation > > > errors. > > > > > > This change enables a more flexible and efficient usage of memory col= lapse > > > operations, providing additional control to userspace applications fo= r > > > system-wide THP optimization. > > > > > > Semantics > > > > > > This call is independent of the system-wide THP sysfs settings, but w= ill > > > fail for memory marked VM_NOHUGEPAGE. If the ranges provided span > > > multiple VMAs, the semantics of the collapse over each VMA is indepen= dent > > > from the others. This implies a hugepage cannot cross a VMA boundary= . If > > > collapse of a given hugepage-aligned/sized region fails, the operatio= n may > > > continue to attempt collapsing the remainder of memory specified. > > > > > > The memory ranges provided must be page-aligned, but are not required= to > > > be hugepage-aligned. If the memory ranges are not hugepage-aligned, = the > > > start/end of the range will be clamped to the first/last hugepage-ali= gned > > > address covered by said range. The memory ranges must span at least = one > > > hugepage-sized region. > > > > > > All non-resident pages covered by the range will first be > > > swapped/faulted-in, before being internally copied onto a freshly > > > allocated hugepage. Unmapped pages will have their data directly > > > initialized to 0 in the new hugepage. However, for every eligible > > > hugepage aligned/sized region to-be collapsed, at least one page must > > > currently be backed by memory (a PMD covering the address range must > > > already exist). > > > > > > Allocation for the new hugepage will not enter direct reclaim and/or > > > compaction, quickly failing if allocation fails. When the system has > > > multiple NUMA nodes, the hugepage will be allocated from the node pro= viding > > > the most native pages. This operation operates on the current state o= f the > > > specified process and makes no persistent changes or guarantees on ho= w pages > > > will be mapped, constructed, or faulted in the future. > > > > > > Use Cases > > > > > > An immediate user of this new functionality is the Go runtime heap al= locator > > > that manages memory in hugepage-sized chunks. In the past, whether it= was a > > > newly allocated chunk through mmap() or a reused chunk released by > > > madvise(MADV_DONTNEED), the allocator attempted to eagerly back memor= y with > > > huge pages using madvise(MADV_HUGEPAGE)[2] and madvise(MADV_COLLAPSE)= [3] > > > respectively. However, both approaches resulted in performance issues= ; for > > > both scenarios, there could be entries into direct reclaim and/or com= paction, > > > leading to unpredictable stalls[4]. Now, the allocator can confidentl= y use > > > process_madvise(MADV_F_COLLAPSE_LIGHT) to attempt the allocation of h= uge pages. > > > > > > [1] https://github.com/torvalds/linux/commit/7d8faaf155454f8798ec5640= 4faca29a82689c77 > > > [2] https://github.com/golang/go/commit/8fa9e3beee8b0e6baa73337409961= 81268b60a3a > > > [3] https://github.com/golang/go/commit/9f9bb26880388c5bead158e9eca3b= e4b3a9bd2af > > > [4] https://github.com/golang/go/issues/63334 > > > > > > [v1] https://lore.kernel.org/lkml/20240117050217.43610-1-ioworker0@gm= ail.com/ > > > > > > Signed-off-by: Lance Yang > > > Suggested-by: Zach O'Keefe > > > Suggested-by: David Hildenbrand > > > --- > > > V1 -> V2: Treat process_madvise(MADV_F_COLLAPSE_LIGHT) as the lighter= -weight alternative > > > to madvise(MADV_COLLAPSE) > > > > > > arch/alpha/include/uapi/asm/mman.h | 1 + > > > arch/mips/include/uapi/asm/mman.h | 1 + > > > arch/parisc/include/uapi/asm/mman.h | 1 + > > > arch/xtensa/include/uapi/asm/mman.h | 1 + > > > include/linux/huge_mm.h | 5 +-- > > > include/uapi/asm-generic/mman-common.h | 1 + > > > mm/khugepaged.c | 15 ++++++-- > > > mm/madvise.c | 36 +++++++++++++++++-= -- > > > tools/include/uapi/asm-generic/mman-common.h | 1 + > > > 9 files changed, 52 insertions(+), 10 deletions(-) > > > > > > diff --git a/arch/alpha/include/uapi/asm/mman.h b/arch/alpha/include/= uapi/asm/mman.h > > > index 763929e814e9..22f23ca04f1a 100644 > > > --- a/arch/alpha/include/uapi/asm/mman.h > > > +++ b/arch/alpha/include/uapi/asm/mman.h > > > @@ -77,6 +77,7 @@ > > > #define MADV_DONTNEED_LOCKED 24 /* like DONTNEED, but drop lo= cked pages too */ > > > > > > #define MADV_COLLAPSE 25 /* Synchronous hugepage colla= pse */ > > > +#define MADV_F_COLLAPSE_LIGHT 26 /* Similar to COLLAPSE, but a= voids direct reclaim and/or compaction */ > > > > > > /* compatibility flags */ > > > #define MAP_FILE 0 > > > diff --git a/arch/mips/include/uapi/asm/mman.h b/arch/mips/include/ua= pi/asm/mman.h > > > index c6e1fc77c996..acec0b643e9c 100644 > > > --- a/arch/mips/include/uapi/asm/mman.h > > > +++ b/arch/mips/include/uapi/asm/mman.h > > > @@ -104,6 +104,7 @@ > > > #define MADV_DONTNEED_LOCKED 24 /* like DONTNEED, but drop lo= cked pages too */ > > > > > > #define MADV_COLLAPSE 25 /* Synchronous hugepage colla= pse */ > > > +#define MADV_F_COLLAPSE_LIGHT 26 /* Similar to COLLAPSE, but a= voids direct reclaim and/or compaction */ > > > > > > /* compatibility flags */ > > > #define MAP_FILE 0 > > > diff --git a/arch/parisc/include/uapi/asm/mman.h b/arch/parisc/includ= e/uapi/asm/mman.h > > > index 68c44f99bc93..812029c98cd7 100644 > > > --- a/arch/parisc/include/uapi/asm/mman.h > > > +++ b/arch/parisc/include/uapi/asm/mman.h > > > @@ -71,6 +71,7 @@ > > > #define MADV_DONTNEED_LOCKED 24 /* like DONTNEED, but drop lo= cked pages too */ > > > > > > #define MADV_COLLAPSE 25 /* Synchronous hugepage colla= pse */ > > > +#define MADV_F_COLLAPSE_LIGHT 26 /* Similar to COLLAPSE, but a= voids direct reclaim and/or compaction */ > > > > > > #define MADV_HWPOISON 100 /* poison a page for testing = */ > > > #define MADV_SOFT_OFFLINE 101 /* soft offline page for test= ing */ > > > diff --git a/arch/xtensa/include/uapi/asm/mman.h b/arch/xtensa/includ= e/uapi/asm/mman.h > > > index 1ff0c858544f..52ef463dd5b6 100644 > > > --- a/arch/xtensa/include/uapi/asm/mman.h > > > +++ b/arch/xtensa/include/uapi/asm/mman.h > > > @@ -112,6 +112,7 @@ > > > #define MADV_DONTNEED_LOCKED 24 /* like DONTNEED, but drop lo= cked pages too */ > > > > > > #define MADV_COLLAPSE 25 /* Synchronous hugepage colla= pse */ > > > +#define MADV_F_COLLAPSE_LIGHT 26 /* Similar to COLLAPSE, but a= voids direct reclaim and/or compaction */ > > > > > > /* compatibility flags */ > > > #define MAP_FILE 0 > > > diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h > > > index 5adb86af35fc..075fdb5d481a 100644 > > > --- a/include/linux/huge_mm.h > > > +++ b/include/linux/huge_mm.h > > > @@ -303,7 +303,7 @@ int hugepage_madvise(struct vm_area_struct *vma, = unsigned long *vm_flags, > > > int advice); > > > int madvise_collapse(struct vm_area_struct *vma, > > > struct vm_area_struct **prev, > > > - unsigned long start, unsigned long end); > > > + unsigned long start, unsigned long end, int beha= vior); > > > void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long= start, > > > unsigned long end, long adjust_next); > > > spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct = *vma); > > > @@ -450,7 +450,8 @@ static inline int hugepage_madvise(struct vm_area= _struct *vma, > > > > > > static inline int madvise_collapse(struct vm_area_struct *vma, > > > struct vm_area_struct **prev, > > > - unsigned long start, unsigned long= end) > > > + unsigned long start, unsigned long= end, > > > + int behavior) > > > { > > > return -EINVAL; > > > } > > > diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/as= m-generic/mman-common.h > > > index 6ce1f1ceb432..92c67bc755da 100644 > > > --- a/include/uapi/asm-generic/mman-common.h > > > +++ b/include/uapi/asm-generic/mman-common.h > > > @@ -78,6 +78,7 @@ > > > #define MADV_DONTNEED_LOCKED 24 /* like DONTNEED, but drop lo= cked pages too */ > > > > > > #define MADV_COLLAPSE 25 /* Synchronous hugepage colla= pse */ > > > +#define MADV_F_COLLAPSE_LIGHT 26 /* Similar to COLLAPSE, but a= voids direct reclaim and/or compaction */ > > > > > > /* compatibility flags */ > > > #define MAP_FILE 0 > > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > > > index 2b219acb528e..2840051c0ae2 100644 > > > --- a/mm/khugepaged.c > > > +++ b/mm/khugepaged.c > > > @@ -97,6 +97,8 @@ static struct kmem_cache *mm_slot_cache __ro_after_= init; > > > struct collapse_control { > > > bool is_khugepaged; > > > > > > + int behavior; > > > + > > > /* Num pages scanned per node */ > > > u32 node_load[MAX_NUMNODES]; > > > > > > @@ -1058,10 +1060,16 @@ static int __collapse_huge_page_swapin(struct= mm_struct *mm, > > > static int alloc_charge_hpage(struct page **hpage, struct mm_struct = *mm, > > > struct collapse_control *cc) > > > { > > > - gfp_t gfp =3D (cc->is_khugepaged ? alloc_hugepage_khugepaged_= gfpmask() : > > > - GFP_TRANSHUGE); > > > int node =3D hpage_collapse_find_target_node(cc); > > > struct folio *folio; > > > + gfp_t gfp; > > > + > > > + if (cc->is_khugepaged) > > > + gfp =3D alloc_hugepage_khugepaged_gfpmask(); > > > + else > > > + gfp =3D (cc->behavior =3D=3D MADV_F_COLLAPSE_LIGHT ? > > > + GFP_TRANSHUGE_LIGHT : > > > + GFP_TRANSHUGE); > > > > > > if (!hpage_collapse_alloc_folio(&folio, gfp, node, &cc->alloc= _nmask)) { > > > *hpage =3D NULL; > > > @@ -2697,7 +2705,7 @@ static int madvise_collapse_errno(enum scan_res= ult r) > > > } > > > > > > int madvise_collapse(struct vm_area_struct *vma, struct vm_area_stru= ct **prev, > > > - unsigned long start, unsigned long end) > > > + unsigned long start, unsigned long end, int beha= vior) > > > { > > > struct collapse_control *cc; > > > struct mm_struct *mm =3D vma->vm_mm; > > > @@ -2718,6 +2726,7 @@ int madvise_collapse(struct vm_area_struct *vma= , struct vm_area_struct **prev, > > > if (!cc) > > > return -ENOMEM; > > > cc->is_khugepaged =3D false; > > > + cc->behavior =3D behavior; > > > > > > mmgrab(mm); > > > lru_add_drain_all(); > > > diff --git a/mm/madvise.c b/mm/madvise.c > > > index 912155a94ed5..9c40226505aa 100644 > > > --- a/mm/madvise.c > > > +++ b/mm/madvise.c > > > @@ -60,6 +60,7 @@ static int madvise_need_mmap_write(int behavior) > > > case MADV_POPULATE_READ: > > > case MADV_POPULATE_WRITE: > > > case MADV_COLLAPSE: > > > + case MADV_F_COLLAPSE_LIGHT: > > > return 0; > > > default: > > > /* be safe, default to 1. list exceptions explicitly = */ > > > @@ -1082,8 +1083,9 @@ static int madvise_vma_behavior(struct vm_area_= struct *vma, > > > if (error) > > > goto out; > > > break; > > > + case MADV_F_COLLAPSE_LIGHT: > > > case MADV_COLLAPSE: > > > - return madvise_collapse(vma, prev, start, end); > > > + return madvise_collapse(vma, prev, start, end, behavi= or); > > > } > > > > > > anon_name =3D anon_vma_name(vma); > > > @@ -1178,6 +1180,7 @@ madvise_behavior_valid(int behavior) > > > case MADV_HUGEPAGE: > > > case MADV_NOHUGEPAGE: > > > case MADV_COLLAPSE: > > > + case MADV_F_COLLAPSE_LIGHT: > > > #endif > > > case MADV_DONTDUMP: > > > case MADV_DODUMP: > > > @@ -1194,6 +1197,17 @@ madvise_behavior_valid(int behavior) > > > } > > > } > > > > > > + > > > +static bool process_madvise_behavior_only(int behavior) > > > +{ > > > + switch (behavior) { > > > + case MADV_F_COLLAPSE_LIGHT: > > > + return true; > > > + default: > > > + return false; > > > + } > > > +} > > > + > > > static bool process_madvise_behavior_valid(int behavior) > > > { > > > switch (behavior) { > > > @@ -1201,6 +1215,7 @@ static bool process_madvise_behavior_valid(int = behavior) > > > case MADV_PAGEOUT: > > > case MADV_WILLNEED: > > > case MADV_COLLAPSE: > > > + case MADV_F_COLLAPSE_LIGHT: > > > return true; > > > default: > > > return false; > > > @@ -1368,6 +1383,8 @@ int madvise_set_anon_name(struct mm_struct *mm,= unsigned long start, > > > * transparent huge pages so the existing pages will not= be > > > * coalesced into THP and new pages will not be allocate= d as THP. > > > * MADV_COLLAPSE - synchronously coalesce pages into new THP. > > > + * MADV_F_COLLAPSE_LIGHT - only for process_madvise, avoids direct = reclaim and/or > > > + * compaction. > > > * MADV_DONTDUMP - the application wants to prevent pages in the gi= ven range > > > * from being included in its core dump. > > > * MADV_DODUMP - cancel MADV_DONTDUMP: no longer exclude from core = dump. > > > @@ -1394,7 +1411,8 @@ int madvise_set_anon_name(struct mm_struct *mm,= unsigned long start, > > > * -EBADF - map exists, but area maps something that isn't a file. > > > * -EAGAIN - a kernel resource was temporarily unavailable. > > > */ > > > -int do_madvise(struct mm_struct *mm, unsigned long start, size_t len= _in, int behavior) > > > +int _do_madvise(struct mm_struct *mm, unsigned long start, size_t le= n_in, > > > + int behavior, bool is_process_madvise) > > > { > > > unsigned long end; > > > int error; > > > @@ -1405,6 +1423,9 @@ int do_madvise(struct mm_struct *mm, unsigned l= ong start, size_t len_in, int beh > > > if (!madvise_behavior_valid(behavior)) > > > return -EINVAL; > > > > > > + if (!is_process_madvise && process_madvise_behavior_only(beha= vior)) > > > + return -EINVAL; > > > + > > > if (!PAGE_ALIGNED(start)) > > > return -EINVAL; > > > len =3D PAGE_ALIGN(len_in); > > > @@ -1448,9 +1469,14 @@ int do_madvise(struct mm_struct *mm, unsigned = long start, size_t len_in, int beh > > > return error; > > > } > > > > > > +int do_madvise(struct mm_struct *mm, unsigned long start, size_t len= _in, int behavior) > > > +{ > > > + return _do_madvise(mm, start, len_in, behavior, false); > > > +} > > > + > > > SYSCALL_DEFINE3(madvise, unsigned long, start, size_t, len_in, int, = behavior) > > > { > > > - return do_madvise(current->mm, start, len_in, behavior); > > > + return _do_madvise(current->mm, start, len_in, behavior, fals= e); > > > } > > > > > > SYSCALL_DEFINE5(process_madvise, int, pidfd, const struct iovec __us= er *, vec, > > > @@ -1504,8 +1530,8 @@ SYSCALL_DEFINE5(process_madvise, int, pidfd, co= nst struct iovec __user *, vec, > > > total_len =3D iov_iter_count(&iter); > > > > > > while (iov_iter_count(&iter)) { > > > - ret =3D do_madvise(mm, (unsigned long)iter_iov_addr(&= iter), > > > - iter_iov_len(&iter), behavior= ); > > > + ret =3D _do_madvise(mm, (unsigned long)iter_iov_addr(= &iter), > > > + iter_iov_len(&iter), behavior= , true); > > > if (ret < 0) > > > break; > > > iov_iter_advance(&iter, iter_iov_len(&iter)); > > > diff --git a/tools/include/uapi/asm-generic/mman-common.h b/tools/inc= lude/uapi/asm-generic/mman-common.h > > > index 6ce1f1ceb432..92c67bc755da 100644 > > > --- a/tools/include/uapi/asm-generic/mman-common.h > > > +++ b/tools/include/uapi/asm-generic/mman-common.h > > > @@ -78,6 +78,7 @@ > > > #define MADV_DONTNEED_LOCKED 24 /* like DONTNEED, but drop lo= cked pages too */ > > > > > > #define MADV_COLLAPSE 25 /* Synchronous hugepage colla= pse */ > > > +#define MADV_F_COLLAPSE_LIGHT 26 /* Similar to COLLAPSE, but a= voids direct reclaim and/or compaction */ > > > > > > /* compatibility flags */ > > > #define MAP_FILE 0 > > > -- > > > 2.33.1 > > >