From patchwork Sun Jul 17 12:58:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Wilson X-Patchwork-Id: 9233643 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 0147F60865 for ; Sun, 17 Jul 2016 12:59:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E52222015F for ; Sun, 17 Jul 2016 12:59:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D779F20855; Sun, 17 Jul 2016 12:59:23 +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=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 432082015F for ; Sun, 17 Jul 2016 12:59:23 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DC1256E1BE; Sun, 17 Jul 2016 12:59:18 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x244.google.com (mail-wm0-x244.google.com [IPv6:2a00:1450:400c:c09::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2238A6E1BE for ; Sun, 17 Jul 2016 12:59:07 +0000 (UTC) Received: by mail-wm0-x244.google.com with SMTP id o80so8767414wme.0 for ; Sun, 17 Jul 2016 05:59:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=IAFrleeURurLsdtf5wRMrJfuEqR1HQDGmNvVQRoi9QE=; b=VWY+wuAe9X24BjSN8lCZJJV4gHL3yIHMzPe1ozTw8tDZjRsj8/YiAbrPcUVC8iQ/xK FFU7q45KSYomWMuAmVYf3fsrLj5+59V4xF3sV2bk23fpg/421a/TYT7U/o8PSu06UKqL ejH3AKSv5lFx1LA4BWy8fBhaG9SV0gH3AvPEXWPl95yyqbQ5pGaO0fI9y3jaRgcq5DC0 gGASLJe52i4ujpkM30wZqGLgEssH0p6wAyorJ0lWZxuU9lJ+RvpVbVQ9fkJ7AWcHaQRP MGd2Xlm2+PbenH0CQFxDt6jVN1kCuon140UVBIQ3RwF+lBCNfNRICe4l2K6+c3jTtWVE sXow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=IAFrleeURurLsdtf5wRMrJfuEqR1HQDGmNvVQRoi9QE=; b=mPdUD0IGv5gSYIhy6y4s6PVmkHWvCWp8Ab50CLhzs5YtkH0VC12v5r0Upjd7lvlrHE yjv9U5RoUMhchpkdCBFVbRw3F4v+Web9z/vwKDtPgXFaqFj0lk/OdvRbjQrCtudjmjp1 v/Bl7pSCdkzn2lPVGfBx6quvY5bWVlCRX1WgV/ohsOQhVzScZbHmkPOTBIr/MnZXbkYq wzfD/bQgFGupv7MfLu6wfkduSOwrbrBS1k3RsUcpGSoiyw5i+nEwyUke0xuHimwdPtcH 1F07LtIMAsKwFKztMoH1VmlEcD+genuwqNx3s+6uMEAWVvRtmnMbgs1wFTN0P6A3ZHWL hNAg== X-Gm-Message-State: ALyK8tJsoZTpzAk3Nuts6ZwcTOkIiIDn5tnMQFTl3gw5xeWSJmcLcokS9+VSihsWYXN5Nw== X-Received: by 10.28.216.75 with SMTP id p72mr51235874wmg.67.1468760340090; Sun, 17 Jul 2016 05:59:00 -0700 (PDT) Received: from haswell.alporthouse.com ([78.156.65.138]) by smtp.gmail.com with ESMTPSA id p76sm8125371wmd.10.2016.07.17.05.58.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Jul 2016 05:58:57 -0700 (PDT) From: Chris Wilson To: linux-kernel@vger.kernel.org Subject: [PATCH v2 2/7] kfence: Wrap hrtimer to provide a time source for a kfence Date: Sun, 17 Jul 2016 13:58:02 +0100 Message-Id: <1468760287-731-3-git-send-email-chris@chris-wilson.co.uk> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1468760287-731-1-git-send-email-chris@chris-wilson.co.uk> References: <1466759333-4703-1-git-send-email-chris@chris-wilson.co.uk> <1468760287-731-1-git-send-email-chris@chris-wilson.co.uk> Cc: "Peter Zijlstra \(Intel\)" , Daniel Vetter , Rasmus Villemoes , dri-devel@lists.freedesktop.org, Alexander Potapenko , Ingo Molnar , Davidlohr Bueso , Shuah Khan , Andrey Ryabinin , "Paul E. McKenney" , linux-media@vger.kernel.org, Kees Cook , Nikolay Aleksandrov , linaro-mm-sig@lists.linaro.org, Dan Williams , Andy Shevchenko , Dmitry Vyukov , Thomas Gleixner , Tejun Heo , Andrew Morton , "David S. Miller" X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP A common requirement when scheduling a task is that it should be not be begun until a certain point in time is passed (e.g. queue_delayed_work()). kfence_await_hrtimer() causes the kfence to asynchronously wait until after the appropriate time before being woken. Signed-off-by: Chris Wilson Cc: Sumit Semwal Cc: Shuah Khan Cc: Tejun Heo Cc: Daniel Vetter Cc: Andrew Morton Cc: Ingo Molnar Cc: Kees Cook Cc: Thomas Gleixner Cc: "Paul E. McKenney" Cc: Dan Williams Cc: Andrey Ryabinin Cc: Davidlohr Bueso Cc: Nikolay Aleksandrov Cc: "David S. Miller" Cc: "Peter Zijlstra (Intel)" Cc: Rasmus Villemoes Cc: Andy Shevchenko Cc: Dmitry Vyukov Cc: Alexander Potapenko Cc: linux-kernel@vger.kernel.org Cc: linux-media@vger.kernel.org Cc: dri-devel@lists.freedesktop.org Cc: linaro-mm-sig@lists.linaro.org --- include/linux/kfence.h | 5 +++++ kernel/kfence.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/test-kfence.c | 44 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) diff --git a/include/linux/kfence.h b/include/linux/kfence.h index 6e32385b3b8c..76a2f95dfb70 100644 --- a/include/linux/kfence.h +++ b/include/linux/kfence.h @@ -16,6 +16,7 @@ #include struct completion; +enum hrtimer_mode; struct kfence { wait_queue_head_t wait; @@ -43,6 +44,10 @@ int kfence_await_kfence(struct kfence *fence, int kfence_await_completion(struct kfence *fence, struct completion *x, gfp_t gfp); +int kfence_await_hrtimer(struct kfence *fence, + clockid_t clock, enum hrtimer_mode mode, + ktime_t delay, u64 slack, + gfp_t gfp); void kfence_complete(struct kfence *fence); void kfence_wake_up_all(struct kfence *fence); void kfence_wait(struct kfence *fence); diff --git a/kernel/kfence.c b/kernel/kfence.c index 693af9da545a..59c27910a749 100644 --- a/kernel/kfence.c +++ b/kernel/kfence.c @@ -48,6 +48,9 @@ * - kfence_await_completion(): the kfence asynchronously waits upon a * completion * + * - kfence_await_hrtimer(): the kfence asynchronously wait for an expiration + * of a timer + * * A kfence is initialised using kfence_init(), and starts off awaiting an * event. Once you have finished setting up the fence, including adding * all of its asynchronous waits, call kfence_complete(). @@ -429,3 +432,58 @@ int kfence_await_completion(struct kfence *fence, return pending; } EXPORT_SYMBOL_GPL(kfence_await_completion); + +struct timer_cb { + struct hrtimer timer; + struct kfence *fence; +}; + +static enum hrtimer_restart +timer_kfence_wake(struct hrtimer *timer) +{ + struct timer_cb *cb = container_of(timer, typeof(*cb), timer); + + kfence_complete(cb->fence); + kfence_put(cb->fence); + kfree(cb); + + return HRTIMER_NORESTART; +} + +/** + * kfence_await_hrtimer - set the fence to wait for a period of time + * @fence: this kfence + * @clock: which clock to program + * @mode: delay given as relative or absolute + * @delay: how long or until what time to wait + * @slack: how much slack that may be applied to the delay + * + * kfence_await_hrtimer() causes the @fence to wait for a a period of time, or + * until a certain point in time. It is a convenience wrapper around + * hrtimer_start_range_ns(). For more details on @clock, @mode, @delay and + * @slack please consult the hrtimer documentation. + * + * Returns 1 if the delay was sucessfuly added to the @fence, or a negative + * error code on failure. + */ +int kfence_await_hrtimer(struct kfence *fence, + clockid_t clock, enum hrtimer_mode mode, + ktime_t delay, u64 slack, + gfp_t gfp) +{ + struct timer_cb *cb; + + cb = kmalloc(sizeof(*cb), gfp); + if (!cb) + return -ENOMEM; + + cb->fence = kfence_get(fence); + kfence_await(fence); + + hrtimer_init(&cb->timer, clock, mode); + cb->timer.function = timer_kfence_wake; + + hrtimer_start_range_ns(&cb->timer, delay, slack, mode); + return 1; +} +EXPORT_SYMBOL_GPL(kfence_await_hrtimer); diff --git a/lib/test-kfence.c b/lib/test-kfence.c index b40719fce967..1b0853fda7c3 100644 --- a/lib/test-kfence.c +++ b/lib/test-kfence.c @@ -352,6 +352,44 @@ static int __init test_completion(void) return 0; } +static int __init test_delay(void) +{ + struct kfence *fence; + ktime_t delay; + int ret; + + /* Test use of a hrtimer as an event source for kfences */ + pr_debug("%s\n", __func__); + + fence = alloc_kfence(); + if (!fence) + return -ENOMEM; + + delay = ktime_get(); + + ret = kfence_await_hrtimer(fence, CLOCK_MONOTONIC, HRTIMER_MODE_REL, + ms_to_ktime(1), 1 << 10, + GFP_KERNEL); + if (ret < 0) + return ret; + if (ret == 0) + return -EINVAL; + + kfence_complete(fence); + kfence_wait(fence); + + delay = ktime_sub(ktime_get(), delay); + kfence_put(fence); + + if (!ktime_to_ms(delay)) { + pr_err("kfence woke too early, delay was only %lldns\n", + (long long)ktime_to_ns(delay)); + return -EINVAL; + } + + return 0; +} + struct task_ipc { struct work_struct work; struct completion started; @@ -522,6 +560,12 @@ static int __init test_kfence_init(void) return ret; } + ret = test_delay(); + if (ret < 0) { + pr_err("delay failed\n"); + return ret; + } + return 0; }