Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1771700ybv; Fri, 21 Feb 2020 03:13:50 -0800 (PST) X-Google-Smtp-Source: APXvYqyFh+KQvaXnKXkV61r1cR9mk2h3TZIzOKuuP+sxbqTuiGkkZ6GBq06PTy6c++ZRCkuxTq1q X-Received: by 2002:a05:6830:1e64:: with SMTP id m4mr28568628otr.244.1582283630838; Fri, 21 Feb 2020 03:13:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582283630; cv=none; d=google.com; s=arc-20160816; b=DW+7yEJMineGycgE/hhN5R3HyWm7UMiFwuUU0UzBNFtVtAW9g4MpjRXje4DHGs3BVu zccWv3Z8SEdJbeps9PHJQ1qjfs5J37F0U8lRsxt7TDYpGubkxhcAInITrf11gJ0o+3Tq coQq/14sAcB8ZL1Lf9IKU4vEGn6WjUI38dSTYcnNKejvMBZPETCGEHrFM6cohPSOOX+I S12xYPpEUONS+Y6zG/X6lRSuFrTk3VAfGGRo4yH5AfK+4L1oQYU2nYdTfFqeGQhGX57y gS47qsng3IwqtNpc6UjOkiwqgKP2Lan5LahjQ7Ezazh/n1MqOcg2So6gxL7hplLPIqoW cqHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=SVjfyIXphMvz+0mMh2uVsdtbFsbq7Vh0uLErS6TwfLs=; b=FV0Tscgl659R3wHdPh9oSoZFXtP8Mes4zylNs0FyoeOU6+yaURhPjyvr6vtveWri4C yjN/dIulFFsTZX5QMIhMaEjSE1til8akWk3E8/InGu3IwcThOE2AYkzLNHtCK3Osayi0 gbFTZkdmJkDBiurZ3EyuxJr7OvItAYxuGKIBRyqGfp+Bx/+xPzIcV1vokoUZ4SIsYXKC oTFweCvajjLuloj7YLvqmYYAhDHgecfd1VNu02HjMkzwyl+4xf4HMfvmpumNHQjuQd5U KLX3wL7NkO9L0AMV7+txtzWbbWEQzfG9QVznd4z6HSIGrsg9qhUwsJ38lEdOutao7rYE zHZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=ipz3LQxm; 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 d6si1313132ote.72.2020.02.21.03.13.36; Fri, 21 Feb 2020 03:13:50 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=ipz3LQxm; 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 S1727932AbgBULLo (ORCPT + 99 others); Fri, 21 Feb 2020 06:11:44 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:44234 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727903AbgBULLo (ORCPT ); Fri, 21 Feb 2020 06:11:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Type:MIME-Version:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:In-Reply-To:References; bh=SVjfyIXphMvz+0mMh2uVsdtbFsbq7Vh0uLErS6TwfLs=; b=ipz3LQxmil6VBbX0LjxdZNhiF5 v38HWarRPxD1CofLKUZxD5lS3+lDbjcq+GC7UOmOoTt0W+PnyGRuqbU6nxV2xXuDG40IbX8Co1cLx zwdL/d2zAE3KhbmLhTIWsiIQaK+srz4oJx+CGOprfIdPdAUoNTOIXuoDzwI3C9FoNQMSZNbTZyEVK Z7e21zLAxB8xvWOQRfqxWwbQG2idydm3HZcNHz0sDibMYuvrwJ8jTU2YnHIpkW1luU+6T21L503gs pcpeL5VoNB+1PydwXZtam8v9BD4VF8b06qhSCWlcMaBHQj/jxUfqUr9vQsXQTqCkiip/SPrNNvJTJ Lb/b8tRw==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1j56Dc-00032d-IN; Fri, 21 Feb 2020 11:11:40 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 87A7730220B; Fri, 21 Feb 2020 12:09:45 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id A735620254E68; Fri, 21 Feb 2020 12:11:38 +0100 (CET) Date: Fri, 21 Feb 2020 12:11:38 +0100 From: Peter Zijlstra To: Andrew Morton Cc: linux-kernel@vger.kernel.org, luto@amacapital.net, axboe@kernel.dk, keescook@chromium.org, torvalds@linux-foundation.org, jannh@google.com, will@kernel.org Subject: [PATCH] mm/tlb: Fix use_mm() vs TLB invalidate Message-ID: <20200221111138.GX14897@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 For SMP systems using IPI based TLB invalidation, looking at current->active_mm is entirely reasonable. This then presents the following race condition: CPU0 CPU1 flush_tlb_mm(mm) use_mm(mm) tsk->active_mm = mm; if (tsk->active_mm == mm) // flush TLBs switch_mm(old_mm,mm,tsk); Where it is possible the IPI flushed the TLBs for @old_mm, not @mm, because the IPI lands before we actually switched. Avoid this by disabling IRQs across changing ->active_mm and switch_mm(). [ There are all sorts of reasons this might be harmless for various architecture specific reasons, but best not leave the door open at all. ] Cc: stable@kernel.org Reported-by: Andy Lutomirski Signed-off-by: Peter Zijlstra (Intel) --- Index: linux-2.6/mm/mmu_context.c =================================================================== --- linux-2.6.orig/mm/mmu_context.c +++ linux-2.6/mm/mmu_context.c @@ -24,14 +24,19 @@ void use_mm(struct mm_struct *mm) struct mm_struct *active_mm; struct task_struct *tsk = current; + BUG_ON(!(tsk->flags & PF_KTHREAD)); + BUG_ON(tsk->mm != NULL); + task_lock(tsk); + local_irq_disable(); active_mm = tsk->active_mm; if (active_mm != mm) { mmgrab(mm); tsk->active_mm = mm; } tsk->mm = mm; - switch_mm(active_mm, mm, tsk); + switch_mm_irqs_off(active_mm, mm, tsk); + local_irq_enable(); task_unlock(tsk); #ifdef finish_arch_post_lock_switch finish_arch_post_lock_switch(); @@ -54,11 +59,15 @@ void unuse_mm(struct mm_struct *mm) { struct task_struct *tsk = current; + BUG_ON(!(tsk->flags & PF_KTHREAD)); + task_lock(tsk); sync_mm_rss(mm); + local_irq_disable(); tsk->mm = NULL; /* active_mm is still 'mm' */ enter_lazy_tlb(mm, tsk); + local_irq_enable(); task_unlock(tsk); } EXPORT_SYMBOL_GPL(unuse_mm);