Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp49566pxa; Fri, 21 Aug 2020 00:24:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZUbByVLeaiOAzicwTJoVWi2n5nV7cyRzCw0Sg/ltkMrFIkojaOSCj1LlYft7KBJv3BQ6s X-Received: by 2002:a05:6402:6da:: with SMTP id n26mr1574281edy.262.1597994639936; Fri, 21 Aug 2020 00:23:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597994639; cv=none; d=google.com; s=arc-20160816; b=iGOnUNIbbtRQNOnzbvdEGIpxgQ1xblpV3Ev2V2iEWvh2CJRAMOeWCf2AcgW9IFTGd7 Z/Dyln3s0QpMUilSWiCk/BciauI5MK7lVlWNz1T2shNNaDl0WSCwOjcJgEr1ZPsNFMZn /SX0latjqZxvKNS3d1n0BsHfkIFdQ4xm5R9SQgJTNtEck+GgNsTRQeDe58CpGZV5wjM+ /lRJQbW3uAwh0ZEKlX0oZzySKHYtM6Fet8xp2EXau5gfkS1udjABtiqnwv025/Iqkg1D M1PFo02fcj1KSm6vxwLCXMVhQuDKh/w7s7d8vA1WKYve9CryVy2Ylddc8TeXUu4kvnOG 9N4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=cPRYxgB2XxX7Z1aiBzTSI/wL9Ebvg2gjReZiaambCFo=; b=rxGsceriXjE718BeDuQpCJiPhMh1vlaBi8nyYtJ9psHYG68oxCFADsJoHyPNcScbzj rLg3CrpnDVFmZJ6aRwmtDG8w2lh5C2+b+j7W6Y1Ae2jHDbZvS6FMqI10xZkv+WZimME1 pDW4bSAU9GVNuH/p2Dqckcljd9bRiQRN7JDjcXKbNuwQfsXSRfwtKHPAnXmUbaMpW7by AbqQgZ81cjuTR461yYuI5KD6id/t0A6WwdcTJNMHmnpL2HyQkkMrtl1NmljeB3NOuTef 9IIrMSrWXplwaJxVBudSb+FBj35TKmArnmWs0ycrMTeGoHeELyVG8S3SSacL1F/3kW0w 4U0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jYElMMjX; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-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 i8si642922ejb.106.2020.08.21.00.23.27; Fri, 21 Aug 2020 00:23:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-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=jYElMMjX; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-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 S1728334AbgHUHWD (ORCPT + 99 others); Fri, 21 Aug 2020 03:22:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727955AbgHUHWB (ORCPT ); Fri, 21 Aug 2020 03:22:01 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D670FC061385 for ; Fri, 21 Aug 2020 00:21:59 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id o5so602208pgb.2 for ; Fri, 21 Aug 2020 00:21:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=cPRYxgB2XxX7Z1aiBzTSI/wL9Ebvg2gjReZiaambCFo=; b=jYElMMjXdG1bg9pmnmr1kINJzz5f4CH+FPxvNf9UX2zqLwkhT3cuaFuPu3ggVeS8NV PctT9lbrnRfAMQXYDY22gsdCYJ9+NnM550iPXKGw5T0U+EgBdV+kUmmEsUTYS2DEZQ7x LIiqMSE5Skpt9IktZ+RLq+zE/wU2V7r8sOp70= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=cPRYxgB2XxX7Z1aiBzTSI/wL9Ebvg2gjReZiaambCFo=; b=H+1hx4Dy3v4UXyr2X/GeEHQKO53ESGSMlRYjNv6MWXZpZkcw9iRVVG7PBbdMjQHlS3 MiCLmlhz3yaua3+7f/4+YkiLBYGhToFB4q0o4jxxWhw4aO7fZpIAepTNyeTsyOlCkKI7 u06aJFLIjVsCPUBhyJCOWhz0X3bYi5gY8V1jmBUTDE7Pd+ZcaAKcSSHRZ1Q70YLmC1Qm BT1vys3Sxs4pdUyAIIpUQI0RMQaf0cMHjf3Tt7n5r9mH68JOgdehj1urJwKxK9r75pE4 TUdwWLH7R6VL/xe1YO+WtqW/eMSfbYfjFrBJ3YpzMbZvzTk15/hM756BTfXm4yne1Hd5 ctlA== X-Gm-Message-State: AOAM530Kq1SI43EaP8WheM+F28edaj6XsJ2bVEhnrwVnpQLa8Jl33GO5 Kd6AQAB7HL9SZoZ0F7lFuXXPsDWhfBhXGg== X-Received: by 2002:a63:3fcf:: with SMTP id m198mr1291656pga.400.1597994518826; Fri, 21 Aug 2020 00:21:58 -0700 (PDT) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id d127sm1371218pfc.175.2020.08.21.00.21.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Aug 2020 00:21:58 -0700 (PDT) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka Subject: [PATCH BlueZ] shared/mgmt: Provide timeout in mgmt_send() Date: Fri, 21 Aug 2020 00:21:55 -0700 Message-Id: <20200821072155.18834-1-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This patch adds mgmt_send_with_timeout() to help callers who want to use mgmt_send with timeout handling. Ideally, all mgmt_send() caller should use timeout, but in this patch only UNPAIR_DEVICE mgmt command is known to have no-reply problem in the kernel. This prevents bluetoothd from getting stuck in a state of waiting for kernel MGMT reply forever. We still need to fix the kernel side for the missing reply. --- src/adapter.c | 8 +++--- src/shared/mgmt.c | 62 ++++++++++++++++++++++++++++++++++++++++++++--- src/shared/mgmt.h | 6 +++++ 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/adapter.c b/src/adapter.c index 5e896a9f0..787d1bbb6 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -114,6 +114,8 @@ static const struct mgmt_blocked_key_info blocked_keys[] = { 0x22, 0x8e, 0x07, 0x56, 0xb4, 0xe8, 0x5f, 0x01}}, }; +#define DEFAULT_MGMT_TIMEOUT 2 /* Timeout for MGMT commands (secs) */ + static DBusConnection *dbus_conn = NULL; static bool kernel_conn_control = false; @@ -7217,9 +7219,9 @@ int btd_adapter_remove_bonding(struct btd_adapter *adapter, cp.addr.type = bdaddr_type; cp.disconnect = 1; - if (mgmt_send(adapter->mgmt, MGMT_OP_UNPAIR_DEVICE, - adapter->dev_id, sizeof(cp), &cp, - NULL, NULL, NULL) > 0) + if (mgmt_send_with_timeout(adapter->mgmt, MGMT_OP_UNPAIR_DEVICE, + adapter->dev_id, sizeof(cp), &cp, NULL, + NULL, NULL, DEFAULT_MGMT_TIMEOUT) > 0) return 0; return -EIO; diff --git a/src/shared/mgmt.c b/src/shared/mgmt.c index 277e361a6..7e70aba43 100644 --- a/src/shared/mgmt.c +++ b/src/shared/mgmt.c @@ -38,6 +38,7 @@ #include "src/shared/queue.h" #include "src/shared/util.h" #include "src/shared/mgmt.h" +#include "src/shared/timeout.h" struct mgmt { int ref_count; @@ -60,6 +61,11 @@ struct mgmt { void *debug_data; }; +struct mgmt_pending_request { + unsigned int request_id; + struct mgmt *mgmt; +}; + struct mgmt_request { unsigned int id; uint16_t opcode; @@ -69,6 +75,8 @@ struct mgmt_request { mgmt_request_func_t callback; mgmt_destroy_func_t destroy; void *user_data; + /* Timeout for the request if > zero, otherwise timer is not used. */ + int timeout_seconds; }; struct mgmt_notify { @@ -157,6 +165,8 @@ static void write_watch_destroy(void *user_data) mgmt->writer_active = false; } +bool mgmt_pending_timeout(void *user_data); + static bool send_request(struct mgmt *mgmt, struct mgmt_request *request) { struct iovec iov; @@ -184,6 +194,14 @@ static bool send_request(struct mgmt *mgmt, struct mgmt_request *request) mgmt->debug_data); queue_push_tail(mgmt->pending_list, request); + if (request->timeout_seconds > 0) { + struct mgmt_pending_request *pending_request = + malloc(sizeof(struct mgmt_pending_request)); + pending_request->request_id = request->id; + pending_request->mgmt = mgmt; + timeout_add(request->timeout_seconds * 1000, + mgmt_pending_timeout, pending_request, NULL); + } return true; } @@ -267,6 +285,29 @@ static void request_complete(struct mgmt *mgmt, uint8_t status, wakeup_writer(mgmt); } +bool mgmt_pending_timeout(void *user_data) +{ + struct mgmt_pending_request *pending_request = user_data; + struct mgmt *mgmt = pending_request->mgmt; + const struct mgmt_request *request = + queue_find(mgmt->pending_list, + match_request_id, + UINT_TO_PTR( + pending_request->request_id)); + + free(pending_request); + + if (!request) + return false; + + /* Pretend that kernel has replied with TIMEOUT status. */ + request_complete(mgmt, MGMT_STATUS_TIMEOUT, request->opcode, + request->index, /* length */ 0, + /* param */ NULL); + + return false; +} + struct event_index { uint16_t event; uint16_t index; @@ -571,10 +612,12 @@ static struct mgmt_request *create_request(uint16_t opcode, uint16_t index, return request; } -unsigned int mgmt_send(struct mgmt *mgmt, uint16_t opcode, uint16_t index, - uint16_t length, const void *param, - mgmt_request_func_t callback, - void *user_data, mgmt_destroy_func_t destroy) +unsigned int mgmt_send_with_timeout( + struct mgmt *mgmt, uint16_t opcode, uint16_t index, + uint16_t length, const void *param, + mgmt_request_func_t callback, + void *user_data, mgmt_destroy_func_t destroy, + int timeout_seconds) { struct mgmt_request *request; @@ -590,6 +633,7 @@ unsigned int mgmt_send(struct mgmt *mgmt, uint16_t opcode, uint16_t index, mgmt->next_request_id = 1; request->id = mgmt->next_request_id++; + request->timeout_seconds = timeout_seconds; if (!queue_push_tail(mgmt->request_queue, request)) { free(request->buf); @@ -602,6 +646,16 @@ unsigned int mgmt_send(struct mgmt *mgmt, uint16_t opcode, uint16_t index, return request->id; } +unsigned int mgmt_send(struct mgmt *mgmt, uint16_t opcode, uint16_t index, + uint16_t length, const void *param, + mgmt_request_func_t callback, + void *user_data, mgmt_destroy_func_t destroy) +{ + return mgmt_send_with_timeout( + mgmt, opcode, index, length, param, callback, user_data, + destroy, /* timeout_seconds */ 0); +} + unsigned int mgmt_send_nowait(struct mgmt *mgmt, uint16_t opcode, uint16_t index, uint16_t length, const void *param, mgmt_request_func_t callback, diff --git a/src/shared/mgmt.h b/src/shared/mgmt.h index 7caeb3844..9aea64010 100644 --- a/src/shared/mgmt.h +++ b/src/shared/mgmt.h @@ -46,6 +46,12 @@ bool mgmt_set_close_on_unref(struct mgmt *mgmt, bool do_close); typedef void (*mgmt_request_func_t)(uint8_t status, uint16_t length, const void *param, void *user_data); +unsigned int mgmt_send_with_timeout( + struct mgmt *mgmt, uint16_t opcode, uint16_t index, + uint16_t length, const void *param, + mgmt_request_func_t callback, + void *user_data, mgmt_destroy_func_t destroy, + int timeout_seconds); unsigned int mgmt_send(struct mgmt *mgmt, uint16_t opcode, uint16_t index, uint16_t length, const void *param, mgmt_request_func_t callback, -- 2.26.2