From patchwork Fri Mar 15 03:26:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 160387 Delivered-To: patch@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp16273533jad; Thu, 14 Mar 2019 20:50:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqwRegVypWSUzCnC6KFY7YSuFrkpcRNRDZYy/Vae6YEDWfXil5e00oEGsa1fGmlIMkl4ZFrm X-Received: by 2002:a7b:c216:: with SMTP id x22mr576042wmi.41.1552621809321; Thu, 14 Mar 2019 20:50:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552621809; cv=none; d=google.com; s=arc-20160816; b=ehcGZah0L3whY/Fw1mVUILSu1Q7dUorDSh4QqNu+8/w2ZsqmpjhIxh3wlgWdOMoPP9 u7ANzbOunGOme114vKRDJ0ntBdsn8ACC6ejPi9gcsUpWpoSJelQdjgapE2/pICvcHzrH 2eoBBRgrrqkwR8RJCvaiAiFMvVnoLUQOQAgwmpS6987+nhIPQul/n3Jc9y0nW1kvPpGC CRe7YDWBUYIIgrHDTE08SrFBPUllit0kJ1hhrQ/3HtOmn0KbkU5szILjGuuKz0NmyLcR XzHakA85duVZPrAeSyZcAlZoF1o1cndd6gRR9uizdLcSlvI0vxgKn8KmsGOOPQzIEQ6l nwQg== 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=3rnkQV2mDwDjm+peqMAHfkM55fLPgrlpN1xVPWcrulw=; b=OaUvWwQUfyJ9BNUfEO1uC8KqgUICj7VgdhW1VAKN2UYEeFMN2rrRwKZqdgXACguh8v DHoTBC5L80Fd19iN0JA+NDJ6ZBXTGSkrpjESDXv1E7Ws1jS3XjbFyRPpTo1fhay8zXp4 9oB0dQ+rJoB8142q1KaLWmaX5ulP9n513wk+g24VsfkTn4LwiJIgyxnoW612VQw2FEQF 0v2TduU9MMcR2SuOmUkaQDlqP6PaLHttSHyBYcG3nYrWlMcMBn54rZHwj37Gf9+AvFNh 1r84VYSkS1lBwg22REVeDRASJClPBhxMQSqpqt84Nh10eFA1h8z8W/cj4JfL4I7XsrW1 nLJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=UA98FuJ1; 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 y16si459643wro.373.2019.03.14.20.50.09 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 14 Mar 2019 20:50:09 -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=UA98FuJ1; 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]:48913 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4drE-00016J-8h for patch@linaro.org; Thu, 14 Mar 2019 23:50:08 -0400 Received: from eggs.gnu.org ([209.51.188.92]:43798) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4dcM-00059t-1g for qemu-devel@nongnu.org; Thu, 14 Mar 2019 23:34:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4dUT-0002jW-60 for qemu-devel@nongnu.org; Thu, 14 Mar 2019 23:26:37 -0400 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]:39423) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h4dUS-0002im-VO for qemu-devel@nongnu.org; Thu, 14 Mar 2019 23:26:37 -0400 Received: by mail-pf1-x441.google.com with SMTP id i20so5282816pfo.6 for ; Thu, 14 Mar 2019 20:26:36 -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=3rnkQV2mDwDjm+peqMAHfkM55fLPgrlpN1xVPWcrulw=; b=UA98FuJ1Vkv5VA/8SatuiOvrtXjTFk3tIiR8NTSiNwHQhKrev91IJpYBABGQvqd8LR +sURLTvbXl7yzMOd9KeHBY45Xd5oabadEzV9LS9Z9YfUcEtJvFGjTzNBGGnOsg6Isgjo TXF8hcgd+klBGcNdaZmoTRrQE7/cOLh3pilLtwYk2R+Z5ez0H2rHx83i4icSNbFN9Dgg t+/8k2Vxw3gPC9014wGPLZPB2U+m1qPHZ0r6nvEcK32q3EZACunssswZJVxa6ZVTCcnY p6sDzyI8IFGTR/2G5D+OM1mpsJuWFKv7NbVMrNrIgW6nuRBhDpzDCYFMIZfVjiyJwqdB um9g== 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=3rnkQV2mDwDjm+peqMAHfkM55fLPgrlpN1xVPWcrulw=; b=cLXSDz7XvcmFQczF2Y1MM+jA9PxxY2twDP7bYNO71c8RYnfc1e1M+dtdFvTbqa0tIo mlAqLYQF1OAozmCLq4D/GFs75R1JZnF9XIlxlngBAfqu3nN+8stlNkKoFGZb8/XXljAp GJtj4B9W/pzxr/hWG7DcloMjrJqge5gPM3pCV7kxxWi/xaTgIKxjyrWUdJdIi9gDxkDP iaKcejtH9Yv0140tY+afPfA4bsPp77Jn2/qsKSAJhy3BMKEFvsXQlFhNw35CQ3hkMh0N mjAsweSXygYwRkFcWUF8AbNPBjIrM9KlNTZ7AvRqhmnnP1vn9NkW94Y21e6jL/piNSNR +xCg== X-Gm-Message-State: APjAAAWLB320OA5P/5eQLJYdq94HrIoLVnDl/IvpDr/KEXczICpIezYc yJhfIgb5Coe3GHa4R3QuxHpSCvmIx6s= X-Received: by 2002:a17:902:b188:: with SMTP id s8mr1720020plr.327.1552620395603; Thu, 14 Mar 2019 20:26:35 -0700 (PDT) Received: from cloudburst.twiddle.net (97-113-188-82.tukw.qwest.net. [97.113.188.82]) by smtp.gmail.com with ESMTPSA id d26sm690816pfn.86.2019.03.14.20.26.34 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 14 Mar 2019 20:26:34 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 14 Mar 2019 20:26:09 -0700 Message-Id: <20190315032629.21234-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190315032629.21234-1-richard.henderson@linaro.org> References: <20190315032629.21234-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::441 Subject: [Qemu-devel] [PATCH v3 03/23] 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.2 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; }