From 9a0a89520e8b57bd63a9343fe3cdc56c61c41f6d Mon Sep 17 00:00:00 2001 From: Lei Feng Date: Wed, 19 Oct 2022 12:10:23 +0800 Subject: [PATCH] LU-16251 obdclass: fill jobid in a safe way jobid_interpret_string() does not fill jobid in an atomic way. So in lustre_get_jobid() give it a buffer first, then copy the buffer to jobid as a whole. Signed-off-by: Lei Feng Change-Id: Ib8f6aaa93df31867982a0d142f33d7374a27234f Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/48915 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Jian Yu Reviewed-by: Oleg Drokin --- lustre/obdclass/jobid.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lustre/obdclass/jobid.c b/lustre/obdclass/jobid.c index 207a88b..ab35998 100644 --- a/lustre/obdclass/jobid.c +++ b/lustre/obdclass/jobid.c @@ -475,7 +475,9 @@ static int jobid_should_free_item(void *obj, void *data) static bool jobid_name_is_valid(char *jobid) { const char *const lustre_reserved[] = { "ll_ping", "ptlrpc", - "ldlm", "ll_sa", NULL }; + "ldlm", "ll_sa", "kworker", + "kswapd", "writeback", "irq", + "ksoftirq", NULL }; int i; if (jobid[0] == '\0') @@ -862,6 +864,8 @@ static struct cfs_hash_ops jobid_hash_ops = { */ int lustre_get_jobid(char *jobid, size_t joblen) { + char id[LUSTRE_JOBID_SIZE] = ""; + int len = min_t(int, joblen, LUSTRE_JOBID_SIZE); int rc = 0; ENTRY; @@ -874,11 +878,14 @@ int lustre_get_jobid(char *jobid, size_t joblen) if (strcmp(obd_jobid_var, JOBSTATS_DISABLE) == 0) { /* Jobstats isn't enabled */ memset(jobid, 0, joblen); - } else if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) == 0) { + RETURN(0); + } + + if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) == 0) { /* Whole node dedicated to single job */ - rc = jobid_interpret_string(obd_jobid_name, jobid, joblen); + rc = jobid_interpret_string(obd_jobid_name, id, len); } else if (strcmp(obd_jobid_var, JOBSTATS_PROCNAME_UID) == 0) { - rc = jobid_interpret_string("%e.%u", jobid, joblen); + rc = jobid_interpret_string("%e.%u", id, len); } else if (strcmp(obd_jobid_var, JOBSTATS_SESSION) == 0 || jobid_name_is_valid(current->comm)) { /* @@ -889,17 +896,18 @@ int lustre_get_jobid(char *jobid, size_t joblen) */ rc = -EAGAIN; if (!strnstr(obd_jobid_name, "%j", joblen)) - rc = jobid_get_from_cache(jobid, joblen); + rc = jobid_get_from_cache(id, len); /* fall back to jobid_name if jobid_var not available */ if (rc < 0) { int rc2 = jobid_interpret_string(obd_jobid_name, - jobid, joblen); + id, len); if (!rc2) rc = 0; } } + memcpy(jobid, id, len); RETURN(rc); } EXPORT_SYMBOL(lustre_get_jobid); -- 1.8.3.1