hulk inclusion category: bugfix bugzilla: https://atomgit.com/openeuler/kernel/issues/8697 ---------------------------------------- When AI tasks request device memory, the timing of this request may precede the creation of the XSched Context. This can lead to failures in device memory allocation due to the absence of a valid context. To address this issue, this commit introduces a check during device memory allocation to verify the existence of the XSched Context. If the context is not found, it will be created prior to proceeding with the memory allocation. This ensures that all subsequent operations have access to a valid XSched Context, thereby preventing memory allocation failures caused by missing contexts. Fixes: 0bfa64812e4b ("xsched/dmem: introduce xsched_dmem_alloc()") Signed-off-by: Liu Kai <liukai284@huawei.com> --- include/uapi/linux/xcu_vstream.h | 2 +- kernel/xsched/dmem.c | 1 + kernel/xsched/vstream.c | 18 +++++++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/xcu_vstream.h b/include/uapi/linux/xcu_vstream.h index 14552fae2159..52abcae44928 100644 --- a/include/uapi/linux/xcu_vstream.h +++ b/include/uapi/linux/xcu_vstream.h @@ -22,7 +22,7 @@ typedef enum VSTREAM_COMMAND { VSTREAM_ALLOC = 0, VSTREAM_FREE, VSTREAM_KICK, - VSTREAM_ALLOC_HBM, + VSTREAM_HBM_ALLOC, VSTREAM_HBM_FREE, MAX_COMMAND } vstream_command_t; diff --git a/kernel/xsched/dmem.c b/kernel/xsched/dmem.c index 530fe77ad331..b202aac6a693 100644 --- a/kernel/xsched/dmem.c +++ b/kernel/xsched/dmem.c @@ -21,6 +21,7 @@ #include <linux/xsched.h> #include <linux/types.h> #include <linux/cgroup_dmem.h> +#include <linux/sizes.h> static struct dmem_cgroup_region *hbm_regions[XSCHED_NR_CUS]; diff --git a/kernel/xsched/vstream.c b/kernel/xsched/vstream.c index 7b769a2e2545..f9998912247e 100644 --- a/kernel/xsched/vstream.c +++ b/kernel/xsched/vstream.c @@ -623,17 +623,29 @@ int vstream_kick(struct vstream_args *arg) static int vstream_hbm_alloc(struct vstream_args *arg) { + vstream_info_t vstream_info; struct xsched_cu *xcu_found; struct xsched_context *ctx; + int ret; xcu_found = xcu_find(XCU_TYPE_XPU, arg->dev_id, arg->channel_id); if (!xcu_found) return -EINVAL; - ctx = ctx_find_by_tgid_and_xcu(current->tgid, xcu_found); - if (!ctx) { + vstream_info.tgid = current->tgid; + vstream_info.xcu = xcu_found; + vstream_info.dev_id = arg->dev_id; + vstream_info.channel_id = arg->channel_id; + vstream_info.fd = arg->fd; + + /* it will either allocate or find a context */ + mutex_lock(&xcu_found->ctx_list_lock); + ret = alloc_ctx_from_vstream(&vstream_info, &ctx); + mutex_unlock(&xcu_found->ctx_list_lock); + + if (ret) { XSCHED_ERR("Failed to find a context for HBM alloc"); - return -EINVAL; + return ret; } return xsched_dmem_alloc(ctx, arg); -- 2.34.1