mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Ajay Kaher <akaher@vmware.com>
Cc: "mhiramat@kernel.org" <mhiramat@kernel.org>,
	"shuah@kernel.org" <shuah@kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-trace-kernel@vger.kernel.org" 
	<linux-trace-kernel@vger.kernel.org>,
	"linux-kselftest@vger.kernel.org"
	<linux-kselftest@vger.kernel.org>,
	Ching-lin Yu <chinglinyu@google.com>,
	Nadav Amit <namit@vmware.com>,
	"srivatsa@csail.mit.edu" <srivatsa@csail.mit.edu>,
	Alexey Makhalov <amakhalov@vmware.com>,
	Vasavi Sirnapalli <vsirnapalli@vmware.com>,
	Tapas Kundu <tkundu@vmware.com>,
	"er.ajay.kaher@gmail.com" <er.ajay.kaher@gmail.com>
Subject: Re: [PATCH v3 03/10] eventfs: adding eventfs dir add functions
Date: Mon, 3 Jul 2023 11:08:57 -0400	[thread overview]
Message-ID: <20230703110857.2d051af5@rorschach.local.home> (raw)
In-Reply-To: <ECB0097D-A323-4CFC-9C9E-D4DA2AA6E662@vmware.com>

On Mon, 3 Jul 2023 10:13:22 +0000
Ajay Kaher <akaher@vmware.com> wrote:

> >> +/**
> >> + * eventfs_down_write - acquire write lock function
> >> + * @eventfs_rwsem: a pointer to rw_semaphore
> >> + *
> >> + * helper function to perform write lock on eventfs_rwsem
> >> + */
> >> +static void eventfs_down_write(struct rw_semaphore *eventfs_rwsem)
> >> +{
> >> +     while (!down_write_trylock(eventfs_rwsem))
> >> +             msleep(10);  
> >
> > What's this loop for? Something like that needs a very good explanation
> > in a comment. Loops like these are usually a sign of a workaround for a
> > bug in the design, or worse, simply hides an existing bug.
> >  
> 
> Yes correct, this logic is to solve deadlock:
> 
> Thread 1                             Thread 2
> down_read_nested()                                 - read lock acquired
>                                          down_write()     - waiting for write lock to acquire
> down_read_nested()                                  - deadlock
> 
> Deadlock is because rwlock wouldn’t allow read lock to be acquired if write lock is waiting.
> down_write_trylock() wouldn’t add the write lock in waiting queue, hence helps to prevent
> deadlock scenario.
> 
> I was stuck with this Deadlock, tried few methods and finally borrowed from cifs, as it’s
> upstreamed, tested and working in cifs, please refer:
> https://elixir.bootlin.com/linux/v6.3.1/source/fs/cifs/file.c#L438

I just looked at that code and the commit, and I honestly believe that
is a horrible hack, and very fragile. It's in the smb code, so it was
unlikely reviewed by anyone outside that subsystem. I really do not
want to prolificate that solution around the kernel. We need to come up
with something else.

I also think it's buggy (yes the cifs code is buggy!) because in the
comment above the down_read_nested() it says:

/*
 * nested locking. NOTE: rwsems are not allowed to recurse
 * (which occurs if the same task tries to acquire the same
 * lock instance multiple times), but multiple locks of the
 * same lock class might be taken, if the order of the locks
 * is always the same. This ordering rule can be expressed
 * to lockdep via the _nested() APIs, but enumerating the
 * subclasses that are used. (If the nesting relationship is
 * static then another method for expressing nested locking is
 * the explicit definition of lock class keys and the use of
 * lockdep_set_class() at lock initialization time.
 * See Documentation/locking/lockdep-design.rst for more details.)
 */

So this is NOT a solution (and the cifs code should be fixed too!)

Can you show me the exact backtrace where the reader lock gets taken
again? We will have to come up with a way to not take the same lock
twice.

We can also look to see if we can implement this with RCU. What exactly
is this rwsem protecting?


> 
> Looking further for your input. I will add explanation in v4.
> 
> 
> >> +}
> >> +

[..]

> >> + *
> >> + * This function creates the top of the trace event directory.
> >> + */
> >> +struct dentry *eventfs_create_events_dir(const char *name,
> >> +                                      struct dentry *parent,
> >> +                                      struct rw_semaphore *eventfs_rwsem)  
> >
> > OK, I'm going to have to really look at this. Passing in a lock to the
> > API is just broken. We need to find a way to solve this another way.  
> 
> eventfs_rwsem is a member of struct trace_array, I guess we should
> pass pointer to trace_array.

No, it should not be part of the trace_array. If we can't do this with
RCU, then we need to add a descriptor that contains the dentry that is
returned above, and have the lock held there. The caller of the
eventfs_create_events_dir() should not care about locking. That's an
implementation detail that should *not* be part of the API.

That is, if you need a lock:

struct eventfs_dentry {
	struct dentry		*dentry;
	struct rwsem		*rwsem;
};

And then get to that lock by using the container_of() macro. All
created eventfs dentry's could have this structure, where the rwsem
points to the top one. Again, that's only if we can't do this with RCU.

-- Steve


> 
> 
> > I'm about to board a plane to JFK shortly, I'm hoping to play with this
> > while flying back.
> >  
> 
> I have replied for major concerns. All other minor I will take care in v4.
> 
> Thanks a lot for giving time to eventfs patches.
> 
> - Ajay
> 

  reply	other threads:[~2023-07-03 15:09 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-01  9:00 [PATCH v3 00/10] tracing: introducing eventfs Ajay Kaher
2023-06-01  9:00 ` [PATCH v3 01/10] tracing: Require all trace events to have a TRACE_SYSTEM Ajay Kaher
2023-06-01  9:00 ` [PATCH v3 02/10] eventfs: introducing struct tracefs_inode Ajay Kaher
2023-07-01 13:25   ` Steven Rostedt
2023-07-02 14:55   ` Masami Hiramatsu
2023-06-01  9:00 ` [PATCH v3 03/10] eventfs: adding eventfs dir add functions Ajay Kaher
2023-07-01 13:54   ` Steven Rostedt
2023-07-03 10:13     ` Ajay Kaher
2023-07-03 15:08       ` Steven Rostedt [this message]
2023-07-03 18:51         ` Ajay Kaher
2023-07-03 19:52           ` Steven Rostedt
     [not found]             ` <20230709215447.536defa6@rorschach.local.home>
2023-07-10  2:17               ` Nadav Amit
2023-07-10  2:53                 ` Steven Rostedt
2023-07-10 18:53               ` Ajay Kaher
2023-07-10 19:06                 ` Steven Rostedt
     [not found]                   ` <20230710150731.4ec2b9f8@gandalf.local.home>
2023-07-10 19:10                     ` Steven Rostedt
2023-07-10 20:15                   ` Steven Rostedt
2023-07-10 21:09                 ` Steven Rostedt
2023-07-11 14:24                 ` Steven Rostedt
2023-06-01  9:00 ` [PATCH v3 04/10] eventfs: adding eventfs file " Ajay Kaher
2023-06-01  9:00 ` [PATCH v3 05/10] eventfs: adding eventfs file, directory remove function Ajay Kaher
2023-06-01  9:00 ` [PATCH v3 06/10] eventfs: adding functions to create eventfs files and directories Ajay Kaher
2023-06-01  9:00 ` [PATCH v3 07/10] eventfs: adding eventfs lookup, read, open functions Ajay Kaher
2023-06-01  9:00 ` [PATCH v3 08/10] eventfs: creating tracefs_inode_cache Ajay Kaher
2023-06-01  9:00 ` [PATCH v3 09/10] eventfs: moving tracing/events to eventfs Ajay Kaher
2023-06-10 15:25   ` kernel test robot
2023-06-13  7:06     ` Ajay Kaher
2023-06-01  9:00 ` [PATCH v3 10/10] test: ftrace: fix kprobe test for eventfs Ajay Kaher
2023-06-13  8:21   ` Masami Hiramatsu
2023-06-01  9:07 ` [PATCH v3 00/10] tracing: introducing eventfs Ajay Kaher
2023-06-19  5:38 ` Ajay Kaher
2023-06-20 15:02   ` Steven Rostedt
2023-06-21 11:42     ` Ajay Kaher
2023-06-22  3:31       ` Masami Hiramatsu

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=20230703110857.2d051af5@rorschach.local.home \
    --to=rostedt@goodmis.org \
    --cc=akaher@vmware.com \
    --cc=amakhalov@vmware.com \
    --cc=chinglinyu@google.com \
    --cc=er.ajay.kaher@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=namit@vmware.com \
    --cc=shuah@kernel.org \
    --cc=srivatsa@csail.mit.edu \
    --cc=tkundu@vmware.com \
    --cc=vsirnapalli@vmware.com \
    /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®