Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4724868pxj; Wed, 12 May 2021 11:46:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzl5fLrH5OmDOxKMUVdxgrqnIy1Kg0kVhz9xqLbr5UQnPaWvCV96YPaezuHAhrWU8614LkG X-Received: by 2002:a05:6402:1bc9:: with SMTP id ch9mr45908841edb.136.1620845215776; Wed, 12 May 2021 11:46:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620845215; cv=none; d=google.com; s=arc-20160816; b=z6ZhZQr2JRXlHqcpK018kg+ThiCTwBMpQ5JMFv2WomFPpvayApCvtbbFUYpUngWHOS VI2xyghTtVOwKP4O+ldcGSovjR0H3qOY9DZdMyJ/wGvwhczObtH1YBWo9o8SYOk3hsGw vNBX46VDXFsSNlc5rlTxVfd+riZXb5YYJgzpDEbpgr1jDX15oTXPTmgXATwpTYiHQseE g/ypzTWO2wDR/0tT6iszr02V6mnBCx6POGn2VMK9QEbW8RjTXe0YU50TbT4uP9GBUxCx iKz+8cb092s2P4LzV3u/44FiRVeMCcKRHgwoWfjfWiJMOtmESjJ3cDCiWh3Kuut7MwHb vc1w== 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=WHOv2GNcdY2SVZVIrEd8Owiq1ewb0NOZMrWwEh957z0=; b=vlvslvfLnCIV751Vr8MKHIpceRrrVNhiIfwqgeFjflZa7vfp1oAgyKTWI2CqitLBCo 7p12gZlPB0Dq/s+5lqJ789zW7ImxRXuKIQ+SNNCCsdDYp1vRArzia5j0FggVqw22VXOb I/JqAf8rH/VQ8Ez/Q4fkvCoD2k7TH2H89n4Gi91UcEOCL5zqRl6UR7iEhXXlnGARoIPI WTOIwIdYLs18ZrysV7tIGFyLtwBeJ2T8FhNKar9+1RpBoUjMLEScWpLaC/CAHlRqsb21 n6beDr24QuGD3eSYd6YslEbIE0jbL1/vcxNH9EVbwquWpfy8aDGX6pg6HARTSeHR05QP 7+Jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=k4IHfWPZ; 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 t15si478451ejd.261.2021.05.12.11.46.31; Wed, 12 May 2021 11:46:55 -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=k4IHfWPZ; 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 S1357970AbhELSmJ (ORCPT + 99 others); Wed, 12 May 2021 14:42:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:33484 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243412AbhELQlI (ORCPT ); Wed, 12 May 2021 12:41:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3866161E3E; Wed, 12 May 2021 16:03:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620835439; bh=zdsE1xqksmcFwm9YsT/ppmvGjSTOljL+ovyFZGRG+3E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k4IHfWPZBPMQHc/o3g6vJrc2ZtKeEte4J54Na6r6IhFS3vpQs7GT63ZXTrBUUgh65 xEO3qyXX+11KEt/x8A5U1CX4hvNzDWnQhEsZpzggQXreCshN5SYrqePBOVqQwgSA8b ycg4aR9VlRwaqtLgHPQ0FB6GGdp3iqykPcJoCMc4= 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.12 346/677] sched/fair: Fix shift-out-of-bounds in load_balance() Date: Wed, 12 May 2021 16:46:32 +0200 Message-Id: <20210512144848.798355328@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144837.204217980@linuxfoundation.org> References: <20210512144837.204217980@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 0eeeeeb66f33..b5ffe808b07b 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -7780,8 +7780,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 10a1522b1e30..e4e4f47cee6a 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