mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jemmy Wong <jemmywong512@gmail.com>
To: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jemmy <jemmywong512@gmail.com>,
	"Alexei Starovoitov" <alexei.starovoitov@gmail.com>,
	"Tejun Heo" <tj@kernel.org>, "Michal Koutný" <mkoutny@suse.com>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"KP Singh" <kpsingh@kernel.org>,
	"Stanislav Fomichev" <sdf@fomichev.me>,
	"Hao Luo" <haoluo@google.com>, "Jiri Olsa" <jolsa@kernel.org>,
	"open list:CONTROL GROUP (CGROUP)" <cgroups@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v0] cgroup: Add lock guard support
Date: Fri, 6 Jun 2025 21:39:39 +0800	[thread overview]
Message-ID: <CB194CAD-0279-4A4B-90EA-A510F1A3438E@gmail.com> (raw)
In-Reply-To: <20250606104248.GA1118@cmpxchg.org>

Hi Johannes,

Thanks for your feedback. I understand your concerns about the 
indentation and visual clarity with the lock guard. 
Let me explain why I think it’s still a good choice:

1. Less Error-Prone
Lock guards automatically release the lock when the scope ends, 
even with early returns or errors. Manual lock/unlock can miss an unlock, 
causing deadlocks—lock guards prevent this.

2. Clear Critical Sections
The scoped_guard clearly shows where the critical section starts and ends, 
making the code easier to follow and maintain, even if it adds indentation.

3. Fixing the Concerns
I agree the extra indentation can look messy. I can refactor to reduce nesting 
or adjust the style to make critical sections clearer. 
I’ll also ensure the lock guard only covers the critical section, 
avoiding non-critical work inside.

Many modern programming languages provide lock guard-like constructs
to manage synchronization safely, 
showing that this approach is a widely accepted best practice. 
Manual lock/unlock is often discouraged because it’s error-prone, 
especially in complex codebases. Here are a few examples:

C++:
std::mutex m;
void example() {
    std::lock_guard<std::mutex> lock(m); // Locks m
    // Critical section
} // Automatically unlocks m when scope ends


Rust:
use std::sync::Mutex;
let m = Mutex::new(0);
{
    let mut guard = m.lock().unwrap(); // Locks mutex
    *guard += 1; // Critical section
} // Automatically unlocks here

Java:
ReentrantLock lock = new ReentrantLock();
try (lock.lock();) { // Locks and ensures unlock
    // Critical section
} // Automatically unlocks here

Python:
from threading import Lock
lock = Lock()
with lock: # Locks here
    # Critical section
# Automatically unlocks here

I think lock guards make the code safer and clearer, 
but I’m happy to tweak the implementation to address your concerns. 
Let me know your thoughts!

Best,
Jemmy


> On Jun 6, 2025, at 6:42 PM, Johannes Weiner <hannes@cmpxchg.org> wrote:
> 
> On Thu, Jun 05, 2025 at 05:54:15PM -0700, Alexei Starovoitov wrote:
>> On Thu, Jun 5, 2025 at 2:11 PM Jemmy Wong <jemmywong512@gmail.com> wrote:
>>> 
>>> This change replaces manual lock acquisition and release with lock guards
>>> to improve code robustness and reduce the risk of lock mismanagement.
>>> No functional changes to the cgroup logic are introduced.
>>> 
>>> Signed-off-by: Jemmy Wong <jemmywong512@gmail.com>
>>> 
>>> ---
>>> include/linux/cgroup.h     |   7 +
>>> kernel/bpf/cgroup.c        |  96 +++---
>>> kernel/bpf/local_storage.c |  12 +-
>> 
>> Nack for bpf bits.
>> It only uglifies the code.
> 
> I agree with this.
> 
> The extra indentation from scoped guard is unfortunate.
> 
> The guard with implicit unlock gives me the heebeejeebees - it's
> asymmetric and critical sections don't stand out visually at all.

There’re two types of guard: guard and socped_guard. 

The guard is asymptomatic and doesn’t stand out visually very well.
We could choose socped_guard which clearly defines the boundaries of critical section.

To clarify usage:
- For critical sections is part of a function, prefer socped_guard, 
    as it explicitly delineates the boundaries of the critical section.
- For critical sections spanning an entire function, prefer guard, 
    as it better suits the broader scope.

> Adjusting critical section boundaries with guard means either:
> * indentation churn to convert to scoped guard,
> * forcing abstraction splits along critical sections (non-API
>  _locked functions), which makes the code flow harder to follow,
> * or straight-up violating abstraction layering and adding
>  non-critical stuff to callers where it doesn't make sense.
> 
> I don't remember the last production bug from forgetting to drop a
> lock. Meanwhile, the things that people actually seem to struggle with
> when it comes to locks appear to become harder with those primitives.

  reply	other threads:[~2025-06-06 13:39 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-05 21:10 Jemmy Wong
2025-06-06  0:54 ` Alexei Starovoitov
2025-06-06  5:58   ` Jemmy Wong
2025-06-06 10:42   ` Johannes Weiner
2025-06-06 13:39     ` Jemmy Wong [this message]
2025-06-06  9:34 ` Michal Koutný
2025-06-06 13:51   ` Jemmy Wong

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=CB194CAD-0279-4A4B-90EA-A510F1A3438E@gmail.com \
    --to=jemmywong512@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=hannes@cmpxchg.org \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mkoutny@suse.com \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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

all inboxes | Powered by JetHome®