git: bb651c77f5f7 - main - linuxkpi: Define `hrtimer_try_to_cancel()`

From: Jean-Sébastien Pédron <dumbbell_at_FreeBSD.org>
Date: Wed, 18 Jan 2023 22:07:16 UTC
The branch main has been updated by dumbbell (ports committer):

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

commit bb651c77f5f71a38dd90b2eee214e7a2224421a7
Author:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
AuthorDate: 2023-01-11 01:00:57 +0000
Commit:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
CommitDate: 2023-01-18 22:02:24 +0000

    linuxkpi: Define `hrtimer_try_to_cancel()`
    
    It is the same as callout_stop(9) but the return values are different.
    
    Reviewed by:    hselasky
    Approved by:    hselasky
    Differential Revision:  https://reviews.freebsd.org/D38081
---
 sys/compat/linuxkpi/common/include/linux/hrtimer.h |  2 ++
 sys/compat/linuxkpi/common/src/linux_hrtimer.c     | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/hrtimer.h b/sys/compat/linuxkpi/common/include/linux/hrtimer.h
index 23e707d906b4..4a63db80cc12 100644
--- a/sys/compat/linuxkpi/common/include/linux/hrtimer.h
+++ b/sys/compat/linuxkpi/common/include/linux/hrtimer.h
@@ -53,6 +53,7 @@ struct hrtimer {
 };
 
 #define	hrtimer_active(hrtimer)	linux_hrtimer_active(hrtimer)
+#define	hrtimer_try_to_cancel(hrtimer)	linux_hrtimer_try_to_cancel(hrtimer)
 #define	hrtimer_cancel(hrtimer)	linux_hrtimer_cancel(hrtimer)
 
 #define	hrtimer_init(hrtimer, clock, mode) do {			\
@@ -79,6 +80,7 @@ struct hrtimer {
 } while (0)
 
 bool	linux_hrtimer_active(struct hrtimer *);
+int	linux_hrtimer_try_to_cancel(struct hrtimer *);
 int	linux_hrtimer_cancel(struct hrtimer *);
 void	linux_hrtimer_init(struct hrtimer *);
 void	linux_hrtimer_set_expires(struct hrtimer *, ktime_t);
diff --git a/sys/compat/linuxkpi/common/src/linux_hrtimer.c b/sys/compat/linuxkpi/common/src/linux_hrtimer.c
index a56485512a14..5d9b376f9561 100644
--- a/sys/compat/linuxkpi/common/src/linux_hrtimer.c
+++ b/sys/compat/linuxkpi/common/src/linux_hrtimer.c
@@ -65,6 +65,28 @@ linux_hrtimer_active(struct hrtimer *hrtimer)
 	return (ret);
 }
 
+/*
+ * Try to cancel active hrtimer.
+ * Return 1 if timer was active and cancellation succeeded, 0 if timer was
+ * inactive, or -1 if the timer is being serviced and can't be cancelled.
+ */
+int
+linux_hrtimer_try_to_cancel(struct hrtimer *hrtimer)
+{
+	int ret;
+
+	mtx_lock(&hrtimer->mtx);
+	ret = callout_stop(&hrtimer->callout);
+	mtx_unlock(&hrtimer->mtx);
+	if (ret > 0) {
+		return (1);
+	} else if (ret < 0) {
+		return (0);
+	} else {
+		return (-1);
+	}
+}
+
 /*
  * Cancel active hrtimer.
  * Return 1 if timer was active and cancellation succeeded, or 0 otherwise.