Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp794216pxb; Tue, 3 Nov 2020 12:42:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJxuBlYh1hrHgRQiFZ2gf2X5x+dkLhC5FYbF0Oyj9u2gfpqjJ1JwVe+mkuCaY81HroE+D72q X-Received: by 2002:a17:906:3644:: with SMTP id r4mr15720273ejb.517.1604436175655; Tue, 03 Nov 2020 12:42:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604436175; cv=none; d=google.com; s=arc-20160816; b=QZ0wNcsEkOeRQkgz9Ntv+QozrcMGBh+RLRElb/UG0ZTuDQxrZHLxYKKNy38VZCL4AM KTUtazXrQ6bmn/ZLUBZ05bdXO285VY9GV2k86yXtt8YUVX/kzcg4zMU/Bno/cVD0uegE f24pcwesLhyeTIrLUWosDs/T9GF+NBNw4WBIotJOAI5cJB8HhdO850R5PiYumLwFAghJ 42Xn4iNai5sFZ/IqmXXvSLlWljJ2xmi8eGj/HfghQINWkRBDmr9GGNdZIKwvN6D2RY15 XBNMc5u8Qr39//BPW8ne8GnQ45qZpIcn6f2+IxA+vR311auo1zlu6w8Jn08jVenE/GJz mciQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=bAG9DLoSvNZJAxYlphLhAxtUy3NdTuhv0yOoglTVwFE=; b=Kta0CkwDUE5XeWuipVD9MxmcYOoJaOjG1uuA49x2cVaW8K8RmLjoT6EcPXuL7yRgr4 MWM7F88oFaptGmi0oPvJwRime5zqg/yEFG8BNnPfBlBNRviEhbhH5cE4aYvozKSP9Wmn vrgFV896rcyMP3EWPIdLgzNN+nq2hdm75enXdCxit4qyFkteiLnYlFXNn69G7orjakoJ W8ZTioXPHac6xVI8xDyN1bAbzCqD5hIL3XgsCvC47uQaQ3EcwijGXNSmiRViKfImHqto QuxynthSNt3qrmyPwIqgqhC5GqNmsYXo1sqAWr6Chm7zq+Vc0paaFjKGwDvJWg7G2+3J rgFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Sp3rn0eS; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i12si12897325eje.696.2020.11.03.12.42.32; Tue, 03 Nov 2020 12:42:55 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=Sp3rn0eS; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730143AbgKCUkF (ORCPT + 99 others); Tue, 3 Nov 2020 15:40:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:51088 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730127AbgKCUj7 (ORCPT ); Tue, 3 Nov 2020 15:39:59 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 983932224E; Tue, 3 Nov 2020 20:39:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604435999; bh=+2fPEDkeuBa0U3jZ0ahFGPzrljTJ2aqNeor3jcAZEDs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Sp3rn0eSoKksPvvqKqI9QYtfAqDFJhX16TdIQFmWrJgZuy6x+XZvw3k95U4Ay3/o+ XbLul5me7hAt0keTfQNq8mrETUFBQPtvlqidsyBvHQpB/9ILx5QaE6wHBU8nilLQdy cmFL4jDZeF6KwZeAUd3BKmwN1DwaTzhiUnWd4rn0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Juergen Gross , "Peter Zijlstra (Intel)" , Sasha Levin Subject: [PATCH 5.9 021/391] x86/alternative: Dont call text_poke() in lazy TLB mode Date: Tue, 3 Nov 2020 21:31:12 +0100 Message-Id: <20201103203349.326507694@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203348.153465465@linuxfoundation.org> References: <20201103203348.153465465@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Juergen Gross [ Upstream commit abee7c494d8c41bb388839bccc47e06247f0d7de ] When running in lazy TLB mode the currently active page tables might be the ones of a previous process, e.g. when running a kernel thread. This can be problematic in case kernel code is being modified via text_poke() in a kernel thread, and on another processor exit_mmap() is active for the process which was running on the first cpu before the kernel thread. As text_poke() is using a temporary address space and the former address space (obtained via cpu_tlbstate.loaded_mm) is restored afterwards, there is a race possible in case the cpu on which exit_mmap() is running wants to make sure there are no stale references to that address space on any cpu active (this e.g. is required when running as a Xen PV guest, where this problem has been observed and analyzed). In order to avoid that, drop off TLB lazy mode before switching to the temporary address space. Fixes: cefa929c034eb5d ("x86/mm: Introduce temporary mm structs") Signed-off-by: Juergen Gross Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20201009144225.12019-1-jgross@suse.com Signed-off-by: Sasha Levin --- arch/x86/kernel/alternative.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index cdaab30880b91..cd6be6f143e85 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -807,6 +807,15 @@ static inline temp_mm_state_t use_temporary_mm(struct mm_struct *mm) temp_mm_state_t temp_state; lockdep_assert_irqs_disabled(); + + /* + * Make sure not to be in TLB lazy mode, as otherwise we'll end up + * with a stale address space WITHOUT being in lazy mode after + * restoring the previous mm. + */ + if (this_cpu_read(cpu_tlbstate.is_lazy)) + leave_mm(smp_processor_id()); + temp_state.mm = this_cpu_read(cpu_tlbstate.loaded_mm); switch_mm_irqs_off(NULL, mm, current); -- 2.27.0