From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F183018FDAB for ; Wed, 20 Aug 2025 22:50:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.188 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755730203; cv=none; b=ZxmznhoiwgylooDr8FZDT22oR4357Df1JLqwpEKX6Eg9NdcrjNtms/25hVywN7D5hvHmQ+xggJdPpjZPNQbZlB6XyzXHr1EhMltGJq+smG84xRnselxprNwmHpBgnE80/WD6F0S6mRwiiJxhjxsdrMKEbqhj+gJRT4dt17v1zZE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755730203; c=relaxed/simple; bh=eCL8GsLf3uYEog+ER630EOsJ52icnZjOcTqvUQPw3lY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=oQ/jqta+6edhDyFy99lqdwsKXwSIdBL1xmEp6yBLVS98LIPU0vKxUKDYFOm3yjiKhhUe5zs66lSe0LmRH5krd03UG6YIiXcHzB+oTnYZoWhrew7+Uu0CRZFPhebFiMYbYvgG9SZjvETLEC2MTXYc5Am0JWgnpsQVcrGbzdjAMT8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=nB9asYOT; arc=none smtp.client-ip=95.215.58.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="nB9asYOT" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1755730200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=UsdDNWT0aPaFWkXNVMSJrwjwzzlXY8/IesqRJaJrUFg=; b=nB9asYOTay3SplClMlGUjqJrUlNm40iBpD5ePjlHlcko7ltyfZdyC8gCY3jD40zA+3I/ZU UINeNtc0dBEbM/WktZuxCAHr4Af/b1uEbFb0lAqxvpNyZU8B1+B1tmnxpcRHvi2ZRfyOR/ NZWzYbGQCeZ/LrkD4Df+fjs3Ag8mOic= From: Roman Gushchin To: Kumar Kartikeya Dwivedi Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Suren Baghdasaryan , Johannes Weiner , Michal Hocko , David Rientjes , Matt Bobrowski , Song Liu , Alexei Starovoitov , Andrew Morton , linux-kernel@vger.kernel.org Subject: Re: [PATCH v1 10/14] bpf: selftests: bpf OOM handler test In-Reply-To: (Kumar Kartikeya Dwivedi's message of "Wed, 20 Aug 2025 11:33:42 +0200") References: <20250818170136.209169-1-roman.gushchin@linux.dev> <20250818170136.209169-11-roman.gushchin@linux.dev> Date: Wed, 20 Aug 2025 15:49:53 -0700 Message-ID: <878qjdobfy.fsf@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain X-Migadu-Flow: FLOW_OUT Kumar Kartikeya Dwivedi writes: > On Mon, 18 Aug 2025 at 19:02, Roman Gushchin wrote: >> >> Implement a pseudo-realistic test for the OOM handling >> functionality. >> >> The OOM handling policy which is implemented in bpf is to >> kill all tasks belonging to the biggest leaf cgroup, which >> doesn't contain unkillable tasks (tasks with oom_score_adj >> set to -1000). Pagecache size is excluded from the accounting. >> >> The test creates a hierarchy of memory cgroups, causes an >> OOM at the top level, checks that the expected process will be >> killed and checks memcg's oom statistics. >> >> Signed-off-by: Roman Gushchin >> --- >> [...] >> + >> +/* >> + * Find the largest leaf cgroup (ignoring page cache) without unkillable tasks >> + * and kill all belonging tasks. >> + */ >> +SEC("struct_ops.s/handle_out_of_memory") >> +int BPF_PROG(test_out_of_memory, struct oom_control *oc) >> +{ >> + struct task_struct *task; >> + struct mem_cgroup *root_memcg = oc->memcg; >> + struct mem_cgroup *memcg, *victim = NULL; >> + struct cgroup_subsys_state *css_pos; >> + unsigned long usage, max_usage = 0; >> + unsigned long pagecache = 0; >> + int ret = 0; >> + >> + if (root_memcg) >> + root_memcg = bpf_get_mem_cgroup(&root_memcg->css); >> + else >> + root_memcg = bpf_get_root_mem_cgroup(); >> + >> + if (!root_memcg) >> + return 0; >> + >> + bpf_rcu_read_lock(); >> + bpf_for_each(css, css_pos, &root_memcg->css, BPF_CGROUP_ITER_DESCENDANTS_POST) { >> + if (css_pos->cgroup->nr_descendants + css_pos->cgroup->nr_dying_descendants) >> + continue; >> + >> + memcg = bpf_get_mem_cgroup(css_pos); >> + if (!memcg) >> + continue; >> + >> + usage = bpf_mem_cgroup_usage(memcg); >> + pagecache = bpf_mem_cgroup_page_state(memcg, NR_FILE_PAGES); >> + >> + if (usage > pagecache) >> + usage -= pagecache; >> + else >> + usage = 0; >> + >> + if ((usage > max_usage) && mem_cgroup_killable(memcg)) { >> + max_usage = usage; >> + if (victim) >> + bpf_put_mem_cgroup(victim); >> + victim = bpf_get_mem_cgroup(&memcg->css); >> + } >> + >> + bpf_put_mem_cgroup(memcg); >> + } >> + bpf_rcu_read_unlock(); >> + >> + if (!victim) >> + goto exit; >> + >> + bpf_for_each(css_task, task, &victim->css, CSS_TASK_ITER_PROCS) { >> + struct task_struct *t = bpf_task_acquire(task); >> + >> + if (t) { >> + if (!bpf_task_is_oom_victim(task)) >> + bpf_oom_kill_process(oc, task, "bpf oom test"); > > Is there a scenario where we want to invoke bpf_oom_kill_process when > the task is not an oom victim? Not really, but... > Would it be better to subsume this check in the kfunc itself? bpf_task_is_oom_victim() is useful by itself, because if we see a task which is about to be killed, we can likely simple bail out. Let me adjust the test to reflect it.