Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1946060pxb; Thu, 16 Sep 2021 21:09:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQvZ7Ldq/TiI/SaVuDb/d/pi9w1q4pDL0TNVDBA3DmoSHzokNN6wvz93Uppd6iOLJf1gg7 X-Received: by 2002:a05:6e02:1a23:: with SMTP id g3mr6377086ile.139.1631851781683; Thu, 16 Sep 2021 21:09:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631851781; cv=none; d=google.com; s=arc-20160816; b=EUCA3LpbbR5VQjGdvcwBd6Toh8a2V09y6O4kD80OGc6sS1NYcTKpCQuq3zPlt7VcP9 5nxKIxYDktHoYFCuDNhBO0G9KAfW4vsOFYi33LZ2gVI6V/9ZQw93ni0OcS6VGPyffAsm 0NzcDP8NpPQZ05r1gtQvBknndD/VtLsPsQESj+kStlj9HrILKCD5UEDhUZ/fgctdzOT6 67NjErSCym99XNLLBHPwk+wSookfrATQ3+ZhleTZkDfPLNfCSwaosJImw+Q4vo+5zNXS ADCGyIUUhNrWEl8pPpEV3jF4JhjjrSL3XP6kOGpdq45sYY7DrARIMcVB/wZLlSD3yFnW +6UA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=O3KCMVPw5uj7BpqYL3PPPo9Mi4urYewq4bpm9rV395I=; b=Oz0tBhelMSNeLqbZg2fz5H1ADJ7Z7y7jHe8gnrysKLUQrT8II6o9/rwxI5kyYEdRwz n7ZNuLD/0VXi20ipC+nbE9SAfucFXJ6PZktGM8Bk4UkKuqZnIMIt02T1z7GtEBnxzAVI a94orzTr/puvgyvcyoNtRfrkMjx61rUO0iUvVud+eVXGKtkJ030PfQSzyBhPc653qiNC KKXvU7KEVkqUeMGQnAVSVOplXZ5Oz0NDAsi+Qz9LwFPgiKY8XlOGS8XemoYlqNKMsXBc TQxs7LPFabN/N/PglBfd5FZw5aQbx1IlgnX7wFosOFF2Cn16hhISRvqN2RNu9sd5/3pX A/5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=T9MV15ub; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l7si5021391iow.113.2021.09.16.21.09.30; Thu, 16 Sep 2021 21:09:41 -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=@linuxfoundation.org header.s=korg header.b=T9MV15ub; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354950AbhIPRo2 (ORCPT + 99 others); Thu, 16 Sep 2021 13:44:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:53746 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353963AbhIPRh5 (ORCPT ); Thu, 16 Sep 2021 13:37:57 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id F0511615A2; Thu, 16 Sep 2021 16:49:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631810984; bh=NHcXO79jXv10lKu0O6cfzY4KRkcVlLCMuWwriAIMQc4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T9MV15ub3jFzJK0gkLGVDjMoKUmCAD0om3AEaACW5ta8AIvjxJJqsGJ8Z+yJm2+nb XsjnSSlsKE02LE/1ghBfsL1a/odSOuGrq1o2/4qq9Jr4LvW3gsY+7JaQQNm48Eo026 dW+Ap/Cd/sfLbyQBoiBeSs2UH9MHzgb/LrwlNDMk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kiran K , Chethan T N , Srivatsa Ravishankar , Manish Mandlik , Marcel Holtmann , Sasha Levin Subject: [PATCH 5.14 308/432] Bluetooth: Fix race condition in handling NOP command Date: Thu, 16 Sep 2021 18:00:57 +0200 Message-Id: <20210916155821.256941877@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155810.813340753@linuxfoundation.org> References: <20210916155810.813340753@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kiran K [ Upstream commit ecb71f2566673553bc067e5b0036756871d0b9d3 ] For NOP command, need to cancel work scheduled on cmd_timer, on receiving command status or commmand complete event. Below use case might lead to race condition multiple when NOP commands are queued sequentially: hci_cmd_work() { if (atomic_read(&hdev->cmd_cnt) { . . . atomic_dec(&hdev->cmd_cnt); hci_send_frame(hdev,...); schedule_delayed_work(&hdev->cmd_timer,...); } } On receiving event for first NOP, the work scheduled on hdev->cmd_timer is not cancelled and second NOP is dequeued and sent to controller. While waiting for an event for second NOP command, work scheduled on cmd_timer for the first NOP can get scheduled, resulting in sending third NOP command (sending back to back NOP commands). This might cause issues at controller side (like memory overrun, controller going unresponsive) resulting in hci tx timeouts, hardware errors etc. The fix to this issue is to cancel the delayed work scheduled on cmd_timer on receiving command status or command complete event for NOP command (this patch handles NOP command same as any other SIG command). Signed-off-by: Kiran K Reviewed-by: Chethan T N Reviewed-by: Srivatsa Ravishankar Acked-by: Manish Mandlik Signed-off-by: Marcel Holtmann Signed-off-by: Sasha Levin --- net/bluetooth/hci_event.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c index f41bd5dfc313..0d0b958b7fe7 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -3282,11 +3282,9 @@ static void hci_remote_features_evt(struct hci_dev *hdev, hci_dev_unlock(hdev); } -static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, - u16 opcode, u8 ncmd) +static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, u8 ncmd) { - if (opcode != HCI_OP_NOP) - cancel_delayed_work(&hdev->cmd_timer); + cancel_delayed_work(&hdev->cmd_timer); if (!test_bit(HCI_RESET, &hdev->flags)) { if (ncmd) { @@ -3661,7 +3659,7 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb, break; } - handle_cmd_cnt_and_timer(hdev, *opcode, ev->ncmd); + handle_cmd_cnt_and_timer(hdev, ev->ncmd); hci_req_cmd_complete(hdev, *opcode, *status, req_complete, req_complete_skb); @@ -3762,7 +3760,7 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb, break; } - handle_cmd_cnt_and_timer(hdev, *opcode, ev->ncmd); + handle_cmd_cnt_and_timer(hdev, ev->ncmd); /* Indicate request completion if the command failed. Also, if * we're not waiting for a special event and we get a success -- 2.30.2