From patchwork Fri May 10 17:30:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 163908 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:142:0:0:0:0 with SMTP id j2csp2669744ilr; Fri, 10 May 2019 10:34:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqwkJ5KIqzIOH7NkCzA5lyYNrUOev710GWBgKJytoObpDXiB/Ula/Uy4O+0WM2pW5bH8qMgT X-Received: by 2002:a50:97d6:: with SMTP id f22mr12402206edb.115.1557509670434; Fri, 10 May 2019 10:34:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557509670; cv=none; d=google.com; s=arc-20160816; b=ISxHORCoRN0q32F8aMYV1atZPCoFlDvoJJ45IVnQKq0siSyVyzUjHEjaVCtPALDepT MAxD/3PXxL1DTXSCTdGifs3DIGbVrsQ8nfN+4SPKlGbd4jJ1lC+kqKPGb+z6DTpmjz0G f7Qkjh7NIzGHhrTb4yyevzSK/Ha0TPaOAIsIrrlxdJbHhA6gtOnZD6HrYMQYK49qxN8T TSdMG/yNZAmWuPBA/AXUQ839yqO9Bl/D0LfouqM0oA4lPmeOOh/hxN0chQJha84qqoLo mKJS6pl7iP17594Z5jQ6uaaDzX/JXUy6r+5ptNasTKVHNmTwUKMazSQbs3oM056VpnNU FBOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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=8Bh7531eyDC2/9D2e9XWW3j4XTcdT7MvDIqTNSwlf4w=; b=cMaj1npZIHVTDaemxhnjYWdQie5K2007loAd+BStQxHxU3as/12aIORC91ljjlmihE OJ0hFlTCFUUxVOMHpCYj8d5NYQHaLkRio1AbtiwrJBm2M1VMrsosUI59xCxTwEy4Rnfu mBsm/UBYkI+o3JRnrv1qsZOI5WVMm4uW7k/l019jTxwJfUUU+m4mms5lc5azVD2OO7No P1gU/Nd/T3gGfwIZsugfj50Zonb5LX+cdB2ugYDPkQ9OD+xArmfFVXvgDzwdrBKCV35w hJQoq4+J+sFsCIBlk+AW7vfvl3Xvuc+CZaffJ60gaE+s4jdxpJ5iPESl1n8HR7Ywn2yW nETQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="rqRq/qk3"; 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 a3si3339502ejp.64.2019.05.10.10.34.30 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 10 May 2019 10:34:30 -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="rqRq/qk3"; 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]:47506 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hP9Ph-00024Q-8q for patch@linaro.org; Fri, 10 May 2019 13:34:29 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58993) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hP9MI-0007Jf-MZ for qemu-devel@nongnu.org; Fri, 10 May 2019 13:30:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hP9MH-00060O-Ok for qemu-devel@nongnu.org; Fri, 10 May 2019 13:30:58 -0400 Received: from mail-pl1-x643.google.com ([2607:f8b0:4864:20::643]:41870) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hP9MH-0005zk-IO for qemu-devel@nongnu.org; Fri, 10 May 2019 13:30:57 -0400 Received: by mail-pl1-x643.google.com with SMTP id f12so1118456plt.8 for ; Fri, 10 May 2019 10:30:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=8Bh7531eyDC2/9D2e9XWW3j4XTcdT7MvDIqTNSwlf4w=; b=rqRq/qk3z5LDC9iz6nYAtBTgYUsQzGLpDUzuUnkdd8e8U47ENPNnTjWZlAyVXFwBat whBGTBdYMfEFgqXDuKGEjd62qQlKYonCB1snwzTL8P6S2M+hDDqFxS85MjKn4NuP4hg4 wIjPLolf6alu4O9/6T+qQrjVORrAIQymVRh55XESRP2xgi5F5IQearzkRGBfVjDnSBLC z73MKP7jYqWbkBUVmUq5JGEopAw1TeBXUTLW8JEiALiLsLayMqNpa0qaaH+UiyNJDznW e7i3n1GHfqNc5T0zYctCx8WdfNvvU2BiCKTAgMrKtoBJLpFot0CWF7uQGagAWGcQ3qt/ Xv4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8Bh7531eyDC2/9D2e9XWW3j4XTcdT7MvDIqTNSwlf4w=; b=QMYELBOMefFjXbMJOVuVeaEcKMev01nOeepIeE2vl8suTqaRPMPdO92ID2E44E54f5 +ZYaEeCX3B6BwBLT9gAJu5YBcmwvIV9wFv18ypQ8xP5GuZRGAcaN96Jg7nlUbZRfhnSr f0q5eaj6VDvAY5p0x8E+KbK3ZVjxmLThVwINXNJTH/pIH7NLys09tEjCYKu8jUCxWu2y fH9YSN/L+U+HwiJNL+n6u79YkY5GEbHJq/JfypmcPAXLE8XlQw3bnTRH8JjeqgVOFbwu nFNKd+iZouf2VU108HSE1d2DFAMjD+lt6I4AXrGHJhommhPgei+hDLfXHdzMlv3ME80L mK7A== X-Gm-Message-State: APjAAAVdpV8s3cUvJ5J6Kpa+WZR3NO2BvyGcp8Li/jEPdI8s9TKiwoUz 7RR9mLWtMzy1uWCWw4OG93zBBPfRMI4= X-Received: by 2002:a17:902:bf44:: with SMTP id u4mr14546915pls.171.1557509456326; Fri, 10 May 2019 10:30:56 -0700 (PDT) Received: from localhost.localdomain (97-113-13-231.tukw.qwest.net. [97.113.13.231]) by smtp.gmail.com with ESMTPSA id v64sm7936792pfv.106.2019.05.10.10.30.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 May 2019 10:30:55 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 10 May 2019 10:30:28 -0700 Message-Id: <20190510173049.28171-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190510173049.28171-1-richard.henderson@linaro.org> References: <20190510173049.28171-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::643 Subject: [Qemu-devel] [PATCH v6 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: , 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 --- v3: Rearrage the read loop again. --- 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; }