From patchwork Fri Apr 3 08:55:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 11472169 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0044792C for ; Fri, 3 Apr 2020 08:56:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B6347214D8 for ; Fri, 3 Apr 2020 08:56:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GjOpPRkX" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B6347214D8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id EC1018E0008; Fri, 3 Apr 2020 04:56:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E49C38E0007; Fri, 3 Apr 2020 04:56:44 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D115E8E0008; Fri, 3 Apr 2020 04:56:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0105.hostedemail.com [216.40.44.105]) by kanga.kvack.org (Postfix) with ESMTP id B39748E0007 for ; Fri, 3 Apr 2020 04:56:44 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 66465180AD817 for ; Fri, 3 Apr 2020 08:56:44 +0000 (UTC) X-FDA: 76665938328.22.art75_1db278f2e094d X-Spam-Summary: 2,0,0,bc0b6a23096a8bb1,d41d8cd98f00b204,laoar.shao@gmail.com,,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1345:1437:1535:1543:1711:1730:1747:1777:1792:2393:2553:2559:2562:2693:2892:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3872:3874:4117:4321:4605:5007:6261:6653:7514:7875:7904:9413:10004:11026:11473:11658:11914:12043:12220:12296:12297:12438:12517:12519:12555:12679:12895:12986:13161:13229:14093:14181:14394:14687:14721:21080:21444:21451:21627:21666:21972:21990:30034:30045:30054:30055:30075:30090,0,RBL:209.85.210.196:@gmail.com:.lbl8.mailshell.net-62.50.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: art75_1db278f2e094d X-Filterd-Recvd-Size: 6014 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf07.hostedemail.com (Postfix) with ESMTP for ; Fri, 3 Apr 2020 08:56:43 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id a24so3175790pfc.8 for ; Fri, 03 Apr 2020 01:56:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=8lWvt493O2I6tDg+eQVWHrCT6h+J28ya8injBt/6hk8=; b=GjOpPRkX00Yt1nDjdASEGeDpVeibhF8xzYkinQqP86YaDYdCfTgUJub9JOAHSj93fi KAR7hGLkUJ1u4PcRnaTXa/U6SO1HLeCPeBcNK/HqKenbuULvK+P3vzw/Sng02CGM84ZX +ouR4UoVtSnUltimRnPno7g/ara4MxOCd7Hxecwp7Uey3LSY0lwmwieDhLjLQK4kBs1g 2oTh5Vue4j5BPmsL9MdujWp6LnFTkmvOIr/BKm5wGC/2b1yUM4Ijo92MegiGwG9LlOkr 9uN/1TXfrl6JcwrHRVfAz9eBWuqN2TojO6i9AzchxyOidoL9F9X+mofXkGXHO7c8eFdu wvBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=8lWvt493O2I6tDg+eQVWHrCT6h+J28ya8injBt/6hk8=; b=VJvKYgrkV+FBg9bDRZXWv0ZDpw7tUvXAWRokmPIRVtFzGzl6xEibNaFyepuSedXMl3 QEFBZLAGvSRJZEtSKHyoyJVX/61h2lj/VAnGkt2mZEeciKm9rcxoT6iKplr7q/KKBV3f 1ceLRrdqC3mVrhwcK6Iem+3WimEV0Sdh0Tsv+Qv4McN451FLX3TIPUl4cUeZ4/yMdBY1 cFRZg5HnzvY8JvbRLz/hv80L0LtzGYPQyAbc7FSJXEHrZ6sld+wrWCKfFZ9wBLAklS80 wAp8xfGPu8KoiyTfYoV+plM16uKSViG2sT48XNhdDinz3jflf6FgTQBWKIQNuaOuWeE7 wSBA== X-Gm-Message-State: AGi0PuZLdoIFSCJAF3tkrzYzTXVY/EresbGmgNSmDACirtUJnSMaBYGF hN1zLhNeIqdeuCcc7XcRafM= X-Google-Smtp-Source: APiQypKLPMgaL/rodf2glmeCJL5XtyQPNLF9R2cKmGDiddkoJxwH+igmg6UGJzx/w3feWxnfnLJDng== X-Received: by 2002:a63:cf:: with SMTP id 198mr7452255pga.447.1585904202794; Fri, 03 Apr 2020 01:56:42 -0700 (PDT) Received: from dev.localdomain ([203.100.54.194]) by smtp.gmail.com with ESMTPSA id e8sm5180980pjt.26.2020.04.03.01.56.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Apr 2020 01:56:41 -0700 (PDT) From: Yafang Shao To: hannes@cmpxchg.org, peterz@infradead.org, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yafang Shao , Daniel Drake , Suren Baghdasaryan Subject: [PATCH] psi: fix randomized calculation in record_times() Date: Fri, 3 Apr 2020 04:55:25 -0400 Message-Id: <1585904125-2819-1-git-send-email-laoar.shao@gmail.com> X-Mailer: git-send-email 1.8.3.1 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In record_times() we use 'now' and groupc->state_start to calculate the delta as bellow, delta = now - groupc->state_start; But note that groupc->state_start may be not initialized yet, IOW, the state_start may be 0 currently. If state_start is 0, this calculation is same with assigning the lower 32-bit of 'now' to delta, that is a random value. To fix this value, we should initialize groupc->state_start before. After we calculate the delta, we will assign 'now' to groupc->state_start then, groupc->state_start = now; This will cause the same issue if groupc->state_start will not be used in a long period. Let's take an example. We create a cgroup foo and run tasks in it. Some of these tasks enter into memstall and state_start is set. Then we move all of these tasks out of cgroup foo for more than (1 << 32) nsecs, and then move them in. That will cause the same issue as above. The root cause of these issues is that we don't initialize the state_start properly. To fix it, we should record how many tasks in this per cpu psi_group. If there's no task in it, we just set state_start and don't calculate the delta, that means it is the begin of the pressure. To avoid redundant calculating the total number of tasks in this per cpu psi_group, a new member 'total_tasks' is introduced in struct psi_group_cpu, which is the sum of array members in tasks[]. Fixes: eb414681d5a0 ("psi: pressure stall information for CPU, memory, and IO") Cc: Johannes Weiner Cc: Peter Zijlstra (Intel) Cc: Daniel Drake Cc: Suren Baghdasaryan Signed-off-by: Yafang Shao --- include/linux/psi_types.h | 2 ++ kernel/sched/psi.c | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/linux/psi_types.h b/include/linux/psi_types.h index 4b7258495a04..b42cbfdb15e9 100644 --- a/include/linux/psi_types.h +++ b/include/linux/psi_types.h @@ -69,6 +69,8 @@ struct psi_group_cpu { /* States of the tasks belonging to this group */ unsigned int tasks[NR_PSI_TASK_COUNTS]; + /* Sum of above array members */ + unsigned int total_tasks; /* Aggregate pressure state derived from the tasks */ u32 state_mask; diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c index 8f45cdb6463b..7061529dc406 100644 --- a/kernel/sched/psi.c +++ b/kernel/sched/psi.c @@ -690,7 +690,10 @@ static void psi_group_change(struct psi_group *group, int cpu, */ write_seqcount_begin(&groupc->seq); - record_times(groupc, cpu, false); + if (groupc->total_tasks) + record_times(groupc, cpu, false); + else + groupc->state_start = cpu_clock(cpu); for (t = 0, m = clear; m; m &= ~(1 << t), t++) { if (!(m & (1 << t))) @@ -703,11 +706,15 @@ static void psi_group_change(struct psi_group *group, int cpu, psi_bug = 1; } groupc->tasks[t]--; + groupc->total_tasks--; } - for (t = 0; set; set &= ~(1 << t), t++) - if (set & (1 << t)) + for (t = 0; set; set &= ~(1 << t), t++) { + if (set & (1 << t)) { groupc->tasks[t]++; + groupc->total_tasks++; + } + } /* Calculate state mask representing active states */ for (s = 0; s < NR_PSI_STATES; s++) {