2016-09-09 21:11:41 +08:00
|
|
|
/*
|
2018-11-29 09:30:51 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2016-09-09 21:11:41 +08:00
|
|
|
* i915_sw_fence.h - library routines for N:M synchronisation points
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Intel Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _I915_SW_FENCE_H_
|
|
|
|
#define _I915_SW_FENCE_H_
|
|
|
|
|
2019-03-22 17:23:25 +08:00
|
|
|
#include <linux/dma-fence.h>
|
2016-09-09 21:11:41 +08:00
|
|
|
#include <linux/gfp.h>
|
|
|
|
#include <linux/kref.h>
|
|
|
|
#include <linux/notifier.h> /* for NOTIFY_DONE */
|
|
|
|
#include <linux/wait.h>
|
|
|
|
|
|
|
|
struct completion;
|
2019-08-11 16:06:32 +08:00
|
|
|
struct dma_resv;
|
2016-09-09 21:11:41 +08:00
|
|
|
|
|
|
|
struct i915_sw_fence {
|
|
|
|
wait_queue_head_t wait;
|
|
|
|
unsigned long flags;
|
|
|
|
atomic_t pending;
|
2019-08-18 07:25:11 +08:00
|
|
|
int error;
|
2016-09-09 21:11:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define I915_SW_FENCE_CHECKED_BIT 0 /* used internally for DAG checking */
|
|
|
|
#define I915_SW_FENCE_PRIVATE_BIT 1 /* available for use by owner */
|
|
|
|
#define I915_SW_FENCE_MASK (~3)
|
|
|
|
|
|
|
|
enum i915_sw_fence_notify {
|
|
|
|
FENCE_COMPLETE,
|
|
|
|
FENCE_FREE
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef int (*i915_sw_fence_notify_t)(struct i915_sw_fence *,
|
|
|
|
enum i915_sw_fence_notify state);
|
|
|
|
#define __i915_sw_fence_call __aligned(4)
|
|
|
|
|
2016-11-15 04:40:56 +08:00
|
|
|
void __i915_sw_fence_init(struct i915_sw_fence *fence,
|
|
|
|
i915_sw_fence_notify_t fn,
|
|
|
|
const char *name,
|
|
|
|
struct lock_class_key *key);
|
|
|
|
#ifdef CONFIG_LOCKDEP
|
|
|
|
#define i915_sw_fence_init(fence, fn) \
|
|
|
|
do { \
|
|
|
|
static struct lock_class_key __key; \
|
|
|
|
\
|
|
|
|
__i915_sw_fence_init((fence), (fn), #fence, &__key); \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define i915_sw_fence_init(fence, fn) \
|
|
|
|
__i915_sw_fence_init((fence), (fn), NULL, NULL)
|
|
|
|
#endif
|
|
|
|
|
drm/i915: Use a ctor for TYPESAFE_BY_RCU i915_request
As we start peeking into requests for longer and longer, e.g.
incorporating use of spinlocks when only protected by an
rcu_read_lock(), we need to be careful in how we reset the request when
recycling and need to preserve any barriers that may still be in use as
the request is reset for reuse.
Quoting Linus Torvalds:
> If there is refcounting going on then why use SLAB_TYPESAFE_BY_RCU?
.. because the object can be accessed (by RCU) after the refcount has
gone down to zero, and the thing has been released.
That's the whole and only point of SLAB_TYPESAFE_BY_RCU.
That flag basically says:
"I may end up accessing this object *after* it has been free'd,
because there may be RCU lookups in flight"
This has nothing to do with constructors. It's ok if the object gets
reused as an object of the same type and does *not* get
re-initialized, because we're perfectly fine seeing old stale data.
What it guarantees is that the slab isn't shared with any other kind
of object, _and_ that the underlying pages are free'd after an RCU
quiescent period (so the pages aren't shared with another kind of
object either during an RCU walk).
And it doesn't necessarily have to have a constructor, because the
thing that a RCU walk will care about is
(a) guaranteed to be an object that *has* been on some RCU list (so
it's not a "new" object)
(b) the RCU walk needs to have logic to verify that it's still the
*same* object and hasn't been re-used as something else.
In contrast, a SLAB_TYPESAFE_BY_RCU memory gets free'd and re-used
immediately, but because it gets reused as the same kind of object,
the RCU walker can "know" what parts have meaning for re-use, in a way
it couidn't if the re-use was random.
That said, it *is* subtle, and people should be careful.
> So the re-use might initialize the fields lazily, not necessarily using a ctor.
If you have a well-defined refcount, and use "atomic_inc_not_zero()"
to guard the speculative RCU access section, and use
"atomic_dec_and_test()" in the freeing section, then you should be
safe wrt new allocations.
If you have a completely new allocation that has "random stale
content", you know that it cannot be on the RCU list, so there is no
speculative access that can ever see that random content.
So the only case you need to worry about is a re-use allocation, and
you know that the refcount will start out as zero even if you don't
have a constructor.
So you can think of the refcount itself as always having a zero
constructor, *BUT* you need to be careful with ordering.
In particular, whoever does the allocation needs to then set the
refcount to a non-zero value *after* it has initialized all the other
fields. And in particular, it needs to make sure that it uses the
proper memory ordering to do so.
NOTE! One thing to be very worried about is that re-initializing
whatever RCU lists means that now the RCU walker may be walking on the
wrong list so the walker may do the right thing for this particular
entry, but it may miss walking *other* entries. So then you can get
spurious lookup failures, because the RCU walker never walked all the
way to the end of the right list. That ends up being a much more
subtle bug.
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20191122094924.629690-1-chris@chris-wilson.co.uk
2019-11-22 17:49:24 +08:00
|
|
|
void i915_sw_fence_reinit(struct i915_sw_fence *fence);
|
|
|
|
|
2016-11-25 21:17:18 +08:00
|
|
|
#ifdef CONFIG_DRM_I915_SW_FENCE_DEBUG_OBJECTS
|
|
|
|
void i915_sw_fence_fini(struct i915_sw_fence *fence);
|
|
|
|
#else
|
|
|
|
static inline void i915_sw_fence_fini(struct i915_sw_fence *fence) {}
|
|
|
|
#endif
|
|
|
|
|
2016-09-09 21:11:41 +08:00
|
|
|
void i915_sw_fence_commit(struct i915_sw_fence *fence);
|
|
|
|
|
|
|
|
int i915_sw_fence_await_sw_fence(struct i915_sw_fence *fence,
|
|
|
|
struct i915_sw_fence *after,
|
2017-06-20 18:06:13 +08:00
|
|
|
wait_queue_entry_t *wq);
|
2016-10-28 20:58:25 +08:00
|
|
|
int i915_sw_fence_await_sw_fence_gfp(struct i915_sw_fence *fence,
|
|
|
|
struct i915_sw_fence *after,
|
|
|
|
gfp_t gfp);
|
2019-03-22 17:23:25 +08:00
|
|
|
|
|
|
|
struct i915_sw_dma_fence_cb {
|
|
|
|
struct dma_fence_cb base;
|
|
|
|
struct i915_sw_fence *fence;
|
|
|
|
};
|
|
|
|
|
|
|
|
int __i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
|
|
|
|
struct dma_fence *dma,
|
|
|
|
struct i915_sw_dma_fence_cb *cb);
|
2016-09-09 21:11:41 +08:00
|
|
|
int i915_sw_fence_await_dma_fence(struct i915_sw_fence *fence,
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence *dma,
|
2016-09-09 21:11:41 +08:00
|
|
|
unsigned long timeout,
|
|
|
|
gfp_t gfp);
|
2019-03-22 17:23:25 +08:00
|
|
|
|
2016-09-09 21:11:41 +08:00
|
|
|
int i915_sw_fence_await_reservation(struct i915_sw_fence *fence,
|
2019-08-11 16:06:32 +08:00
|
|
|
struct dma_resv *resv,
|
2016-10-25 20:00:45 +08:00
|
|
|
const struct dma_fence_ops *exclude,
|
2016-09-09 21:11:41 +08:00
|
|
|
bool write,
|
|
|
|
unsigned long timeout,
|
|
|
|
gfp_t gfp);
|
|
|
|
|
2019-03-02 01:09:00 +08:00
|
|
|
void i915_sw_fence_await(struct i915_sw_fence *fence);
|
|
|
|
void i915_sw_fence_complete(struct i915_sw_fence *fence);
|
|
|
|
|
2016-11-25 21:17:17 +08:00
|
|
|
static inline bool i915_sw_fence_signaled(const struct i915_sw_fence *fence)
|
|
|
|
{
|
|
|
|
return atomic_read(&fence->pending) <= 0;
|
|
|
|
}
|
|
|
|
|
2016-09-09 21:11:41 +08:00
|
|
|
static inline bool i915_sw_fence_done(const struct i915_sw_fence *fence)
|
|
|
|
{
|
|
|
|
return atomic_read(&fence->pending) < 0;
|
|
|
|
}
|
|
|
|
|
2016-10-28 20:58:25 +08:00
|
|
|
static inline void i915_sw_fence_wait(struct i915_sw_fence *fence)
|
|
|
|
{
|
|
|
|
wait_event(fence->wait, i915_sw_fence_done(fence));
|
|
|
|
}
|
|
|
|
|
2019-08-18 07:25:11 +08:00
|
|
|
static inline void
|
|
|
|
i915_sw_fence_set_error_once(struct i915_sw_fence *fence, int error)
|
|
|
|
{
|
|
|
|
cmpxchg(&fence->error, 0, error);
|
|
|
|
}
|
|
|
|
|
2016-09-09 21:11:41 +08:00
|
|
|
#endif /* _I915_SW_FENCE_H_ */
|