Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp2153215rwo; Thu, 3 Aug 2023 05:38:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlHffGjweJv+S8QnX9jOLHhv4KoFGP7k9AvD4snYRuoTiwLUV8EVB1jB8zmHK+ygbCezbfrS X-Received: by 2002:a05:6a21:339f:b0:12e:4d86:c017 with SMTP id yy31-20020a056a21339f00b0012e4d86c017mr23169354pzb.10.1691066317776; Thu, 03 Aug 2023 05:38:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691066317; cv=none; d=google.com; s=arc-20160816; b=jiO5YLoRzemf4jA8WDxcVFwNC2Zmmt+gl4QmwuwywN6aV3jNRo6KGYxf9OoJ2OFGfS 19eQBj5Qg3frnLo4y04zClSGtJlj0uvkEOajeJHr8Zwd253ge30vdFVWN4SSCYOJt5CV VIDLcJW6COc4+Y0Xj2+On4Hf6N0xnC4902hskIGQ3okJ7x1Ho7XtlRZ15LMt12CL6aiG HFtUS/gGWpiYRzQg2OCiuDghnYEJHUJurkY7/tVsUgf+aRUO/3D2pn6gAtuEGjjLRfgj bKQJ2tx9Dw1muayZVKsve9UYLNI1LDCPmo7njqYbSsXzuJGXuyw9XNhqYPtQMDHJKsf5 mmZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=aN3/UY6RCA2oDcAiQC4DSHQmT3J1AIJKbAMvJpn19R4=; fh=WNC/3iejx0lgb+CReVtlZMG//pRhPIpeV3mXCRen3Sw=; b=tvKwER8FBHhEU1zL7EpXhMgGS2m+I1rz43wIQ1bwgHPErZlrPYgzOsN4c3FeX+kbQ0 /sVyc7QEAR2XdegtIjHxqjOwVW4O2YaoelS+YE5QAhUquB3HBfdY0wDeoCeluaCJOWN1 9Nb0hITTpJXyR9xG41q8ii59Irz2sDlO2/j2Io8ofxb5nGtLF5w7W2cgo9K+VRCsfpVI NheZx26FnbGBFn8zqaNSp8EW6FHOIE6cQP6UzOCFxxHXBkMZi/XPY3C2lpuJoKXxex7K zflv6FRw/3vmZis9iz/rdpy3zndm/kZJHamoHo2EIFX0vKzqz4N33/1laKstGMLznAFK UdYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZuhuqSed; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n127-20020a632785000000b0055c7eb346e1si12739497pgn.757.2023.08.03.05.38.15; Thu, 03 Aug 2023 05:38:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZuhuqSed; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235838AbjHCMNU (ORCPT + 99 others); Thu, 3 Aug 2023 08:13:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235832AbjHCMNS (ORCPT ); Thu, 3 Aug 2023 08:13:18 -0400 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24B273581 for ; Thu, 3 Aug 2023 05:13:17 -0700 (PDT) Received: by mail-oi1-x232.google.com with SMTP id 5614622812f47-3a426e70575so540686b6e.0 for ; Thu, 03 Aug 2023 05:13:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1691064796; x=1691669596; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=aN3/UY6RCA2oDcAiQC4DSHQmT3J1AIJKbAMvJpn19R4=; b=ZuhuqSedVHSX8Rkghnf9yMCrQ17yyZVxBEC3uzV0zJO+V276F4NU5ImZmz/JdV9sSw OEW31RYv+BA02Bql8u+Ig91T/X6xNMl5lVTulhi++RJdhph5U2uen2lnC48c90I8RBOS dyMKAV3mpaqXbGZoVVaV/SaBeuYagidIW7uAE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691064796; x=1691669596; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aN3/UY6RCA2oDcAiQC4DSHQmT3J1AIJKbAMvJpn19R4=; b=PD7mvflBzAc5Un15ltENFyq5oBogdjNMuNqFSeZnwprmhGKDL4wOanKyBLGPvMYOGS mE5OwUBOtZwHOzpFoCiDK5xblnJnukGKXVxkd0l2muUHSlELH/om8XIctYiXv/DJj5V4 Dh/DW+XURz6qiCmukjPu9E9Ja/obfGNjZtK2NIl/6Ei/oHQkTsH38AkvPNi4qdH46k9z o7YhYmaxh4HZCLC8ktF0q/4HgbzkjoMWOXQY1nyHboPb6QiOrIPckKlnWxwX4OVZIKrt X26Dkp26VXjvbU80GUiXoXeQINpJDF0hbmZ7QUphDQawAIxI5HtqXDm5FM4YXveNREL+ oHfg== X-Gm-Message-State: ABy/qLZMNC5TaAF7FpwZmGFp7wKsfDGe12+empeFDE2RwvzwHf4wc1cJ qcxwkHrwhujgiPttVtK0aN3FNkAgEOLhG3s9orNs/Q== X-Received: by 2002:a05:6808:14cd:b0:3a6:fb15:399b with SMTP id f13-20020a05680814cd00b003a6fb15399bmr19380000oiw.36.1691064796306; Thu, 03 Aug 2023 05:13:16 -0700 (PDT) MIME-Version: 1.0 References: <20230802170923.1151605-1-revest@chromium.org> In-Reply-To: From: Florent Revest Date: Thu, 3 Aug 2023 14:13:05 +0200 Message-ID: Subject: Re: crypto: api - Use work queue in crypto_destroy_instance To: Herbert Xu Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-sctp@vger.kernel.org, davem@davemloft.net, hillf.zj@alibaba-inc.com, marcelo.leitner@gmail.com, lucien.xin@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Thu, Aug 3, 2023 at 11:59=E2=80=AFAM Herbert Xu wrote: > > On Wed, Aug 02, 2023 at 07:09:22PM +0200, Florent Revest wrote: > > > > I found that the following program reliably reproduces a "BUG: sleeping= function > > called from invalid context" backtrace in crypto code: > > Great detective work! And thanks for cc'ing me :) Thank you! > This is definitely a bug in the Crypto API. Although it's hard to > trigger because you need to unregister the instance before the last > user frees it in atomic context. The fact that it triggers for your > test program probably means that we're not creating the template > correctly and it gets unregistered as soon as it's created. > > As to the fix I think we should move the work into crypto_destroy_instanc= e > since that's the function that is being called from atomic context > and then does something that should only be done from process context. Sounds good to me :) > So here's my patch based on your work: FWIW, Tested-by: Florent Revest Acked-by: Florent Revest > ---8<--- > The function crypto_drop_spawn expects to be called in process > context. However, when an instance is unregistered while it still > has active users, the last user may cause the instance to be freed > in atomic context. > > Fix this by delaying the freeing to a work queue. > > Fixes: 6bfd48096ff8 ("[CRYPTO] api: Added spawns") > Reported-by: Florent Revest > Reported-by: syzbot+d769eed29cc42d75e2a3@syzkaller.appspotmail.com > Reported-by: syzbot+610ec0671f51e838436e@syzkaller.appspotmail.com > Signed-off-by: Herbert Xu > > diff --git a/crypto/algapi.c b/crypto/algapi.c > index 5e7cd603d489..4fe95c448047 100644 > --- a/crypto/algapi.c > +++ b/crypto/algapi.c > @@ -17,6 +17,7 @@ > #include > #include > #include > +#include > > #include "internal.h" > > @@ -74,15 +75,26 @@ static void crypto_free_instance(struct crypto_instan= ce *inst) > inst->alg.cra_type->free(inst); > } > > -static void crypto_destroy_instance(struct crypto_alg *alg) > +static void crypto_destroy_instance_workfn(struct work_struct *w) > { > - struct crypto_instance *inst =3D (void *)alg; > + struct crypto_instance *inst =3D container_of(w, struct crypto_in= stance, > + free_work); > struct crypto_template *tmpl =3D inst->tmpl; > > crypto_free_instance(inst); > crypto_tmpl_put(tmpl); > } > > +static void crypto_destroy_instance(struct crypto_alg *alg) > +{ > + struct crypto_instance *inst =3D container_of(alg, > + struct crypto_instanc= e, > + alg); > + > + INIT_WORK(&inst->free_work, crypto_destroy_instance_workfn); > + schedule_work(&inst->free_work); > +} > + > /* > * This function adds a spawn to the list secondary_spawns which > * will be used at the end of crypto_remove_spawns to unregister > diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h > index 6156161b181f..ca86f4c6ba43 100644 > --- a/include/crypto/algapi.h > +++ b/include/crypto/algapi.h > @@ -12,6 +12,7 @@ > #include > #include > #include > +#include > > /* > * Maximum values for blocksize and alignmask, used to allocate > @@ -82,6 +83,8 @@ struct crypto_instance { > struct crypto_spawn *spawns; > }; > > + struct work_struct free_work; > + > void *__ctx[] CRYPTO_MINALIGN_ATTR; > };