Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp1241195rwi; Thu, 3 Nov 2022 02:58:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5IkSc0m2YGC4GOQeuTkbUNY6D85S6Q3Qxpet80v3YoxkuHj+xxfKa4ggEA9mgCMKwzTNN7 X-Received: by 2002:a65:4303:0:b0:439:f026:ccee with SMTP id j3-20020a654303000000b00439f026cceemr25223958pgq.322.1667469519612; Thu, 03 Nov 2022 02:58:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667469519; cv=none; d=google.com; s=arc-20160816; b=Fy4v9aOujK0fNV953XGLESW3eWlQlPUB9Lj41hsRYsMvOlIkVwHNmI0/TH1WW4Amae mZRikJUP1uXudzbnIeryzR/O+vzQ1KU2l0O48pMcFY6cFG9qnV8BtdGfYM1/QCL3JPln uCcsyCicC5hIMAXQ7Il/RDAO/JPXJ+H+0xbz94HwwYkFJSIIHva8scJvZ5OpDt3g/8YU nLZ/l7fLI/cagWQH2BSNUSZApLEkj8hBAd7Oox5XopxkqHc8nuqUDSaN3Q8kum7HdjAm +Ajj0GGzmDL5y0chxJpb3NCHdG1abnZLJnwDxnCbyp5u4i4oPPU79bIXFy6iNaypzRXL BvNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=he9+l/70m2lr35cIAprLwH48TPFg7J4DOfr7a4QfBkQ=; b=p7tWJbpuLDwiDYRFRF07qRrEWIKHSFSGXyRtImINQy8c46f+9RaQCH5CKVHSgmOApg cr+wilgM62RWNMVSH6/ldgVHnJ4gS1ckAw8SsCCS+nG1J+inNZGI2Q2T6FyPJU7oL/n3 OKJf/IZe0XFHRcg1upWb4kmLERuJSaW/zjzSSbqQxl1V2p0gA8xkNmvb0WK5Ol1/kWXH L1VPO1xrRDziR/sjTEyRWVMh9x/6AVe0TNLSqDdmYR087Bznv6CPLraYmuj/P3cekhdG W9X2kk++icxnAQc9OJfV6eRJifuVAyPVpPimNKLmITQHZOV1BWImeG2xpdLa5qgz8MbH t8bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=Is4ZCo3A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i17-20020a63e451000000b0046edc0a9123si432964pgk.634.2022.11.03.02.58.26; Thu, 03 Nov 2022 02:58:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@protonmail.com header.s=protonmail3 header.b=Is4ZCo3A; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230202AbiKCJi0 (ORCPT + 98 others); Thu, 3 Nov 2022 05:38:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229551AbiKCJi0 (ORCPT ); Thu, 3 Nov 2022 05:38:26 -0400 Received: from mail-0301.mail-europe.com (mail-0301.mail-europe.com [188.165.51.139]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E809A2BFA for ; Thu, 3 Nov 2022 02:38:24 -0700 (PDT) Date: Thu, 03 Nov 2022 09:38:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1667468300; x=1667727500; bh=he9+l/70m2lr35cIAprLwH48TPFg7J4DOfr7a4QfBkQ=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=Is4ZCo3AzYEW6MQ4uobjGOIb4AUmYzrEgz1vB9kRIR0yZZAHQVi4dHpZzLTgx5m+t 3G/xk7Oo3lqcaLlp31qo07vk7jIBVhgBrMA5kv1NP4tSZDO+vV5W4C5MaKQA2hmtPr xFOceBnTxi3a8QaK+h+lauZz4VcwFVfc54o5+jja2WDfMDQgf6iu+F2I5nqNqbHi37 A2SliPS8nMiGcz/6SB6GIwHXyC7TPVONi/mTfab8H98LpeM94dFDi14USiLFMQtQLy mUXTlQK9qaFzxq5kW44Dz6b6WvwqW/BWt9V+56EcZOqR0fAI3y+zEAiOTM5dR+gDib zrTlV8Vu3Ooag== To: Dennis Dai From: =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: rust nvme driver: potential sleep-in-atomic-context Message-ID: In-Reply-To: References: Feedback-ID: 27884398:user:proton MIME-Version: 1.0 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,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS,WEIRD_PORT autolearn=ham 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-kernel@vger.kernel.org On Thursday, November 3rd, 2022 at 07:12, Dennis Dai wrote: > The rust nvme driver [1] (which is still pending to be merged into > mainline [2]) has a potential sleep-in-atomic-context bug. >=20 > The potential buggy code is below >=20 > // drivers/block/nvme.rs:192 > dev.queues.lock().io.try_reserve(nr_io_queues as _)?; > // drivers/block/nvme.rs:227 > dev.queues.lock().io.try_push(io_queue.clone())?; >=20 > The queues field is wrapped in SpinLock, which means that we cannot > sleep (or indirectly call any function that may sleep) when the lock > is held. > However try_reserve function may indirectly call krealloc with a > sleepable flag GFP_KERNEL (that's default behaviour of the global rust > allocator). > The the case is similar for try_push. >=20 > I wonder if the bug could be confirmed. >=20 >=20 > [1] https://github.com/metaspace/rust-linux/commit/d88c3744d6cbdf11767e08= bad56cbfb67c4c96d0 > [2] https://lore.kernel.org/lkml/202210010816.1317F2C@keescook/ setup_io_queues is only called by dev_add which in turn is only called Nvme= Device::probe. This last function is responsible for creating the &Ref that ends up being passed to setup_io_queues. It doesn't seem like = any reference is passed to another thread between &Ref. As such= no other thread can block on the current thread due to holding the lock. A= s far as I understand this means that sleeping while the lock is held is ha= rmless. I think it would be possible to replace the &Ref argume= nt with an Pin<&mut DeviceData> argument by moving the add_dev call to befo= re Ref::::from(data). This would make it clear that only the cu= rrent thread holds a reference and would also allow using a method like get= _mut [1] to get a reference to the protected data without actually locking = the spinlock as it is statically enforced that nobody can else can hold the= lock. It seems that get_mut is missing from all of the locks offered in th= e kernel crate. I opened an issue for this. [2] [1]: https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html#method.get= _mut [2]: https://github.com/Rust-for-Linux/linux/issues/924 Cheers, Bj=C3=B6rn