From patchwork Tue May 14 19:16:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 164229 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp3000675ili; Tue, 14 May 2019 12:17:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqxvT3uPgZDkAFSkb5IMEM5Lzwoo2aa8B/6VYfo1tQJkFwvZiXrgV+6RvzRK9Jy6iyMlzGWx X-Received: by 2002:a50:f482:: with SMTP id s2mr38930791edm.44.1557861458554; Tue, 14 May 2019 12:17:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557861458; cv=none; d=google.com; s=arc-20160816; b=MdXqTzcTi94Lc/5DNhDV9H4/l3xTO3eFljj5q+qcEXBcip0dPGGyQhv72LAN9g3LOT 5rpkdfu4VprU4Xfhqm7u7jMiHmuM2/mbWzvNzPxZDXrpKOV+o8hZo7M6LzoANKMZBBg2 7xqTfTece8dS25KY5xwQcOGZteuRUoMwHe4gtwdIwzt+6s+KKe4QFPlUWFCDTQ85mk+1 Kd5bwyWA+IRiQyoV3nRicxOVBRGR8M1TOC74T+5SKyH57AtohfzZPTS9swI39NPXOA/7 Pl9YB9i0yXYkcjPy7/rqROAwMRShpJ5UrFigKZVp5Zehp/KRhxmtZhoE79/7FkzLCmbj uZtQ== 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=F+DNdi8aZ9w72uvzmDc6fnI5IhfoZ+5jAzOsC1+FxuQB8b6euaxw3xioigoksYqgUF w5ZwUluLLrqv1wFvjQzkq3si06CSwxHIjMpW6IKoXuyPZkRIZaUSG9why5TMtOKbe2+b 5ZYbVYlq193nN7tyju+yzQGqYBRq3hngF+6/OSw6RKMRN/6cjTTkLlPeSlwI2+NjfTCo +mOOC3KH79/hnCJVWWf3EcOCLhPnTVnoyqvGMUOw0aeUvaPg88DKCta9x9ymQWJxJ2JN Q25ETAHibh7Xw12HwS7AQA/DA/r1XoyKXeiRJKNAe5NHnLBrVYxo2Na9KRsnKVQQ4z2J 93Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=MV1an0vg; 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 b11si2506964eda.406.2019.05.14.12.17.38 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 14 May 2019 12:17:38 -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=MV1an0vg; 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]:52910 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQcvh-0002V0-Fb for patch@linaro.org; Tue, 14 May 2019 15:17:37 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48732) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQcv8-0002SE-Fb for qemu-devel@nongnu.org; Tue, 14 May 2019 15:17:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hQcv7-0006cz-HD for qemu-devel@nongnu.org; Tue, 14 May 2019 15:17:02 -0400 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]:46217) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hQcv7-0006by-By for qemu-devel@nongnu.org; Tue, 14 May 2019 15:17:01 -0400 Received: by mail-pl1-x644.google.com with SMTP id r18so59138pls.13 for ; Tue, 14 May 2019 12:17:01 -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=MV1an0vg/aEqlkOwBdnA9NRpAfsJuH1ukbkckmaIgldQYs+e8INRbWBqaMeKoFt/RH H3WtchNWEb3bXwtdWHJi9JvqgmzvsuQ2a/YCoZgqQpj9N/izR4jldIXaXmdPivg1+qtg R09ts4LpJQrvvpj2FRSGAdmBO/VsmL/ni0IakBhLSLFPcXadNvVDohPmvo1e6FxBpYm+ sI29s+bMRGnRVnsTckOg5sgSSywdrVRd+mvWhXDcULZ5uq0EVendavYO9AzYLCJclOy/ 7XJ0Zs24mUD597t8Ok3pUDInm8NRmJZyZKcjKeiut+5SQGbZ/exf2CsozEIh/5ho9TxH /zqw== 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=rnZnGLZbU8sCUxS1cAaDvk8b1KABeIDgQk84u8vvDIujg94bDCpqjL1lN0VGDY1AQ/ 1AFFjcNUfLa5hJQMPHrk7g+Iucl7WKbZQtSXw7aSYZFu/7gBP60FUC2x7GqrziEJnJnE Uyehy1y/CbbJUnXBJ0Wxt/1qC9x5b9tVOl1BdJnq7ktXNmWp1v6zDkjX4QrBtpIB8kUW 9l7ebeI+KJ5VeVrkTH3mD3SHuFNWBI3Waphh+e8FUQawi534Sh4NAG6G8YAhBDxngtvU mPNKU+2qylyEmKs+yUx5+r1XteWPHG99NuI4CXtaSbHgIH3KmMhEKluBEpc8bfcAmGuU oduA== X-Gm-Message-State: APjAAAXGd+N4pzCN3vFeGGzzi3VxiZz3l6F7aJfCupenWjH7t8c2+58p 6EYqwy3IrYkKzeQd1xgkwnQyzzaMMhk= X-Received: by 2002:a17:902:5e1:: with SMTP id f88mr38615351plf.226.1557861420082; Tue, 14 May 2019 12:17:00 -0700 (PDT) Received: from localhost.localdomain (97-113-13-231.tukw.qwest.net. [97.113.13.231]) by smtp.gmail.com with ESMTPSA id o6sm18682982pfa.88.2019.05.14.12.16.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 May 2019 12:16:59 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 14 May 2019 12:16:32 -0700 Message-Id: <20190514191653.31488-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190514191653.31488-1-richard.henderson@linaro.org> References: <20190514191653.31488-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::644 Subject: [Qemu-devel] [PATCH v7 03/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: , Cc: lvivier@redhat.com, berrange@redhat.com, armbru@redhat.com 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; }