Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2120090rwd; Fri, 26 May 2023 01:53:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5bOYhVkcLAPYLzjggtLBxFOiM2vE2YaeAodmE8QUlM+OvmNAH21DnlJr15dBWh8tbiWUb9 X-Received: by 2002:a05:6a21:998f:b0:10e:d4fb:b031 with SMTP id ve15-20020a056a21998f00b0010ed4fbb031mr1374672pzb.61.1685091229855; Fri, 26 May 2023 01:53:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685091229; cv=none; d=google.com; s=arc-20160816; b=vzHLGIavuT1X6fAPKZQwf/NWNn+GR+22qJ53HeV5XPpiT4Gd7FauCfyiY0nm74zRnj VU4/qc0S5M0zIvsQZARxBStv/3h2mDnEcSexLkwvIQ+kt/caZRSYJ6WskP/7k+f2EsRb jRBqybH+KivjGUoVQtZLaSHUdL/fibVpO6x3kSlKbAWGx6dWfvJmmlQJuXHH2t77yv+K 65Dh91/PeMzdlo3+NQN9jwjqoj4JhGrcUZmoBagl6RXFv9YP6LOYq/9OxQbIWgZb3/HX UvzOrPgWC7yc2IJz/V6oFPLnglQfD1Fn+rRQV9K7/3Yhsl+aEtRHbO44oXGsDgjR1yFB xN3g== 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 :message-id:date:subject:cc:to:from; bh=9w2ugqhHU3ZPRFLwcf9DF5d/MEh4hD+8tBiZzJbCQD8=; b=lg6JOxMku9JAhMqcelTNZwMaGCOzZURlid7Pb5tpfxWKdsEYj1mDBdvcCXvanR+zp7 4c43T5/wzJQwHmPCGPYHuCmIgicyuwemPHcdF/mzmHW6npcVAfZ+6Gjq2tpxVwqw864b SbI/nIL6GckKYwGUpdGOFkirg8vdXYotuG6rVyvMYAbJcpclY9XcgPsekrCSrY4gIKO0 ujnK1Agr+J8h3igSiDHLNWvA3e8Z9jr9tSJumVj59MTqdJIHft/ElnSvYclPLqWe6pdW i/BmBYIQZyRyomHCcRqzaSChY6DkKPL4aI8bBrQBvU8/aC/38Y/kVuocM/rNZAa0TwQ4 TSqQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y185-20020a638ac2000000b005133c334fe9si3149634pgd.847.2023.05.26.01.53.35; Fri, 26 May 2023 01:53:49 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242145AbjEZIT4 (ORCPT + 99 others); Fri, 26 May 2023 04:19:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242690AbjEZITy (ORCPT ); Fri, 26 May 2023 04:19:54 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B800F3 for ; Fri, 26 May 2023 01:19:53 -0700 (PDT) Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q2Sfs-0004gO-NK; Fri, 26 May 2023 10:19:48 +0200 Received: from [2a0a:edc0:0:1101:1d::ac] (helo=dude04.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1q2Sfr-002ueN-UV; Fri, 26 May 2023 10:19:47 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1q2Sfr-00303d-66; Fri, 26 May 2023 10:19:47 +0200 From: Oleksij Rempel To: Robin van der Gracht , Oliver Hartkopp , Marc Kleine-Budde Cc: Oleksij Rempel , David Jander , kernel@pengutronix.de, linux-can@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1] can: j1939: j1939_sk_send_loop_abort(): improved error queue handling in J1939 Socket Date: Fri, 26 May 2023 10:19:46 +0200 Message-Id: <20230526081946.715190-1-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 This patch addresses an issue within the j1939_sk_send_loop_abort() function in the j1939/socket.c file, specifically in the context of Transport Protocol (TP) sessions. Without this patch, when a TP session is initiated and a Clear To Send (CTS) frame is received from the remote side requesting one data packet, the kernel dispatches the first Data Transport (DT) frame and then waits for the next CTS. If the remote side doesn't respond with another CTS, the kernel aborts due to a timeout. This leads to the user-space receiving an EPOLLERR on the socket, and the socket becomes active. However, when trying to read the error queue from the socket with sock.recvmsg(, , socket.MSG_ERRQUEUE), it returns -EAGAIN, given that the socket is non-blocking. This situation results in an infinite loop: the user-space repeatedly calls epoll(), epoll() returns the socket file descriptor with EPOLLERR, but the socket then blocks on the recv() of ERRQUEUE. This patch introduces an additional check for the J1939_SOCK_ERRQUEUE flag within the j1939_sk_send_loop_abort() function. If the flag is set, it indicates that the application has subscribed to receive error queue messages. In such cases, the kernel can communicate the current transfer state via the error queue. This allows for the function to return early, preventing the unnecessary setting of the socket into an error state, and breaking the infinite loop. It is crucial to note that a socket error is only needed if the application isn't using the error queue, as, without it, the application wouldn't be aware of transfer issues. Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol") Reported-by: David Jander Tested-by: David Jander Signed-off-by: Oleksij Rempel --- net/can/j1939/socket.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c index 1790469b2580..35970c25496a 100644 --- a/net/can/j1939/socket.c +++ b/net/can/j1939/socket.c @@ -1088,6 +1088,11 @@ void j1939_sk_errqueue(struct j1939_session *session, void j1939_sk_send_loop_abort(struct sock *sk, int err) { + struct j1939_sock *jsk = j1939_sk(sk); + + if (jsk->state & J1939_SOCK_ERRQUEUE) + return; + sk->sk_err = err; sk_error_report(sk); -- 2.39.2