Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp618149pxx; Mon, 26 Oct 2020 17:04:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwMTrRQ67GQX1cET2PQYGQkv29KFgPSB9uxmlkzBcn6PXbUb4GRfu36/r2fDG45POyIeoQF X-Received: by 2002:a17:907:42d2:: with SMTP id ng2mr17745166ejb.124.1603757078769; Mon, 26 Oct 2020 17:04:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603757078; cv=none; d=google.com; s=arc-20160816; b=0Q5D2KmdB3HY4/ACYyNO+fWZoGU4fpaDab5upCMKGVbNICgR7zBY9Gg73CKQIOYu8o 0urQ95WaqbziKuVvLSxeyyKx6WtAIhSsvlm6vIlJed90SUyEC37ZhHzYCZMflYL+Kr2g 83pv0sOHoF21PfC8DEsMtOCJrellbeN4GtxI5F0PXTHRKLAGp+jBVowlM7JBb7fdZqyb dK5WeMcpP0eC6aAwpg0gYaXJEaDq/ThTMpCxiZiALoxGWzFRmsk8uNT/ggMit0yFS88l QkLQ8rrvbvCrfnrvumALQ+dybXSMd8AXLdbOCvAQNLPRG5Xm5DIL4yNM/Zvv8aWX6vpZ HebQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=krMi7GzhK2+exaqgboNbBVvzoktKWV0jo7W9b8tZRmI=; b=K1lpRb4CjaAjkZQD/MLCoJHtJoQr7DpPU20598KVh9yRkQ6KbtDuIH2mj1ZL+O/EsZ HUhZvTc6CuggJURa9bx3527ht9m/vQuSzArdrdN9aQZMfPV/n6+Ihy67a9HY+QRMN6iL BjSlTyb0wLEntHbmeo9hsty8jBsDOsdGDoa4Gh9R5RvBiptXYxkbNVhhmhvK448C88RQ s4aIfu3R1hMtYx7yEtj8FEYiesELIaI4FIdzzauC1poJv74VP0TiWGXKtDI/r+AB6Odn dWy7746YpLrkXkxbtEZ3JPgfoqswabYcjNSg/lXHALKGfdAnZLDwmwWSdOV/6eksehbm 79NQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="P/b7FC9e"; 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 g4si8074698ejw.351.2020.10.26.17.04.15; Mon, 26 Oct 2020 17:04:38 -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="P/b7FC9e"; 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 S2389485AbgJZVhO (ORCPT + 99 others); Mon, 26 Oct 2020 17:37:14 -0400 Received: from mail-qk1-f193.google.com ([209.85.222.193]:45356 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389424AbgJZVhO (ORCPT ); Mon, 26 Oct 2020 17:37:14 -0400 Received: by mail-qk1-f193.google.com with SMTP id 188so9867871qkk.12 for ; Mon, 26 Oct 2020 14:37:13 -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=krMi7GzhK2+exaqgboNbBVvzoktKWV0jo7W9b8tZRmI=; b=P/b7FC9edg/gIXGWO5DJUbhyvsE+MLFa/Yb1c0sq8Fjx1ZmWZyqITSCe3BuD/RAoQc rkEPy2iBX9XYvlDsLk3fe2NdUAsYYiDZJwNuh3DBClvJnHZRj5umms/7OP77dWVTGIer DX3mMngwc0xorYldva7+b9KwN+lEWnvb33L29HesNRgn3m3nNUWA6yJAu8BJUbfBu9pM 5kJY0GXgFxjiSAiT5VdsE/6gfdwxUzazYO0X2cWTN/OlQWB+fZPW44ZdUvVs7BA6R3T0 kO63aojZrZ/v0ZtYW8O1t42+uhPwSl3d8qjkRLMneUZLE2cvccMWI/jlzdzRSjJd9Ouy I4Qw== 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=krMi7GzhK2+exaqgboNbBVvzoktKWV0jo7W9b8tZRmI=; b=SXNEHADY6Rwlsx00U2u+gb5GRPb3/lXIfp64i43eN/Y9cwZWOy1X7PPcsmrIZ8VmKN HyuH2+PLLsnLRRG9fIys5YsrpN4z/PnM4Iu+Hk+kZ3fk3qr79XM/POfN8v3Yaump0YdM hq6lVmLHNXDy+5Okjvs7GEPz7SOf75bw/KwAWystpX+odri50HUckuzjQAZ1mZeMf5uh rGXio8MmF2p3UJOhBPdEVWZ9hDOdOZrYbOUkswYk2TpfTAEFASDpPXtdhNBmeuDqupAb F6lFRThE+0MxslRc2yhc31ki9MVASpPqc2gepyag6h6U/QApL2D49gGRn3PLNyUehNSq 0m3A== X-Gm-Message-State: AOAM531OMPKgoyaJtTRWJSEGO+mE7e3l53c+0dAMdWpeKchPsZylEABz M9thPOI80SIyjerrP2RT7pUuBibyt35iiqPue4o= X-Received: by 2002:a37:d81:: with SMTP id 123mr16465372qkn.79.1603748232874; Mon, 26 Oct 2020 14:37:12 -0700 (PDT) MIME-Version: 1.0 References: <20201025162730.47247-1-marijns95@gmail.com> In-Reply-To: From: Marijn Suijten Date: Mon, 26 Oct 2020 22:37:00 +0100 Message-ID: Subject: Re: [PATCH BlueZ] audio/a2dp: a2dp_channel should have a refcount on avdtp session To: Luiz Augusto von Dentz Cc: "linux-bluetooth@vger.kernel.org" , =?UTF-8?Q?Pali_Roh=C3=A1r?= Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi Luiz, On Mon, 26 Oct 2020 at 21:39, Luiz Augusto von Dentz wrote: > > Hi Marijn, > > On Mon, Oct 26, 2020 at 1:22 PM Marijn Suijten wrote: > > > > Hi Luiz, > > > > On Mon, 26 Oct 2020 at 21:15, Luiz Augusto von Dentz > > wrote: > > > > > > Hi Marijn, > > > > > > On Sun, Oct 25, 2020 at 9:27 AM Marijn Suijten wrote: > > > > > > > > a2dp_channel keeps a reference to an avdtp session without incrementing > > > > its refcount. Not only does this appear wrong, it causes unexpected > > > > disconnections when the remote SEP responds with rejections. > > > > > > > > During testing with an audio application disconnections are observed > > > > when a codec config change through MediaEndpoint1.SetConfiguration > > > > fails. As soon as BlueZ receives this failure from the peer the > > > > corresponding a2dp_setup object is cleaned up which holds the last > > > > refcount to an avdtp session, in turn starting the disconnect process. > > > > An eventual open sink/source and transport have already closed by that > > > > time and released their refcounts. > > > > > > > > Adding refcounting semantics around a2dp_channel resolves the > > > > disconnections and allows future calls on MediaEndpoint1 to safely > > > > access the sesion stored within this channel. > > > > --- > > > > profiles/audio/a2dp.c | 7 ++++++- > > > > 1 file changed, 6 insertions(+), 1 deletion(-) > > > > > > > > diff --git a/profiles/audio/a2dp.c b/profiles/audio/a2dp.c > > > > index cc4866b5b..0eac0135f 100644 > > > > --- a/profiles/audio/a2dp.c > > > > +++ b/profiles/audio/a2dp.c > > > > @@ -1507,6 +1507,9 @@ static void channel_free(void *data) > > > > > > > > avdtp_remove_state_cb(chan->state_id); > > > > > > > > + if (chan->session) > > > > + avdtp_unref(chan->session); > > > > + > > > > queue_destroy(chan->seps, remove_remote_sep); > > > > free(chan->last_used); > > > > g_free(chan); > > > > @@ -2065,7 +2068,7 @@ static void avdtp_state_cb(struct btd_device *dev, struct avdtp *session, > > > > break; > > > > case AVDTP_SESSION_STATE_CONNECTED: > > > > if (!chan->session) > > > > - chan->session = session; > > > > + chan->session = avdtp_ref(session); > > > > > > Afaik this was done on purpose since we only need a weak reference as > > > taking a reference would prevent the session to be disconnected when > > > there is no setup in place, so I pretty sure this will cause > > > regressions, instead we should probably add a reference when > > > reconfiguring is in place and have a grace period for switching to > > > another codec. > > > > Allright, so it's either an in-progress setup or ongoing transport > > keeping the session alive, nothing else? I guess this is as simple as > > setting a higher dc_timeout when calling set_disconnect_timer in > > avdtp_unref? > > Yep, actually now that you mentioned the dc_timeout that should have > prevented us to disconnect immediately when reconfiguring as we should > have been in AVDTP_SESSION_STATE_CONNECTED state but I think the > problem is that we use avdtp_close to reconfigure which does set the > dc_timeout to 0 Yep a2dp_reconfig calls that functionand sets it to 0. > since that is normally used for cleaning up the > session, so I think we may need to restore the dc_timeout to > DISCONNECT_TIMEOUT if we attempt to set a configuration: My thinking as well, assuming DISCONNECT_TIMEOUT is enough (1s). > diff --git a/profiles/audio/avdtp.c b/profiles/audio/avdtp.c > index ae93fb26f..97b4d1b44 100644 > --- a/profiles/audio/avdtp.c > +++ b/profiles/audio/avdtp.c > @@ -3498,6 +3498,7 @@ int avdtp_set_configuration(struct avdtp *session, > session->streams = g_slist_append(session->streams, new_stream); > if (stream) > *stream = new_stream; > + session->dc_timeout = DISCONNECT_TIMEOUT; > } > > g_free(req); > > Thanks, works like a charm! profiles/audio/avdtp.c:avdtp_unref() 0x5565f10db6f0: ref=1 profiles/audio/avdtp.c:avdtp_set_configuration() 0x5565f10db6f0: int_seid=8, acp_seid=1 profiles/audio/avdtp.c:session_cb() profiles/audio/avdtp.c:avdtp_parse_rej() SET_CONFIGURATION request rejected: Configuration not supported (41) profiles/audio/a2dp.c:setconf_cfm() Source 0x5565f10fc620: Set_Configuration_Cfm profiles/audio/a2dp.c:setup_ref() 0x5565f110d540: ref=2 profiles/audio/a2dp.c:setup_unref() 0x5565f110d540: ref=1 profiles/audio/a2dp.c:setup_unref() 0x5565f110d540: ref=0 profiles/audio/a2dp.c:setup_free() 0x5565f110d540 profiles/audio/avdtp.c:avdtp_unref() 0x5565f10db6f0: ref=0 profiles/audio/avdtp.c:avdtp_ref() 0x5565f10db6f0: ref=1 profiles/audio/avdtp.c:set_disconnect_timer() timeout 1 profiles/audio/avdtp.c:avdtp_ref() 0x5565f10db6f0: ref=2 profiles/audio/avdtp.c:avdtp_unref() 0x5565f10db6f0: ref=1 profiles/audio/a2dp.c:setup_ref() 0x5565f110d540: ref=1 profiles/audio/avdtp.c:avdtp_set_configuration() 0x5565f10db6f0: int_seid=8, acp_seid=7 profiles/audio/avdtp.c:session_cb() profiles/audio/avdtp.c:avdtp_parse_resp() SET_CONFIGURATION request succeeded profiles/audio/a2dp.c:setconf_cfm() Source 0x5565f10fc620: Set_Configuration_Cfm Since you suggested this change I guess it's up to you to write a commit around it and apply it or submit it for additional review, or should I do it and combine it with the original message for a v2? > > > > > > > load_remote_seps(chan); > > > > break; > > > > } > > > > @@ -2145,6 +2148,7 @@ found: > > > > channel_remove(chan); > > > > return NULL; > > > > } > > > > + avdtp_ref(chan->session); > > > > > > > > return avdtp_ref(chan->session); > > > > } > > > > @@ -2165,6 +2169,7 @@ static void connect_cb(GIOChannel *io, GError *err, gpointer user_data) > > > > error("Unable to create AVDTP session"); > > > > goto fail; > > > > } > > > > + avdtp_ref(chan->session); > > > > } > > > > > > > > g_io_channel_unref(chan->io); > > > > -- > > > > 2.29.1 > > > > > > > > Marijn Suijten > > > > > > > > > > > > -- > > > Luiz Augusto von Dentz > > > > Marijn Suijten > > > > -- > Luiz Augusto von Dentz Marijn Suijten