From patchwork Mon Mar 7 09:27:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1601981 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20210112 header.b=fH+29QeN; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::940; helo=mail-ua1-x940.google.com; envelope-from=swupdate+bncbcwivbv7sugrb5e7s6iqmgqemhucfeq@googlegroups.com; receiver=) Received: from mail-ua1-x940.google.com (mail-ua1-x940.google.com [IPv6:2607:f8b0:4864:20::940]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KBtQd4Bkkz9s8s for ; Mon, 7 Mar 2022 20:27:19 +1100 (AEDT) Received: by mail-ua1-x940.google.com with SMTP id a15-20020ab01c0f000000b003477465b9e2sf7998878uaj.2 for ; Mon, 07 Mar 2022 01:27:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1646645236; cv=pass; d=google.com; s=arc-20160816; b=mHIAdULalcYY9hzeyp9QiYTRR+VAidKBkxoLrWV+k9rrHdfI+owcf/k3O/s/JP77Ly HbWH5tIIjFPcx0v5VFow28ZQNciTGuxNnq5A095qeH6hkThftsyolGGkQmXnR2x+F2z3 OXtD3IuRidLRAT7cZgYghYFhnFDd7Ea77KXngVeFqFkVjgb4UKeORIte/Gw7trOxFB7V UczrKRAjVEUzUlJQwk7Kjlehv4Dx4fBVn87pts1E5jRGqVMKohWAWTHQ633/0iZxdEwV W7KthbyoOQlWVwHSItd0AZROcr6N6dCUIbThRj9SyTl1wDhDGD2DT/yZv/bAs+1Mro2a vmqA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=Y/xy1wYX4KyUUghWhQhvRFxn3BElcLJSmQNy+/bDaf0=; b=ZoyarZO+UM3uTDnRa417hXHbfWurfb0bXzzJ7WK0x5CBmO0sJ4vGwLbB9YJhTboKrz HHLfzXQLy9fJRc1yKXIoNpWJDbjrJgrpld0MZXuqTYCV5HawV47HvVyFc3LuCdrM2Z39 1piVMdQA4jiGUbfdQWBLTkMLtnqxJB/tb0GlZpActP4ZlxxPktGQfndBsUGhaLKsitoD 7ZWiAarF23C8iczfd2PvF24nFBu7ng/oMTOe63tveBQeU8WoAx/t1PLCAk35CflcKIht IO87i/blVcgj9SFh/Z1/lz5cZyw/nECRX6mSupCQPzfX7O6R2MZnKpCxRqTegD7qXpPR aLng== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=Y/xy1wYX4KyUUghWhQhvRFxn3BElcLJSmQNy+/bDaf0=; b=fH+29QeN6RrXsO3la1f/k7RpBUnjsXZ/PM98CZBF61jzbjgGoaAMETL9nVyPMZRBw9 qHCrUk5XcgslPesd84v8xVuLKKz1Ud0fYoKKs8HNBkpsWZ+8ixT723G8SJFs0uu+xmyr EOrpIu9HM14U5z0k08Q5RbT4zKacZvuS8BGRBBHO2DBgL0WHnGIg8288JVLFQDFygZnV v1K7a9JC0ACpyEVFq0AZce+gVKcsi9fm7ZREP/nx1Iz70YSMmEP7zurM2Js6ZnckoYQq pIpzcVWB76Sc/Zz5UY3HXjMBOjec+vE/yEdssvNnbGs6n1B0adxzppSWdpNMmEfN7duG Bp5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=Y/xy1wYX4KyUUghWhQhvRFxn3BElcLJSmQNy+/bDaf0=; b=XOu+y364FHfCl4AKqKwuv+2RAMNsH2ddSuqjZqFqfZCtA/xqwJpn44PyQ6YNZI4Cbw hfYrL0QNl29BIyfpLMC4sV+GzbZrVMPB1mtZ+/RWI3Q+rPv31cJ/69qMGOlXTtVY9dmg X3PVfy6eLB+vX/C/XMRXjkVpiY8ywa66JwuR7gaDAbA8HyCZt7Cr0m0XkmjQ48Gq5oy/ /cCNbvHOIQnyZOD9I5klq39U7/fzNhhY7ZIOoy+geD6zWishfNmQFLGgbgQGjKNOPvqV cxyoZv98TAaXBc+T3wECJYGu50L2cBFAa9m7lcO9Pxw9sSFwwmKHm1w93Hf99+2o9W08 i2AQ== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM533iVAw9sxzHbwzt0QKEw+g2S4Tz/P9TY+H4HLbJfdgUIu2OeoE3 7jtDgmgiHNlBzi5wA44w74k= X-Google-Smtp-Source: ABdhPJz/Lk5T4/Yrw0gZS9nJLEpGDroDHmgLPmWqso74V2L8a7wBztAGtSkFmtY+0Zt5IwjU2C35lw== X-Received: by 2002:ab0:14f3:0:b0:345:e9aa:60d6 with SMTP id f48-20020ab014f3000000b00345e9aa60d6mr3130957uae.56.1646645236359; Mon, 07 Mar 2022 01:27:16 -0800 (PST) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a67:e30d:0:b0:320:c398:e533 with SMTP id j13-20020a67e30d000000b00320c398e533ls219051vsf.1.gmail; Mon, 07 Mar 2022 01:27:15 -0800 (PST) X-Received: by 2002:a05:6102:14a5:b0:31e:7404:50e0 with SMTP id d37-20020a05610214a500b0031e740450e0mr3909782vsv.62.1646645235821; Mon, 07 Mar 2022 01:27:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646645235; cv=none; d=google.com; s=arc-20160816; b=DgpQDt0PYBqRq8hCHt4tAhLZbW1q7k2QW841JDWV8TAxCWCURQVNPMxfxRJQnothe6 PiH1f1KWkgfn4COxwFEBPX76N47+l3LZiDjcy/JcM3ihZMrQoapaz7FgIzs7TAH1qikv 9dNZMvkI4hGsGrUz0d/r+Ay158PINE63d5V+BsHJb+q2E+ZMDfzk05Rk7Do7HdrHdJ/+ DsWhnN9f52a/wib91XeGYPNQ48Q6cm3ISuFl7Vob5DLWjKbdKWAk4G6hxn9X+n7iNszM iiNSjYqaTJzxIZiPPqUB1uGVkXtrGga/YTPKUmF9296zNpI1zZLe4mg6ej2VPuLtfdxS qsWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=+l3uXIWmCCW1vggn3PUUyQZM2dsafgMnzxQM0wpFXos=; b=F5a3E8ggmoOQrkAOsQp1fe9rlSAeIaYlbmyXNw7qDdjd1SSnrgI8Xk8QUuR33RhTDo Iw/xPVzA3pGC2Ov1/+EwP2Q5fxEf4cvtGhF8PEOiAho0qvIhdB5B8a+0g03dShqgP4y8 JEmq15cs8qGLf+M7IBRVRgHC2DFlj3Ve4Xgwd4wlwsKCFaVIPgM0Uk9j/m5nXdmcWUFw ZWO/y1n9vdtyC/AEABCvrYhjeXHmy6UJQ7C6caGZC9NJ6uI+XEqBbtILao+jz3ewTI6L XPCK0+NSWcBUMnq1SQatXutaREt1p/wpwoSWATho/MwjGGu4OOgqguC5/UCXi4MAIa1q +KVw== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Received: from gw2.atmark-techno.com (gw2.atmark-techno.com. [35.74.137.57]) by gmr-mx.google.com with ESMTP id f22-20020a67fd16000000b00320a3066befsi234992vsr.1.2022.03.07.01.27.15 for ; Mon, 07 Mar 2022 01:27:15 -0800 (PST) Received-SPF: pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) client-ip=35.74.137.57; Received: from gw2.atmark-techno.com (localhost [127.0.0.1]) by gw2.atmark-techno.com (Postfix) with ESMTP id 973AB20D29 for ; Mon, 7 Mar 2022 18:27:13 +0900 (JST) Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by gw2.atmark-techno.com (Postfix) with ESMTPS id 7131820D29 for ; Mon, 7 Mar 2022 18:27:13 +0900 (JST) Received: by mail-pf1-f198.google.com with SMTP id y193-20020a62ceca000000b004f6f5bbaf7cso2006571pfg.16 for ; Mon, 07 Mar 2022 01:27:13 -0800 (PST) X-Received: by 2002:a17:90b:1645:b0:1bf:11:66ae with SMTP id il5-20020a17090b164500b001bf001166aemr11796484pjb.198.1646645232406; Mon, 07 Mar 2022 01:27:12 -0800 (PST) X-Received: by 2002:a17:90b:1645:b0:1bf:11:66ae with SMTP id il5-20020a17090b164500b001bf001166aemr11796469pjb.198.1646645232176; Mon, 07 Mar 2022 01:27:12 -0800 (PST) Received: from pc-0115 (35.112.198.104.bc.googleusercontent.com. [104.198.112.35]) by smtp.gmail.com with ESMTPSA id q21-20020a63e215000000b00373efe2cbcbsm11725472pgh.80.2022.03.07.01.27.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Mar 2022 01:27:11 -0800 (PST) Received: from martinet by pc-0115 with local (Exim 4.95) (envelope-from ) id 1nR9e2-00Ds3s-FX; Mon, 07 Mar 2022 18:27:10 +0900 From: Dominique Martinet To: sbabic@denx.de Cc: swupdate@googlegroups.com, Dominique Martinet Subject: [swupdate] [RFC PATCH] ipc/swupdate_async_thread: check before writing to socket Date: Mon, 7 Mar 2022 18:27:09 +0900 Message-Id: <20220307092709.3306065-1-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Original-Sender: dominique.martinet@atmark-techno.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , swupdate-client could get stuck writing swu content to swupdate when swupdate was itself busy sending other notifications and not reading its own socket. Use select() to make sure the socket is writable before attempting to write, and keep waiting for more input instead. Link: https://groups.google.com/g/swupdate/c/iWEgNaZ46uw/m/wQRTa9MsAwAJ Signed-off-by: Dominique Martinet --- Notes: - I've seen your comment about using exit() in here, this is just a quick version so you can test, we should be consistent with that so I'm waiting for your fix on this - It's still possible the 'wr' function will read too much of the swu, that the socket is in writable state but we cannot write the full buffer content... Async IO is complicated, and since we always check if the write was full we cannot just make the socket non-blocking here for continued writes. Honestly after looking at this I'm tempted to agree this can be fully rewritten and this is just a stopgap fix. I'm leaving the decision for this to you. Thanks! ipc/network_ipc-if.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ipc/network_ipc-if.c b/ipc/network_ipc-if.c index 90c49709b0fb..202ecbf09ef3 100644 --- a/ipc/network_ipc-if.c +++ b/ipc/network_ipc-if.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "network_ipc.h" @@ -37,7 +38,8 @@ static void *swupdate_async_thread(void *data) sigset_t saved_mask; struct timespec zerotime = {0, 0}; struct async_lib *rq = (struct async_lib *)data; - int notify_fd, ret; + int notify_fd, ret, maxfd; + fd_set rfds, wfds; ipc_message msg; msg.data.notify.status = RUN; @@ -55,14 +57,30 @@ static void *swupdate_async_thread(void *data) exit(1); } + FD_ZERO(&rfds); + FD_ZERO(&wfds); + maxfd = (notify_fd > rq->connfd ? notify_fd : rq->connfd) + 1; + /* Start writing the image until end */ do { if (!rq->wr) break; - rq->wr(&pbuf, &size); - if (size) - swupdate_image_write(pbuf, size); + FD_SET(notify_fd, &rfds); + FD_SET(rq->connfd, &wfds); + ret = select(maxfd, &rfds, &wfds, NULL, NULL); + if (ret < 0) { + if (errno == EINTR) + continue; + perror("select"); + exit(1); + } + + if (FD_ISSET(rq->connfd, &wfds)) { + rq->wr(&pbuf, &size); + if (size) + swupdate_image_write(pbuf, size); + } /* handle any notification coming */ while ((ret = ipc_notify_receive(¬ify_fd, &msg, 0))