Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4677459pxj; Wed, 12 May 2021 10:40:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDucJIG/icFfRLUtIbg0NmBjDvpm+SxEFxXN5Axch2BNdS+bAiXliDXIF0ASEVBxn/MlOm X-Received: by 2002:a17:906:2854:: with SMTP id s20mr11946516ejc.335.1620841218880; Wed, 12 May 2021 10:40:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620841218; cv=none; d=google.com; s=arc-20160816; b=jZJpkzKc7EEebGZfSWa7wnUoePvik8j9fDMaqhq4MebUPC7F8hjocrMf6QDxAs/y/b yz1CA6ArZ8qYghtXCt4gAuKKPSv4HWVB6Z5B7lNA/lCVrgFDVZdEelORb3Epu6E2Pgzi kYfFafdUzIXuK0btYXSR2bYIK0mKaXfXzZszxgGL7UIPVzOteuipt02U+x7gXAZMBlVn v2oRRUvr6vAsCscnfG3KjZxJYRdKbV5RtJxg/Nq+ro5S5YSLFg42//zGSlUZrg/MCyOP DqCIzvtyLluZtIA7p4muSMxefrB97eWARMemCarRsDIy32TmCUpnaXPDXvJjyRdCuKep u8/g== 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=JXBVRwDcLN35xpp0dIYdoJMzd3DlpRcvj4QpAEhfy2I=; b=wS7zcGZ56Op8f1UiGlYDuymsXVJ5FVDlOI7Ez60Ck8KOtXE475hogid9LOWDkHaubd 7yd+Hr1fDJKxyDGuQVkSBiFkwmlgqeRLnvgoBp+DeU9uJsGOMF/iY1sKfbSKE97hVWLg NM8gnY7cPGRMVH/YmugVhBEn+WMNgA6adeFqYvLGasQjgk6PBZJE/NgREyRoN6ZZXlWD JJu7Rhlg+sdXrXzo+RepFU0Fr1AS2vc0kJHKTf+jB20CoPkinEHxjEekG7wt2TeijIDw G+VoYezWqY0pGGkzkFAiOV084vMei8IH/D7fVEjSS5KaoeKro8X0OwsMlejRVAWaUyeZ tieg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=hz0rjIRY; 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 s27si240231edw.377.2021.05.12.10.39.54; Wed, 12 May 2021 10:40:18 -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=hz0rjIRY; 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 S1347387AbhELR2V (ORCPT + 99 others); Wed, 12 May 2021 13:28:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:34570 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239329AbhELQHw (ORCPT ); Wed, 12 May 2021 12:07:52 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 779A461C54; Wed, 12 May 2021 15:37:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620833847; bh=uAyWzjP+VFUryf2ea6q//+V3JGUv2yUoEopTHFiUnf4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hz0rjIRYuJt+G+NQU8gnnFcUCnqnborsqMKrg3LbpJdsEu5xb1NJJXK5X8LCZqvej z0EQ//aQPW43SQtXZN14ww5OCXSui8m4IGYlAcNZOUkR9sRbUG15np7LkwCELL6Ghg lmx5u1wzCJnrkMXx7U1qs7kaMJybVuz0wSSeu7Ac= 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.11 312/601] sched/fair: Fix shift-out-of-bounds in load_balance() Date: Wed, 12 May 2021 16:46:29 +0200 Message-Id: <20210512144838.093476724@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144827.811958675@linuxfoundation.org> References: <20210512144827.811958675@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 828978320e44..d9182af98988 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -7760,8 +7760,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 282a6bbaacd7..d52c6bb6ed7d 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -204,6 +204,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