Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2273894yba; Mon, 15 Apr 2019 08:22:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqyO7nZtFdGZZS7OusOiVNXtigBr9MOLJRQNwCLAv4atF8gg70DULSyy6LdkmQNMlFIXhhCQ X-Received: by 2002:a65:554e:: with SMTP id t14mr60287860pgr.107.1555341733797; Mon, 15 Apr 2019 08:22:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555341733; cv=none; d=google.com; s=arc-20160816; b=J+Oyp3cn3LlrJMJiMEezE274XXbbqAoP+9H5wTaKn6r+/ZbQC1jiC8NiJKVuR+d0kQ SrgfoDebRspbzBLdrqpTcyox3usd1vB32pv2AVoZPO/DMXGIzOtokvXB4ilku4ekg5Ta Dru6oiZo+FKZcEItHo30vjNff9Cd2AgD8K2UJ98gUM6iQQqojvDBKH+B8Lx8vI/LwtIf 3+MzJo7+8h9ECJpZ9lKDja9w8fF5yH6FmQyXUHE4XAk3JWu5u9Z9lDvn8pcaoNKyKKyG TKvA90pU7VvW5nUK4RJvoUdwDzS5PzOh8BPCXwkEqJs8emsOUW05P8/UxhigSFWAiux0 UULg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=Gn4b8pJVUovNg0B+KcnWjG7OPjavHPDTGqZAcmtLvGo=; b=Awd5Qdo8ZqLTywyGTjb9LLklHwLB+W30yUVF5nCgAW1s3a5n8qwvj74+e5xqAQ9p1x JgyBc6zOzTqcihtrQXdPIxqOeh11Rzvvo0Z0r6OeKRrt1tDhLEk952EWJY0rQwUxvuIO jyJpXBQm16ZaybJUlQXsnyBD812aOZCMZmESAKzODgihcFAaLOMMJyLqmRI6aKgIcEsD CZSsTclQc9glczRrfhg20E23HpJxiSh+M1ho4jqc6R7lKhIBbLoZPLIa7NXEdhW4iLvV t3fC7BxqHHpnHU3yemQKR7uQnuUjCJRAG8qmNgqn4nhVxBm+b/pl6mNtWrPTP1tyWCW9 06zA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e24si41201620pgh.403.2019.04.15.08.21.57; Mon, 15 Apr 2019 08:22:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727682AbfDOPUv (ORCPT + 99 others); Mon, 15 Apr 2019 11:20:51 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:41858 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726034AbfDOPUv (ORCPT ); Mon, 15 Apr 2019 11:20:51 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 6EEEE427724505D695B4; Mon, 15 Apr 2019 23:20:46 +0800 (CST) Received: from [127.0.0.1] (10.184.38.59) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.408.0; Mon, 15 Apr 2019 23:20:38 +0800 Subject: Re: [PATCH] sched/fair: Use 'unsigned long' for group_shares,group_runnable To: Peter Zijlstra CC: , , , , , "chengjian (D)" , Yang Yingliang References: <20190413033234.30002-1-cj.chengjian@huawei.com> <20190415124617.GP11158@hirez.programming.kicks-ass.net> From: "chengjian (D)" Message-ID: Date: Mon, 15 Apr 2019 23:20:31 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.2 MIME-Version: 1.0 In-Reply-To: <20190415124617.GP11158@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Originating-IP: [10.184.38.59] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, Peter On 2019/4/15 20:46, Peter Zijlstra wrote: I write a demo about this, which I described it as overflow. #cat test.c //test.c #include #include #include #include int main(void) {     long a = 1048576 * 9144968455305; /* shares = tg_shares * load */     unsigned long b = a;     //unsigned long e = 1048576 * 9144968455305;     printf("LONG_MAX = %ld, 0x%lx\n", LONG_MAX, LONG_MAX);     printf("ULONG_MAX = %lu, 0x%lx\n", ULONG_MAX, ULONG_MAX);     if (b > LONG_MAX)         printf("==overflow!!!==\n");   // OVERFLOW     printf("a = %20ld,0x%016lx\n", a, a);     printf("b = %20lu,0x%016lx\n", b, b);     /* shares /= tg_weight */     printf("a/3 = 0x%016lx\n", (a / 3));                        // WRONG     printf("a/3 = 0x%016lx\n", (((unsigned long)a) / 3));   // unsigned     printf("a/3 = 0x%016lx\n", (a / (unsigned long)3));    // unsigned     printf("b/3 = 0x%016lx\n", b / 3);            // unsigned     return EXIT_SUCCESS; } #./test LONG_MAX = 9223372036854775807, 0x7fffffffffffffff ULONG_MAX = 18446744073709551615, 0xffffffffffffffff ==overflow!!!== a = -8857549630719655936,0x8513a98a48900000 b =  9589194442989895680,0x8513a98a48900000 a/3 = 0xd7068dd8c2daaaab     //  WRONG a/3 = 0x2c5be32e18300000 a/3 = 0x2c5be32e18300000 b/3 = 0x2c5be32e18300000 > On Sat, Apr 13, 2019 at 03:32:34AM +0000, Cheng Jian wrote: >> group_share and group_runnable are tracked as 'unsigned long', >> however some functions using them as 'long' which is ultimately >> assigned back to 'unsigned long' variables in reweight_entity. >> >> Since there is not scope on using a different and signed type, >> this change improves code consistency and avoids further type >> conversions. More important, to prevent undefined behavior >> caused by overflow. > There is no undefined behaviour due to overflow.UBSAN is broken, > upgrade to GCC8 or later. > > . So, function calc_group_shares will return the wrong value. ```cpp static long calc_group_shares(struct cfs_rq *cfs_rq) {     // ......     shares = (tg_shares * load);     if (tg_weight)         shares /= tg_weight; } ``` The same to calc_group_runnable. Thanks.     CHENG Jian.