linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Beau Belgrave <beaub@linux.microsoft.com>,
	Christian Brauner <brauner@kernel.org>
Cc: Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-trace-kernel@vger.kernel.org,
	Alexei Starovoitov <ast@kernel.org>,
	dcook@linux.microsoft.com
Subject: Re: [PATCH v2 3/5] tracing/user_events: Add auto cleanup and a flag to persist events
Date: Tue, 6 Jun 2023 18:26:56 -0700	[thread overview]
Message-ID: <CAADnVQL3bJaXW6mzTrTFTbAyCaBfiHYet+gNorF1N69a0X5TXQ@mail.gmail.com> (raw)
In-Reply-To: <20230605233900.2838-4-beaub@linux.microsoft.com>

On Mon, Jun 5, 2023 at 4:39 PM Beau Belgrave <beaub@linux.microsoft.com> wrote:
> +       /*
> +        * When the event is not enabled for auto-delete there will always
> +        * be at least 1 reference to the event. During the event creation
> +        * we initially set the refcnt to 2 to achieve this. In those cases
> +        * the caller must acquire event_mutex and after decrement check if
> +        * the refcnt is 1, meaning this is the last reference. When auto
> +        * delete is enabled, there will only be 1 ref, IE: refcnt will be
> +        * only set to 1 during creation to allow the below checks to go
> +        * through upon the last put. The last put must always be done with
> +        * the event mutex held.
> +        */
> +       if (!locked) {
> +               lockdep_assert_not_held(&event_mutex);
> +               delete = refcount_dec_and_mutex_lock(&user->refcnt, &event_mutex);
> +       } else {
> +               lockdep_assert_held(&event_mutex);
> +               delete = refcount_dec_and_test(&user->refcnt);
> +       }
> +
> +       if (!delete)
> +               return;
> +
> +       /* We now have the event_mutex in all cases */
> +
> +       if (user->reg_flags & USER_EVENT_REG_PERSIST) {
> +               /* We should not get here when persist flag is set */
> +               pr_alert("BUG: Auto-delete engaged on persistent event\n");
> +               goto out;
> +       }
> +
> +       /*
> +        * Unfortunately we have to attempt the actual destroy in a work
> +        * queue. This is because not all cases handle a trace_event_call
> +        * being removed within the class->reg() operation for unregister.
> +        */
> +       INIT_WORK(&user->put_work, delayed_destroy_user_event);
> +
> +       /*
> +        * Since the event is still in the hashtable, we have to re-inc
> +        * the ref count to 1. This count will be decremented and checked
> +        * in the work queue to ensure it's still the last ref. This is
> +        * needed because a user-process could register the same event in
> +        * between the time of event_mutex release and the work queue
> +        * running the delayed destroy. If we removed the item now from
> +        * the hashtable, this would result in a timing window where a
> +        * user process would fail a register because the trace_event_call
> +        * register would fail in the tracing layers.
> +        */
> +       refcount_set(&user->refcnt, 1);

The recnt-ing scheme is quite unorthodox.
Atomically decrementing it to zero and then immediately set it back to 1?
Smells racy.
Another process can go through the same code and do another dec and set to 1
and we'll have two work queued?
Will mutex_lock help somehow? If yes, then why atomic refcnt?

  reply	other threads:[~2023-06-07  1:27 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-05 23:38 [PATCH v2 0/5] tracing/user_events: Add auto cleanup and a flag to persist events Beau Belgrave
2023-06-05 23:38 ` [PATCH v2 1/5] tracing/user_events: Store register flags on events Beau Belgrave
2023-06-05 23:38 ` [PATCH v2 2/5] tracing/user_events: Track refcount consistently via put/get Beau Belgrave
2023-06-05 23:38 ` [PATCH v2 3/5] tracing/user_events: Add auto cleanup and a flag to persist events Beau Belgrave
2023-06-07  1:26   ` Alexei Starovoitov [this message]
2023-06-07 19:16     ` Beau Belgrave
2023-06-08 20:25   ` Steven Rostedt
2023-06-08 21:22     ` Beau Belgrave
2023-06-13 19:26   ` Steven Rostedt
2023-06-05 23:38 ` [PATCH v2 4/5] tracing/user_events: Add self-test for persist flag Beau Belgrave
2023-06-05 23:39 ` [PATCH v2 5/5] tracing/user_events: Add persist flag documentation Beau Belgrave

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=CAADnVQL3bJaXW6mzTrTFTbAyCaBfiHYet+gNorF1N69a0X5TXQ@mail.gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=ast@kernel.org \
    --cc=beaub@linux.microsoft.com \
    --cc=brauner@kernel.org \
    --cc=dcook@linux.microsoft.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.org \
    /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;
as well as URLs for NNTP newsgroup(s).