From patchwork Thu Oct 25 14:10:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 149529 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp1390453ljp; Thu, 25 Oct 2018 07:11:32 -0700 (PDT) X-Google-Smtp-Source: AJdET5fg/zgzfoPJvqpqic+siosjDDIX+3T9jrKBqwNkveMTbslh4P0Yq2pPq+FDXtSFMoO+HlCF X-Received: by 2002:a17:902:d704:: with SMTP id w4-v6mr1724212ply.230.1540476692302; Thu, 25 Oct 2018 07:11:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540476692; cv=none; d=google.com; s=arc-20160816; b=uYW95Vxse8nsZlXmJfoKfuYaxWKWIr95dTqFBOzkd1aNae6+ohWPNVgpbIneMml+p6 Xk7o5SjCcUAUSsXLU4ovkd28CWSFmUy6ZfRDoKqMC6bfQdObvVOY+hglBI9/sfVeCMVe sFNLRtU/a6VxuneQ5vgxGWRXQj4e62sHlRABGZREQrJdY7ozENz/MrS923YOP5Y7IjYb xrusLfvwQw1rC/1ZUpkfBre4W4uA6BsGZTmGNK+GEShH3qk8iqhZe2ZFJuTZxCTL5omI YRYjou6wbWxjcYu10lu5BbbqQT0YokavOShjmoEhfWwO0fLl5McRLxK+Mm/rj+DoceYW oSZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=3McfX6VM4GT6FWy4p7LlBt+mDTyQf0U4s16RACY8JMM=; b=vxWfMWowqpSPgTyRpcfkrL0A37gZiWDwIQ+dYJ7KLmfWd69UJDwgHID0o6y/Trm2Z0 dQ4xIssuhRxSSCGQi9RJfR/nZstmbpJX7T1cza14CEv0Lbt2dUVJ4U2hhE/ofCvqn1hv ufa6BNyMaV37YBn3tLP0k4DwqOO9rWrkZ6zCN8cIU5i6uHXA7p6Cz9PdnsTsYD81XPSS BycsmU+G84/84T575jSN24KuzB1jL3FAa5pbcPzimWSQ+WB/afBC5SARBKTNikhpVohe qq/iXolZLUiDkIBefxWf+UZYJjQZlppXF5JPMSR6GaPl2Z0OSNaulbZIIgtH/0oPM59f tKpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BMm56oO5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1-v6si8252504pls.296.2018.10.25.07.11.30; Thu, 25 Oct 2018 07:11:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BMm56oO5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727865AbeJYWoB (ORCPT + 32 others); Thu, 25 Oct 2018 18:44:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:52542 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727699AbeJYWoA (ORCPT ); Thu, 25 Oct 2018 18:44:00 -0400 Received: from sasha-vm.mshome.net (unknown [167.98.65.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A20262054F; Thu, 25 Oct 2018 14:11:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1540476665; bh=74ySAQMBThih2FK3Rot/66i2+Wp8eF2sA/P/6jvoxGo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BMm56oO5DC881ZtyzHcwUkwY+UgZoTLyWJ9E6pPUJh9x+PUJt7LZjY9e/mZ3R0ybh 50+B/Iw9Pvw8/yNYwYebgFFSr62Kh7QHp4PmSLE13zKWLIpaHBTfUdluEg9mchp3hz n8st+rZxFsyGEUQHXWnRS1FWJkSn6t3OYXlIqkjs= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Arnd Bergmann , "Martin K . Petersen" , Sasha Levin Subject: [PATCH AUTOSEL 4.14 07/46] scsi: aacraid: address UBSAN warning regression Date: Thu, 25 Oct 2018 10:10:14 -0400 Message-Id: <20181025141053.213330-7-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181025141053.213330-1-sashal@kernel.org> References: <20181025141053.213330-1-sashal@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Arnd Bergmann [ Upstream commit d18539754d97876503275efc7d00a1901bb0cfad ] As reported by Meelis Roos, my previous patch causes an incorrect calculation of the timeout, through an undefined signed integer overflow: [ 12.228155] UBSAN: Undefined behaviour in drivers/scsi/aacraid/commsup.c:2514:49 [ 12.228229] signed integer overflow: [ 12.228283] 964297611 * 250 cannot be represented in type 'long int' The problem is that doing a multiplication with HZ first and then dividing by USEC_PER_SEC worked correctly for 32-bit microseconds, but not for 32-bit nanoseconds, which would require up to 41 bits. This reworks the calculation to first convert the nanoseconds into jiffies, which should give us the same result as before and not overflow. Unfortunately I did not understand the exact intention of the algorithm, in particular the part where we add half a second, so it's possible that there is still a preexisting problem in this function. I added a comment that this would be handled more nicely using usleep_range(), which generally works better for waking up at a particular time than the current schedule_timeout() based implementation. I did not feel comfortable trying to implement that without being sure what the intent is here though. Fixes: 820f18865912 ("scsi: aacraid: use timespec64 instead of timeval") Tested-by: Meelis Roos Signed-off-by: Arnd Bergmann Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/aacraid/commsup.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) -- 2.17.1 diff --git a/drivers/scsi/aacraid/commsup.c b/drivers/scsi/aacraid/commsup.c index 998788a967be..3e38bae6ecde 100644 --- a/drivers/scsi/aacraid/commsup.c +++ b/drivers/scsi/aacraid/commsup.c @@ -2506,8 +2506,8 @@ int aac_command_thread(void *data) /* Synchronize our watches */ if (((NSEC_PER_SEC - (NSEC_PER_SEC / HZ)) > now.tv_nsec) && (now.tv_nsec > (NSEC_PER_SEC / HZ))) - difference = (((NSEC_PER_SEC - now.tv_nsec) * HZ) - + NSEC_PER_SEC / 2) / NSEC_PER_SEC; + difference = HZ + HZ / 2 - + now.tv_nsec / (NSEC_PER_SEC / HZ); else { if (now.tv_nsec > NSEC_PER_SEC / 2) ++now.tv_sec; @@ -2531,6 +2531,10 @@ int aac_command_thread(void *data) if (kthread_should_stop()) break; + /* + * we probably want usleep_range() here instead of the + * jiffies computation + */ schedule_timeout(difference); if (kthread_should_stop())