From patchwork Fri May 10 01:24:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 163792 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:142:0:0:0:0 with SMTP id j2csp1721965ilr; Thu, 9 May 2019 18:28:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqxRm9nKkVcndEv5tU+qYUYGgFIVagedsI2IimlYgtu+26kF92/b8SdNwDko6piBAhfFyWdd X-Received: by 2002:adf:ec04:: with SMTP id x4mr5391226wrn.18.1557451715987; Thu, 09 May 2019 18:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557451715; cv=none; d=google.com; s=arc-20160816; b=EajYDw/bTyOLAITsZIfdmkOlEAHcajBo6OJn6uTh6ii96M6SSZyxLpB1tEIypHAemI h1h5u4QAfF+ZeBKLuv2tQg2LPzHm3yTud43dUK1J5+n+agsUEKDD5lc6b+1egT4J2vBb DsWwq0WoF+VxeiR3iN+F65jWkbaHvU+fEQqR18bxZl1r4OLet/J7vIVKfIIR3vID9ONH eOJFwZ0G/OORXgxXwRmKzLJ4uLoibuO36Ei4P1GMRBjpXRGyILQHMxC2Fh3ZMS+zU9KO xxw0yJcvrn8RVAHv2FxqmabKXbd6yt/jPUyBSsd/AKU5LvJBKEHi2KFPOREhA7wEFbIX Z1+Q== 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=lbakVQlDbexqJ2Y9S5ZvFpP3MQgO7PX2F1kYhFhPcmmCX6kYS7C+MRmrlFZp72e0yq jiUuSubbqBHAWcgzaMz0f6m6e81IMYMZ5mPQXiztUukh7qFvtnQzT6NlNqZQiitl5cx2 osoxW1CFh+zjj17YLQDoJDZt0rH0YbUi4athZboPswpfe5v9L5B4+Kx5s8SbLJ8eUEcQ YL+D1rO3aPz4Siw2idn+uVLVHvbNxnQ/jvvIEUHJYxbyYhoqmlpO02BkQ47HNHQd3dUB /HSzKAcw4HIwTYU0zIsdJBEI5f5ZGvCXYkEv0nZirs1NlvnlYKNqBudpSMXEE7lLAJE6 TEfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=DwcFA9lK; 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 j5si2566323wme.179.2019.05.09.18.28.35 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 09 May 2019 18:28:35 -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=DwcFA9lK; 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]:35030 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hOuKw-0000se-UN for patch@linaro.org; Thu, 09 May 2019 21:28:34 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58616) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hOuHk-0006Il-4t for qemu-devel@nongnu.org; Thu, 09 May 2019 21:25:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hOuHj-000147-3r for qemu-devel@nongnu.org; Thu, 09 May 2019 21:25:16 -0400 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]:40271) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hOuHi-0000vJ-TD for qemu-devel@nongnu.org; Thu, 09 May 2019 21:25:15 -0400 Received: by mail-pg1-x542.google.com with SMTP id d31so2124830pgl.7 for ; Thu, 09 May 2019 18:25:06 -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=DwcFA9lKFVT3ktTPBdbJzkZtg1ifSLhXJomWhhU457RtwvhB+1003y9TgSYGyTBbyM /z2t6iHq552O93wdb/aYLTkfJAAGBfO0YjBNo3D/Z+zYXgoU7Wuj59ZIi39uy3kMUq/L lcZvUPsnxQyu/HnLIdWIYGkJvJBbhF7oDzUJxTbTQCR0Via3ambGFaBgQ3/3MEPpaIRU Gp9EtjI4RnnjFRaMQEV5//jOUgha3LEcCnjNX8VJbAjJlVfSF+p+uW5t6lpjdpCDDzpF 3oFvj+uUaCnXK16KLelYS4hukbS5p/hUuMa4JHrfhnCphhdy9GbHpPoQV6zUXpf2DYW3 7DZA== 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=BVQXGPm+CrEl7641O9EKC5zPd6O9raAFqt9+zY654CF/T/ZTmbtPFoz37rzu5sVWj+ NxlNTf6OuPLRt/KAcJRVoL7vvN28lFn7+MXVzXLxFWAnInCBFPGSJLnZ+Isu5VdCM9jW qFhKmTu+aRgdVgil8Ve1Ei1VZXQv15zUNVlwNsZg0ifh1/YoaDkO1/T52n6D2Nj1XEve XDP2zVvrsAWAUTUMRxUNNmEOthlIPDmNHKHceeooXXb7pOPxMfEYi82xjdlq7Qm0Qtee sAHs1Mf5g1CQ/Qn/rK8P46+dilIWTu3X80XEqRKpfblFbLATgtpqwtDbxRCwq6w5EOEt jzYA== X-Gm-Message-State: APjAAAVxovuw/xRid+n6DLxfxBaGZ7OI/6zF+lWvHat4RaPd9swICcZH QDK4gcuZuEVQ5L6ykVEotT+dKzvNIkw= X-Received: by 2002:a62:1a0d:: with SMTP id a13mr9876113pfa.198.1557451505482; Thu, 09 May 2019 18:25:05 -0700 (PDT) Received: from localhost.localdomain (97-113-13-231.tukw.qwest.net. [97.113.13.231]) by smtp.gmail.com with ESMTPSA id n18sm8252700pfi.48.2019.05.09.18.25.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 May 2019 18:25:04 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 9 May 2019 18:24:38 -0700 Message-Id: <20190510012458.22706-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190510012458.22706-1-richard.henderson@linaro.org> References: <20190510012458.22706-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::542 Subject: [Qemu-devel] [PATCH v5 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: 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; }