From patchwork Mon May 6 17:33:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 163424 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:81:0:0:0:0 with SMTP id l1csp257798ilm; Mon, 6 May 2019 10:39:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqy9PTobYN0mPBubHb1ETgmgpvQyNQHfcAvEsRMjr0KRxKukVrRm0Q0YcFj09FUcNvo1Sext X-Received: by 2002:a5d:654a:: with SMTP id z10mr19599929wrv.153.1557164393217; Mon, 06 May 2019 10:39:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557164393; cv=none; d=google.com; s=arc-20160816; b=byU/d/DNjJ7EEZTqmioUOl6ZvSGoQbu1PsbuXTtUs63YWQMtGAKF4xS33s+YMEOdka 7oCrVhGohjKR064orNsU1+uh1U5A7IygDHLMv+XB2CQIVJDEYvq+Ny6R0hokPXuukdm1 x+9GtzMLmLiwCZyj2OhINRDMo4caHvyETrGAPIITdQjWnc7YwT6eQOESlh0YRE356zr4 jwaRimGYpX6WtklQaPTnTrH/4rZ7WovrHtrOZbblZR4BQ5BSaeKrrZplRXgpjNy5nk2X Ct5e5pnOoqBOWUIGjoyYwIM6XubJ2jbINcnfvgftFXYTc+yoCiOiliSueL9uD5Q55CfY nIjw== 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=SxHcSgvXH46Lcz+sBihH+DQ9BaQG6plA/B5ZKvOuxJA=; b=yqCz8qwfcgTqgchaT/1DI5PvP7o0yDcv0ATzlHTBZ90o8mGR07Qf4aVnJIoBi9fKvJ cTwqjkW4f7/5ujQRrkhaH7h+8tKAlA4wnIG7YHVGoTJxlRB1GlgFV0DOtgyQZ1StdPJS HTfcygQNYWZP436fNB7Z8nym/UOu2xA0szR84U3S4yp9+9NbtljGTj+L2zrN3P5L0lCW OT3dWMqnFF9wTtmJv4xSm8CEmCyIHvOhItJyaNkunhJEcoaBUDiq8qPfrLQkeUtjc1HT nH5TApcCXcxML5lLCfTNg0XQpShOYWRhzwu1nx3cbrXxsgdYJPNClAT0kAUP5ZqwU8xi hn3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=pkil7NXw; 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 t62si12196756wmb.2.2019.05.06.10.39.52 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 06 May 2019 10:39:53 -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=pkil7NXw; 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]:59954 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNhai-0006Cr-5z for patch@linaro.org; Mon, 06 May 2019 13:39:52 -0400 Received: from eggs.gnu.org ([209.51.188.92]:39815) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNhVA-00011n-5x for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hNhV9-0001IE-3l for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:08 -0400 Received: from mail-pf1-x443.google.com ([2607:f8b0:4864:20::443]:33274) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hNhV5-0001Ee-03 for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:04 -0400 Received: by mail-pf1-x443.google.com with SMTP id z28so7145429pfk.0 for ; Mon, 06 May 2019 10:34:01 -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=SxHcSgvXH46Lcz+sBihH+DQ9BaQG6plA/B5ZKvOuxJA=; b=pkil7NXwQ0LqaEBzco3zRNKPlgnZYoK7PI32QoHcMkfhprBVa99RoQqn/KxfbNUL6f RzNppNhYONng6jOice7MGDn2zEVZHhdecPTJSWaMIKKLHAH2vamr9OdQ8PvekzWNkR20 /5q+jzRDAzNwpQB3YnfEcbJ4BZxtGIO3pSSu/RPY3MxaLfbkF6ZEQo0nhHc+YjQLcHDN i9fXAcPn/IJOMT5DuRTDMP/EY2DYx2Lm6H0WeDlgBLG3DUN+h9aj4u+4il7mWeSVrVwF 7R4JBa0ynT2c7RW151zD6hJnZmrqnMsjlmlTVzdb7vCpoEy6dQ44Bz90jc8c6ZZ93gSo S5Bw== 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=SxHcSgvXH46Lcz+sBihH+DQ9BaQG6plA/B5ZKvOuxJA=; b=cMS6Tbr4oiVKNJ56B7PzqlVYh9ooEgFOFq0PlUBPEzwBwFKqb/pBVM3woy6wkLbjOg UaEFdCSH0i3MZFS8qaJwOOvTWvs12/r6u29vfV2R0ETb2jcifRCtz/sQTKsjkFTo1fcQ u0rB7Ahu6pnI4hrRHXC1SYRiKoFQQsxKUCiDop6oUYC02qXt05xhiNXOqeGn0a3Yuhuw w9wLdWaoE1xclv/42hrbpZa1k1wFthL2ziL87nc/ZXZDW8Y27Pj7Wx1Vlw1sRDHzAbuG fca+wQXJcdKZxJ4DgxwBjKAKI2SmlyNQNljHiHuY/c5AHrvGevzqoG8b2LwnwCOr9ALQ WyCA== X-Gm-Message-State: APjAAAXcBHo4lzwkyNNwwTEmCFkxMfBPFXieFuUkLAGrFouQ2nCBFy/O sFr96i+oRoLSYt+PmBqGO71pOfoQu9Y= X-Received: by 2002:a63:4a5a:: with SMTP id j26mr32302687pgl.361.1557164040520; Mon, 06 May 2019 10:34:00 -0700 (PDT) Received: from localhost.localdomain (97-113-189-189.tukw.qwest.net. [97.113.189.189]) by smtp.gmail.com with ESMTPSA id k9sm2268839pfa.180.2019.05.06.10.33.59 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 May 2019 10:33:59 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 6 May 2019 10:33:33 -0700 Message-Id: <20190506173353.32206-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190506173353.32206-1-richard.henderson@linaro.org> References: <20190506173353.32206-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::443 Subject: [Qemu-devel] [PATCH v4 04/24] 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: 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 Reviewed-by: Laurent Vivier 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; }