git: 9b8016548ef6 - main - linuxkpi: Add cond_resched_lock

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Wed, 23 Mar 2022 13:38:59 UTC
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=9b8016548ef61e2c42271c7dce35d0322540d2f1

commit 9b8016548ef61e2c42271c7dce35d0322540d2f1
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2022-03-21 10:14:05 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2022-03-23 13:36:55 +0000

    linuxkpi: Add cond_resched_lock
    
    If we need to resched it takes the lock, resched, release the lock
    and returns 1, otherwise simply returns 0.
    
    Needed by drm v5.9
    
    MFC after:      1 month
    Reviewed by:    hselasky
    Sponsored by:   Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D34620
---
 sys/compat/linuxkpi/common/include/linux/sched.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/sched.h b/sys/compat/linuxkpi/common/include/linux/sched.h
index 5bb9047640b8..aa4bdbc65c5b 100644
--- a/sys/compat/linuxkpi/common/include/linux/sched.h
+++ b/sys/compat/linuxkpi/common/include/linux/sched.h
@@ -45,6 +45,7 @@
 #include <linux/pid.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/spinlock.h>
 #include <linux/time.h>
 
 #include <asm/atomic.h>
@@ -127,6 +128,18 @@ put_task_struct(struct task_struct *task)
 
 #define	need_resched() (curthread->td_flags & TDF_NEEDRESCHED)
 
+static inline int
+cond_resched_lock(spinlock_t *lock)
+{
+
+	if (need_resched() == 0)
+		return (0);
+	spin_lock(lock);
+	cond_resched();
+	spin_unlock(lock);
+	return (1);
+}
+
 bool linux_signal_pending(struct task_struct *task);
 bool linux_fatal_signal_pending(struct task_struct *task);
 bool linux_signal_pending_state(long state, struct task_struct *task);