Re: [PATCH v2] sched/psi: fix race between file release and pressure write

From: Edward Adam Davis

Date: Sun Apr 12 2026 - 22:12:22 EST


On Mon, 13 Apr 2026 09:51:19 +0800, Chen Ridong wrote:
>On 2026/4/11 12:25, Edward Adam Davis wrote:
>> On Fri, 10 Apr 2026 09:14:05 -1000, Tejun Heo wrote:
>>>> static ssize_t pressure_write(struct kernfs_open_file *of, char *buf,
>>>> size_t nbytes, enum psi_res res)
>>>> {
>>>> - struct cgroup_file_ctx *ctx = of->priv;
>>>> + struct cgroup_file_ctx *ctx;
>>>> struct psi_trigger *new;
>>>> struct cgroup *cgrp;
>>>> struct psi_group *psi;
>>>> + ssize_t ret = 0;
>>>>
>>>> cgrp = cgroup_kn_lock_live(of->kn, false);
>>>> if (!cgrp)
>>>> return -ENODEV;
>>>>
>>>> + ctx = of->priv;
>>>> + if (!ctx) {
>>>
>>> This test likely isn't necessary but that's pre-existing.
>> Where?
>> Are you referring to the check for of->released within:
>> 'kernfs_fop_write_iter()->kernfs_get_active_of()'? This check is not
>> performed under the protection of the cgroup_mutex; consequently, it
>> is susceptible to race conditions, rendering the value unreliable, as
>> it could be updated at any moment.
>>>
>>>> + ret = -ENODEV;
>>>> + goto out_unlock;
>>>> + }
>>>> +
>>>> cgroup_get(cgrp);
>>>
>>> We don't need get/put if we don't drop the mutex, right?
>> I believe that is indeed the case; the cgroup_get() call here is intended
>> to facilitate subsequent operations, such as executing an smp store.
>>
>
>Sorry, I don't quite understand why get/put is needed. Could you elaborate a bit
>more?
Oh, I had overlooked the fact that acquiring the live kn lock simultaneously
performs a cgroup_get; therefore, after extending the scope of the mutex,
the original explicit get/put operations are no longer necessary. I will
issue a new version of the patch shortly to address this specific point.

Edward
BR