From nobody Thu Sep 19 01:23:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1721321357; cv=none; d=zohomail.com; s=zohoarc; b=Yeog2MyJMJ1lOfJ/rahsIlyLE5RLlomdEgnhQeTPmsHTMXgL6iFBKO0NJNpSK034uk+6sniIIlaJDMbV4SUVkDq6OryDMsjMujKqjurZ/ncxtPK0R5U28473fQBAZ0bigpGzTJU7LtygL9sjnqyQEjeoNoPmlOGUOzRWSbpthrY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721321357; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=yYc4CYlNYiXlhGTE3F3Z17D5dfT00FoaAtahQbVBGvo=; b=fUFASl/GNkHu3BNsYt/5NdveCg56oBgkfDC2x8IQrJg6E0LgFnuOn8/UTHeFWE69CauT9fKQNSVjbSm/vXwZNxDBneDIpNnw3EkXVcq3uB1w21mWZPHeLzsmYApHZw+VN06GYSfft8cro/Ke4akPSJRza38pdOshRUgYstpzTEM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 17213213576966.478570181265013; Thu, 18 Jul 2024 09:49:17 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.760598.1170530 (Exim 4.92) (envelope-from ) id 1sUUJM-0000Rm-63; Thu, 18 Jul 2024 16:48:56 +0000 Received: by outflank-mailman (output) from mailman id 760598.1170530; Thu, 18 Jul 2024 16:48:56 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJM-0000RT-2j; Thu, 18 Jul 2024 16:48:56 +0000 Received: by outflank-mailman (input) for mailman id 760598; Thu, 18 Jul 2024 16:48:54 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sUUJK-0007P6-OC for xen-devel@lists.xenproject.org; Thu, 18 Jul 2024 16:48:54 +0000 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [2a00:1450:4864:20::230]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 9d8477d4-4525-11ef-8776-851b0ebba9a2; Thu, 18 Jul 2024 18:48:53 +0200 (CEST) Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2eeef45d865so13798371fa.0 for ; Thu, 18 Jul 2024 09:48:53 -0700 (PDT) Received: from andrewcoop.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc820f2bsm576882466b.206.2024.07.18.09.48.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 09:48:50 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9d8477d4-4525-11ef-8776-851b0ebba9a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1721321332; x=1721926132; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yYc4CYlNYiXlhGTE3F3Z17D5dfT00FoaAtahQbVBGvo=; b=XwKwEFgd0gPh1dsPy587tpI62YA4S3W96UeQ3afyyO92rhQjwqSJMUxVrwUQolNi0j anD7lc2vVEfHGj2ObLxgDutwMcV8s020d6e1fJY8Pe6xFhu6gPdiJiitU5f8UH0Nd/nv 8y+dt0souw3scDL4bjf+AlES5omp6jHlkOfi0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721321332; x=1721926132; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yYc4CYlNYiXlhGTE3F3Z17D5dfT00FoaAtahQbVBGvo=; b=dIjTrRttSs32iY2q8551YvE8MdqsPB9jnnA1p7ScEeyLK131A5ec1MQfMzz45+yTVK Awf9ixE3msu6fZxZhExFm31cp6k/X3QpgcPaYcHyI6Q0ouWipuNUg9AkkjnqpWq7n3/J t9b/o2o4XbQ9NrU5TpFy2/3/Sbf0gbf+6JTMcPGNSERGig6Sn8qCDPDf5fB2x4XyaS2c Goll1G/RS2H3vy4HuHcRyyA7fvvQEv8rMrWPijEruYH9JM+n6J+LJmcAwc7MOoZ3IogN VdNbPdqA5vC3UzY/jrfDz/1fl2Uo79HNvfylZdtQgS/GsGTF73QVqYn/Em49Zb/d9xM6 QzKA== X-Gm-Message-State: AOJu0Yxr8Qz7qJ91kgFk0/m9k36Batb3NINU9X6QN9TWvNqxds2vmEA3 807jLiVbUme8zflPXHFb/Qqq1Qrn8bjBSmvq8CDkr5M4GJLK/8pQfI40g8lCno8yci1q2Q6Vry9 q X-Google-Smtp-Source: AGHT+IEY1gnYXw/cRpFVXBzT2Q9vxvDB6ROlWIMl2FDXaLt4meEJFMN2uWHJZXsyiVHGgXIQQMBViw== X-Received: by 2002:a05:6512:6cf:b0:52c:905b:ea5f with SMTP id 2adb3069b0e04-52ee545400amr5237900e87.63.1721321331774; Thu, 18 Jul 2024 09:48:51 -0700 (PDT) From: Andrew Cooper To: Xen-devel Cc: Andrew Cooper , Anthony PERARD , Juergen Gross Subject: [PATCH 5/6] tools/libxs: Use writev()/sendmsg() instead of write() Date: Thu, 18 Jul 2024 17:48:41 +0100 Message-Id: <20240718164842.3650702-6-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240718164842.3650702-1-andrew.cooper3@citrix.com> References: <20240718164842.3650702-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1721321359190116600 Content-Type: text/plain; charset="utf-8" With the input data now conveniently arranged, use writev()/sendmsg() inste= ad of decomposing it into write() calls. This causes all requests to be submitted with a single system call, rather than at least two. While in principle short writes can occur, the chances = of it happening are slim given that most xenbus comms are only a handful of bytes. Nevertheless, provide {writev,sendmsg}_exact() wrappers which take care of resubmitting on EINTR or short write. Signed-off-by: Andrew Cooper --- CC: Anthony PERARD CC: Juergen Gross --- tools/libs/store/xs.c | 104 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c index f4edeb05f03b..9db5c02661f5 100644 --- a/tools/libs/store/xs.c +++ b/tools/libs/store/xs.c @@ -563,6 +563,105 @@ static void *read_reply( return body; } =20 +/* + * Wrapper around sendmsg() to resubmit on EINTR or short write. Returns + * @true if all data was transmitted, or @false with errno for an error. + * Note: May alter @iov in place on resubmit. + */ +static bool sendmsg_exact(int fd, struct iovec *iov, unsigned int nr) +{ + struct msghdr hdr =3D { + .msg_iov =3D iov, + .msg_iovlen =3D nr, + }; + + /* Sanity check first element isn't empty */ + assert(iov->iov_len =3D=3D sizeof(struct xsd_sockmsg)); + + while (hdr.msg_iovlen) { + ssize_t res =3D sendmsg(fd, &hdr, 0); + + if (res < 0 && errno =3D=3D EINTR) + continue; + if (res <=3D 0) + return false; + + while (res) { + if (res >=3D hdr.msg_iov->iov_len) { + /* Skip full element. */ + res -=3D hdr.msg_iov->iov_len; + hdr.msg_iovlen--; + hdr.msg_iov++; + continue; + } + + /* Partial element, adjust base/len. */ + hdr.msg_iov->iov_len -=3D res; + hdr.msg_iov->iov_base +=3D res; + break; + } + + while (hdr.msg_iov->iov_len =3D=3D 0) { + /* Skip empty element. */ + hdr.msg_iovlen--; + hdr.msg_iov++; + } + } + + return true; +} + +/* + * Wrapper around sendmsg() to resubmit on EINTR or short write. Returns + * @true if all data was transmitted, or @false with errno for an error. + * Note: May alter @iov in place on resubmit. + */ +static bool writev_exact(int fd, struct iovec *iov, unsigned int nr) +{ + /* Sanity check first element isn't empty */ + assert(iov->iov_len =3D=3D sizeof(struct xsd_sockmsg)); + + while (nr) { + ssize_t res =3D writev(fd, iov, nr); + + if (res < 0 && errno =3D=3D EINTR) + continue; + if (res <=3D 0) + return false; + + while (res) { + if (res >=3D iov->iov_len) { + /* Skip full element. */ + res -=3D iov->iov_len; + nr--; + iov++; + continue; + } + + /* Partial element, adjust base/len. */ + iov->iov_len -=3D res; + iov->iov_base +=3D res; + break; + } + + while (iov->iov_len =3D=3D 0) { + /* Skip empty element. */ + nr--; + iov++; + } + } + + return true; +} + +static bool write_request(struct xs_handle *h, struct iovec *iov, unsigned= int nr) +{ + if (h->is_socket) + return sendmsg_exact(h->fd, iov, nr); + else + return writev_exact(h->fd, iov, nr); +} + /* * Send message to xenstore, get malloc'ed reply. NULL and set errno on e= rror. * @@ -605,9 +704,8 @@ static void *xs_talkv(struct xs_handle *h, =20 mutex_lock(&h->request_mutex); =20 - for (i =3D 0; i < num_vecs; i++) - if (!xs_write_all(h->fd, iovec[i].iov_base, iovec[i].iov_len)) - goto fail; + if (!write_request(h, iovec, num_vecs)) + goto fail; =20 ret =3D read_reply(h, &reply_type, len); if (!ret) --=20 2.39.2