Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Hui Zhu" <hui.zhu@linux.dev>
To: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Martin  KaFai Lau" <martin.lau@linux.dev>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Muchun Song" <muchun.song@linux.dev>,
	"JP Kobryn" <inwardvessel@gmail.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Shuah Khan" <shuah@kernel.org>,
	davem@davemloft.net, "Jakub Kicinski" <kuba@kernel.org>,
	"Jesper Dangaard Brouer" <hawk@kernel.org>,
	"Stanislav  Fomichev" <sdf@fomichev.me>,
	"KP Singh" <kpsingh@kernel.org>,
	"Tao Chen" <chen.dylane@linux.dev>,
	"Mykyta Yatsenko" <yatsenko@meta.com>,
	"Leon  Hwang" <leon.hwang@linux.dev>,
	"Anton Protopopov" <a.s.protopopov@gmail.com>,
	"Amery Hung" <ameryhung@gmail.com>,
	"Tobias  Klauser" <tklauser@distanz.ch>,
	"Eyal Birger" <eyal.birger@gmail.com>,
	"Rong Tao" <rongtao@cestc.cn>, "Hao Luo" <haoluo@google.com>,
	"Peter  Zijlstra" <peterz@infradead.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Kees Cook" <kees@kernel.org>, "Tejun Heo" <tj@kernel.org>,
	"Jeff Xu" <jeffxu@chromium.org>,
	mkoutny@suse.com, "Jan Hendrik Farr" <kernel@jfarr.cc>,
	"Christian  Brauner" <brauner@kernel.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Brian Gerst" <brgerst@gmail.com>,
	"Masahiro Yamada" <masahiroy@kernel.org>,
	"Willem de Bruijn" <willemb@google.com>,
	"Jason  Xing" <kerneljasonxing@gmail.com>,
	"Paul Chaignon" <paul.chaignon@gmail.com>,
	"Lance Yang" <lance.yang@linux.dev>,
	"Jiayuan  Chen" <jiayuan.chen@linux.dev>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"Barry Song" <baohua@kernel.org>,
	"Geliang Tang" <geliang@kernel.org>,
	linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	cgroups@vger.kernel.org, linux-mm@kvack.org,
	netdev@vger.kernel.org, linux-kselftest@vger.kernel.org
Cc: "Hui Zhu" <zhuhui@kylinos.cn>
Subject: Re: [PATCH bpf-next 2/4] bpf: add bpf_thread_wq kthread-backed workqueue with cgroup placement
Date: Thu, 13 Aug 2026 08:48:34 +0000	[thread overview]
Message-ID: <93e42fc2cd73bb1dd93fac74670899366ea58947@linux.dev> (raw)
In-Reply-To: <DKNHV09PBQZP.IRQL20BY574I@gmail.com>

> 
> On Fri Aug 7, 2026 at 9:01 AM CEST, Hui Zhu wrote:
> 
> > 
> > From: Hui Zhu <zhuhui@kylinos.cn>
> > 
> >  Introduce bpf_thread_wq, a new BPF embedded map field similar to
> >  bpf_wq but backed by a dedicated kthread_worker instead of a system
> >  workqueue. The worker kthread can be attached to a specific cgroup at
> >  init time so BPF-deferred callbacks run under the resource limits of
> >  the target cgroup.
> > 
> >  Three kfuncs are exposed:
> >  bpf_thread_wq_init(twq, map, cgroup_id, flags) [KF_SLEEPABLE]
> >  bpf_thread_wq_set_callback(twq, cb, flags, aux)
> >  bpf_thread_wq_start(twq, flags)
> > 
> >  bpf_thread_wq_init() is registered only for BPF_PROG_TYPE_SYSCALL
> >  programs. It creates a kthread worker and may attach it to a cgroup;
> >  those paths can sleep and acquire kthread and cgroup locks. Restricting
> >  init to syscall programs prevents it from running in BPF contexts that
> >  may already hold locks which could deadlock with those paths.
> > 
> >  bpf_thread_wq intentionally avoids the bpf_async infrastructure used by
> >  bpf_timer and bpf_wq. That infrastructure drives cleanup from irq_work
> >  in hardirq context, while bpf_thread_wq cancellation and final teardown
> >  may need to sleep through kthread_cancel_work_sync(),
> >  kthread_destroy_worker() and a final cgroup_put().
> >  bpf_thread_wq_cancel_and_free() therefore cancels work synchronously and
> >  drops the context reference; the last put waits for tasks-trace RCU
> >  readers and then schedules process-context work to run bpf_prog_put(),
> >  cgroup_put(), kthread_destroy_worker() and kfree().
> > 
> >  Add BTF/map support for bpf_thread_wq fields, map teardown hooks,
> >  verifier handling for the callback kfunc, and cgroup_kthread_attach() to
> >  move the worker into the requested cgroup.
> > 
> >  Supported map types are BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_LRU_HASH, and
> >  BPF_MAP_TYPE_ARRAY, consistent with bpf_wq and bpf_task_work.
> > 
> >  Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
> >  ---
> > 
> Hi Hui,
> 
> Thanks for sharing the patches. I think Sashiko and Mykyta already pointed out a
> couple of issues with the current implementation, but I would like to comment on
> the higher-level approach.
> 
> If I understood the past discussions and current set correctly, the reason for
> your choice to move from bpf_wq to bpf_thread_wq was primarily to enable correct
> CPU accounting of the work done by threads to specific cgroups.
> 
> I think this is a step in the right direction, but looking at the bigger
> picture, I feel we need a more flexible solution.
> 
> In practice, users deciding to do async reclaim through such a BPF interface
> would want to scale and compact the number of threads doing reclaim-related work
> dynamically, based on available idle resources, but also application-specific
> metrics, and I do not think the bpf_thread_wq abstraction provides enough
> flexibility in managing work scheduling related aspects precisely.
> 
> What we probably should expose is the ability for programs to manage their own
> wait queues and subscribe kthreads managed by BPF programs dynamically to them.
> User-defined policies can dictate how many threads remain active, whether they
> busy poll, and when they go to sleep, completely under the program's control.
> 
> Looking beyond this particular example, there are cases where work is stashed in
> queues, and threads draw items from the pool and process them. In such cases
> having flexibility in deciding the mapping between threads and queues is also
> important. We want to thus allow management of the work item queues from the
> program itself, and not hide it behind the API to offer the desired level of
> control. The order in which items are ranked in individual queues, and the
> order in which queues are processed horizontally by threads is also a desirable
> property.
> 
> Correctly associating the BPF-managed kthread to a cgroup should still be
> possible in a manner similar to what you did in this set. But the amount of
> control programs can exert over how work is scheduled and the level of
> concurrency will be much higher if we disaggregate and generalize each part of
> the picture (wait queues, kthreads, and work item queues, which can be
> implemented in BPF itself).
> 
> I am not aware of ways to back charge time spent to remote cgroups, but if
> desired we could also explore that option when a single thread does work on
> behalf of multiple cgroups. It is something to be explored.
> 
> I've been working on related patches, and will post RFC set for bpf_kthread and
> bpf_waitq management in due time. Until then I recommend that you continue
> experimenting with the existing async execution primitives for now.

Hi Kumar,

Agreed, the disaggregated design sounds like the right direction.

I'll keep updating the remaining commits of this series, and wait
for your bpf_kthread/bpf_waitq RFC.

Best,
Hui

> 
> > 
> > [...]
> >
>


  reply	other threads:[~2026-08-13  8:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-07  7:01 [PATCH bpf-next 0/4] bpf: BPF-driven proactive memcg reclaim Hui Zhu
2026-08-07  7:01 ` [PATCH bpf-next 1/4] mm/bpf: Add bpf_try_to_free_mem_cgroup_pages kfunc Hui Zhu
2026-08-07  7:01 ` [PATCH bpf-next 2/4] bpf: add bpf_thread_wq kthread-backed workqueue with cgroup placement Hui Zhu
2026-08-11 21:52   ` Mykyta Yatsenko
2026-08-13  3:26   ` Kumar Kartikeya Dwivedi
2026-08-13  8:48     ` Hui Zhu [this message]
2026-08-07  7:04 ` [PATCH bpf-next 3/4] selftests/bpf: add thread_wq cgroup test Hui Zhu
2026-08-07  7:04 ` [PATCH bpf-next 4/4] selftests/bpf: add memcg async reclaim test for bpf_wq/bpf_thread_wq Hui Zhu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=93e42fc2cd73bb1dd93fac74670899366ea58947@linux.dev \
    --to=hui.zhu@linux.dev \
    --cc=a.s.protopopov@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=ameryhung@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=baohua@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=brgerst@gmail.com \
    --cc=cgroups@vger.kernel.org \
    --cc=chen.dylane@linux.dev \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=eyal.birger@gmail.com \
    --cc=geliang@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=haoluo@google.com \
    --cc=hawk@kernel.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=inwardvessel@gmail.com \
    --cc=jeffxu@chromium.org \
    --cc=jiayuan.chen@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=kernel@jfarr.cc \
    --cc=kerneljasonxing@gmail.com \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=leon.hwang@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=masahiroy@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=nathan@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=paul.chaignon@gmail.com \
    --cc=peterz@infradead.org \
    --cc=rdunlap@infradead.org \
    --cc=roman.gushchin@linux.dev \
    --cc=rongtao@cestc.cn \
    --cc=sdf@fomichev.me \
    --cc=shakeel.butt@linux.dev \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=tklauser@distanz.ch \
    --cc=willemb@google.com \
    --cc=yatsenko@meta.com \
    --cc=yonghong.song@linux.dev \
    --cc=zhuhui@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox