mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tiffany Yang <ynaffit@google.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Martijn Coenen" <maco@android.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Christian Brauner" <brauner@kernel.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	linux-kernel@vger.kernel.org, kernel-team@android.com
Subject: Re: [PATCH v3 1/2] binder: Refactor binder_node print synchronization
Date: Thu, 08 May 2025 19:01:38 +0000	[thread overview]
Message-ID: <dbx8a57nrkod.fsf@ynaffit-andsys.c.googlers.com> (raw)
In-Reply-To: <aByjK9-FR6KsYx_7@google.com> (Alice Ryhl's message of "Thu, 8 May 2025 12:27:23 +0000")

Alice Ryhl <aliceryhl@google.com> writes:

> On Wed, May 07, 2025 at 09:10:05PM +0000, Tiffany Y. Yang wrote:
>> +/**
>> + * print_next_binder_node_ilocked() - Print binder_node from a locked list
>> + * @m:          struct seq_file for output via seq_printf()
>> + * @node:       struct binder_node to print fields of
>> + * @prev_node:	struct binder_node we hold a temporary reference to (if any)
>> + *
>> + * Helper function to handle synchronization around printing a struct
>> + * binder_node while iterating through @node->proc->nodes or the dead nodes
>> + * list. Caller must hold either @node->proc->inner_lock (for live nodes) or
>> + * binder_dead_nodes_lock. This lock will be released during the body of this
>> + * function, but it will be reacquired before returning to the caller.
>> + *
>> + * Return:	pointer to the struct binder_node we hold a tmpref on
>> + */
>> +static struct binder_node *
>> +print_next_binder_node_ilocked(struct seq_file *m, struct binder_node *node,
>> +			       struct binder_node *prev_node)
>> +{
>> +	/*
>> +	 * Take a temporary reference on the node so that isn't removed from
>> +	 * its proc's tree or the dead nodes list while we print it.
>> +	 */
>> +	binder_inc_node_tmpref_ilocked(node);
>> +	/*
>> +	 * Live nodes need to drop the inner proc lock and dead nodes need to
>> +	 * drop the binder_dead_nodes_lock before trying to take the node lock.
>> +	 */
>> +	if (node->proc)
>> +		binder_inner_proc_unlock(node->proc);
>> +	else
>> +		spin_unlock(&binder_dead_nodes_lock);
>> +	if (prev_node)
>> +		binder_put_node(prev_node);
>
> I don't buy this logic. Imagine the following scenario:
>
> 1. print_binder_proc is called, and we loop over proc->nodes.
> 2. We call binder_inner_proc_unlock(node->proc).
> 3. On another thread, binder_deferred_release() is called.
> 4. The node is removed from proc->nodes and node->proc is set to NULL.
> 5. Back in print_next_binder_node_ilocked(), we now call
>    spin_lock(&binder_dead_nodes_lock) and return.
> 6. In print_binder_proc(), we think that we hold the proc lock, but
>    actually we hold the dead nodes lock instead. BOOM.
>
> What happens with the current code is that print_binder_proc() takes the
> proc lock again after the node was removed from proc->nodes, and then it
> exits the loop because rb_next(n) returns NULL when called on a node not
> in any rb-tree.
>
> Alice


Thanks for catching this!! I think this race could be solved by passing
"proc" in as a parameter (NULL if iterating over the dead_nodes_list),
and locking/unlocking based on that instead of node->proc. WDYT?

-- 
Tiffany Y. Yang

  reply	other threads:[~2025-05-08 19:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-07 21:10 Tiffany Y. Yang
2025-05-07 21:10 ` [PATCH v3 2/2] binder: Create safe versions of binder log files Tiffany Y. Yang
2025-05-07 23:27   ` Carlos Llamas
2025-05-08 12:27 ` [PATCH v3 1/2] binder: Refactor binder_node print synchronization Alice Ryhl
2025-05-08 19:01   ` Tiffany Yang [this message]
2025-05-09  8:57     ` Alice Ryhl

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=dbx8a57nrkod.fsf@ynaffit-andsys.c.googlers.com \
    --to=ynaffit@google.com \
    --cc=aliceryhl@google.com \
    --cc=arve@android.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=joel@joelfernandes.org \
    --cc=kernel-team@android.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maco@android.com \
    --cc=surenb@google.com \
    --cc=tkjos@android.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®