Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4607479pxj; Wed, 12 May 2021 09:08:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwPIfcR/h593Lk/3z5nvjeCg1Z+GzC1LaKf+6pFVf7mNItNIrNdxv5hk0Ax78REnHmvKrLu X-Received: by 2002:adf:f891:: with SMTP id u17mr47059223wrp.352.1620835719512; Wed, 12 May 2021 09:08:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620835719; cv=none; d=google.com; s=arc-20160816; b=SyoNPMKdK8vA9e8iifzj52/3XdXqJaiF1lHIwiXZy3QO9aCAs+OdoZS1wFbGu7PByO 0GTiYOBFZEwNH0xnOABlIlJfXiFKMNjfyx0SSuaL1wnX4e0IhdqXgpc7SXAhYTPUvqCG WwSdmIrzcEKsHxAD0qqHHgXp16M4pgd7tdgh2wYcJwTLzA9uA8mbvwH1zLcYJxENePcE wrAFgLyeKXPOp9QGUyvIswVjmiO822yatmQ+T56BCL/0hwLqLF9ibqPfd0CT2J9ni/Ao c/IY9u+olC4fCp/dTC5BDw1M9nRhP2z7gzr+p6JPV7FZNv+w2c7WJowh7JIeELdMrGLn Ynsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=CFQSQGLauQuFPmBs80NCtI08UBokUyWjxFz99OJq7/s=; b=Cv79vhLLfiglqadBvdfxJD2crwK3bHM1S8pXtnJJkEJnmUhS19ElOME5jVCvrIIZNb FQ3AJcMYKZNuxbbklGE8XOJ51xIpyjE8vQ3c6xPUhG1BpHrvtadegVqt99AnMyip91JA ptoVwHkc3NJ1WiEIv3BaHhA7SS7aAJ/7omecCZ8b77++MvGtE7ZG6OrFUxvydjkU3FJA gXgJxFlM3rTPNLH/BUskXrF7+KFFJ3gxMhE8iqhUQpxEFQSdgqBHMGfAXY9b6+qIIfKq cQCl9Yt4O9+L40aEv1OCg12WPDJ4aLtq9wEe/ruTuUhsmtucuapVBcIVx31u1Pksvilm RnRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Qml71Da9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b22si374767eju.274.2021.05.12.09.08.16; Wed, 12 May 2021 09:08:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Qml71Da9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238969AbhELQG4 (ORCPT + 99 others); Wed, 12 May 2021 12:06:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:40900 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235583AbhELP2e (ORCPT ); Wed, 12 May 2021 11:28:34 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id BAFB361C28; Wed, 12 May 2021 15:14:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620832442; bh=Ccpil1lsjVYtsb27r5DhOZBARhMh6yaAtrRkONSysM0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qml71Da9espq8OE79WX0zslQUXjBMBT3E4uwcNSBFfrIqXr3NF4QCNbd2YwrAnQh0 fwJ/M0V91FF5hPD1jeATafy8pmb+yZhD1pPIuhUYOCHHGCSGdO1+yZhfpNI27mH+DE yXFyP0fwD5n/1aCK8iNGp1qnozKmE4xeSQOTxmAQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+d7581744d5fd27c9fbe1@syzkaller.appspotmail.com, Valentin Schneider , "Peter Zijlstra (Intel)" , Ingo Molnar , Sasha Levin Subject: [PATCH 5.10 281/530] sched/fair: Fix shift-out-of-bounds in load_balance() Date: Wed, 12 May 2021 16:46:31 +0200 Message-Id: <20210512144829.045871925@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144819.664462530@linuxfoundation.org> References: <20210512144819.664462530@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Valentin Schneider [ Upstream commit 39a2a6eb5c9b66ea7c8055026303b3aa681b49a5 ] Syzbot reported a handful of occurrences where an sd->nr_balance_failed can grow to much higher values than one would expect. A successful load_balance() resets it to 0; a failed one increments it. Once it gets to sd->cache_nice_tries + 3, this *should* trigger an active balance, which will either set it to sd->cache_nice_tries+1 or reset it to 0. However, in case the to-be-active-balanced task is not allowed to run on env->dst_cpu, then the increment is done without any further modification. This could then be repeated ad nauseam, and would explain the absurdly high values reported by syzbot (86, 149). VincentG noted there is value in letting sd->cache_nice_tries grow, so the shift itself should be fixed. That means preventing: """ If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined. """ Thus we need to cap the shift exponent to BITS_PER_TYPE(typeof(lefthand)) - 1. I had a look around for other similar cases via coccinelle: @expr@ position pos; expression E1; expression E2; @@ ( E1 >> E2@pos | E1 >> E2@pos ) @cst depends on expr@ position pos; expression expr.E1; constant cst; @@ ( E1 >> cst@pos | E1 << cst@pos ) @script:python depends on !cst@ pos << expr.pos; exp << expr.E2; @@ # Dirty hack to ignore constexpr if exp.upper() != exp: coccilib.report.print_report(pos[0], "Possible UB shift here") The only other match in kernel/sched is rq_clock_thermal() which employs sched_thermal_decay_shift, and that exponent is already capped to 10, so that one is fine. Fixes: 5a7f55590467 ("sched/fair: Relax constraint on task's load during load balance") Reported-by: syzbot+d7581744d5fd27c9fbe1@syzkaller.appspotmail.com Signed-off-by: Valentin Schneider Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Link: http://lore.kernel.org/r/000000000000ffac1205b9a2112f@google.com Signed-off-by: Sasha Levin --- kernel/sched/fair.c | 3 +-- kernel/sched/sched.h | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index a0239649c741..c80d1a039d19 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -7735,8 +7735,7 @@ static int detach_tasks(struct lb_env *env) * scheduler fails to find a good waiting task to * migrate. */ - - if ((load >> env->sd->nr_balance_failed) > env->imbalance) + if (shr_bound(load, env->sd->nr_balance_failed) > env->imbalance) goto next; env->imbalance -= load; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index fac1b121d113..fdebfcbdfca9 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -205,6 +205,13 @@ static inline void update_avg(u64 *avg, u64 sample) *avg += diff / 8; } +/* + * Shifting a value by an exponent greater *or equal* to the size of said value + * is UB; cap at size-1. + */ +#define shr_bound(val, shift) \ + (val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1)) + /* * !! For sched_setattr_nocheck() (kernel) only !! * -- 2.30.2