Skip to content

Commit b81dde7

Browse files
committed
drm/i915: Allow userspace to clone contexts on creation
A usecase arose out of handling context recovery in mesa, whereby they wish to recreate a context with fresh logical state but preserving all other details of the original. Currently, they create a new context and iterate over which bits they want to copy across, but it would much more convenient if they were able to just pass in a target context to clone during creation. This essentially extends the setparam during creation to pull the details from a target context instead of the user supplied parameters. The ideal here is that we don't expose control over anything more than can be obtained via CONTEXT_PARAM. That is userspace retains explicit control over all features, and this api is just convenience. For example, you could replace struct context_param p = { .param = CONTEXT_PARAM_VM }; param.ctx_id = old_id; gem_context_get_param(&p.param); new_id = gem_context_create(); param.ctx_id = new_id; gem_context_set_param(&p.param); gem_vm_destroy(param.value); /* drop the ref to VM_ID handle */ with struct create_ext_param p = { { .name = CONTEXT_CREATE_CLONE }, .clone_id = old_id, .flags = CLONE_FLAGS_VM } new_id = gem_context_create_ext(&p); and not have to worry about stray namespace pollution etc. Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20190521211134.16117-5-chris@chris-wilson.co.uk
1 parent 8319f44 commit b81dde7

2 files changed

Lines changed: 221 additions & 0 deletions

File tree

‎drivers/gpu/drm/i915/i915_gem_context.c‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,8 +1723,214 @@ static int create_setparam(struct i915_user_extension __user *ext, void *data)
17231723
return ctx_setparam(arg->fpriv, arg->ctx, &local.param);
17241724
}
17251725

1726+
static int clone_engines(struct i915_gem_context *dst,
1727+
struct i915_gem_context *src)
1728+
{
1729+
struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
1730+
struct i915_gem_engines *clone;
1731+
bool user_engines;
1732+
unsigned long n;
1733+
1734+
clone = kmalloc(struct_size(e, engines, e->num_engines), GFP_KERNEL);
1735+
if (!clone)
1736+
goto err_unlock;
1737+
1738+
clone->i915 = dst->i915;
1739+
for (n = 0; n < e->num_engines; n++) {
1740+
if (!e->engines[n]) {
1741+
clone->engines[n] = NULL;
1742+
continue;
1743+
}
1744+
1745+
clone->engines[n] =
1746+
intel_context_create(dst, e->engines[n]->engine);
1747+
if (!clone->engines[n]) {
1748+
__free_engines(clone, n);
1749+
goto err_unlock;
1750+
}
1751+
}
1752+
clone->num_engines = n;
1753+
1754+
user_engines = i915_gem_context_user_engines(src);
1755+
i915_gem_context_unlock_engines(src);
1756+
1757+
free_engines(dst->engines);
1758+
RCU_INIT_POINTER(dst->engines, clone);
1759+
if (user_engines)
1760+
i915_gem_context_set_user_engines(dst);
1761+
else
1762+
i915_gem_context_clear_user_engines(dst);
1763+
return 0;
1764+
1765+
err_unlock:
1766+
i915_gem_context_unlock_engines(src);
1767+
return -ENOMEM;
1768+
}
1769+
1770+
static int clone_flags(struct i915_gem_context *dst,
1771+
struct i915_gem_context *src)
1772+
{
1773+
dst->user_flags = src->user_flags;
1774+
return 0;
1775+
}
1776+
1777+
static int clone_schedattr(struct i915_gem_context *dst,
1778+
struct i915_gem_context *src)
1779+
{
1780+
dst->sched = src->sched;
1781+
return 0;
1782+
}
1783+
1784+
static int clone_sseu(struct i915_gem_context *dst,
1785+
struct i915_gem_context *src)
1786+
{
1787+
struct i915_gem_engines *e = i915_gem_context_lock_engines(src);
1788+
struct i915_gem_engines *clone;
1789+
unsigned long n;
1790+
int err;
1791+
1792+
clone = dst->engines; /* no locking required; sole access */
1793+
if (e->num_engines != clone->num_engines) {
1794+
err = -EINVAL;
1795+
goto unlock;
1796+
}
1797+
1798+
for (n = 0; n < e->num_engines; n++) {
1799+
struct intel_context *ce = e->engines[n];
1800+
1801+
if (clone->engines[n]->engine->class != ce->engine->class) {
1802+
/* Must have compatible engine maps! */
1803+
err = -EINVAL;
1804+
goto unlock;
1805+
}
1806+
1807+
/* serialises with set_sseu */
1808+
err = intel_context_lock_pinned(ce);
1809+
if (err)
1810+
goto unlock;
1811+
1812+
clone->engines[n]->sseu = ce->sseu;
1813+
intel_context_unlock_pinned(ce);
1814+
}
1815+
1816+
err = 0;
1817+
unlock:
1818+
i915_gem_context_unlock_engines(src);
1819+
return err;
1820+
}
1821+
1822+
static int clone_timeline(struct i915_gem_context *dst,
1823+
struct i915_gem_context *src)
1824+
{
1825+
if (src->timeline) {
1826+
GEM_BUG_ON(src->timeline == dst->timeline);
1827+
1828+
if (dst->timeline)
1829+
i915_timeline_put(dst->timeline);
1830+
dst->timeline = i915_timeline_get(src->timeline);
1831+
}
1832+
1833+
return 0;
1834+
}
1835+
1836+
static int clone_vm(struct i915_gem_context *dst,
1837+
struct i915_gem_context *src)
1838+
{
1839+
struct i915_hw_ppgtt *ppgtt;
1840+
1841+
rcu_read_lock();
1842+
do {
1843+
ppgtt = READ_ONCE(src->ppgtt);
1844+
if (!ppgtt)
1845+
break;
1846+
1847+
if (!kref_get_unless_zero(&ppgtt->ref))
1848+
continue;
1849+
1850+
/*
1851+
* This ppgtt may have be reallocated between
1852+
* the read and the kref, and reassigned to a third
1853+
* context. In order to avoid inadvertent sharing
1854+
* of this ppgtt with that third context (and not
1855+
* src), we have to confirm that we have the same
1856+
* ppgtt after passing through the strong memory
1857+
* barrier implied by a successful
1858+
* kref_get_unless_zero().
1859+
*
1860+
* Once we have acquired the current ppgtt of src,
1861+
* we no longer care if it is released from src, as
1862+
* it cannot be reallocated elsewhere.
1863+
*/
1864+
1865+
if (ppgtt == READ_ONCE(src->ppgtt))
1866+
break;
1867+
1868+
i915_ppgtt_put(ppgtt);
1869+
} while (1);
1870+
rcu_read_unlock();
1871+
1872+
if (ppgtt) {
1873+
__assign_ppgtt(dst, ppgtt);
1874+
i915_ppgtt_put(ppgtt);
1875+
}
1876+
1877+
return 0;
1878+
}
1879+
1880+
static int create_clone(struct i915_user_extension __user *ext, void *data)
1881+
{
1882+
static int (* const fn[])(struct i915_gem_context *dst,
1883+
struct i915_gem_context *src) = {
1884+
#define MAP(x, y) [ilog2(I915_CONTEXT_CLONE_##x)] = y
1885+
MAP(ENGINES, clone_engines),
1886+
MAP(FLAGS, clone_flags),
1887+
MAP(SCHEDATTR, clone_schedattr),
1888+
MAP(SSEU, clone_sseu),
1889+
MAP(TIMELINE, clone_timeline),
1890+
MAP(VM, clone_vm),
1891+
#undef MAP
1892+
};
1893+
struct drm_i915_gem_context_create_ext_clone local;
1894+
const struct create_ext *arg = data;
1895+
struct i915_gem_context *dst = arg->ctx;
1896+
struct i915_gem_context *src;
1897+
int err, bit;
1898+
1899+
if (copy_from_user(&local, ext, sizeof(local)))
1900+
return -EFAULT;
1901+
1902+
BUILD_BUG_ON(GENMASK(BITS_PER_TYPE(local.flags) - 1, ARRAY_SIZE(fn)) !=
1903+
I915_CONTEXT_CLONE_UNKNOWN);
1904+
1905+
if (local.flags & I915_CONTEXT_CLONE_UNKNOWN)
1906+
return -EINVAL;
1907+
1908+
if (local.rsvd)
1909+
return -EINVAL;
1910+
1911+
rcu_read_lock();
1912+
src = __i915_gem_context_lookup_rcu(arg->fpriv, local.clone_id);
1913+
rcu_read_unlock();
1914+
if (!src)
1915+
return -ENOENT;
1916+
1917+
GEM_BUG_ON(src == dst);
1918+
1919+
for (bit = 0; bit < ARRAY_SIZE(fn); bit++) {
1920+
if (!(local.flags & BIT(bit)))
1921+
continue;
1922+
1923+
err = fn[bit](dst, src);
1924+
if (err)
1925+
return err;
1926+
}
1927+
1928+
return 0;
1929+
}
1930+
17261931
static const i915_user_extension_fn create_extensions[] = {
17271932
[I915_CONTEXT_CREATE_EXT_SETPARAM] = create_setparam,
1933+
[I915_CONTEXT_CREATE_EXT_CLONE] = create_clone,
17281934
};
17291935

17301936
static bool client_is_banned(struct drm_i915_file_private *file_priv)

‎include/uapi/drm/i915_drm.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,21 @@ struct drm_i915_gem_context_create_ext_setparam {
16241624
struct drm_i915_gem_context_param param;
16251625
};
16261626

1627+
struct drm_i915_gem_context_create_ext_clone {
1628+
#define I915_CONTEXT_CREATE_EXT_CLONE 1
1629+
struct i915_user_extension base;
1630+
__u32 clone_id;
1631+
__u32 flags;
1632+
#define I915_CONTEXT_CLONE_ENGINES (1u << 0)
1633+
#define I915_CONTEXT_CLONE_FLAGS (1u << 1)
1634+
#define I915_CONTEXT_CLONE_SCHEDATTR (1u << 2)
1635+
#define I915_CONTEXT_CLONE_SSEU (1u << 3)
1636+
#define I915_CONTEXT_CLONE_TIMELINE (1u << 4)
1637+
#define I915_CONTEXT_CLONE_VM (1u << 5)
1638+
#define I915_CONTEXT_CLONE_UNKNOWN -(I915_CONTEXT_CLONE_VM << 1)
1639+
__u64 rsvd;
1640+
};
1641+
16271642
struct drm_i915_gem_context_destroy {
16281643
__u32 ctx_id;
16291644
__u32 pad;

0 commit comments

Comments
 (0)