From patchwork Mon Jun 19 06:45:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepa Dinamani X-Patchwork-Id: 9795265 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2935A6020B for ; Mon, 19 Jun 2017 06:47:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 119F327E5A for ; Mon, 19 Jun 2017 06:47:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 05BFE27EE2; Mon, 19 Jun 2017 06:47:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8174527E5A for ; Mon, 19 Jun 2017 06:47:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753845AbdFSGqs (ORCPT ); Mon, 19 Jun 2017 02:46:48 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:36213 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752386AbdFSGpt (ORCPT ); Mon, 19 Jun 2017 02:45:49 -0400 Received: by mail-pg0-f65.google.com with SMTP id e187so5535462pgc.3; Sun, 18 Jun 2017 23:45:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6MaR79hy7CbcMiHZA46bEbxCleTHreR3fvVncrNbNnE=; b=AMwo78McdIj4LmYOoxEIomjoTF2fxqp9P59qdMUdmDPT2yUnQhwxv5+BHXyLC3YSIY 7Ym0lS24VbJSV8l4XsVOn28tuI22WyXTizk/jqwuGM28Aoy458e36aXOVjgIVMnM/VkP OyqgU4Qo42x4sx6wMsSSVHTlgYkWLfqy195ilFfwv1Kb0Rv1D0ehc7Sv/Xc1nxrk54aR UTr6Ch+XtCmilIvNN2y90L2ucDDmRri/EQ375HxCqpdrDr4AiwdCieDcI+6NcDqvGB5x F+pOUoK1em/da/gmIvXRytGA+EyejL5FBq/J7nCdLeUIjwUmck9v1MvCHpy+hOYQZLTS MY+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6MaR79hy7CbcMiHZA46bEbxCleTHreR3fvVncrNbNnE=; b=n5kcaGZ5TU6ftu+zBcSBAuOYeCyvTriXYJL4YZT792N9YU10aqoII6YYDa/kaaf1yD vy+PJVmp/LILeCQwlAcZQtwUMd+oQ5SC2MSEUbThRYQ8SioOOFstSeKCCWOsRhKKASYt w55O1t8v/+s9YUeVof8FbSiOGrrYsyLaQajU31YwY8JjJp22+hFGwSYUy6RuuRxUXRjp V84FCOg1fcZRmVhFqVtK3WKwRx9hn6dbyF4K1S5aoIFhtDwdXJFio3vbNHkUUJDcR11q 87HuLbt4emjjKTSDo9kMhUpHMHy7Hg4tyNSAm/XZGqO5Y5aikOz8pS4MqLBwcJ+49qD8 822w== X-Gm-Message-State: AKS2vOxgg11FKpdm4afzZPRzPhgmQWciDPJpDJOh9XtN9fMPa/y95c5z LUV/qjJn6HMUzQ== X-Received: by 10.84.175.132 with SMTP id t4mr6975757plb.135.1497854748804; Sun, 18 Jun 2017 23:45:48 -0700 (PDT) Received: from localhost.localdomain ([2601:647:5000:6620:ecf7:aabb:b31b:424d]) by smtp.gmail.com with ESMTPSA id o66sm19620318pga.64.2017.06.18.23.45.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 18 Jun 2017 23:45:48 -0700 (PDT) From: Deepa Dinamani To: tglx@linutronix.de, viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org Cc: john.stultz@linaro.org, nicolas.pitre@linaro.org, arnd@arndb.de, y2038@lists.linaro.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 7/8] posix_clocks: Use get_itimerspec64() and put_itimerspec64() Date: Sun, 18 Jun 2017 23:45:14 -0700 Message-Id: <20170619064515.922-8-deepa.kernel@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170619064515.922-1-deepa.kernel@gmail.com> References: <20170619064515.922-1-deepa.kernel@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- kernel/time/posix-timers.c | 138 +++++++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 54 deletions(-) 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,