Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp130549pxy; Fri, 30 Apr 2021 01:52:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwN30QJtUeX+xTyXiaI70ik9rKIELpR8VBzgVF0Rei8CXHqoj8HfSOzGhqSpWj3kOz8IxKw X-Received: by 2002:a62:1c88:0:b029:255:fbb5:f79d with SMTP id c130-20020a621c880000b0290255fbb5f79dmr3911701pfc.51.1619772733943; Fri, 30 Apr 2021 01:52:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619772733; cv=none; d=google.com; s=arc-20160816; b=PZ0UUWMB5Qkufzy+CTyXzq3UvalYGSr9T4qQr7ZPqmtsbezyLY0tYPF/rmj3ewRIMN Du2/+93JDZ/Zlw13C34yjm4KBoxgC7z1GXSlmZDQtEQhYpFMhn8/Es0jMkxt5BgY1t9E F239frzazu4mKR+Vw4NGhRZyuWa9BZolbr3NwqIEc9ML9F5tEl784sSIbTEOImLMezeg HXTTLm1pVLBzoY27JgNXZwv1rfn45YS4g+VwV4iZ48ph8XvRTmSSACf8/jyFSTJ0H4wt McVJ4/17VkXpWPfC+pt1dzbZ3sRo7P0jCJWZxE4zD6RJ0zoKfjdLalE9gVmtkTfeF95G WB8w== 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=q0yIHgo1JUyr9NoiprUJP0BOnhoVbOctvNZNK5RJx5E=; b=OQg6rjvbeIQpPXCS5n9PsJCJZXT0O+peqhjk/S0XAFzBsJBiCKolhxOuovH9filhBV 9SA9ta9hTJeQOwYQHO+AK9igb/ojkZ5p+qIjmvLd/bJluB7UPO0CLxn91t3YeOB/uG7o 6o3/hOjstepMnKiOeV0TeCcSv1yyg4PE6soESfH7k8ijh3Ig+UXQDuxWcDlIIF/cWWnB q5snoKI5To+lRtUnqllIBJL/pdZEYGdgYX+omXPqtrHSTy3EH27209NTfxl4NoGHq5SC Ffs6HhJNagS278/z+hc9LiOvuT5zuKu6vt8bQyyj/Sk/OKkzFS8lw+yhap6z+BzuPAjc iM8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Z7RZxT2e; 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 p15si2989710pli.158.2021.04.30.01.52.00; Fri, 30 Apr 2021 01:52:13 -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=Z7RZxT2e; 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 S230484AbhD3Iuw (ORCPT + 99 others); Fri, 30 Apr 2021 04:50:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229507AbhD3Iuv (ORCPT ); Fri, 30 Apr 2021 04:50:51 -0400 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89CACC06174A for ; Fri, 30 Apr 2021 01:50:03 -0700 (PDT) Received: by mail-lf1-x12e.google.com with SMTP id b23so26434703lfv.8 for ; Fri, 30 Apr 2021 01:50:03 -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=q0yIHgo1JUyr9NoiprUJP0BOnhoVbOctvNZNK5RJx5E=; b=Z7RZxT2etLjVangalvPR8VTZoPL4Yz7c4tQX6kYWzzvroScMF6qw2aLTwWOF/LyIzA dqT/7Wvgr20rBQd4S9G6DeQCi+Am35QeSZZ9BRMzs3gwR7O6MEKZ+Z43DkpXvg8WdouC HS64KBCoWYe6hxBOMzNSKTPOdjcwMuSKVzNEgMDvJtzLmCvbEp80F8SNlxwZfaY1mgc0 xV5xge1LY3ygi4boxMy3sIRtpR3cCpjv8fCqSfSooiWy8CWzBumX6A1g2R1TemnLyoyK UrDZBMFicHO5CCnhLMxq1LmXv/pv2RBLCpxxTZPCYOnN/c6H0tkMZUbt+eZrx5FMJ41K EpIg== 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=q0yIHgo1JUyr9NoiprUJP0BOnhoVbOctvNZNK5RJx5E=; b=lOQA6yDQYbu1pKHFa/qHfOhzrklGo/YivkhsqDc19uPKYfUhi3zKfJtAvtfZPUD1fj 25ojQz4absLou7CDqQgYCtx7PDJQLGeZCaNW/XPABkxLP2OAgp2nGsNQGFStAT8EF4Nv OscH3B2Xw4ZPvwhkTcW2ObXKmQLHwNP3N0hI84opGddLWcqD9CVbQmk9Ei3y7Ys1282d jzPyJz2H6fxE5d+y76NvpzGA2mOpo7jVY+RfbmUmm9Xd4HDOc/t76oiKp6dWVxccxrnE k/ayzqgX6j1xtZRzK5mWUQC0i8v9r94ol8zANBlkGPbumlii5MnX6ALgdRBW1MieUzOd xU/A== X-Gm-Message-State: AOAM533xaEnDhbSqmdKAkZ3KKnNKoel9w6NGWEZJy0xZKxbetcxbJUoI qGlA9VHOgROvKvraZvMK5VuSYW7AweT8Cz3UG9yjIA== X-Received: by 2002:ac2:4f03:: with SMTP id k3mr2615472lfr.254.1619772601846; Fri, 30 Apr 2021 01:50:01 -0700 (PDT) MIME-Version: 1.0 References: <20210429152656.4118460-1-qperret@google.com> In-Reply-To: From: Vincent Guittot Date: Fri, 30 Apr 2021 10:49:50 +0200 Message-ID: Subject: Re: [PATCH v2] 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 10:25, Quentin Perret wrote: > > On Friday 30 Apr 2021 at 09:45:32 (+0200), Vincent Guittot wrote: > > On Thu, 29 Apr 2021 at 17:27, 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. > > > > > > Fix the math to compute the bucket size. > > > > > > Fixes: 69842cba9ace ("sched/uclamp: Add CPU's clamp buckets refcounting") > > > Suggested-by: Qais Yousef > > > Signed-off-by: Quentin Perret > > > > > > --- > > > > > > Changes in v2: > > > - replaced the DIV_ROUND_UP(a,b) with a/b+1 (Dietmar) > > > > Doesn't this create unfairness between buckets ? > > > > If we take your example above of 20 buckets, delta is now 52. Then we > > expect the last bucket to get the range [972-1024] but values lower > > than 988 will go in the idx 18. > > Well, that's just the limitation of integer arithmetics isn't it? > > > And the more bucket you will have, the > > worse it will be > > Sure, but 20 is a hard limit, and if we ever need more than that then > maybe we should think about getting rid of the buckets altogether. > > > Your problem comes from the fact that we use 1025 values instead of > > 1024. > > I don't understand what you mean here. Right now we'll assign bucket id > 20 for any clamp in the range [1020-1024], so I don't think we can > special case 1024. 20 buckets is probably not the best example because of the rounding of the division. With 16 buckets, each bucket should be exactly 64 steps large except the last one which will have 65 steps because of the value 1024. With your change, buckets will be 65 large and the last one will be only 49 large > > > Wouldn't it be easier to have a special condition for > > SCHED_CAPACITY_SCALE value > > As per the above, I don't see how that'll work. > > Thanks, > Quentin