Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp426471pxa; Fri, 21 Aug 2020 10:46:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfvAjJvXqYo3xiOIvNOl7PHWQq0+4xjCo5AaErf9Ox4ygk01WFtp4xvQIlMi16JiafvQkq X-Received: by 2002:a17:906:85da:: with SMTP id i26mr4175818ejy.458.1598031995855; Fri, 21 Aug 2020 10:46:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598031995; cv=none; d=google.com; s=arc-20160816; b=0PJMIMz4zIGtmRcxTfg0xgc2PKC6Db5TI3HN4ureQgxIp3fsM1VmcHnUNo2z+NhfrT n2n623FktSaNVMUvvqzLdlE46GZaFmgt7cyikMQhKR47QJ4i1LAxA17YjKXu0rWaZ/1+ FCxnTraeoX6prttKh/AztkWEdjOYg+649Fm2H9OzYTFHD6mIxlij0fGmm0nQOH1Y5wEd A6r0erLsyyDcvi8bXaccX4uU2cd9IcS2Ag9vPL/jh1KqCRTFfVnD3xSHqEFtZz6k2WKg zrSe+qw00duqHTlqr1aKp3WCwuxu1h3JQpF4wb+Wr2hpk3iZ6GmMB3LeQ8+WXBgvWYb7 MYYg== 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=PEjL0dyRIMnvU+TEOondv6UZTt27vQTUxacVcxcjF5s=; b=Pf5uW/SW4Aipkj3zG5OgKDvUSwAvyWpYkEHRsqrmSOyjekpJkGDwPC6pTDVbZZpYie xMD9ENnKN9SG/7MdK6Dzok60YEj8EuEyxHJpAjz4XYcS6aDv50IjMQuDxqHTRfW3rQ4u UF3KKTsbimYXUldA4Jm7A63twbRPr4xcXrYOQz5gCrBiwFBoOwf1F1Oz/4MEJv/PqSg4 HhsPoz1tLEvi3pe4pnQEJq6DjwCJLq46YpsyRC6cKooyc4AeJr6DpYgN5N/KS66NqT2m MjgXmcygieqsbT6da5y8q0bNlpS+lQS97QAKbnqEV58HNf5ImRjXbMlDiOExc0q4846n 8fQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=EozGs8tj; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v24si1708670edl.35.2020.08.21.10.46.08; Fri, 21 Aug 2020 10:46:35 -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=@gmail.com header.s=20161025 header.b=EozGs8tj; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726723AbgHURqG (ORCPT + 99 others); Fri, 21 Aug 2020 13:46:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726504AbgHURqF (ORCPT ); Fri, 21 Aug 2020 13:46:05 -0400 Received: from mail-oo1-xc42.google.com (mail-oo1-xc42.google.com [IPv6:2607:f8b0:4864:20::c42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8FF4C061573 for ; Fri, 21 Aug 2020 10:46:04 -0700 (PDT) Received: by mail-oo1-xc42.google.com with SMTP id z10so516170ooi.10 for ; Fri, 21 Aug 2020 10:46:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PEjL0dyRIMnvU+TEOondv6UZTt27vQTUxacVcxcjF5s=; b=EozGs8tjRWtICLn7RZ4+/hlE0wSJfoz8bzwrDJq3rc0UooK2p7XqPhQ+nCShO3WqOo d6U515KExmvgyjaILb9nuPtcu2GoPdsA1Qf853jfwnAFVg95k3FARzvGOPeRUnSJDDGX cbN8zNFCL0chYoJRdcIgbhxA32xDN19VsScIG9EFFPZXsZPhZ3F9akW7uI1LoCAvwhU5 1KHRFCIdvKZ4TswLMryXUvwn8xGpkGnkq15ckac8OhFqTT39ayecuVn+tuvBJKAgY4Pc Tjy+DFxi0B6uQ7+YVOX6brFYUeEtyqX8jo3zhq6Hia/ZYlTioH0y5gNoec1VPnXcupFU D09w== 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=PEjL0dyRIMnvU+TEOondv6UZTt27vQTUxacVcxcjF5s=; b=Jhso988qH+tr9fWjJAGdbu93cthIv+bJ2/hNv4a5ie1d3XCOKeBzRMgAUn006JxLhJ 1QduhXI+AyzbkdFMIxCsFgFgTejQ1TGKqgjm8Ja1IS8QmQsL1aLSXkt2yHsF57qccJae xdy6wzpZva+s8WGZkx1NwPhWuqPfs4txm0OB4bOFDd77FIk4TtTNy3GX8x9n+HF3DhPu CXAiCzxIAqI68ge39J7x0Z7bG7UZLHfA3LpORpLLsrjMhnAW/vv/iLPGBhfnsJWktI5o PiBdAhDBrGiiAHVd8kwIeriLOybsawOsraFdPKnq1sRTfdwHgVvnMLopvbr9LQ3Tpcn2 BmTA== X-Gm-Message-State: AOAM531i9UXHStb3RrggMAZljT897dglJOTvkHSmKy+JUvZGCkYNjTGe AwO00IkIhEynyeSEQAuNfZXCFABNNNE2dZ6OrhE= X-Received: by 2002:a4a:924b:: with SMTP id g11mr2992050ooh.9.1598031963473; Fri, 21 Aug 2020 10:46:03 -0700 (PDT) MIME-Version: 1.0 References: <20200821072155.18834-1-sonnysasaka@chromium.org> In-Reply-To: <20200821072155.18834-1-sonnysasaka@chromium.org> From: Luiz Augusto von Dentz Date: Fri, 21 Aug 2020 10:45:53 -0700 Message-ID: Subject: Re: [PATCH BlueZ] shared/mgmt: Provide timeout in mgmt_send() To: Sonny Sasaka Cc: "linux-bluetooth@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi Sonny, On Fri, Aug 21, 2020 at 12:26 AM Sonny Sasaka wrote: > > 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)); Use new0 instead of malloc. > + pending_request->request_id = request->id; > + pending_request->mgmt = mgmt; > + timeout_add(request->timeout_seconds * 1000, > + mgmt_pending_timeout, pending_request, NULL); You might need to attach the pending request to the request itself so if it is cancel/complete you will need to free the pointer. > + } > > 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)); Make the request aware of the timeout so it can cancel it when it is cancelled/complete it just cancel the timeout. > + 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 > -- Luiz Augusto von Dentz