Skip to content

Commit 7da4480

Browse files
liming011AlisonSchofield
authored andcommitted
cxl/list: show region locked status to user
A region is not allowed to be destroyed if it is in locked status. cxl destroy-region command will fail and ask user to try it again as root, but it is not the real reason and it will confuse user if user is already a root user. This patch will show the region locked status in region information and output an explicit log to user that operation is not permitted like below. Before the patch: cxl list -ir region0 [ { "region":"region0", "resource":53955526656, "size":536870912, "type":"ram", "interleave_ways":2, "interleave_granularity":256, "decode_state":"commit", "state":"disabled", "qos_class_mismatch":true } ] cxl destroy-region region0 libcxl: write_attr: failed to write 0 to /sys/bus/cxl/devices/root0/decoder0.0/region0/commit: Operation not permitted hint: try running as root or using sudo cxl region: destroy_region: region0: failed to reset decode: Operation not permitted cxl region: decoder_region_action: region0: failed: Operation not permitted cxl region: region_action: one or more failures, last failure: Operation not permitted cxl region: cmd_destroy_region: destroyed 0 regions After the patch: cxl list -ir region0 [ { "region":"region0", "resource":53955526656, "size":536870912, "type":"ram", "interleave_ways":2, "interleave_granularity":256, "decode_state":"commit", "state":"disabled", "locked":true, "qos_class_mismatch":true } ] cxl destroy-region region0 cxl region: destroy_region: region0: Cannot destroy a locked region. cxl region: decoder_region_action: region0: failed: Operation not permitted cxl region: region_action: one or more failures, last failure: Operation not permitted cxl region: cmd_destroy_region: destroyed 0 regions [ as: return enum cxl_region_locked_state from accessor, map sysfs values explicitly to enum states, replace enum-to-bool conversion with explicit enum comparisons ] Signed-off-by: Li Ming <ming.li@zohomail.com> Reviewed-by: Dave Jiang <dave.jiang@intel.com> Tested-by: Alison Schofield <alison.schofield@intel.com> Link: https://lore.kernel.org/r/20260403050459.2236-1-ming.li@zohomail.com Signed-off-by: Alison Schofield <alison.schofield@intel.com>
1 parent 38068a5 commit 7da4480

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

cxl/json.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ struct json_object *util_cxl_region_to_json(struct cxl_region *region,
10071007
{
10081008
enum cxl_decoder_mode mode = cxl_region_get_mode(region);
10091009
const char *devname = cxl_region_get_devname(region);
1010+
enum cxl_region_locked_state state;
10101011
struct json_object *jregion, *jobj;
10111012
u64 val;
10121013

@@ -1077,6 +1078,13 @@ struct json_object *util_cxl_region_to_json(struct cxl_region *region,
10771078
json_object_object_add(jregion, "state", jobj);
10781079
}
10791080

1081+
state = cxl_region_locked_state(region);
1082+
if (state != CXL_REGION_LOCKED_UNKNOWN) {
1083+
jobj = json_object_new_boolean(state == CXL_REGION_LOCKED);
1084+
if (jobj)
1085+
json_object_object_add(jregion, "locked", jobj);
1086+
}
1087+
10801088
if (flags & UTIL_JSON_MEDIA_ERRORS) {
10811089
jobj = util_cxl_poison_list_to_json(region, NULL, flags);
10821090
if (jobj)

cxl/lib/libcxl.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,12 @@ CXL_EXPORT int cxl_region_is_enabled(struct cxl_region *region)
472472
return is_enabled(path);
473473
}
474474

475+
CXL_EXPORT enum cxl_region_locked_state
476+
cxl_region_locked_state(struct cxl_region *region)
477+
{
478+
return region->locked;
479+
}
480+
475481
CXL_EXPORT bool cxl_region_qos_class_mismatch(struct cxl_region *region)
476482
{
477483
struct cxl_decoder *root_decoder = cxl_region_get_decoder(region);
@@ -689,6 +695,13 @@ static void *add_cxl_region(void *parent, int id, const char *cxlregion_base)
689695
if (sysfs_read_attr(ctx, path, buf) == 0)
690696
region->module = util_modalias_to_module(ctx, buf);
691697

698+
sprintf(path, "%s/locked", cxlregion_base);
699+
if (sysfs_read_attr(ctx, path, buf) < 0)
700+
region->locked = CXL_REGION_LOCKED_UNKNOWN;
701+
else
702+
region->locked = strtoul(buf, NULL, 0) ?
703+
CXL_REGION_LOCKED : CXL_REGION_UNLOCKED;
704+
692705
cxl_region_foreach_safe(decoder, region_dup, _r)
693706
if (region_dup->id == region->id) {
694707
list_del_from(&decoder->regions, &region_dup->list);

cxl/lib/libcxl.sym

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,8 @@ global:
315315
cxl_memdev_clear_poison;
316316
cxl_debugfs_exists;
317317
} LIBCXL_10;
318+
319+
LIBCXL_12 {
320+
global:
321+
cxl_region_locked_state;
322+
} LIBCXL_11;

cxl/lib/private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct cxl_region {
189189
u64 start;
190190
u64 size;
191191
u64 cache_size;
192+
enum cxl_region_locked_state locked;
192193
unsigned int interleave_ways;
193194
unsigned int interleave_granularity;
194195
enum cxl_decode_state decode_state;

cxl/libcxl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,15 @@ int cxl_region_decode_commit(struct cxl_region *region);
354354
int cxl_region_decode_reset(struct cxl_region *region);
355355
bool cxl_region_qos_class_mismatch(struct cxl_region *region);
356356

357+
enum cxl_region_locked_state {
358+
CXL_REGION_LOCKED_UNKNOWN = -1,
359+
CXL_REGION_UNLOCKED,
360+
CXL_REGION_LOCKED,
361+
};
362+
363+
enum cxl_region_locked_state
364+
cxl_region_locked_state(struct cxl_region *region);
365+
357366
#define cxl_region_foreach(decoder, region) \
358367
for (region = cxl_region_get_first(decoder); region != NULL; \
359368
region = cxl_region_get_next(region))

cxl/region.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,12 @@ static int destroy_region(struct cxl_region *region)
837837
unsigned int ways, i;
838838
int rc;
839839

840+
if (cxl_region_locked_state(region) == CXL_REGION_LOCKED) {
841+
log_err(&rl, "%s: cannot destroy a locked region\n",
842+
devname);
843+
return -EPERM;
844+
}
845+
840846
/* First, unbind/disable the region if needed */
841847
if (cxl_region_is_enabled(region)) {
842848
if (param.force) {

0 commit comments

Comments
 (0)