mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dave Hansen <dave.hansen@intel.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: "Edgecombe, Rick P" <rick.p.edgecombe@intel.com>,
	"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>,
	"keescook@chromium.org" <keescook@chromium.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"x86@kernel.org" <x86@kernel.org>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>,
	Peter Zijlstra <peterz@infradead.org>
Subject: Re: [GIT PULL] x86/shstk for 6.4
Date: Mon, 15 May 2023 14:37:27 -0700	[thread overview]
Message-ID: <bfa65f6c-8d6f-3111-caf8-6c02cf78877c@intel.com> (raw)
In-Reply-To: <93ae88a4-1dac-77bf-37f6-f8708a6d83b7@intel.com>

[-- Attachment #1: Type: text/plain, Size: 103 bytes --]

On 5/15/23 14:36, Dave Hansen wrote:
> Is this worth pursuing?

... and with the actual patch this time

[-- Attachment #2: get_task_mm-locking.patch --]
[-- Type: text/x-patch, Size: 4720 bytes --]



---

 b/arch/x86/include/asm/pgtable.h |    6 ++
 b/include/linux/sched.h          |    1 
 b/kernel/fork.c                  |   81 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 1 deletion(-)

diff -puN kernel/fork.c~get_task_mm-locking kernel/fork.c
--- a/kernel/fork.c~get_task_mm-locking	2023-05-15 08:55:59.168918971 -0700
+++ b/kernel/fork.c	2023-05-15 14:17:34.434547231 -0700
@@ -661,6 +661,12 @@ static __latent_entropy int dup_mmap(str
 		retval = -EINTR;
 		goto fail_uprobe_end;
 	}
+
+	if ((atomic_read(&oldmm->mm_users) == 1) &&
+	    (atomic_read(&oldmm->mm_count) == 1))
+		current->task_doing_fast_fork = 1;
+
+
 	flush_cache_dup_mm(oldmm);
 	uprobe_dup_mmap(oldmm, mm);
 	/*
@@ -774,6 +780,7 @@ loop_out:
 out:
 	mmap_write_unlock(mm);
 	flush_tlb_mm(oldmm);
+	current->task_doing_fast_fork = 0;
 	mmap_write_unlock(oldmm);
 	dup_userfaultfd_complete(&uf);
 fail_uprobe_end:
@@ -1527,6 +1534,46 @@ struct file *get_task_exe_file(struct ta
 	return exe_file;
 }
 
+/*
+ * A "fast fork()" task might be taking shortcuts that make
+ * the mm's address space unstable for multithreaded access.
+ * The mm can't be considered stable until holding
+ * mmap_write_lock() to ensure that the fork() is finished.
+ *
+ * This function is a "maybe" because it can "return true"
+ * for many reasons other than an mm that is doing a fast
+ * fork.  But it should be right enough of the time to keep
+ * callers using their fast paths the majority of the time.
+ */
+static bool mm_maybe_fast_fork(struct mm_struct *mm)
+{
+	/* Fast fork() won't start if ->mm_users is elevated */
+	if (atomic_read(&mm->mm_users) != 1)
+		return false;
+
+	/*
+	 * Some users, like proc_mem_operations want to avoid pinning
+	 * the address space with ->mm_users.  They instead elevate
+	 * ->mm_count and then (temporarily) "upgrade" that ->mm_count
+	 * ref to an ->mm_users ref with mmget_not_zero().
+	 *
+	 * Check for ->mm_count==1.  This ensures that no one will
+	 * later upgrade that ref with mmget_not_zero() and access
+	 * the page tables without the "slow" mmap_lock path.
+	 */
+	if (atomic_read(&mm->mm_count) != 1)
+		return false;
+
+	/*
+	 * A task can not not be doing a fork() if the lock
+	 * is not held.
+	 */
+	if (!rwsem_is_locked(&mm->mmap_lock))
+		return false;
+
+	return true;
+}
+
 /**
  * get_task_mm - acquire a reference to the task's mm
  *
@@ -1543,6 +1590,8 @@ struct mm_struct *get_task_mm(struct tas
 	task_lock(task);
 	mm = task->mm;
 	if (mm) {
+		if (mm_maybe_fast_fork(mm))
+			goto slow;
 		if (task->flags & PF_KTHREAD)
 			mm = NULL;
 		else
@@ -1550,6 +1599,31 @@ struct mm_struct *get_task_mm(struct tas
 	}
 	task_unlock(task);
 	return mm;
+slow: {
+	struct mm_struct *ret_mm = mm;
+
+	mmgrab(mm);
+	task_unlock(task);
+
+	/*
+	 * Thanks to the mmgrab(), 'mm' itself is now stable.
+	 * 'task' might exit but can not free the mm.
+	 */
+
+	/* If a fork() was happening, wait for it to complete: */
+	mmap_write_lock(mm);
+	if (!mmget_not_zero(mm)) {
+		/*
+		 * The mm's address space has gone away.  Tell
+		 * the caller that the task's mm was unavailable:
+		 */
+		ret_mm = NULL;
+	}
+	mmap_write_unlock(mm);
+	mmdrop(mm);
+
+	return ret_mm;
+      }
 }
 EXPORT_SYMBOL_GPL(get_task_mm);
 
@@ -3561,3 +3635,10 @@ int sysctl_max_threads(struct ctl_table
 
 	return 0;
 }
+
+bool current_doing_fast_fork(void)
+{
+	WARN_ON(current->task_doing_fast_fork && (atomic_read(&current->mm->mm_users) > 1));
+	return current->task_doing_fast_fork;
+}
+
diff -puN include/linux/sched.h~get_task_mm-locking include/linux/sched.h
--- a/include/linux/sched.h~get_task_mm-locking	2023-05-15 09:32:57.393721962 -0700
+++ b/include/linux/sched.h	2023-05-15 09:33:31.240599499 -0700
@@ -762,6 +762,7 @@ struct task_struct {
 	/* Per task flags (PF_*), defined further below: */
 	unsigned int			flags;
 	unsigned int			ptrace;
+	bool task_doing_fast_fork;
 
 #ifdef CONFIG_SMP
 	int				on_cpu;
diff -puN arch/x86/include/asm/pgtable.h~get_task_mm-locking arch/x86/include/asm/pgtable.h
--- a/arch/x86/include/asm/pgtable.h~get_task_mm-locking	2023-05-15 09:36:13.030361316 -0700
+++ b/arch/x86/include/asm/pgtable.h	2023-05-15 10:27:45.112652940 -0700
@@ -1094,7 +1094,11 @@ static inline pte_t ptep_get_and_clear_f
 static inline void ptep_set_wrprotect(struct mm_struct *mm,
 				      unsigned long addr, pte_t *ptep)
 {
-	clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
+	extern bool current_doing_fast_fork(void);
+	if (current_doing_fast_fork())
+		__clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
+	else
+		clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
 }
 
 #define flush_tlb_fix_spurious_fault(vma, address, ptep) do { } while (0)
_

  reply	other threads:[~2023-05-15 21:37 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-24 21:21 Dave Hansen
2023-04-28 18:17 ` Linus Torvalds
2023-04-29  0:26   ` Edgecombe, Rick P
2023-04-29  0:40     ` Dave Hansen
2023-05-06 19:34       ` Linus Torvalds
2023-05-06 20:09         ` Linus Torvalds
2023-05-07  0:18           ` Edgecombe, Rick P
2023-05-07  0:38             ` Linus Torvalds
2023-05-07 15:57               ` Edgecombe, Rick P
2023-05-08 22:57           ` Dave Hansen
2023-05-08 23:31             ` Linus Torvalds
2023-05-08 23:47               ` Linus Torvalds
2023-05-12 17:34                 ` Dave Hansen
2023-05-12 21:55                   ` Linus Torvalds
2023-05-15 21:36                     ` Dave Hansen
2023-05-15 21:37                       ` Dave Hansen [this message]
2023-05-15 22:40                       ` Linus Torvalds
2023-05-15 23:02                         ` Linus Torvalds
2023-05-16 20:38                         ` Linus Torvalds
2023-05-16 20:42                           ` Dave Hansen
2023-05-09  0:07               ` Dave Hansen
2023-05-07  0:10         ` Edgecombe, Rick P
2023-05-07  0:19           ` Linus Torvalds
2023-05-07 16:24             ` Edgecombe, Rick P
2023-05-15 21:22               ` Deepak Gupta
2023-05-25 16:20                 ` Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bfa65f6c-8d6f-3111-caf8-6c02cf78877c@intel.com \
    --to=dave.hansen@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=rick.p.edgecombe@intel.com \
    --cc=torvalds@linux-foundation.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®