Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4476988ybx; Mon, 4 Nov 2019 14:04:57 -0800 (PST) X-Google-Smtp-Source: APXvYqz6o0fc/WfXambzYmWWjuAtve6qziDdW/VK2oC3xIt2HiM0znfrOZ+yBaubX7F7Dc4EKBca X-Received: by 2002:aa7:de92:: with SMTP id j18mr12360492edv.119.1572905097472; Mon, 04 Nov 2019 14:04:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572905097; cv=none; d=google.com; s=arc-20160816; b=eIVNkA3mijm7p+f6gUXuAhH3BKCqSHT9JLyKH7bmyX0E7CirykJlXC8XSNl9GJVCpw LoGbkVL/ozXwParkgrZE1r0xyD78nEjiUz5AFnSSDRVDjOPSQI9yxl8Zg69qbzuKt08J dYzKD9cQ1BKy8CS4qGioZCyHlkmuf6qFN/6YrMh6PLqF/Fpwf+4XDjuVBQguN1itXuwK 1iO6jgmmKu9JV9B52UTeduNFzJZ/cDlqYvUKdAmGad47aEj71XWXe/vKhfNEqRGGpX59 w6KBJkm1mIGzFoGOYYqyQqDQUBs89JvHzVdlXNj136EBTcj8nfPX4vKAWoLKhUO/f1Dy WQHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=W+TcrnEE+busFRfe1p+20sPlITAL7E6DcLpkpebpUOM=; b=PSM0ZDcmGhOAJQChlRchXQ/X+5vE+g1k3aIQmHr2u0jM5cFtLgNJiRPDFizRgdERsV d9L/4zznYTWst2P2wRT7sHPG36b8vhXUQTFYqYOHC9E03+gh/mpraRlLUKWCTTnZbqaQ sHEKlA+2mSg664eEIFwSXkcANm9e7jHetJVmJxK5at7ZImZAn9iEMWwb4n8qJuV/Pf1R fuxJtxuPUhD1vXiSpE+62ev4pkzv1/9AGS+FHbokBmMxjr/i5NjAymZ440VYpztC7iDR DODOu/TFXyofwR4x9be6+RNlZ4itavIA+oJ9w9maQApCWsnYxI7Ksrf/zyhcyZ/ufOjV DIdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=CON++YFb; 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 dv23si3648693ejb.338.2019.11.04.14.04.34; Mon, 04 Nov 2019 14:04:57 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=CON++YFb; 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 S2388915AbfKDWAr (ORCPT + 99 others); Mon, 4 Nov 2019 17:00:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:58512 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388906AbfKDWAo (ORCPT ); Mon, 4 Nov 2019 17:00:44 -0500 Received: from localhost (6.204-14-84.ripe.coltfrance.com [84.14.204.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5853E20650; Mon, 4 Nov 2019 22:00:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572904842; bh=vftKeGfZD1oTHtGExan21UU6YoxR31kKt1v/Sey+J10=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CON++YFbVscXG9JX77TQnJp9vtODTOC3XJU5sI3qftRdWMfUeTg7wdoSZDvmD0wjJ C1xkg0C/Uj6VacJj7qRbvMwqNf7V341/J1QAdT9GQbKXJ0Qhf5yi3MP2Vy1pcAIfJ9 gh3D+nwg7TtNG8/W8/hmVfne+KJ5vbP/viRVUwBk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Frederic Weisbecker , "Peter Zijlstra (Intel)" , Linus Torvalds , Rik van Riel , Thomas Gleixner , Wanpeng Li , Ingo Molnar , Sasha Levin Subject: [PATCH 4.19 091/149] sched/vtime: Fix guest/system mis-accounting on task switch Date: Mon, 4 Nov 2019 22:44:44 +0100 Message-Id: <20191104212142.854834719@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212126.090054740@linuxfoundation.org> References: <20191104212126.090054740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Frederic Weisbecker [ Upstream commit 68e7a4d66b0ce04bf18ff2ffded5596ab3618585 ] vtime_account_system() assumes that the target task to account cputime to is always the current task. This is most often true indeed except on task switch where we call: vtime_common_task_switch(prev) vtime_account_system(prev) Here prev is the scheduling-out task where we account the cputime to. It doesn't match current that is already the scheduling-in task at this stage of the context switch. So we end up checking the wrong task flags to determine if we are accounting guest or system time to the previous task. As a result the wrong task is used to check if the target is running in guest mode. We may then spuriously account or leak either system or guest time on task switch. Fix this assumption and also turn vtime_guest_enter/exit() to use the task passed in parameter as well to avoid future similar issues. Signed-off-by: Frederic Weisbecker Signed-off-by: Peter Zijlstra (Intel) Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Rik van Riel Cc: Thomas Gleixner Cc: Wanpeng Li Fixes: 2a42eb9594a1 ("sched/cputime: Accumulate vtime on top of nsec clocksource") Link: https://lkml.kernel.org/r/20190925214242.21873-1-frederic@kernel.org Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- kernel/sched/cputime.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index 0796f938c4f0d..54eb9457b21d3 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -739,7 +739,7 @@ void vtime_account_system(struct task_struct *tsk) write_seqcount_begin(&vtime->seqcount); /* We might have scheduled out from guest path */ - if (current->flags & PF_VCPU) + if (tsk->flags & PF_VCPU) vtime_account_guest(tsk, vtime); else __vtime_account_system(tsk, vtime); @@ -782,7 +782,7 @@ void vtime_guest_enter(struct task_struct *tsk) */ write_seqcount_begin(&vtime->seqcount); __vtime_account_system(tsk, vtime); - current->flags |= PF_VCPU; + tsk->flags |= PF_VCPU; write_seqcount_end(&vtime->seqcount); } EXPORT_SYMBOL_GPL(vtime_guest_enter); @@ -793,7 +793,7 @@ void vtime_guest_exit(struct task_struct *tsk) write_seqcount_begin(&vtime->seqcount); vtime_account_guest(tsk, vtime); - current->flags &= ~PF_VCPU; + tsk->flags &= ~PF_VCPU; write_seqcount_end(&vtime->seqcount); } EXPORT_SYMBOL_GPL(vtime_guest_exit); -- 2.20.1