Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2617823lqt; Mon, 22 Apr 2024 16:58:45 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUfwpBefNAiHIgC4sMOkDjnJtFRKPhdP+1xVjUzxIvuCIwf4YahQtlVdMvAGIkxnhoqJl8CG5bo7u6MidLzJzITlAtvi+Ie5jtSvsvwjw== X-Google-Smtp-Source: AGHT+IEECUFt5PovkwAFpjYVYKKSUOw4fXyqZzYwkwg2kURqK2dmf9HDBuOIA3pb0yodSuW9pr5x X-Received: by 2002:a05:6a21:3384:b0:1a9:c80a:bdcf with SMTP id yy4-20020a056a21338400b001a9c80abdcfmr16154540pzb.2.1713830325441; Mon, 22 Apr 2024 16:58:45 -0700 (PDT) Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id r127-20020a632b85000000b005dc4912432csi8615795pgr.577.2024.04.22.16.58.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 16:58:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-154233-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@kernel.org header.s=k20201202 header.b=lcj4Glk6; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-154233-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154233-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 72CDF286DDD for ; Mon, 22 Apr 2024 23:57:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A0CBB158204; Mon, 22 Apr 2024 23:54:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lcj4Glk6" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4575157E9E; Mon, 22 Apr 2024 23:54:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713830074; cv=none; b=HNLnE1AjiHp+aoODpqzajRA2OMrvuqqRAVe9AkopYWPh/gHEcd9um9qyEZ/bYlsIE8dGU9ZEsjsmc94moK1cvpPV8xAQ6JP0iBCKM8zqDaurZlYV/Jt8IlYMdYGpRr0cUv9OoY5kAbJYO4FqgBBe6EDYTVKo/EsHcXLujIssqYo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713830074; c=relaxed/simple; bh=qyalTRlySVo94DoFJq6NMhigMn6l+of2ZwomjRJIb7M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ViM4NjNVqyMOj2wnoTXJvFOx1NyPRQ5LrPrJz/FcQTPF3/7+nkND7YtlrLFhiIfnpPVfgmlMzAuU3lVn/bwfmUn01oURl+BD2t8m5vbkmKa8Uw5EkOvaL7TxCkk7j82Y3nAno35/+aciS3YzkyzzWcnJHm9FMgNUC3I4bp9LXrQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lcj4Glk6; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9194BC2BD11; Mon, 22 Apr 2024 23:54:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713830074; bh=qyalTRlySVo94DoFJq6NMhigMn6l+of2ZwomjRJIb7M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lcj4Glk6dz790n1yidIGQsU+V9MrWI5jZter7p4gzuVQ75m2DS9+x0OLqtUUxC0Dv MDoOYMKm5ss/83rwHhWXpk3L3hALyrt+FYQJmCJ9bkjYmNWn833tdvtnf7QKU1HPI8 TwDiTyFzY1r3ZBskSNxRnNOVt+Kubr6wvISe8FiNwXKKHiNTzn8rA9QW8oRqP8ID00 E4HTdFrXZT3+0dMjiEs8idniX1YmviEi02IhGcnA3j8jvdVn3Amzehww2O0rBNgJlk H+5DUH4ygWTm4o3TsO9suCUQMu4CxLGjWLwOOrx27RvqkQlsLSZnzpjEAgblrDQ0lg xqpR8B4SX6eOQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Rik van Riel , Tejun Heo , Josef Bacik , Jens Axboe , Sasha Levin , cgroups@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH AUTOSEL 6.8 16/43] blk-iocost: avoid out of bounds shift Date: Mon, 22 Apr 2024 19:14:02 -0400 Message-ID: <20240422231521.1592991-16-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240422231521.1592991-1-sashal@kernel.org> References: <20240422231521.1592991-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.8.7 Content-Transfer-Encoding: 8bit From: Rik van Riel [ Upstream commit beaa51b36012fad5a4d3c18b88a617aea7a9b96d ] UBSAN catches undefined behavior in blk-iocost, where sometimes iocg->delay is shifted right by a number that is too large, resulting in undefined behavior on some architectures. [ 186.556576] ------------[ cut here ]------------ UBSAN: shift-out-of-bounds in block/blk-iocost.c:1366:23 shift exponent 64 is too large for 64-bit type 'u64' (aka 'unsigned long long') CPU: 16 PID: 0 Comm: swapper/16 Tainted: G S E N 6.9.0-0_fbk700_debug_rc2_kbuilder_0_gc85af715cac0 #1 Hardware name: Quanta Twin Lakes MP/Twin Lakes Passive MP, BIOS F09_3A23 12/08/2020 Call Trace: dump_stack_lvl+0x8f/0xe0 __ubsan_handle_shift_out_of_bounds+0x22c/0x280 iocg_kick_delay+0x30b/0x310 ioc_timer_fn+0x2fb/0x1f80 __run_timer_base+0x1b6/0x250 .. Avoid that undefined behavior by simply taking the "delay = 0" branch if the shift is too large. I am not sure what the symptoms of an undefined value delay will be, but I suspect it could be more than a little annoying to debug. Signed-off-by: Rik van Riel Cc: Tejun Heo Cc: Josef Bacik Cc: Jens Axboe Acked-by: Tejun Heo Link: https://lore.kernel.org/r/20240404123253.0f58010f@imladris.surriel.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- block/blk-iocost.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/block/blk-iocost.c b/block/blk-iocost.c index 04d44f0bcbc85..b1c4c874d4201 100644 --- a/block/blk-iocost.c +++ b/block/blk-iocost.c @@ -1347,7 +1347,7 @@ static bool iocg_kick_delay(struct ioc_gq *iocg, struct ioc_now *now) { struct ioc *ioc = iocg->ioc; struct blkcg_gq *blkg = iocg_to_blkg(iocg); - u64 tdelta, delay, new_delay; + u64 tdelta, delay, new_delay, shift; s64 vover, vover_pct; u32 hwa; @@ -1362,8 +1362,9 @@ static bool iocg_kick_delay(struct ioc_gq *iocg, struct ioc_now *now) /* calculate the current delay in effect - 1/2 every second */ tdelta = now->now - iocg->delay_at; - if (iocg->delay) - delay = iocg->delay >> div64_u64(tdelta, USEC_PER_SEC); + shift = div64_u64(tdelta, USEC_PER_SEC); + if (iocg->delay && shift < BITS_PER_LONG) + delay = iocg->delay >> shift; else delay = 0; -- 2.43.0