Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4555701ybi; Tue, 30 Jul 2019 04:10:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqztzTbou8AyqeVER85OlG4SFN1z3tJPKCxw/VT1wKPwIluDj8U/CbJjIDZUSSJCNmeO6Sml X-Received: by 2002:a17:90a:8c0c:: with SMTP id a12mr116457879pjo.67.1564485004861; Tue, 30 Jul 2019 04:10:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564485004; cv=none; d=google.com; s=arc-20160816; b=o6/VBA0t8uZX1w/uCI+Doncep/OKSvrKMcEp0l4lMr9JVif0OlySk9b9HM6ZNSud/0 gTRu9Dv6S/Ax/lgWWTSwMv0NhuThSSBB2yiLWVSM3s/y6Yeih1LRa7Aw4fYWnpPrmNVc IsoVOi8/S8Ndg2rwKx10L5aCbp1J2bE/LsM+OVx+qcQyKqX63YdazonkXfyNVmEav4eR DKDJ9wS+4QcEqbMEN5pFPdQYfyWcaK1DnJW1LCWzna74JTLHmbI7y+fUVMWTnco/Lw+E DwSleiPk71wiaVw3uE8lcejQSHwlJoQ19PvPdoepM4k3yEru7Unu2m5NS1CSk59oyEU1 27wQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=5dtvEs2ayMMPDWlDVD/VDsH5GdZ7NguoxBUGDu1v+1A=; b=CLp3uUpcqROCLWjm7JcQXmZYZxkZ+Van/uPn6g1jNWl+GG62l4/8C9C+IeergFY+Pf wjmpx5WFvfzrJxeubR+geIZwtaKoLmmSNScaZSR3EBOeZhlo6W4cFlBn4j67S/oe84ge lxIFnAiBWDXJG1JtvR3ENWV3eIhQR2EioPX5LVuMvxNUvmYcIXRRa4X4rbnE3RF2kQNE wrH4pVRlnKIxxdzyVxiskxiJlONZcw7fMeTg8uxkSwmrIzusxMs7rllt9C+suQ+UuyCD 5xoSP7iVJMlrIbYO7DQVtZK3t1Pzjzrx8gcea0ZeEWiMYwZiWCV31U8kD2dJZic1Y/O7 8CcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=xG7r1Kkm; 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 h63si25782526pjb.106.2019.07.30.04.09.49; Tue, 30 Jul 2019 04:10:04 -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; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=xG7r1Kkm; 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 S1731043AbfG3In1 (ORCPT + 99 others); Tue, 30 Jul 2019 04:43:27 -0400 Received: from merlin.infradead.org ([205.233.59.134]:56092 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726255AbfG3In1 (ORCPT ); Tue, 30 Jul 2019 04:43:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=5dtvEs2ayMMPDWlDVD/VDsH5GdZ7NguoxBUGDu1v+1A=; b=xG7r1KkmYiasqjaGmGOz72Bvj 0y27GNWNOy+MpQ4KS37k8JDOY7tYtI8IAqNjYPncukYi7M95ydimStrBb3IpiD/YIKOTorj5dlkW/ diNd1zqx3gUgaRb8lc8nPTkVsTdY1frc9BUQxXbhADAYnqs+7K/9Caz0prQ0CRz8JZJJ5mQEMVfKU bs8EgdLviD8nLA9drj4K+9xd70p5QlYcvdecXAxq07ZZNDeVr1uxRx1RXpFCyLLLpXfug6MwlrklD CqSHOGDXKs21BZagSWGc1Ripc5vOQCTL/vcdsMEHwuIuAzFlAZlsmjFVItaBpT0hgmeYaUHpikALG 94PhG1ZPw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92 #3 (Red Hat Linux)) id 1hsNj9-0005YZ-KW; Tue, 30 Jul 2019 08:43:23 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id EB84320AFFE9F; Tue, 30 Jul 2019 10:43:21 +0200 (CEST) Date: Tue, 30 Jul 2019 10:43:21 +0200 From: Peter Zijlstra To: Waiman Long Cc: Ingo Molnar , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Phil Auld , Michal Hocko , Rik van Riel Subject: Re: [PATCH v3] sched/core: Don't use dying mm as active_mm of kthreads Message-ID: <20190730084321.GL31381@hirez.programming.kicks-ass.net> References: <20190729210728.21634-1-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190729210728.21634-1-longman@redhat.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jul 29, 2019 at 05:07:28PM -0400, Waiman Long wrote: > It was found that a dying mm_struct where the owning task has exited > can stay on as active_mm of kernel threads as long as no other user > tasks run on those CPUs that use it as active_mm. This prolongs the > life time of dying mm holding up some resources that cannot be freed > on a mostly idle system. > > Fix that by forcing the kernel threads to use init_mm as the active_mm > during a kernel thread to kernel thread transition if the previous > active_mm is dying (!mm_users). This will allows the freeing of resources > associated with the dying mm ASAP. > > The presence of a kernel-to-kernel thread transition indicates that > the cpu is probably idling with no higher priority user task to run. > So the overhead of loading the mm_users cacheline should not really > matter in this case. > > My testing on an x86 system showed that the mm_struct was freed within > seconds after the task exited instead of staying alive for minutes or > even longer on a mostly idle system before this patch. > > Signed-off-by: Waiman Long > --- > kernel/sched/core.c | 21 +++++++++++++++++++-- > 1 file changed, 19 insertions(+), 2 deletions(-) > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 795077af4f1a..41997e676251 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -3214,6 +3214,8 @@ static __always_inline struct rq * > context_switch(struct rq *rq, struct task_struct *prev, > struct task_struct *next, struct rq_flags *rf) > { > + struct mm_struct *next_mm = next->mm; > + > prepare_task_switch(rq, prev, next); > > /* > @@ -3229,8 +3231,22 @@ context_switch(struct rq *rq, struct task_struct *prev, > * > * kernel -> user switch + mmdrop() active > * user -> user switch > + * > + * kernel -> kernel and !prev->active_mm->mm_users: > + * switch to init_mm + mmgrab() + mmdrop() > */ > - if (!next->mm) { // to kernel > + if (!next_mm) { // to kernel > + /* > + * Checking is only done on kernel -> kernel transition > + * to avoid any performance overhead while user tasks > + * are running. > + */ > + if (unlikely(!prev->mm && > + !atomic_read(&prev->active_mm->mm_users))) { > + next_mm = next->active_mm = &init_mm; > + mmgrab(next_mm); > + goto mm_switch; > + } > enter_lazy_tlb(prev->active_mm, next); > > next->active_mm = prev->active_mm; So I _really_ hate this complication. I'm thinking if you really care about this the time is much better spend getting rid of the active_mm tracking for x86 entirely.