Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp773615ybz; Wed, 22 Apr 2020 07:46:17 -0700 (PDT) X-Google-Smtp-Source: APiQypJm7vktb5tcj5cQNKBx6BnbSK/agr7yoBYAcrwGMXC2/I35FkIQycpY8uKvKge+cC6JNi1Y X-Received: by 2002:a17:906:f1c4:: with SMTP id gx4mr25683265ejb.171.1587566777561; Wed, 22 Apr 2020 07:46:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587566777; cv=none; d=google.com; s=arc-20160816; b=z9/GLPlbH4QHf+vjEsb/VhB/Hp2zgs+GgSpkWIGFZWTFhAir9ex07Cz6ScERhX+qyF d4jYw7mPOPFV/4Rw5224LoS1Goxk1LikiWwfTWVcERvRLbgaKFuiFZ6sp2CJZMbNFBXe Ls15rlSkaa9QphrN17lw6qY7SK+C5iD54g8ODX5vqXh0S1eQtNhHoFot/12y+bCTBNBw ky9JgtkTMfBRo5DiaUSIBaOoAGNbvXN8b5VF62ADbRJdaWwLEGlP8H4AH7G8tfWVUEB6 7Of8pXV942RQD121wSVnsby7NjQN2wH1JSqRb1VDQVlXFkkPSXXOUGgkLCbeoMPUD9JE lfpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=pZQnWaEtEpKf2Wj6zHV/GGH/0K8tGS1tb8fzyJOPZpI=; b=LtxJHjk8meLaWrAXtrLgchZBhVFXZ/Vtd4e+UlzUrd408LuV9pzHp9z3UoeN3puZFj N662mXf6LT4ndalTHUXZJRthCaeOQ3aWrerK+vX0YlZaWSziEE6Lu3dv/LCXC4dWZ0ji cptsUO/I5n72xQTk/4zTt4ZNVRb1iZuI3WrZJymi8ywuhcM46195u057RiFCxklGop90 FH/4O+M1UGm/fbnlpB7c2tQOdqH+1dsvlHDWU6QDRJO7U2fDgvp0RI2ObsOU5WhOZYHO TNkkeBbgdWG1dIjcYOOrxXGbwa4coR2un26Zy8NCVrPnhfWoxLRPWviWGPQT67QSsy85 ClGQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ld23si3513125ejb.242.2020.04.22.07.45.40; Wed, 22 Apr 2020 07:46:17 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726765AbgDVOlQ (ORCPT + 99 others); Wed, 22 Apr 2020 10:41:16 -0400 Received: from mx2.suse.de ([195.135.220.15]:47528 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725934AbgDVOlP (ORCPT ); Wed, 22 Apr 2020 10:41:15 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 123C3ACF2; Wed, 22 Apr 2020 14:41:13 +0000 (UTC) From: Giovanni Gherdovich To: Ingo Molnar , Peter Zijlstra , "Rafael J . Wysocki" Cc: x86@kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Linus Torvalds , Srinivas Pandruvada , Thomas Gleixner , Borislav Petkov , Len Brown , Giovanni Gherdovich Subject: [PATCH] x86, sched: Prevent divisions by zero in frequency invariant accounting Date: Wed, 22 Apr 2020 16:40:55 +0200 Message-Id: <20200422144055.18171-1-ggherdovich@suse.cz> X-Mailer: git-send-email 2.16.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The product mcnt * arch_max_freq_ratio could be zero if it overflows u64. For context, a large value for arch_max_freq_ratio would be 5000, corresponding to a turbo_freq/base_freq ratio of 5 (normally it's more like 1500-2000). A large increment frequency for the MPERF counter would be 5GHz (the base clock of all CPUs on the market today is less than that). With these figures, a CPU would need to go without a scheduler tick for around 8 days for the u64 overflow to happen. It is unlikely, but the check is warranted. Signed-off-by: Giovanni Gherdovich Fixes: 1567c3e3467c ("x86, sched: Add support for frequency invariance") --- arch/x86/kernel/smpboot.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c index 8c89e4d9ad28..fb71395cbcad 100644 --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -2055,14 +2055,14 @@ void arch_scale_freq_tick(void) acnt = aperf - this_cpu_read(arch_prev_aperf); mcnt = mperf - this_cpu_read(arch_prev_mperf); - if (!mcnt) - return; this_cpu_write(arch_prev_aperf, aperf); this_cpu_write(arch_prev_mperf, mperf); acnt <<= 2*SCHED_CAPACITY_SHIFT; mcnt *= arch_max_freq_ratio; + if (!mcnt) + return; freq_scale = div64_u64(acnt, mcnt); -- 2.16.4