diff mbox

[7/8] posix_clocks: Use get_itimerspec64() and put_itimerspec64()

Message ID 20170619064515.922-8-deepa.kernel@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Deepa Dinamani June 19, 2017, 6:45 a.m. UTC
Usage of these apis and their compat versions makes
the syscalls: timer_settime and timer_gettime and their
compat implementations simpler.

This patch also serves as a preparatory patch for changing
syscalls to use new time_t data types to support the
y2038 effort by isolating the processing of user pointers
through these apis.

Signed-off-by: Deepa Dinamani <deepa.kernel@gmail.com>
---
 kernel/time/posix-timers.c | 138 +++++++++++++++++++++++++++------------------
 1 file changed, 84 insertions(+), 54 deletions(-)
diff mbox

Patch

diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c
index 58ed4e759485..c642c6a959ff 100644
--- a/kernel/time/posix-timers.c
+++ b/kernel/time/posix-timers.c
@@ -690,32 +690,54 @@  void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting)
 	}
 }
 
+/* common set of checks before we invoke timer_gettime and
+ * timer_settime apis.
+ */
+static int common_prep_timer(const struct k_clock **kc_ptr,
+		struct k_itimer **timr_ptr,
+		timer_t timer_id,
+		long *flags,
+		struct itimerspec64 *it)
+{
+	int error = 0;
+	const struct k_clock *kc;
+	struct k_itimer *timr;
+
+	if (it && !itimerspec64_valid(it))
+		return -EINVAL;
+
+	*timr_ptr = timr = lock_timer(timer_id, flags);
+	if (!timr)
+		return -EINVAL;
+
+	*kc_ptr = kc =  timr->kclock;
+
+	if (WARN_ON_ONCE(!kc || !kc->timer_set))
+		error = -EINVAL;
+
+	return error;
+}
+
 /* Get the time remaining on a POSIX.1b interval timer. */
 SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
 		struct itimerspec __user *, setting)
 {
-	struct itimerspec64 cur_setting64;
-	struct itimerspec cur_setting;
+	struct itimerspec64 cur_setting;
 	struct k_itimer *timr;
 	const struct k_clock *kc;
 	unsigned long flags;
 	int ret = 0;
 
-	timr = lock_timer(timer_id, &flags);
-	if (!timr)
-		return -EINVAL;
-
-	memset(&cur_setting64, 0, sizeof(cur_setting64));
-	kc = timr->kclock;
-	if (WARN_ON_ONCE(!kc || !kc->timer_get))
-		ret = -EINVAL;
+	memset(&cur_setting, 0, sizeof(cur_setting));
+	ret = common_prep_timer(&kc, &timr, timer_id, &flags, NULL);
+	if (ret)
+		return ret;
 	else
-		kc->timer_get(timr, &cur_setting64);
+		kc->timer_get(timr, &cur_setting);
 
 	unlock_timer(timr, flags);
 
-	cur_setting = itimerspec64_to_itimerspec(&cur_setting64);
-	if (!ret && copy_to_user(setting, &cur_setting, sizeof (cur_setting)))
+	if (!ret && put_itimerspec64(&cur_setting, setting))
 		return -EFAULT;
 
 	return ret;
@@ -813,9 +835,8 @@  SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
 		const struct itimerspec __user *, new_setting,
 		struct itimerspec __user *, old_setting)
 {
-	struct itimerspec64 new_spec64, old_spec64;
-	struct itimerspec64 *rtn = old_setting ? &old_spec64 : NULL;
-	struct itimerspec new_spec, old_spec;
+	struct itimerspec64 new_spec, old_spec;
+	struct itimerspec64 *rtn = old_setting ? &old_spec : NULL;
 	struct k_itimer *timr;
 	unsigned long flag;
 	const struct k_clock *kc;
@@ -824,23 +845,15 @@  SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
 	if (!new_setting)
 		return -EINVAL;
 
-	if (copy_from_user(&new_spec, new_setting, sizeof (new_spec)))
-		return -EFAULT;
-	new_spec64 = itimerspec_to_itimerspec64(&new_spec);
+	error = get_itimerspec64(&new_spec, new_setting);
+	if (error)
+		return error;
 
-	if (!timespec64_valid(&new_spec64.it_interval) ||
-	    !timespec64_valid(&new_spec64.it_value))
-		return -EINVAL;
 retry:
-	timr = lock_timer(timer_id, &flag);
-	if (!timr)
-		return -EINVAL;
+	error = common_prep_timer(&kc, &timr, timer_id, &flag, &new_spec);
 
-	kc = timr->kclock;
-	if (WARN_ON_ONCE(!kc || !kc->timer_set))
-		error = -EINVAL;
-	else
-		error = kc->timer_set(timr, flags, &new_spec64, rtn);
+	if (!error)
+		error = kc->timer_set(timr, flags, &new_spec, rtn);
 
 	unlock_timer(timr, flag);
 	if (error == TIMER_RETRY) {
@@ -848,9 +861,8 @@  SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
 		goto retry;
 	}
 
-	old_spec = itimerspec64_to_itimerspec(&old_spec64);
 	if (old_setting && !error &&
-	    copy_to_user(old_setting, &old_spec, sizeof (old_spec)))
+	    put_itimerspec64(&old_spec, old_setting))
 		error = -EFAULT;
 
 	return error;
@@ -1220,21 +1232,33 @@  COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
 		       struct compat_itimerspec __user *, new,
 		       struct compat_itimerspec __user *, old)
 {
+	struct itimerspec64 newts, oldts;
+	struct itimerspec64 *rtn = old ? &oldts : NULL;
+	struct k_itimer *timr;
+	unsigned long flag;
+	const struct k_clock *kc;
 	long err;
-	mm_segment_t oldfs;
-	struct itimerspec newts, oldts;
 
 	if (!new)
 		return -EINVAL;
-	if (get_compat_itimerspec(&newts, new))
-		return -EFAULT;
-	oldfs = get_fs();
-	set_fs(KERNEL_DS);
-	err = sys_timer_settime(timer_id, flags,
-				(struct itimerspec __user *) &newts,
-				(struct itimerspec __user *) &oldts);
-	set_fs(oldfs);
-	if (!err && old && put_compat_itimerspec(old, &oldts))
+	err = get_compat_itimerspec64(&newts, new);
+	if (err)
+		return err;
+
+retry:
+
+	err = common_prep_timer(&kc, &timr, timer_id, &flag, &newts);
+
+	if (!err)
+		err = kc->timer_set(timr, flags, &newts, rtn);
+
+	unlock_timer(timr, flag);
+	if (err == TIMER_RETRY) {
+		rtn = NULL;	// We already got the old time...
+		goto retry;
+	}
+
+	if (!err && old && put_compat_itimerspec64(&oldts, old))
 		return -EFAULT;
 	return err;
 }
@@ -1242,18 +1266,24 @@  COMPAT_SYSCALL_DEFINE4(timer_settime, timer_t, timer_id, int, flags,
 COMPAT_SYSCALL_DEFINE2(timer_gettime, timer_t, timer_id,
 		       struct compat_itimerspec __user *, setting)
 {
-	long err;
-	mm_segment_t oldfs;
-	struct itimerspec ts;
-
-	oldfs = get_fs();
-	set_fs(KERNEL_DS);
-	err = sys_timer_gettime(timer_id,
-				(struct itimerspec __user *) &ts);
-	set_fs(oldfs);
-	if (!err && put_compat_itimerspec(setting, &ts))
+	struct itimerspec64 ts;
+	struct k_itimer *timr;
+	const struct k_clock *kc;
+	unsigned long flags;
+	int ret = 0;
+
+	memset(&ts, 0, sizeof(ts));
+	ret = common_prep_timer(&kc, &timr, timer_id, &flags, NULL);
+	if (ret)
+		return ret;
+
+	kc->timer_get(timr, &ts);
+
+	unlock_timer(timr, flags);
+
+	if (!ret && put_compat_itimerspec64(&ts, setting))
 		return -EFAULT;
-	return err;
+	return ret;
 }
 
 COMPAT_SYSCALL_DEFINE2(clock_settime, clockid_t, which_clock,