Add a delay, configurable via debugfs (default 34ms), to disable scheduling of a context after the pin count goes to zero. Disable scheduling is a costly operation as it requires synchronizing with the GuC. So the idea is that a delay allows the user to resubmit something before doing this operation. This delay is only done if the context isn't closed and less than a given threshold (default is 3/4) of the guc_ids are in use. Alan Previn: Matt Brost first introduced this patch back in Oct 2021. However no real world workload with measured performance impact was available to prove the intended results. Today, this series is being republished in response to a real world workload that benefited greatly from it along with measured performance improvement. Workload description: 36 containers were created on a DG2 device where each container was performing a combination of 720p 3d game rendering and 30fps video encoding. The workload density was configured in a way that guaranteed each container to ALWAYS be able to render and encode no less than 30fps with a predefined maximum render + encode latency time. That means the totality of all 36 containers and their workloads were not saturating the engines to their max (in order to maintain just enough headrooom to meet the min fps and max latencies of incoming container submissions). Problem statement: It was observed that the CPU core processing the i915 soft IRQ work was experiencing severe load. Using tracelogs and an instrumentation patch to count specific i915 IRQ events, it was confirmed that the majority of the CPU cycles were caused by the gen11_other_irq_handler() -> guc_irq_handler() code path. The vast majority of the cycles was determined to be processing a specific G2H IRQ: i.e. INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_DONE. These IRQs are sent by GuC in response to i915 KMD sending H2G requests: INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET. Those H2G requests are sent whenever a context goes idle so that we can unpin the context from GuC. The high CPU utilization % symptom was limiting density scaling. Root Cause Analysis: Because the incoming execution buffers were spread across 36 different containers (each with multiple contexts) but the system in totality was NOT saturated to the max, it was assumed that each context was constantly idling between submissions. This was causing a thrashing of unpinning contexts from GuC at one moment, followed quickly by repinning them due to incoming workload the very next moment. These event-pairs were being triggered across multiple contexts per container, across all containers at the rate of > 30 times per sec per context. Metrics: When running this workload without this patch, we measured an average of ~69K INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_DONE events every 10 seconds or ~10 million times over ~25+ mins. With this patch, the count reduced to ~480 every 10 seconds or about ~28K over ~10 mins. The improvement observed is ~99% for the average counts per 10 seconds. Design awareness: Selftest impact. As temporary WA disable this feature for the selftests. Selftests are very timing sensitive and any change in timing can cause failure. A follow up patch will fixup the selftests to understand this delay. Design awareness: Race between guc_request_alloc and guc_context_close. If a context close is issued while there is a request submission in flight and a delayed schedule disable is pending, guc_context_close and guc_request_alloc will race to cancel the delayed disable. To close the race, make sure that guc_request_alloc waits for guc_context_close to finish running before checking any state. Design awareness: GT Reset event. If a gt reset is triggered, as preparation steps, add an additional step to ensure all contexts that have a pending delay-disable-schedule task be flushed of it. Move them directly into the closed state after cancelling the worker. This is okay because the existing flow flushes all yet-to-arrive G2H's dropping them anyway. Signed-off-by: Matthew Brost <matthew.brost@intel.com> Signed-off-by: Alan Previn <alan.previn.teres.alexis@intel.com> Signed-off-by: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com> Reviewed-by: John Harrison <John.C.Harrison@Intel.com> Signed-off-by: John Harrison <John.C.Harrison@Intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20221006225121.826257-2-alan.previn.teres.alexis@intel.com
381 lines
9.8 KiB
C
381 lines
9.8 KiB
C
/* SPDX-License-Identifier: MIT */
|
|
/*
|
|
* Copyright © 2019 Intel Corporation
|
|
*/
|
|
|
|
#ifndef __INTEL_CONTEXT_H__
|
|
#define __INTEL_CONTEXT_H__
|
|
|
|
#include <linux/bitops.h>
|
|
#include <linux/lockdep.h>
|
|
#include <linux/types.h>
|
|
|
|
#include "i915_active.h"
|
|
#include "i915_drv.h"
|
|
#include "intel_context_types.h"
|
|
#include "intel_engine_types.h"
|
|
#include "intel_ring_types.h"
|
|
#include "intel_timeline_types.h"
|
|
#include "i915_trace.h"
|
|
|
|
#define CE_TRACE(ce, fmt, ...) do { \
|
|
const struct intel_context *ce__ = (ce); \
|
|
ENGINE_TRACE(ce__->engine, "context:%llx " fmt, \
|
|
ce__->timeline->fence_context, \
|
|
##__VA_ARGS__); \
|
|
} while (0)
|
|
|
|
#define INTEL_CONTEXT_BANNED_PREEMPT_TIMEOUT_MS (1)
|
|
|
|
struct i915_gem_ww_ctx;
|
|
|
|
void intel_context_init(struct intel_context *ce,
|
|
struct intel_engine_cs *engine);
|
|
void intel_context_fini(struct intel_context *ce);
|
|
|
|
void i915_context_module_exit(void);
|
|
int i915_context_module_init(void);
|
|
|
|
struct intel_context *
|
|
intel_context_create(struct intel_engine_cs *engine);
|
|
|
|
int intel_context_alloc_state(struct intel_context *ce);
|
|
|
|
void intel_context_free(struct intel_context *ce);
|
|
|
|
int intel_context_reconfigure_sseu(struct intel_context *ce,
|
|
const struct intel_sseu sseu);
|
|
|
|
#define PARENT_SCRATCH_SIZE PAGE_SIZE
|
|
|
|
static inline bool intel_context_is_child(struct intel_context *ce)
|
|
{
|
|
return !!ce->parallel.parent;
|
|
}
|
|
|
|
static inline bool intel_context_is_parent(struct intel_context *ce)
|
|
{
|
|
return !!ce->parallel.number_children;
|
|
}
|
|
|
|
static inline bool intel_context_is_pinned(struct intel_context *ce);
|
|
|
|
static inline struct intel_context *
|
|
intel_context_to_parent(struct intel_context *ce)
|
|
{
|
|
if (intel_context_is_child(ce)) {
|
|
/*
|
|
* The parent holds ref count to the child so it is always safe
|
|
* for the parent to access the child, but the child has a
|
|
* pointer to the parent without a ref. To ensure this is safe
|
|
* the child should only access the parent pointer while the
|
|
* parent is pinned.
|
|
*/
|
|
GEM_BUG_ON(!intel_context_is_pinned(ce->parallel.parent));
|
|
|
|
return ce->parallel.parent;
|
|
} else {
|
|
return ce;
|
|
}
|
|
}
|
|
|
|
static inline bool intel_context_is_parallel(struct intel_context *ce)
|
|
{
|
|
return intel_context_is_child(ce) || intel_context_is_parent(ce);
|
|
}
|
|
|
|
void intel_context_bind_parent_child(struct intel_context *parent,
|
|
struct intel_context *child);
|
|
|
|
#define for_each_child(parent, ce)\
|
|
list_for_each_entry(ce, &(parent)->parallel.child_list,\
|
|
parallel.child_link)
|
|
#define for_each_child_safe(parent, ce, cn)\
|
|
list_for_each_entry_safe(ce, cn, &(parent)->parallel.child_list,\
|
|
parallel.child_link)
|
|
|
|
/**
|
|
* intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
|
|
* @ce - the context
|
|
*
|
|
* Acquire a lock on the pinned status of the HW context, such that the context
|
|
* can neither be bound to the GPU or unbound whilst the lock is held, i.e.
|
|
* intel_context_is_pinned() remains stable.
|
|
*/
|
|
static inline int intel_context_lock_pinned(struct intel_context *ce)
|
|
__acquires(ce->pin_mutex)
|
|
{
|
|
return mutex_lock_interruptible(&ce->pin_mutex);
|
|
}
|
|
|
|
/**
|
|
* intel_context_is_pinned - Reports the 'pinned' status
|
|
* @ce - the context
|
|
*
|
|
* While in use by the GPU, the context, along with its ring and page
|
|
* tables is pinned into memory and the GTT.
|
|
*
|
|
* Returns: true if the context is currently pinned for use by the GPU.
|
|
*/
|
|
static inline bool
|
|
intel_context_is_pinned(struct intel_context *ce)
|
|
{
|
|
return atomic_read(&ce->pin_count);
|
|
}
|
|
|
|
static inline void intel_context_cancel_request(struct intel_context *ce,
|
|
struct i915_request *rq)
|
|
{
|
|
GEM_BUG_ON(!ce->ops->cancel_request);
|
|
return ce->ops->cancel_request(ce, rq);
|
|
}
|
|
|
|
/**
|
|
* intel_context_unlock_pinned - Releases the earlier locking of 'pinned' status
|
|
* @ce - the context
|
|
*
|
|
* Releases the lock earlier acquired by intel_context_unlock_pinned().
|
|
*/
|
|
static inline void intel_context_unlock_pinned(struct intel_context *ce)
|
|
__releases(ce->pin_mutex)
|
|
{
|
|
mutex_unlock(&ce->pin_mutex);
|
|
}
|
|
|
|
int __intel_context_do_pin(struct intel_context *ce);
|
|
int __intel_context_do_pin_ww(struct intel_context *ce,
|
|
struct i915_gem_ww_ctx *ww);
|
|
|
|
static inline bool intel_context_pin_if_active(struct intel_context *ce)
|
|
{
|
|
return atomic_inc_not_zero(&ce->pin_count);
|
|
}
|
|
|
|
static inline int intel_context_pin(struct intel_context *ce)
|
|
{
|
|
if (likely(intel_context_pin_if_active(ce)))
|
|
return 0;
|
|
|
|
return __intel_context_do_pin(ce);
|
|
}
|
|
|
|
static inline int intel_context_pin_ww(struct intel_context *ce,
|
|
struct i915_gem_ww_ctx *ww)
|
|
{
|
|
if (likely(intel_context_pin_if_active(ce)))
|
|
return 0;
|
|
|
|
return __intel_context_do_pin_ww(ce, ww);
|
|
}
|
|
|
|
static inline void __intel_context_pin(struct intel_context *ce)
|
|
{
|
|
GEM_BUG_ON(!intel_context_is_pinned(ce));
|
|
atomic_inc(&ce->pin_count);
|
|
}
|
|
|
|
void __intel_context_do_unpin(struct intel_context *ce, int sub);
|
|
|
|
static inline void intel_context_sched_disable_unpin(struct intel_context *ce)
|
|
{
|
|
__intel_context_do_unpin(ce, 2);
|
|
}
|
|
|
|
static inline void intel_context_unpin(struct intel_context *ce)
|
|
{
|
|
if (!ce->ops->sched_disable) {
|
|
__intel_context_do_unpin(ce, 1);
|
|
} else {
|
|
/*
|
|
* Move ownership of this pin to the scheduling disable which is
|
|
* an async operation. When that operation completes the above
|
|
* intel_context_sched_disable_unpin is called potentially
|
|
* unpinning the context.
|
|
*/
|
|
while (!atomic_add_unless(&ce->pin_count, -1, 1)) {
|
|
if (atomic_cmpxchg(&ce->pin_count, 1, 2) == 1) {
|
|
ce->ops->sched_disable(ce);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void intel_context_enter_engine(struct intel_context *ce);
|
|
void intel_context_exit_engine(struct intel_context *ce);
|
|
|
|
static inline void intel_context_enter(struct intel_context *ce)
|
|
{
|
|
lockdep_assert_held(&ce->timeline->mutex);
|
|
if (!ce->active_count++)
|
|
ce->ops->enter(ce);
|
|
}
|
|
|
|
static inline void intel_context_mark_active(struct intel_context *ce)
|
|
{
|
|
lockdep_assert(lockdep_is_held(&ce->timeline->mutex) ||
|
|
test_bit(CONTEXT_IS_PARKING, &ce->flags));
|
|
++ce->active_count;
|
|
}
|
|
|
|
static inline void intel_context_exit(struct intel_context *ce)
|
|
{
|
|
lockdep_assert_held(&ce->timeline->mutex);
|
|
GEM_BUG_ON(!ce->active_count);
|
|
if (!--ce->active_count)
|
|
ce->ops->exit(ce);
|
|
}
|
|
|
|
static inline struct intel_context *intel_context_get(struct intel_context *ce)
|
|
{
|
|
kref_get(&ce->ref);
|
|
return ce;
|
|
}
|
|
|
|
static inline void intel_context_put(struct intel_context *ce)
|
|
{
|
|
kref_put(&ce->ref, ce->ops->destroy);
|
|
}
|
|
|
|
static inline struct intel_timeline *__must_check
|
|
intel_context_timeline_lock(struct intel_context *ce)
|
|
__acquires(&ce->timeline->mutex)
|
|
{
|
|
struct intel_timeline *tl = ce->timeline;
|
|
int err;
|
|
|
|
if (intel_context_is_parent(ce))
|
|
err = mutex_lock_interruptible_nested(&tl->mutex, 0);
|
|
else if (intel_context_is_child(ce))
|
|
err = mutex_lock_interruptible_nested(&tl->mutex,
|
|
ce->parallel.child_index + 1);
|
|
else
|
|
err = mutex_lock_interruptible(&tl->mutex);
|
|
if (err)
|
|
return ERR_PTR(err);
|
|
|
|
return tl;
|
|
}
|
|
|
|
static inline void intel_context_timeline_unlock(struct intel_timeline *tl)
|
|
__releases(&tl->mutex)
|
|
{
|
|
mutex_unlock(&tl->mutex);
|
|
}
|
|
|
|
int intel_context_prepare_remote_request(struct intel_context *ce,
|
|
struct i915_request *rq);
|
|
|
|
struct i915_request *intel_context_create_request(struct intel_context *ce);
|
|
|
|
struct i915_request *
|
|
intel_context_find_active_request(struct intel_context *ce);
|
|
|
|
static inline bool intel_context_is_barrier(const struct intel_context *ce)
|
|
{
|
|
return test_bit(CONTEXT_BARRIER_BIT, &ce->flags);
|
|
}
|
|
|
|
static inline void intel_context_close(struct intel_context *ce)
|
|
{
|
|
set_bit(CONTEXT_CLOSED_BIT, &ce->flags);
|
|
|
|
if (ce->ops->close)
|
|
ce->ops->close(ce);
|
|
}
|
|
|
|
static inline bool intel_context_is_closed(const struct intel_context *ce)
|
|
{
|
|
return test_bit(CONTEXT_CLOSED_BIT, &ce->flags);
|
|
}
|
|
|
|
static inline bool intel_context_has_inflight(const struct intel_context *ce)
|
|
{
|
|
return test_bit(COPS_HAS_INFLIGHT_BIT, &ce->ops->flags);
|
|
}
|
|
|
|
static inline bool intel_context_use_semaphores(const struct intel_context *ce)
|
|
{
|
|
return test_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
|
|
}
|
|
|
|
static inline void intel_context_set_use_semaphores(struct intel_context *ce)
|
|
{
|
|
set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
|
|
}
|
|
|
|
static inline void intel_context_clear_use_semaphores(struct intel_context *ce)
|
|
{
|
|
clear_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
|
|
}
|
|
|
|
static inline bool intel_context_is_banned(const struct intel_context *ce)
|
|
{
|
|
return test_bit(CONTEXT_BANNED, &ce->flags);
|
|
}
|
|
|
|
static inline bool intel_context_set_banned(struct intel_context *ce)
|
|
{
|
|
return test_and_set_bit(CONTEXT_BANNED, &ce->flags);
|
|
}
|
|
|
|
bool intel_context_ban(struct intel_context *ce, struct i915_request *rq);
|
|
|
|
static inline bool intel_context_is_schedulable(const struct intel_context *ce)
|
|
{
|
|
return !test_bit(CONTEXT_EXITING, &ce->flags) &&
|
|
!test_bit(CONTEXT_BANNED, &ce->flags);
|
|
}
|
|
|
|
static inline bool intel_context_is_exiting(const struct intel_context *ce)
|
|
{
|
|
return test_bit(CONTEXT_EXITING, &ce->flags);
|
|
}
|
|
|
|
static inline bool intel_context_set_exiting(struct intel_context *ce)
|
|
{
|
|
return test_and_set_bit(CONTEXT_EXITING, &ce->flags);
|
|
}
|
|
|
|
bool intel_context_revoke(struct intel_context *ce);
|
|
|
|
static inline bool
|
|
intel_context_force_single_submission(const struct intel_context *ce)
|
|
{
|
|
return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags);
|
|
}
|
|
|
|
static inline void
|
|
intel_context_set_single_submission(struct intel_context *ce)
|
|
{
|
|
__set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags);
|
|
}
|
|
|
|
static inline bool
|
|
intel_context_nopreempt(const struct intel_context *ce)
|
|
{
|
|
return test_bit(CONTEXT_NOPREEMPT, &ce->flags);
|
|
}
|
|
|
|
static inline void
|
|
intel_context_set_nopreempt(struct intel_context *ce)
|
|
{
|
|
set_bit(CONTEXT_NOPREEMPT, &ce->flags);
|
|
}
|
|
|
|
static inline void
|
|
intel_context_clear_nopreempt(struct intel_context *ce)
|
|
{
|
|
clear_bit(CONTEXT_NOPREEMPT, &ce->flags);
|
|
}
|
|
|
|
u64 intel_context_get_total_runtime_ns(const struct intel_context *ce);
|
|
u64 intel_context_get_avg_runtime_ns(struct intel_context *ce);
|
|
|
|
static inline u64 intel_context_clock(void)
|
|
{
|
|
/* As we mix CS cycles with CPU clocks, use the raw monotonic clock. */
|
|
return ktime_get_raw_fast_ns();
|
|
}
|
|
|
|
#endif /* __INTEL_CONTEXT_H__ */
|