Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp448931ybg; Tue, 28 Jul 2020 09:55:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy6ejBwOhhvefWFhHChoGgftEolY8FJ1NwkretgsKsBf7SqXXO67AKCp0XqURBwdcp+BkpX X-Received: by 2002:a17:906:7153:: with SMTP id z19mr24193553ejj.319.1595955313819; Tue, 28 Jul 2020 09:55:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595955313; cv=none; d=google.com; s=arc-20160816; b=qLOC70BImTF3ismKby676QTzyAqFD7YvSwiYkSRzG+HSDhbua3mBZRF+ntpiz4vX94 5h2FRi8PirowWh93UcrkWACljbX3ZbiHWeLqKdluhWqHFz0mCoKSXV3mM8yFPF42RGXe 7PKKo6KFVPUz2i5vLQF6+v90sDK1PXyfAoAEwByk5PwY6gGz0pUAraw9Ett7ghGzv7J3 KZEZsuUUZ/cTjSc5bsI3R3DYwI/jDT+s08pdktG51PCuHFVlmcLD1llDu1f7gQSezPqU HGEg4d+z7qNxdc5l9nYW7IlHkmKMvJGOWODON5+IUjEsMsInjV/TYkb8vq4qLzx0au7j 08gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=Yvpeg2Hti/Lz6KJkvcbKXpV9L6rmb3s/f9V+aV3VkrA=; b=v59iDEB6YiOO8ffrzdn2KmXlMDNKBW0MgN/NhzQDIpDfZcel9kUMmhaSey6/sEH5Ne Au7wqViF4hbU2XCE8ENWE+WmV/3pPcU0yAu/Ji1z+CVC7qZJdsyaV5tK12w3R4+Bp7Lb uNGJMrun+Ho98LzZO1yChi8DbOFBS2jKKlYNv4qspSp6ztFeqzAVwT1y66MGRA+VYtnh S/5yWWOWGH1GPTZQSIk0FodBg90vHDbbE+ia4RdHDtq2sw909pY5wTE8k4ILbiq8IpRJ Y41QrEt+aQXEht661ChMljssG4qRWfFVLl8/idk5Al8pi/wJl6KOnyRcMkwOsSS0B1Xf zMYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OlTI5XA+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b2si7922486edt.413.2020.07.28.09.54.51; Tue, 28 Jul 2020 09:55:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OlTI5XA+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 S1731573AbgG1Qxv (ORCPT + 99 others); Tue, 28 Jul 2020 12:53:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731597AbgG1Qxv (ORCPT ); Tue, 28 Jul 2020 12:53:51 -0400 Received: from mail-ua1-x943.google.com (mail-ua1-x943.google.com [IPv6:2607:f8b0:4864:20::943]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D47BEC0619D4 for ; Tue, 28 Jul 2020 09:53:50 -0700 (PDT) Received: by mail-ua1-x943.google.com with SMTP id g4so6714074uaq.10 for ; Tue, 28 Jul 2020 09:53:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Yvpeg2Hti/Lz6KJkvcbKXpV9L6rmb3s/f9V+aV3VkrA=; b=OlTI5XA+ZU7Eo568gSfdlWA6MNxaxKrClrzkgduxaM/FEXkDPxXZvNQnLBmu+3IxLU acAo2NhLc2fnCjvigk781R0yc19BV8juQIEXZ0HFT+sQbOqIfYMHzhnLZ/ng8ZWap5+1 X14YoTNoMOybWBoUNlj+U9K+rSVqb4ofqZWRI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Yvpeg2Hti/Lz6KJkvcbKXpV9L6rmb3s/f9V+aV3VkrA=; b=pwgro+kQ6ICPe+ia84HElemI4b+bGM8tlurCEwigfOrWehi5UAxze5D65BnLvXWPXC yYQnhaRkP4oxNribwAS+ycwNliHkYaa9BFatileug1GpnH69zXrlErr3D1gyQHG6Rnm8 hiHRknPSlwo4gbM6nisFcWLLTxEboJTW85hALSHfUxgHc1mL7Q18+69dN82l0co6Q7AM kVRAYxVGyXixn4E/N5Nd5PeZ9urxGpPK71oUn1PswVHmWelNurUBp+4z0CO9c3VjGTho l7S8M8WC7/7ysxxWS4i1X3rE3QTZx4UN3qgkrbr+NBF1BCJIWMm27+y0tj1sp433PYMj ATsw== X-Gm-Message-State: AOAM5311N46ULzHheuXKYEPIt+jCM5rfltMgVOOmmoD4Yr6h9g7wDUmW Jrg0LLehGVO9POZl/+kOsBOInlXWBo5AspcWYFapmA== X-Received: by 2002:a9f:2197:: with SMTP id 23mr10603446uac.60.1595955229872; Tue, 28 Jul 2020 09:53:49 -0700 (PDT) MIME-Version: 1.0 References: <20200728095253.v2.1.I7ebe9eaf684ddb07ae28634cb4d28cf7754641f1@changeid> In-Reply-To: <20200728095253.v2.1.I7ebe9eaf684ddb07ae28634cb4d28cf7754641f1@changeid> From: Abhishek Pandit-Subedi Date: Tue, 28 Jul 2020 09:53:37 -0700 Message-ID: Subject: Re: [PATCH v2] Bluetooth: Fix suspend notifier race To: Marcel Holtmann Cc: ChromeOS Bluetooth Upstreaming , Bluez mailing list , Miao-chen Chou , "David S. Miller" , Johan Hedberg , netdev , LKML , Jakub Kicinski Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org I sent this a bit too quick without a Fixes tag. Please disregard. v3 coming up. On Tue, Jul 28, 2020 at 9:53 AM Abhishek Pandit-Subedi wrote: > > Unregister from suspend notifications and cancel suspend preparations > before running hci_dev_do_close. Otherwise, the suspend notifier may > race with unregister and cause cmd_timeout even after hdev has been > freed. > > Below is the trace from when this panic was seen: > > [ 832.578518] Bluetooth: hci_core.c:hci_cmd_timeout() hci0: command 0x0c05 tx timeout > [ 832.586200] BUG: kernel NULL pointer dereference, address: 0000000000000000 > [ 832.586203] #PF: supervisor read access in kernel mode > [ 832.586205] #PF: error_code(0x0000) - not-present page > [ 832.586206] PGD 0 P4D 0 > [ 832.586210] PM: suspend exit > [ 832.608870] Oops: 0000 [#1] PREEMPT SMP NOPTI > [ 832.613232] CPU: 3 PID: 10755 Comm: kworker/3:7 Not tainted 5.4.44-04894-g1e9dbb96a161 #1 > [ 832.630036] Workqueue: events hci_cmd_timeout [bluetooth] > [ 832.630046] RIP: 0010:__queue_work+0xf0/0x374 > [ 832.630051] RSP: 0018:ffff9b5285f1fdf8 EFLAGS: 00010046 > [ 832.674033] RAX: ffff8a97681bac00 RBX: 0000000000000000 RCX: ffff8a976a000600 > [ 832.681162] RDX: 0000000000000000 RSI: 0000000000000009 RDI: ffff8a976a000748 > [ 832.688289] RBP: ffff9b5285f1fe38 R08: 0000000000000000 R09: ffff8a97681bac00 > [ 832.695418] R10: 0000000000000002 R11: ffff8a976a0006d8 R12: ffff8a9745107600 > [ 832.698045] usb 1-6: new full-speed USB device number 119 using xhci_hcd > [ 832.702547] R13: ffff8a9673658850 R14: 0000000000000040 R15: 000000000000001e > [ 832.702549] FS: 0000000000000000(0000) GS:ffff8a976af80000(0000) knlGS:0000000000000000 > [ 832.702550] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > [ 832.702550] CR2: 0000000000000000 CR3: 000000010415a000 CR4: 00000000003406e0 > [ 832.702551] Call Trace: > [ 832.702558] queue_work_on+0x3f/0x68 > [ 832.702562] process_one_work+0x1db/0x396 > [ 832.747397] worker_thread+0x216/0x375 > [ 832.751147] kthread+0x138/0x140 > [ 832.754377] ? pr_cont_work+0x58/0x58 > [ 832.758037] ? kthread_blkcg+0x2e/0x2e > [ 832.761787] ret_from_fork+0x22/0x40 > [ 832.846191] ---[ end trace fa93f466da517212 ]--- > > Signed-off-by: Abhishek Pandit-Subedi > Reviewed-by: Miao-chen Chou > --- > Hi Marcel, > > This fixes a race between hci_unregister_dev and the suspend notifier. > > The suspend notifier handler seemed to be scheduling commands even after > it was cleaned up and this was resulting in a panic in cmd_timeout (when > it tries to requeue the cmd_timer). > > This was tested on 5.4 kernel with a suspend+resume stress test for 500+ > iterations. I also confirmed that after a usb disconnect, the suspend > notifier times out before the USB device is probed again (fixing the > original race between the usb_disconnect + probe and the notifier). > > Thanks > Abhishek > > > Changes in v2: > * Moved oops into commit message > > net/bluetooth/hci_core.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c > index 5394ab56c915a9..4ba23b821cbf4a 100644 > --- a/net/bluetooth/hci_core.c > +++ b/net/bluetooth/hci_core.c > @@ -3767,9 +3767,10 @@ void hci_unregister_dev(struct hci_dev *hdev) > > cancel_work_sync(&hdev->power_on); > > - hci_dev_do_close(hdev); > - > unregister_pm_notifier(&hdev->suspend_notifier); > + cancel_work_sync(&hdev->suspend_prepare); > + > + hci_dev_do_close(hdev); > > if (!test_bit(HCI_INIT, &hdev->flags) && > !hci_dev_test_flag(hdev, HCI_SETUP) && > -- > 2.28.0.rc0.142.g3c755180ce-goog >