Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp438719pxy; Fri, 30 Apr 2021 08:30:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy1a6UaCsa+kZKJmlS3YRDhOKpejJVz84JuTxL1jlZmpYCW+U4qN+MIQE7THLk6Tokm1rAq X-Received: by 2002:a17:906:b0cb:: with SMTP id bk11mr5100658ejb.310.1619796656923; Fri, 30 Apr 2021 08:30:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619796656; cv=none; d=google.com; s=arc-20160816; b=mlx3GyBBsbn6NwGRN365MOjgzXQ3tRrXEIfeMVjUZWsamLQR2QdbDlQVoHsI2igELX VAkcbpY/Ib8TfRZtdAcmt7EQvUv+/T2eyp68tkFDugJZc2Z88Y9qbdsXigp1P2K9ciJx A9/kLKja97lmTd6URptLxohG6Jc7YNi5nxzetMAOzuV0QodEnhfUJQYtOgp282uNEweu NpxBKnJ7aff8/oCb9P0HN3EdKzsDPnJtQAiBQ36OiOx2ebAG6S+BtvJNNL9XENfmAkam YQB1ZCDuKALOmlSEugJ2IdnXCabN/JL20P5sagffPc5u5PL9HISLVmkBZlH+4GVT+H6n +DEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Mh9VQ4lqbG7PU0zxXU61/zkGzqt9dmCvOlEJ8ZziR28=; b=DQsPQzo6di7MP65vhwI/w5E6RvI0QKy1wMMr4rJUGo1AqIq7KJBdEOA3Tl+cFCwTHk Yw2CuLcoIayIH8qXOogcINPJWOvhQJv9GBhKiJyIWrtthj97iwOGnsmNybFiexRWTyNg 0o+SDt6vf4Jpkax5CkFursijZbcRXm59T2mi0uTw62wALDzdxGZu6+rK3IHDrzFGQ4/M Lxdgkq621pHu5ZNusQfzHkFX0v0FUa48zZ0E2HOpLB5zW29+olVjNveh/ru9fnN3v/Y0 RM3SOJ7IV1lUyuDwpjW/U5i6u7nLsmTlWOE9e1884qxDq0kZ5eGK0IvWEfno8jukaXFj ZWyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ea9RAHoe; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g14si3408743ejx.307.2021.04.30.08.30.33; Fri, 30 Apr 2021 08:30:56 -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=@linaro.org header.s=google header.b=ea9RAHoe; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229797AbhD3P23 (ORCPT + 99 others); Fri, 30 Apr 2021 11:28:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229712AbhD3P23 (ORCPT ); Fri, 30 Apr 2021 11:28:29 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF7F8C06174A for ; Fri, 30 Apr 2021 08:27:40 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id a36so70445078ljq.8 for ; Fri, 30 Apr 2021 08:27:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Mh9VQ4lqbG7PU0zxXU61/zkGzqt9dmCvOlEJ8ZziR28=; b=ea9RAHoe2Ux5JNof44BAAmQ+7CdlNsH0U5oQ6DbQ5dFbl/jcRANVrFEG/Ble+DepZA nTASlF5DqX4NkNDhpruSeSdq61WKLCxlcmCc7027/SKbtctl9I1uhm/J3QSkk6Kytw97 D1FyoITx0h4kdaGYISpk1AzXFEw2hB/zuc57U4phXp0zD3S8ZXz4rpaDCc3S7y4bNfak 8ltgHlNCY2giMpyfV6Q5zBXHujWTEvxOjHqC28Wgf6fozeWcCmof6WOjjgiuGnkjwofR tnpj1TcRdiYLtAVMUZPSncupK432OP0wGyY+QTiaMElNgt3DGhseznl3ndNiRJj6SKI/ PfLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Mh9VQ4lqbG7PU0zxXU61/zkGzqt9dmCvOlEJ8ZziR28=; b=eOgwt5kIIz0RXCqN6j1fTkm5mtWVuU6IqNym0hhpl5VOmPKEK8/qGhHyQil84PB9Qo 0MwX7OcYEj+bzFFvjivOHmmxpt+u6hVZW6sJAlB+9xOKAyEDtguWCCyHW9EeO4QgVdfi Xc4woA46oXRKVCzeUFQJIT7LMRM2SYYnHxto/b5PdMc3DP6ziz4Y0CFmggMgEFO4/rAK 0E9YUwEnBFVGXdJZlyq0ZYtCiE2XFZoYN1O9RWp9+BdtEZrCevMOQDC/Qwre/aoneppV mdBpInzCiVJrevW7c3rFdrx/xy+Zh0oQ01yRONGPIQoXOTgGIc6382yOh0RfV4YKLXKX Hg1Q== X-Gm-Message-State: AOAM531no3MShL+PLRNqXhuFmGbQpr4Td07viF0vPByH76B4O/V6Yzqz R7P6vUDTKdDZdEEmcsj/wergAf5LXN/r4W4OSVhX0Q== X-Received: by 2002:a2e:8794:: with SMTP id n20mr4031314lji.401.1619796459418; Fri, 30 Apr 2021 08:27:39 -0700 (PDT) MIME-Version: 1.0 References: <20210430151412.160913-1-qperret@google.com> In-Reply-To: <20210430151412.160913-1-qperret@google.com> From: Vincent Guittot Date: Fri, 30 Apr 2021 17:27:27 +0200 Message-ID: Subject: Re: [PATCH v3] sched: Fix out-of-bound access in uclamp To: Quentin Perret Cc: Ingo Molnar , Peter Zijlstra , Juri Lelli , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Qais Yousef , Android Kernel Team , linux-kernel , Patrick Bellasi Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 30 Apr 2021 at 17:14, Quentin Perret wrote: > > Util-clamp places tasks in different buckets based on their clamp values > for performance reasons. However, the size of buckets is currently > computed using a rounding division, which can lead to an off-by-one > error in some configurations. > > For instance, with 20 buckets, the bucket size will be 1024/20=51. A > task with a clamp of 1024 will be mapped to bucket id 1024/51=20. Sadly, > correct indexes are in range [0,19], hence leading to an out of bound > memory access. > > Clamp the bucket id to fix the issue. > > Fixes: 69842cba9ace ("sched/uclamp: Add CPU's clamp buckets refcounting") > Suggested-by: Qais Yousef > Signed-off-by: Quentin Perret Reviewed-by: Vincent Guittot > --- > Changes in v3: > - Keep rounding div to improve fairness (Vincent) > --- > kernel/sched/core.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 98191218d891..c12ec648423e 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -928,7 +928,7 @@ DEFINE_STATIC_KEY_FALSE(sched_uclamp_used); > > static inline unsigned int uclamp_bucket_id(unsigned int clamp_value) > { > - return clamp_value / UCLAMP_BUCKET_DELTA; > + return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1); > } > > static inline unsigned int uclamp_none(enum uclamp_id clamp_id) > -- > 2.31.1.527.g47e6f16901-goog >