From patchwork Wed May 22 18:42:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 164886 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp1255699ili; Wed, 22 May 2019 12:30:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqwJXkILsUwI7etglbrAuas4YseUJKW/4TNu5d9sLLOvS7Hb7CKhqn7XKy4LT0x9FOV27S5S X-Received: by 2002:a17:906:9609:: with SMTP id s9mr20150165ejx.35.1558553429281; Wed, 22 May 2019 12:30:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558553429; cv=none; d=google.com; s=arc-20160816; b=EUJzDaLYOJzGyxXSJ2D6OLHKW5JnXGu3u6I2f00zyh3Cjo+d23T6+Auaibgn0akq4D 095UDGQXYWUirEJrgEykkGMNCsBy3VErCIcuGIQ8ShUesPX7KudnbVf6AxxJFLp/dmtg THiz28wiGCGCkLBqBMns9Y5Refxq04iie+47eejyzLXtGPpiqLmfgBiriSd4bfoBrp7k X3P3l8G7+4lELSQxMejzqlxY6s9wf1xfLe8IvzMv89277xI0gkhXO949sYk3YU++n5Kt ZJU59SZcwcnK0iP+rFfsHXaQOpvgBvLUzsD9FY+bgg2a3ZxoKUIJsim/IloeDHkTTkzi b/xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=VqY3mH4YRn0WtpmEi7qlWT5KRhdZPDg5AxBMBXlF56s=; b=Ct2XdYUsevvBPN9ERxFvwqmQKSUq+4G8hBX5sxmF4ZVZiIeOqcJtjmnsWvZbda6Zl2 WcHzcUSMPLMMSpf3kKpr1DzM6JFmwEJgPj1MM74iHvyrdqihJDzJ6fpHpXpatg5zcdEG Sxdqix34V52P5vkeHaOC9xsxhHQAyPlsTn8slbAFOuyU0VvVwz5PWPAMfwPs2sTZ2ogH n1cZyyY6oJp9SUPz6ne/GircuG/RFUEOzT2xIdxSTmkNufxZSYS7mWB6xByyTjMkC2o+ KCmzr+tfYYm30B4mYs99jOVTBbrFRJjuFEKZCxijTx0dOD/NfQTjczyy0wHBX/71hh7a o+OA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=C2fXkltt; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 6si2278115edu.147.2019.05.22.12.30.28 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 22 May 2019 12:30:29 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=C2fXkltt; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:50188 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTWwV-0005Od-Uv for patch@linaro.org; Wed, 22 May 2019 15:30:27 -0400 Received: from eggs.gnu.org ([209.51.188.92]:34793) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTWOr-00086d-Be for qemu-devel@nongnu.org; Wed, 22 May 2019 14:55:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hTWC9-00082C-II for qemu-devel@nongnu.org; Wed, 22 May 2019 14:42:34 -0400 Received: from mail-yb1-xb43.google.com ([2607:f8b0:4864:20::b43]:33820) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hTWC9-00081c-CR for qemu-devel@nongnu.org; Wed, 22 May 2019 14:42:33 -0400 Received: by mail-yb1-xb43.google.com with SMTP id v78so1266115ybv.1 for ; Wed, 22 May 2019 11:42:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VqY3mH4YRn0WtpmEi7qlWT5KRhdZPDg5AxBMBXlF56s=; b=C2fXklttaFB55rJ9U5Po24wtMKgUXJCFBv8eyRy82xHDen3Y67tB6R0FHjz+XIxhMf TVbBFlEUBGn1mwgOeIZL/Z5mWUeY6SXf3L2gSVrD2L4Gnp0/1Icf6W1+MExagrXiN/12 wU/NZ1W3Cc4ROgH8/iDeGkNrbYnZGzpRk49VISUf52ZbwxMuN0MZPTaFQFbZ1rxLi+DY XCrAIpwqUveEhfbZ/Oh9p/7EgU1UYTCUzQHAZGlkFvYKEj/MPZrnncqxTvn2/pPKEHks 5ZvNIjd5vcVHdRoyHQo9RB9bXUAnemWYvxIXTqQLn961ErKS79Sju/WZyruO9q3UaUK3 7sUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VqY3mH4YRn0WtpmEi7qlWT5KRhdZPDg5AxBMBXlF56s=; b=lEq42MIfoHqVnkhYviWu2BdIcbJrzk548BHCuxxRYEPMqhcB4kLrmiORDmYw3J3Uxp hbezfEl+xoIspqnCT9WtcyK0+Q2E+zsW5EHjuh4z14SARclsZZL57SYWiuTDgKqspsQC 6r24atuvEjciKoyQhvTwVBulwwG38J0yH/4l+nQ21ak/5Cf72WN2c2o4raYzMPGUa5xY xf/ed1sykS4yXRjtPbry+CtH2C4HM4PehgsfKWPZRqgjeKQI2WxgzJLiOmCbl/D16RjI Iz+YOMVlPwTdNvamqsNxxsh4t8mcn5UAvhbJthAE1AYY4mq7s9obK+NPz59E1T0NklfF 3dNQ== X-Gm-Message-State: APjAAAUgKOZU0xZscQUDRXzqWtIMnuila7MbAzKRWYqpDpy3T0vO1ESZ efMhX5awJpmM1CsczcG4vSbuekcBkEs= X-Received: by 2002:a25:ef10:: with SMTP id g16mr10101072ybd.510.1558550552342; Wed, 22 May 2019 11:42:32 -0700 (PDT) Received: from localhost.localdomain ([71.46.56.17]) by smtp.gmail.com with ESMTPSA id 206sm1299445ywq.74.2019.05.22.11.42.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 May 2019 11:42:31 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 22 May 2019 14:42:05 -0400 Message-Id: <20190522184226.17871-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190522184226.17871-1-richard.henderson@linaro.org> References: <20190522184226.17871-1-richard.henderson@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::b43 Subject: [Qemu-devel] [PULL 04/25] crypto: Do not fail for EINTR during qcrypto_random_bytes X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" We can always get EINTR for read; /dev/urandom is no exception. Rearrange the order of tests for likelihood; allow degenerate buflen==0 case to perform a no-op zero-length read. This means that the normal success path is a straight line with a single test for success. Reviewed-by: Laurent Vivier Reviewed-by: Daniel P. Berrangé Signed-off-by: Richard Henderson --- crypto/random-platform.c | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) -- 2.17.1 diff --git a/crypto/random-platform.c b/crypto/random-platform.c index f995fc0ef1..260b64564d 100644 --- a/crypto/random-platform.c +++ b/crypto/random-platform.c @@ -65,29 +65,23 @@ int qcrypto_random_bytes(uint8_t *buf G_GNUC_UNUSED, "Unable to read random bytes"); return -1; } - - return 0; #else - int ret = -1; - int got; - - while (buflen > 0) { - got = read(fd, buf, buflen); - if (got < 0) { - error_setg_errno(errp, errno, - "Unable to read random bytes"); - goto cleanup; - } else if (!got) { - error_setg(errp, - "Unexpected EOF reading random bytes"); - goto cleanup; + while (1) { + ssize_t got = read(fd, buf, buflen); + if (likely(got == buflen)) { + return 0; + } + if (got > 0) { + buflen -= got; + buf += got; + } else if (got == 0) { + error_setg(errp, "Unexpected EOF reading random bytes"); + return -1; + } else if (errno != EINTR) { + error_setg_errno(errp, errno, "Unable to read random bytes"); + return -1; } - buflen -= got; - buf += got; } - - ret = 0; - cleanup: - return ret; #endif + return 0; }