linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Jordan <daniel.m.jordan@oracle.com>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Eric Biggers <ebiggers@kernel.org>,
	Linux Crypto Mailing List <linux-crypto@vger.kernel.org>,
	Steffen Klassert <steffen.klassert@secunet.com>,
	Daniel Jordan <daniel.m.jordan@oracle.com>
Subject: Re: [v2 PATCH] crypto: pcrypt - Avoid deadlock by using per-instance padata queues
Date: Tue, 26 Nov 2019 00:32:38 -0500	[thread overview]
Message-ID: <20191126053238.yxhtfbt5okcjycuy@ca-dmjordan1.us.oracle.com> (raw)
In-Reply-To: <20191119185827.nerskpvddkcsih25@gondor.apana.org.au>

On Wed, Nov 20, 2019 at 02:58:27AM +0800, Herbert Xu wrote:
>  /* Replace the internal control structure with a new one. */
> -static void padata_replace(struct padata_instance *pinst,
> -			   struct parallel_data *pd_new)
> +static int padata_replace_one(struct padata_shell *ps)
>  {
> -	struct parallel_data *pd_old = pinst->pd;
> +	struct parallel_data *pd_old = rcu_dereference_protected(ps->pd, 1);
> +	struct parallel_data *pd_new;
>  	int notification_mask = 0;
>  
> -	pinst->flags |= PADATA_RESET;
> -
> -	rcu_assign_pointer(pinst->pd, pd_new);
> +	pd_new = padata_alloc_pd(ps);
> +	if (!pd_new)
> +		return -ENOMEM;
>  
> -	synchronize_rcu();
> +	rcu_assign_pointer(ps->pd, pd_new);
>  
>  	if (!cpumask_equal(pd_old->cpumask.pcpu, pd_new->cpumask.pcpu))
>  		notification_mask |= PADATA_CPU_PARALLEL;
> @@ -510,10 +515,25 @@ static void padata_replace(struct padata_instance *pinst,
>  	if (atomic_dec_and_test(&pd_old->refcnt))
>  		padata_free_pd(pd_old);
>  
> +	return notification_mask;
> +}
> +
> +static void padata_replace(struct padata_instance *pinst)
> +{
> +	int notification_mask = 0;
> +	struct padata_shell *ps;
> +
> +	pinst->flags |= PADATA_RESET;
> +
> +	list_for_each_entry(ps, &pinst->pslist, list)
> +		notification_mask |= padata_replace_one(ps);
> +
> +	synchronize_rcu();
> +
>  	if (notification_mask)
>  		blocking_notifier_call_chain(&pinst->cpumask_change_notifier,
>  					     notification_mask,
> -					     &pd_new->cpumask);
> +					     &pinst->cpumask);
>  
>  	pinst->flags &= ~PADATA_RESET;
>  }

I think it's possible for a task in padata_do_parallel() racing with another in
padata_replace() to use a pd after free.  The synchronize_rcu() comes after the
pd_old->refcnt's are dec'd.

                                          padata_do_parallel()
                                            rcu_dereference_bh(ps->pd)
                                            // doesn't see PADATA_RESET set
padata_replace()
  pinst->flags |= PADATA_RESET
  padata_replace_one()
    rcu_assign_pointer(ps->pd, pd_new)
    atomic_dec_and_test(&pd_old->refcnt)
      padata_free_pd()
                                            atomic_inc(&pd->refcnt) // too late

If I'm not missing something, one way out is adding a list_head to
parallel_data for remembering the old pd's on a local list in padata_replace()
so that this function can loop over it and drop the refs after
synchronize_rcu().  A padata_do_parallel() call will have then had a chance to
take a ref on the pd it's using.


And, not this patch, but with the removal of flushing it seems there's no need
for PADATA_RESET, so it and its EBUSY error can go away.

  parent reply	other threads:[~2019-11-26  5:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-19 13:05 [PATCH] crypto: pcrypt - Avoid deadlock by using per-instance padata queues Herbert Xu
2019-11-19 17:37 ` Eric Biggers
2019-11-19 18:58   ` [v2 PATCH] " Herbert Xu
2019-11-22 16:28     ` Daniel Jordan
2019-11-26  5:32     ` Daniel Jordan [this message]
2019-11-26  7:58       ` [v3 " Herbert Xu
2019-11-27 19:14         ` Eric Biggers
2019-11-29  8:40           ` [PATCH] crypto: pcrypt - Do not clear MAY_SLEEP flag in original request Herbert Xu
2019-11-29 19:24             ` Eric Biggers
2019-11-27 23:38         ` [v3 PATCH] crypto: pcrypt - Avoid deadlock by using per-instance padata queues Daniel Jordan
2019-11-29 19:25         ` Eric Biggers

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=20191126053238.yxhtfbt5okcjycuy@ca-dmjordan1.us.oracle.com \
    --to=daniel.m.jordan@oracle.com \
    --cc=ebiggers@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=steffen.klassert@secunet.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;
as well as URLs for NNTP newsgroup(s).