Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3111114rwb; Mon, 15 Aug 2022 18:21:56 -0700 (PDT) X-Google-Smtp-Source: AA6agR4b0jPhfPwdxTjb6+YLJOj61j/Rhqh7HzLPXCrztzNTKEvcm6mxFa2TMrFRxB0Rnbuo+ax/ X-Received: by 2002:a17:907:6e08:b0:731:83a3:58b6 with SMTP id sd8-20020a1709076e0800b0073183a358b6mr12236347ejc.12.1660612916217; Mon, 15 Aug 2022 18:21:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660612916; cv=none; d=google.com; s=arc-20160816; b=Ooez/A/pjLPVWRKquVhuOmaAgZdXFflcdE6BrRzzJoeMY/ZxkuUOrrhl3gYzNvx+zi +0oLx5SobLpLVov4TKbiWxQ9K9fBzNWtUnh4wVy5p9rIe5bDO+SPu2S2DYJ6TX7szUIG hKKLop1wB1ivgV78MlJ+W8x33ya6cRjHAAbLkqJjkH5pJm5gzOyl6CZZxpYo/f6Augmt xcvdM8PXsbEsu6SYnGQkyLE4i8Hd6AcpVAs7oagcOBNMmH+/VEDphBzedXuHiHunklaZ HIKPX9hocG40WggJWuG2aeakthFT1lQAwizpWNTh0rWCFnotYHWbBG5eF7U4WkEwD8Tv c++Q== 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=NfdFm94ru43DUwtkVPF8bOTWYPeEqZcO32hvHvPVjlY=; b=DItbNWUKkrfL1vaygeoPBfCOB51WMVxQN9yEcbqAp0yPN8kk9VQiOgIvsuxPC3FQC8 8hzEOU9B1koct1HQw/ggYDOMu7f6sFAV0JC2NHErTkt4f3h8uVwTGsXWHnIdJABm/jzX LpVigxwrMi+iA4DwpoS1lKa4ZB/WkNuTwaxzd32Uadaq/JdEnn0ahj8spk1VgcCIu6cY 9b4bEryNULQT6+veBb9C1R3x9Gz9jHaZLN+MWmFgUejnYW7dronq3pQkudrRJ7TKAvjc yjTkDwBSvOaZNFFxE1zlha82FoThuh6B72sDFLlfPELD5nmw7G7JhX/gtiVWQ3ZLTbq6 YVUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2m6P8XiP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id go16-20020a1709070d9000b0072f1f3ec581si11120019ejc.264.2022.08.15.18.21.30; Mon, 15 Aug 2022 18:21:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2m6P8XiP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S245023AbiHPAnv (ORCPT + 99 others); Mon, 15 Aug 2022 20:43:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349695AbiHPAld (ORCPT ); Mon, 15 Aug 2022 20:41:33 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 999ED7D1D2; Mon, 15 Aug 2022 13:39:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8A30761227; Mon, 15 Aug 2022 20:39:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7859FC433D6; Mon, 15 Aug 2022 20:39:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660595958; bh=DfWgohC7suDSL0n1rFNXgTIr0wp+Ji3Ao9eR2xdJ/jk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2m6P8XiPjPMg9KGKUql/ujusgj0DTqy8OK03yr/t2AQnJP+xfM7ojiOOPwH0SkUX9 NB2+QLrkjFx15+T/kCHACQtPtYQDeym3HVvuM7bu22ncK/OisLwCYqa/J4FfLZC4UF 09psA5QMkpIMB6bIEAZ8CTkHWAvL0oeukSjiuPfU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Starke , Sasha Levin Subject: [PATCH 5.19 0927/1157] tty: n_gsm: fix wrong queuing behavior in gsm_dlci_data_output() Date: Mon, 15 Aug 2022 20:04:43 +0200 Message-Id: <20220815180516.543642578@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Starke [ Upstream commit 556fc8ac06513cced381588d6d58c184d95cc4fe ] 1) The function drains the fifo for the given user tty/DLCI without considering 'TX_THRESH_HI' and different to gsm_dlci_data_output_framed(), which moves only one packet from the user side to the internal transmission queue. We can only handle one packet at a time here if we want to allow DLCI priority handling in gsm_dlci_data_sweep() to avoid link starvation. 2) Furthermore, the additional header octet from convergence layer type 2 is not counted against MTU. It is part of the UI/UIH frame message which needs to be limited to MTU. Hence, it is wrong not to consider this octet. 3) Finally, the waiting user tty is not informed about freed space in its send queue. Take at most one packet worth of data out of the DLCI fifo to fix 1). Limit the max user data size per packet to MTU - 1 in case of convergence layer type 2 to leave space for the control signal octet which is added in the later part of the function. This fixes 2). Add tty_port_tty_wakeup() to wake up the user tty if new write space has been made available to fix 3). Fixes: 268e526b935e ("tty/n_gsm: avoid fifo overflow in gsm_dlci_data_output") Signed-off-by: Daniel Starke Link: https://lore.kernel.org/r/20220701061652.39604-3-daniel.starke@siemens.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/tty/n_gsm.c | 74 +++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c index e8c0ce114c07..d056b15b4d61 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -886,41 +886,51 @@ static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci) { struct gsm_msg *msg; u8 *dp; - int len, total_size, size; - int h = dlci->adaption - 1; + int h, len, size; - total_size = 0; - while (1) { - len = kfifo_len(&dlci->fifo); - if (len == 0) - return total_size; - - /* MTU/MRU count only the data bits */ - if (len > gsm->mtu) - len = gsm->mtu; - - size = len + h; - - msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype); - /* FIXME: need a timer or something to kick this so it can't - get stuck with no work outstanding and no buffer free */ - if (msg == NULL) - return -ENOMEM; - dp = msg->data; - switch (dlci->adaption) { - case 1: /* Unstructured */ - break; - case 2: /* Unstructed with modem bits. - Always one byte as we never send inline break data */ - *dp++ = (gsm_encode_modem(dlci) << 1) | EA; - break; - } - WARN_ON(kfifo_out_locked(&dlci->fifo, dp , len, &dlci->lock) != len); - __gsm_data_queue(dlci, msg); - total_size += size; + /* for modem bits without break data */ + h = ((dlci->adaption == 1) ? 0 : 1); + + len = kfifo_len(&dlci->fifo); + if (len == 0) + return 0; + + /* MTU/MRU count only the data bits but watch adaption mode */ + if ((len + h) > gsm->mtu) + len = gsm->mtu - h; + + size = len + h; + + msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype); + /* FIXME: need a timer or something to kick this so it can't + * get stuck with no work outstanding and no buffer free + */ + if (!msg) + return -ENOMEM; + dp = msg->data; + switch (dlci->adaption) { + case 1: /* Unstructured */ + break; + case 2: /* Unstructured with modem bits. + * Always one byte as we never send inline break data + */ + *dp++ = (gsm_encode_modem(dlci) << 1) | EA; + break; + default: + pr_err("%s: unsupported adaption %d\n", __func__, + dlci->adaption); + break; } + + WARN_ON(len != kfifo_out_locked(&dlci->fifo, dp, len, + &dlci->lock)); + + /* Notify upper layer about available send space. */ + tty_port_tty_wakeup(&dlci->port); + + __gsm_data_queue(dlci, msg); /* Bytes of data we used up */ - return total_size; + return size; } /** -- 2.35.1