From: Peter Zijlstra <peterz@infradead.org>
To: Chris Wilson <chris@chris-wilson.co.uk>
Cc: linux-kernel@vger.kernel.org,
Sumit Semwal <sumit.semwal@linaro.org>,
Shuah Khan <shuahkh@osg.samsung.com>, Tejun Heo <tj@kernel.org>,
Daniel Vetter <daniel.vetter@ffwll.ch>,
Andrew Morton <akpm@linux-foundation.org>,
Ingo Molnar <mingo@kernel.org>, Kees Cook <keescook@chromium.org>,
Thomas Gleixner <tglx@linutronix.de>,
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
Dan Williams <dan.j.williams@intel.com>,
Andrey Ryabinin <aryabinin@virtuozzo.com>,
Davidlohr Bueso <dave@stgolabs.net>,
Nikolay Aleksandrov <nikolay@cumulusnetworks.com>,
"David S. Miller" <davem@davemloft.net>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Dmitry Vyukov <dvyukov@google.com>,
Alexander Potapenko <glider@google.com>,
linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
linaro-mm-sig@lists.linaro.org
Subject: Re: [PATCH 2/9] async: Introduce kfence, a N:M completion mechanism
Date: Wed, 13 Jul 2016 12:26:25 +0200 [thread overview]
Message-ID: <20160713102625.GA30921@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <1466759333-4703-3-git-send-email-chris@chris-wilson.co.uk>
On Fri, Jun 24, 2016 at 10:08:46AM +0100, Chris Wilson wrote:
> +struct kfence {
> + wait_queue_head_t wait;
> + unsigned long flags;
> + struct kref kref;
> + atomic_t pending;
> +};
> +#define KFENCE_CHECKED_BIT 0
> +
> +static void kfence_free(struct kref *kref)
> +{
> + struct kfence *fence = container_of(kref, typeof(*fence), kref);
> +
> + WARN_ON(atomic_read(&fence->pending) > 0);
> +
> + kfree(fence);
> +}
> +
> +/**
> + * kfence_put - release a reference to a kfence
> + * @fence: the kfence being disposed of
> + */
> +void kfence_put(struct kfence *fence)
> +{
> + if (fence)
> + kref_put(&fence->kref, kfence_free);
It seems very poor semantics to allow to put NULL, that would indicate a
severe logic fail.
> +}
> +EXPORT_SYMBOL_GPL(kfence_put);
> +/**
> + * kfence_get - acquire a reference to a kfence
> + * @fence: the kfence being used
> + *
> + * Returns the pointer to the kfence, with its reference count incremented.
> + */
> +struct kfence *kfence_get(struct kfence *fence)
> +{
> + if (fence)
> + kref_get(&fence->kref);
Similar, getting NULL is just horrible taste.
> + return fence;
> +}
> +EXPORT_SYMBOL_GPL(kfence_get);
> +static void __kfence_wake_up_all(struct kfence *fence,
> + struct list_head *continuation)
> +{
> + wait_queue_head_t *x = &fence->wait;
> + unsigned long flags;
> +
> + /* To prevent unbounded recursion as we traverse the graph
Broken comment style.
> + * of kfences, we move the task_list from this ready fence
> + * to the tail of the current fence we are signaling.
> + */
> + spin_lock_irqsave_nested(&x->lock, flags, 1 + !!continuation);
> + if (continuation)
> + list_splice_tail_init(&x->task_list, continuation);
> + else while (!list_empty(&x->task_list))
> + __wake_up_locked_key(x, TASK_NORMAL, &x->task_list);
> + spin_unlock_irqrestore(&x->lock, flags);
> +}
> +
> +static void __kfence_signal(struct kfence *fence,
> + struct list_head *continuation)
> +{
> + if (!atomic_dec_and_test(&fence->pending))
> + return;
> +
> + atomic_dec(&fence->pending);
You decrement twice?
> + __kfence_wake_up_all(fence, continuation);
> +}
> +
> +/**
> + * kfence_pending - mark the fence as pending a signal
I would say: increment the pending count, requiring one more completion
before the fence is done.
'Mark' completely misses the point. You need to balance these increments
with decrements, its not a boolean state.
> + * @fence: the kfence to be signaled
> + *
> + */
> +void kfence_pending(struct kfence *fence)
> +{
> + WARN_ON(atomic_inc_return(&fence->pending) <= 1);
> +}
> +EXPORT_SYMBOL_GPL(kfence_pending);
> +/**
> + * kfence_create - create a fence
> + * @gfp: the allowed allocation type
> + *
> + * A fence is created with a reference count of one, and pending a signal.
> + * After you have completed setting up the fence for use, call kfence_signal()
> + * to signal completion.
> + *
> + * Returns the newly allocated fence, or NULL on error.
> + */
> +struct kfence *kfence_create(gfp_t gfp)
> +{
> + struct kfence *fence;
> +
> + fence = kmalloc(sizeof(*fence), gfp);
> + if (!fence)
> + return NULL;
> +
> + kfence_init(fence);
> + return fence;
> +}
> +EXPORT_SYMBOL_GPL(kfence_create);
Why? What is the purpose of this here thing? We never provide allocation
wrappers.
> +
> +/**
> + * kfence_add - set one fence to wait upon another
Since you're going to do a whole lot other kfence_add_$foo() thingies,
why isn't this called kfence_add_kfence() ?
> + * @fence: this kfence
> + * @signaler: target kfence to wait upon
> + * @gfp: the allowed allocation type
> + *
> + * kfence_add() causes the @fence to wait upon completion of @signaler.
> + * Internally the @fence is marked as pending a signal from @signaler.
> + *
> + * Returns 1 if the @fence was added to the waiqueue of @signaler, 0
> + * if @signaler was already complete, or a negative error code.
> + */
> +int kfence_add(struct kfence *fence, struct kfence *signaler, gfp_t gfp)
> +{
> + wait_queue_t *wq;
> + unsigned long flags;
> + int pending;
> +
> + if (!signaler || kfence_complete(signaler))
Again, wth would you allow adding NULL? That's just horrible.
> + return 0;
> +
> + /* The dependency graph must be acyclic */
> + if (unlikely(kfence_check_if_after(fence, signaler)))
> + return -EINVAL;
> +
> + wq = kmalloc(sizeof(*wq), gfp);
> + if (unlikely(!wq)) {
> + if (!gfpflags_allow_blocking(gfp))
> + return -ENOMEM;
> +
> + kfence_wait(signaler);
> + return 0;
> + }
> +
> + wq->flags = 0;
> + wq->func = kfence_wake;
> + wq->private = kfence_get(fence);
> +
> + kfence_pending(fence);
> +
> + spin_lock_irqsave(&signaler->wait.lock, flags);
> + if (likely(!kfence_complete(signaler))) {
> + __add_wait_queue_tail(&signaler->wait, wq);
> + pending = 1;
> + } else {
> + INIT_LIST_HEAD(&wq->task_list);
> + kfence_wake(wq, 0, 0, NULL);
> + pending = 0;
> + }
> + spin_unlock_irqrestore(&signaler->wait.lock, flags);
> +
> + return pending;
> +}
> +EXPORT_SYMBOL_GPL(kfence_add);
> +
> +/**
> + * kfence_add_completion - set the fence to wait upon a completion
> + * @fence: this kfence
> + * @x: target completion to wait upon
> + * @gfp: the allowed allocation type
> + *
> + * kfence_add_completiond() causes the @fence to wait upon a completion.
> + * Internally the @fence is marked as pending a signal from @x.
> + *
> + * Returns 1 if the @fence was added to the waiqueue of @x, 0
> + * if @x was already complete, or a negative error code.
> + */
> +int kfence_add_completion(struct kfence *fence, struct completion *x, gfp_t gfp)
> +{
> + wait_queue_t *wq;
> + unsigned long flags;
> + int pending;
> +
> + if (!x || completion_done(x))
> + return 0;
> +
> + wq = kmalloc(sizeof(*wq), gfp);
> + if (unlikely(!wq)) {
> + if (!gfpflags_allow_blocking(gfp))
> + return -ENOMEM;
> +
> + wait_for_completion(x);
> + return 0;
> + }
> +
> + wq->flags = 0;
> + wq->func = kfence_wake;
> + wq->private = kfence_get(fence);
> +
> + kfence_pending(fence);
> +
> + spin_lock_irqsave(&x->wait.lock, flags);
> + if (likely(!READ_ONCE(x->done))) {
> + __add_wait_queue_tail(&x->wait, wq);
> + pending = 1;
> + } else {
> + INIT_LIST_HEAD(&wq->task_list);
> + kfence_wake(wq, 0, 0, NULL);
> + pending = 0;
> + }
> + spin_unlock_irqrestore(&x->wait.lock, flags);
> +
> + return pending;
> +}
> +EXPORT_SYMBOL_GPL(kfence_add_completion);
It appears to me these two function share a _lot_ of code, surely that
can be reduced a bit?
next prev parent reply other threads:[~2016-07-13 10:27 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-24 9:08 Introduce fences for N:M completion variables Chris Wilson
2016-06-24 9:08 ` [PATCH 1/9] lib: Add kselftests for async-domains Chris Wilson
2016-06-24 9:08 ` [PATCH 2/9] async: Introduce kfence, a N:M completion mechanism Chris Wilson
2016-07-13 9:38 ` Peter Zijlstra
2016-07-13 10:20 ` Chris Wilson
2016-07-13 11:02 ` Daniel Vetter
2016-07-13 10:26 ` Peter Zijlstra [this message]
2016-06-24 9:08 ` [PATCH 3/9] async: Extend kfence to allow struct embedding Chris Wilson
2016-07-13 10:31 ` Peter Zijlstra
2016-06-24 9:08 ` [PATCH 4/9] async: Extend kfences for listening on DMA fences Chris Wilson
2016-06-24 9:08 ` [PATCH 5/9] async: Wrap hrtimer to provide a time source for a kfence Chris Wilson
2016-07-13 10:32 ` Peter Zijlstra
2016-06-24 9:08 ` [PATCH 6/9] async: Add a convenience wrapper for waiting on implicit dma-buf Chris Wilson
2016-06-24 9:08 ` [PATCH 7/9] async: Add support for explicit fine-grained barriers Chris Wilson
2016-06-24 9:08 ` [PATCH 8/9] async: Add execution barriers Chris Wilson
2016-06-24 9:08 ` [PATCH 9/9] async: Introduce a dependency resolver for parallel execution Chris Wilson
2016-07-17 12:58 ` Introduce fences for N:M completion variables [v2] Chris Wilson
2016-07-17 12:58 ` [PATCH v2 1/7] kfence: Introduce kfence, a N:M completion mechanism Chris Wilson
2016-07-17 12:58 ` [PATCH v2 2/7] kfence: Wrap hrtimer to provide a time source for a kfence Chris Wilson
2016-07-17 12:58 ` [PATCH v2 3/7] kfence: Extend kfences for listening on DMA fences Chris Wilson
2016-07-17 12:58 ` [PATCH v2 4/7] async: Add kselftests for async-domains Chris Wilson
2016-07-17 12:58 ` [PATCH v2 5/7] async: Add support for explicit fine-grained barriers Chris Wilson
2016-07-17 12:58 ` [PATCH v2 6/7] async: Add execution barriers Chris Wilson
2016-07-17 12:58 ` [PATCH v2 7/7] async: Introduce a dependency resolver for parallel execution Chris Wilson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160713102625.GA30921@twins.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=aryabinin@virtuozzo.com \
--cc=chris@chris-wilson.co.uk \
--cc=dan.j.williams@intel.com \
--cc=daniel.vetter@ffwll.ch \
--cc=dave@stgolabs.net \
--cc=davem@davemloft.net \
--cc=dri-devel@lists.freedesktop.org \
--cc=dvyukov@google.com \
--cc=glider@google.com \
--cc=keescook@chromium.org \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mingo@kernel.org \
--cc=nikolay@cumulusnetworks.com \
--cc=paulmck@linux.vnet.ibm.com \
--cc=shuahkh@osg.samsung.com \
--cc=sumit.semwal@linaro.org \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox