Re: debugfs: was: Re: [PATCH v4] printk: Userspace format enumeration support

From: Chris Down
Date: Wed Feb 17 2021 - 10:51:13 EST


Petr Mladek writes:
> > + debugfs_remove(ps->file);
>
> IMHO, we should remove the file before we remove the way how
> to read it. This should be done in the opposite order
> than in store_printk_fmt_sec().

There is a subtle issue with doing this as-is: debugfs_remove(ps->file)
cannot be called under printk_fmts_mutex, because we may deadlock due to a
pinned debugfs refcnt if debugfs_remove() and _show happen at the same time.

Do we need to call debugfs_remove(ps->file) under printk_fmts_mutex?

Ah, my concern was simultaneous entries into remove_printk_fmt_sec (which would require setting a separate flag under the mutex), but now I think about it, the module notifier synchronously waits, so that can't happen anyway.

As such it should be safe to just do:

remove()
{
mutex_lock(&printk_fmts_mutex);
ps = find_printk_fmt_sec();
mutex_unlock(&printk_fmts_mutex);

if (!ps)
return;

/* waits for _show */
debugfs_remove(ps->file);

mutex_lock(&printk_fmts_mutex);
/* Do the data structure teardown */
mutex_unlock(&printk_fmts_mutex);
}

Sounds good to me, I'll do that for v5. Thanks! :-)