lib: sbi: Ensure SBI extension is available
PATCH 1/1 lib: sbi: Ensure SBI extension is available
Ensure attempts to invoke SBI extension functions fail with SBI_ERR_NOT_SUPPORTED when the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.
--- include/sbi/sbi_ecall.h | 6 ++++++ lib/sbi/sbi_ecall.c | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-)
include/sbi/sbi_ecall.h +6 -0
@@ -20,10 +20,16 @@struct sbi_trap_regs ;struct sbi_trap_info ; +enum sbi_ext_status { +SBI_EXT_AVAILABLE =1 , +SBI_EXT_UNAVAILABLE , +}; +struct sbi_ecall_extension {struct sbi_dlist head;unsigned long extid_start;unsigned long extid_end; +enum sbi_ext_status status;int (* probe)(unsigned long extid,unsigned long *out_val); int (* handle)(unsigned long extid, unsigned long funcid, const struct sbi_trap_regs *regs,
lib/sbi/sbi_ecall.c +14 -4
@@ -42,16 +42,26 @@ static SBI_LIST_HEAD(ecall_exts_list);struct sbi_ecall_extension *sbi_ecall_find_extension (unsigned long extid) { -struct sbi_ecall_extension *t, *ret =NULL ; +struct sbi_ecall_extension *t; +unsigned long out_val;sbi_list_for_each_entry (t, &ecall_exts_list, head) {if (t->extid_start <= extid && extid <= t->extid_end) { - ret = t; -break ; +if (t->status ==SBI_EXT_AVAILABLE ) +return t; +if (t->status ==SBI_EXT_UNAVAILABLE ) +return NULL ; +if (t->probe && (t->probe (extid, &out_val) || !out_val)) { + t->status =SBI_EXT_UNAVAILABLE ; +return NULL ; + } + + t->status =SBI_EXT_AVAILABLE ; +return t; } } -return ret; +return NULL ; }int sbi_ecall_register_extension (struct sbi_ecall_extension *ext)
Signature · 1 line
2.39.2
PATCH 1/1 lib: sbi: Ensure SBI extension is available
? 2023-04-26???? 19:27 +0200?Andrew Jones???
Quoted 6 lines
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.
Adding this state may cause errors. For example: the id numberof an extension is from extid_start to extid_end, which has an unused ID. Searching for an ID in this extension will return NULL
if an unused ID is searched first.
Regards,
Xiang W
Quoted 67 lines
Signed-off-by Andrew Jones <[email protected]>---
?include/sbi/sbi_ecall.h |? 6 ++++++ ?lib/sbi/sbi_ecall.c???? | 18 ++++++++++++++---- ?2 files changed, 20 insertions(+), 4 deletions(-)include/sbi/sbi_ecall.h +0
-0@@ -20,10 +20,16 @@ ?struct sbi_trap_regs; ?struct sbi_trap_info;?
include/sbi/sbi_ecall.h +5
-0+enum sbi_ext_status { +???????SBI_EXT_AVAILABLE = 1, +???????SBI_EXT_UNAVAILABLE, +}; +?struct
sbi_ecall_extension{
????????structsbi_dlisthead;
????????unsigned longextid_start;
????????unsigned longextid_end;
+???????enumsbi_ext_statusstatus;
????????int (* probe)(unsigned long extid, unsigned long*out_val); ????????int (* handle)(unsigned long extid, unsigned long funcid, ?????????????????????? const structsbi_trap_regs*regs,lib/sbi/sbi_ecall.c +0
-0@@ -42,16 +42,26 @@ static SBI_LIST_HEAD(ecall_exts_list);?
?structsbi_ecall_extension*sbi_ecall_find_extension(unsigned long extid) ?{
-???????structsbi_ecall_extension*t, *ret = NULL;
+???????structsbi_ecall_extension*t;
+???????unsigned longout_val;
?
????????sbi_list_for_each_entry(t,&ecall_exts_list, head) { ????????????????if (t->extid_start<= extid && extid <=t->extid_end) { -???????????????????????ret = t;
-???????????????????????break;
+???????????????????????if (t->status==SBI_EXT_AVAILABLE) +???????????????????????????????return t;
+???????????????????????if (t->status==SBI_EXT_UNAVAILABLE) +???????????????????????????????return NULL; +???????????????????????if (t->probe&& (t->probe(extid, &out_val)|| !out_val)) { +???????????????????????????????t->status =SBI_EXT_UNAVAILABLE; +???????????????????????????????return NULL; +???????????????????????}
+
+???????????????????????t->status =SBI_EXT_AVAILABLE; +???????????????????????return t;
????????????????}
????????}
?
-???????return ret;
+???????return NULL;
?}
?
?intsbi_ecall_register_extension(struct sbi_ecall_extension *ext)Signature · 1 line
2.39.2
PATCH 1/1 lib: sbi: Ensure SBI extension is available
Quoted 11 lines · Xiang W
? 2023-04-26???? 19:27 +0200?Andrew Jones???
Quoted 6 lines
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Adding this state may cause errors. For example: the id numberof an extension is from
extid_starttoextid_end, which has an unused ID. Searching for an ID in this extension will return NULL
if an unused ID is searched first.
Hi Xiang,
You're right. Either sbi_ecall_extension::status needs to be a function of extid, like probe is, or we should just always call probe (when it's available) and leave any optimizations to the individual probe functions. How about this?
lib/sbi/sbi_ecall.c +6 -4
@@ -42,16 +42,18 @@ static SBI_LIST_HEAD(ecall_exts_list);struct sbi_ecall_extension *sbi_ecall_find_extension (unsigned long extid) { -struct sbi_ecall_extension *t, *ret =NULL ; +struct sbi_ecall_extension *t; +unsigned long out_val;sbi_list_for_each_entry (t, &ecall_exts_list, head) {if (t->extid_start <= extid && extid <= t->extid_end) { - ret = t; -break ; +if (t->probe && (t->probe (extid, &out_val) || !out_val)) +return NULL ; +return t; } } -return ret; +return NULL ; }int sbi_ecall_register_extension (struct sbi_ecall_extension *ext)
Thanks,
drew
Quoted 72 lines
Regards,
Xiang WQuoted 67 lines
Signed-off-by Andrew Jones <[email protected]>---
?include/sbi/sbi_ecall.h |? 6 ++++++ ?lib/sbi/sbi_ecall.c???? | 18 ++++++++++++++---- ?2 files changed, 20 insertions(+), 4 deletions(-)include/sbi/sbi_ecall.h +0
-0@@ -20,10 +20,16 @@ ?struct sbi_trap_regs; ?struct sbi_trap_info;?
include/sbi/sbi_ecall.h +5
-0+enum sbi_ext_status { +???????SBI_EXT_AVAILABLE = 1, +???????SBI_EXT_UNAVAILABLE, +}; +?struct
sbi_ecall_extension{
????????structsbi_dlisthead;
????????unsigned longextid_start;
????????unsigned longextid_end;
+???????enumsbi_ext_statusstatus;
????????int (* probe)(unsigned long extid, unsigned long*out_val); ????????int (* handle)(unsigned long extid, unsigned long funcid, ?????????????????????? const structsbi_trap_regs*regs,lib/sbi/sbi_ecall.c +0
-0@@ -42,16 +42,26 @@ static SBI_LIST_HEAD(ecall_exts_list);?
?structsbi_ecall_extension*sbi_ecall_find_extension(unsigned long extid) ?{
-???????structsbi_ecall_extension*t, *ret = NULL;
+???????structsbi_ecall_extension*t;
+???????unsigned longout_val;
?
????????sbi_list_for_each_entry(t,&ecall_exts_list, head) { ????????????????if (t->extid_start<= extid && extid <=t->extid_end) { -???????????????????????ret = t;
-???????????????????????break;
+???????????????????????if (t->status==SBI_EXT_AVAILABLE) +???????????????????????????????return t;
+???????????????????????if (t->status==SBI_EXT_UNAVAILABLE) +???????????????????????????????return NULL; +???????????????????????if (t->probe&& (t->probe(extid, &out_val)|| !out_val)) { +???????????????????????????????t->status =SBI_EXT_UNAVAILABLE; +???????????????????????????????return NULL; +???????????????????????}
+
+???????????????????????t->status =SBI_EXT_AVAILABLE; +???????????????????????return t;
????????????????}
????????}
?
-???????return ret;
+???????return NULL;
?}
?
?intsbi_ecall_register_extension(struct sbi_ecall_extension *ext)Signature · 1 line
2.39.2
PATCH 1/1 lib: sbi: Ensure SBI extension is available
Quoted 46 lines · Andrew Jones
z4xshfbykgvcgcozn at rqv44g76lo4w>
References: <[email protected]>
<[email protected]>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
In-Reply-To: <[email protected]>Quoted 11 lines · Xiang W
? 2023-04-26???? 19:27 +0200?Andrew Jones???
Quoted 6 lines
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Adding this state may cause errors. For example: the id numberof an extension is from
extid_starttoextid_end, which has an unused ID. Searching for an ID in this extension will return NULL
if an unused ID is searched first.Hi Xiang,
You're right. Either
sbi_ecall_extension::statusneeds to be a function of extid, like probe is, or we should just always call probe (when it's available) and leave any optimizations to the individual probe functions. How about this?lib/sbi/sbi_ecall.c +3
-3@@ -42,16 +42,18 @@ static SBI_LIST_HEAD(ecall_exts_list);struct sbi_ecall_extension *sbi_ecall_find_extension (unsigned long extid) { -struct sbi_ecall_extension *t, *ret =NULL ; +struct sbi_ecall_extension *t; +unsigned long out_val;sbi_list_for_each_entry (t, &ecall_exts_list, head) {if (t->extid_start <= extid && extid <= t->extid_end) { - ret = t; -break ; +if (t->probe && (t->probe (extid, &out_val) || !out_val))
lib/sbi/sbi_ecall.c static SBI_LIST_HEAD(ecall_exts_list); · line 59
Quoted 10 lines
lib/sbi/sbi_ecall.c +3
-1+return NULL ; +return t; } } -return ret; +return NULL ; }int sbi_ecall_register_extension (struct sbi_ecall_extension *ext)
And, as a separate patch, to optimize probing of srst, which has a single extid for its range, and therefore doesn't care about it in its probe,
lib/sbi/sbi_ecall_srst.c +9 -1
@@ -50,8 +50,15 @@ static int sbi_ecall_srst_handler(unsigned long extid, unsigned long funcid,static int sbi_ecall_srst_probe (unsigned long extid,unsigned long *out_val) { +static bool probed = false; +static unsigned long val;u32 type, count =0 ; +if (probed) { + *out_val = val; +return 0 ; + } +/* * At least one standard reset types should be supported by * the platform for SBI SRST extension to be usable.
@@ -63,7 +70,8 @@ static int sbi_ecall_srst_probe(unsigned long extid, unsigned long *out_val) count++; } - *out_val = (count) ?1 :0 ; + *out_val = val = (count) ?1 :0 ; + probed = true;return 0 ; }
And susp can be optimized in exactly the same way.
Thanks,
drew
PATCH 1/1 lib: sbi: Ensure SBI extension is available
? 2023-04-27???? 09:18 +0200?Andrew Jones???
Quoted 91 lines · Andrew Jones
Quoted 48 lines
z4xshfbykgvcgcozn at rqv44g76lo4w>
References: <[email protected]> ?<[email protected]>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
In-Reply-To: <[email protected]>Quoted 11 lines · Xiang W
? 2023-04-26???? 19:27 +0200?Andrew Jones???
Quoted 6 lines
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Adding this state may cause errors. For example: the id numberof an extension is from
extid_starttoextid_end, which has an unused ID. Searching for an ID in this extension will return NULL
if an unused ID is searched first.Hi Xiang,
You're right. Either
sbi_ecall_extension::statusneeds to be a function of extid, like probe is, or we should just always call probe (when it's available) and leave any optimizations to the individual probe functions. How about this?lib/sbi/sbi_ecall.c +0
-0@@ -42,16 +42,18 @@ static SBI_LIST_HEAD(ecall_exts_list);?
?structsbi_ecall_extension*sbi_ecall_find_extension(unsigned long extid) ?{
-???????structsbi_ecall_extension*t, *ret = NULL;
+???????structsbi_ecall_extension*t;
+???????unsigned longout_val;
?
????????sbi_list_for_each_entry(t,&ecall_exts_list, head) { ????????????????if (t->extid_start<= extid && extid <=t->extid_end) { -???????????????????????ret = t;
-???????????????????????break;
+???????????????????????if (t->probe&& (t->probe(extid, &out_val)|| !out_val)) +???????????????????????????????return NULL; +???????????????????????return t;Quoted 8 lines
????????????????}
????????}
?
-???????return ret;
+???????return NULL;
?}
?
?intsbi_ecall_register_extension(struct sbi_ecall_extension *ext)And, as a separate patch, to optimize probing of srst, which has a single extid for its range, and therefore doesn't care about it in its probe,
lib/sbi/sbi_ecall_srst.c +0
-0@@ -50,8 +50,15 @@ static int sbi_ecall_srst_handler(unsigned long extid, unsigned long funcid,?
?static intsbi_ecall_srst_probe(unsigned long extid, unsigned long *out_val)?{
+???????static bool probed = false;
+???????static unsigned long val;
????????u32 type, count = 0;
?
+???????if (probed) {
+???????????????*out_val = val;
+???????????????return 0;
+???????}
+
????????/*
???????? * At least one standard reset types should be supported by ???????? * the platform for SBI SRST extension to be usable.lib/sbi/sbi_ecall_srst.c +0
-0@@ -63,7 +70,8 @@ static int sbi_ecall_srst_probe(unsigned long extid, unsigned long *out_val) ????????????????????????count++; ????????}?
-???????*out_val = (count) ? 1 : 0;
+???????*out_val = val = (count) ? 1 : 0;
+???????probed = true;
????????return 0;
?}
Quoted 5 lines
And susp can be optimized in exactly the same way.
Thanks,
drew
This can be a generic optimization. Since most extensions have only one extension id, here is my suggestion
lib/sbi/sbi_ecall.c +14 -4
@@ -42,16 +42,26 @@ static SBI_LIST_HEAD(ecall_exts_list);struct sbi_ecall_extension *sbi_ecall_find_extension (unsigned long extid) { -struct sbi_ecall_extension *t, *ret =NULL ; +struct sbi_ecall_extension *t;sbi_list_for_each_entry (t, &ecall_exts_list, head) {if (t->extid_start <= extid && extid <= t->extid_end) { - ret = t; -break ; +if (t->extid_start == t->extid_end) { +if (t->status ==SBI_EXT_AVAILABLE ) +return t; +if (t->status ==SBI_EXT_UNAVAILABLE ) +return NULL ; + } +if (t->probe && (t->probe (extid, &out_val) || !out_val)) { + t->status =SBI_EXT_UNAVAILABLE ; + return NULL + } + t->status =SBI_EXT_AVAILABLE ; +return t; } } -return ret; +return NULL ; }
PATCH 1/1 lib: sbi: Ensure SBI extension is available
Quoted 125 lines · Xiang W
? 2023-04-27???? 09:18 +0200?Andrew Jones???
Quoted 91 lines · Andrew Jones
Quoted 48 lines
z4xshfbykgvcgcozn at rqv44g76lo4w>
References: <[email protected]> ?<[email protected]>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
In-Reply-To: <[email protected]>Quoted 11 lines · Xiang W
? 2023-04-26???? 19:27 +0200?Andrew Jones???
Quoted 6 lines
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Adding this state may cause errors. For example: the id numberof an extension is from
extid_starttoextid_end, which has an unused ID. Searching for an ID in this extension will return NULL
if an unused ID is searched first.Hi Xiang,
You're right. Either
sbi_ecall_extension::statusneeds to be a function of extid, like probe is, or we should just always call probe (when it's available) and leave any optimizations to the individual probe functions. How about this?lib/sbi/sbi_ecall.c +0
-0@@ -42,16 +42,18 @@ static SBI_LIST_HEAD(ecall_exts_list);?
?structsbi_ecall_extension*sbi_ecall_find_extension(unsigned long extid) ?{
-???????structsbi_ecall_extension*t, *ret = NULL;
+???????structsbi_ecall_extension*t;
+???????unsigned longout_val;
?
????????sbi_list_for_each_entry(t,&ecall_exts_list, head) { ????????????????if (t->extid_start<= extid && extid <=t->extid_end) { -???????????????????????ret = t;
-???????????????????????break;
+???????????????????????if (t->probe&& (t->probe(extid, &out_val)|| !out_val)) +???????????????????????????????return NULL; +???????????????????????return t;Quoted 8 lines
????????????????}
????????}
?
-???????return ret;
+???????return NULL;
?}
?
?intsbi_ecall_register_extension(struct sbi_ecall_extension *ext)And, as a separate patch, to optimize probing of srst, which has a single extid for its range, and therefore doesn't care about it in its probe,
lib/sbi/sbi_ecall_srst.c +0
-0@@ -50,8 +50,15 @@ static int sbi_ecall_srst_handler(unsigned long extid, unsigned long funcid,?
?static intsbi_ecall_srst_probe(unsigned long extid, unsigned long *out_val)?{
+???????static bool probed = false;
+???????static unsigned long val;
????????u32 type, count = 0;
?
+???????if (probed) {
+???????????????*out_val = val;
+???????????????return 0;
+???????}
+
????????/*
???????? * At least one standard reset types should be supported by ???????? * the platform for SBI SRST extension to be usable.lib/sbi/sbi_ecall_srst.c +0
-0@@ -63,7 +70,8 @@ static int sbi_ecall_srst_probe(unsigned long extid, unsigned long *out_val) ????????????????????????count++; ????????}?
-???????*out_val = (count) ? 1 : 0;
+???????*out_val = val = (count) ? 1 : 0;
+???????probed = true;
????????return 0;
?}Quoted 5 lines
And susp can be optimized in exactly the same way.
Thanks,
drewThis can be a generic optimization. Since most extensions have only one extension id, here is my suggestion
lib/sbi/sbi_ecall.c +10
-3@@ -42,16 +42,26 @@ static SBI_LIST_HEAD(ecall_exts_list);struct sbi_ecall_extension *sbi_ecall_find_extension (unsigned long extid) { -struct sbi_ecall_extension *t, *ret =NULL ; +struct sbi_ecall_extension *t;sbi_list_for_each_entry (t, &ecall_exts_list, head) {if (t->extid_start <= extid && extid <= t->extid_end) { - ret = t; -break ; +if (t->extid_start == t->extid_end) { +if (t->status ==SBI_EXT_AVAILABLE ) +return t; +if (t->status ==SBI_EXT_UNAVAILABLE ) +return NULL ; + } +if (t->probe && (t->probe (extid, &out_val) || !out_val)) { + t->status =SBI_EXT_UNAVAILABLE ; +return NULL
lib/sbi/sbi_ecall.c static SBI_LIST_HEAD(ecall_exts_list); · line 66
Quoted 10 lines
lib/sbi/sbi_ecall.c +4
-1+ } + t->status =SBI_EXT_AVAILABLE ; +return t; } } -return ret; +return NULL ; }
Yeah, LGTM. v2 coming up.
Thanks!
drew
PATCH 1/1 lib: sbi: Ensure SBI extension is available
In reply to Andrew Jones? 2023-04-27???? 09:00 +0200?Andrew Jones???
Quoted 48 lines · Xiang W
Quoted 11 lines
? 2023-04-26???? 19:27 +0200?Andrew Jones???
Quoted 6 lines
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Adding this state may cause errors. For example: the id numberof an extension is from
extid_starttoextid_end, which has an unused ID. Searching for an ID in this extension will return NULL
if an unused ID is searched first.Hi Xiang,
You're right. Either
sbi_ecall_extension::statusneeds to be a function of extid, like probe is, or we should just always call probe (when it's available) and leave any optimizations to the individual probe functions. How about this?lib/sbi/sbi_ecall.c +0
-0@@ -42,16 +42,18 @@ static SBI_LIST_HEAD(ecall_exts_list);?
?structsbi_ecall_extension*sbi_ecall_find_extension(unsigned long extid) ?{
-???????structsbi_ecall_extension*t, *ret = NULL;
+???????structsbi_ecall_extension*t;
+???????unsigned longout_val;
?
????????sbi_list_for_each_entry(t,&ecall_exts_list, head) { ????????????????if (t->extid_start<= extid && extid <=t->extid_end) { -???????????????????????ret = t;
-???????????????????????break;
+???????????????????????if (t->probe&& (t->probe(extid, &out_val)|| !out_val)) +???????????????????????????????return NULL; +???????????????????????return t;
????????????????}
????????}
?
-???????return ret;
+???????return NULL;
?}
?
?intsbi_ecall_register_extension(struct sbi_ecall_extension *ext)
Quoted 2 lines
Thanks,
drew
Good. If so modified we can modify sbi_ecall_base_probe as well. As follows:
lib/sbi/sbi_ecall_base.c +1 -9
@@ -20,15 +20,7 @@ static int sbi_ecall_base_probe(unsigned long extid, unsigned long *out_val)struct sbi_ecall_extension *ext; ext =sbi_ecall_find_extension (extid); -if (!ext) { - *out_val =0 ; -return 0 ; - } - -if (ext->probe) -return ext->probe (extid, out_val); - - *out_val =1 ; + *out_val = ext ?1 :0 ;return 0 ; }
Regards,
Xiang W
Quoted 73 lines
Quoted 73 lines
Regards,
Xiang WQuoted 67 lines
Signed-off-by Andrew Jones <[email protected]>---
?include/sbi/sbi_ecall.h |? 6 ++++++ ?lib/sbi/sbi_ecall.c???? | 18 ++++++++++++++---- ?2 files changed, 20 insertions(+), 4 deletions(-)include/sbi/sbi_ecall.h +0
-0@@ -20,10 +20,16 @@ ?struct sbi_trap_regs; ?struct sbi_trap_info;?
include/sbi/sbi_ecall.h +5
-0+enum sbi_ext_status { +???????SBI_EXT_AVAILABLE = 1, +???????SBI_EXT_UNAVAILABLE, +}; +?struct
sbi_ecall_extension{
????????structsbi_dlisthead;
????????unsigned longextid_start;
????????unsigned longextid_end;
+???????enumsbi_ext_statusstatus;
????????int (* probe)(unsigned long extid, unsigned long*out_val); ????????int (* handle)(unsigned long extid, unsigned long funcid, ?????????????????????? const structsbi_trap_regs*regs,lib/sbi/sbi_ecall.c +0
-0@@ -42,16 +42,26 @@ static SBI_LIST_HEAD(ecall_exts_list);?
?structsbi_ecall_extension*sbi_ecall_find_extension(unsigned long extid) ?{
-???????structsbi_ecall_extension*t, *ret = NULL;
+???????structsbi_ecall_extension*t;
+???????unsigned longout_val;
?
????????sbi_list_for_each_entry(t,&ecall_exts_list, head) { ????????????????if (t->extid_start<= extid && extid <=t->extid_end) { -???????????????????????ret = t;
-???????????????????????break;
+???????????????????????if (t->status==SBI_EXT_AVAILABLE) +???????????????????????????????return t;
+???????????????????????if (t->status==SBI_EXT_UNAVAILABLE) +???????????????????????????????return NULL; +???????????????????????if (t->probe&& (t->probe(extid, &out_val)|| !out_val)) { +???????????????????????????????t->status =SBI_EXT_UNAVAILABLE; +???????????????????????????????return NULL; +???????????????????????}
+
+???????????????????????t->status =SBI_EXT_AVAILABLE; +???????????????????????return t;
????????????????}
????????}
?
-???????return ret;
+???????return NULL;
?}
?
?intsbi_ecall_register_extension(struct sbi_ecall_extension *ext)Signature · 1 line
2.39.2
PATCH 1/1 lib: sbi: Ensure SBI extension is available
Quoted 65 lines · Xiang W
? 2023-04-27???? 09:00 +0200?Andrew Jones???
Quoted 48 lines · Xiang W
Quoted 11 lines
? 2023-04-26???? 19:27 +0200?Andrew Jones???
Quoted 6 lines
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Adding this state may cause errors. For example: the id numberof an extension is from
extid_starttoextid_end, which has an unused ID. Searching for an ID in this extension will return NULL
if an unused ID is searched first.Hi Xiang,
You're right. Either
sbi_ecall_extension::statusneeds to be a function of extid, like probe is, or we should just always call probe (when it's available) and leave any optimizations to the individual probe functions. How about this?lib/sbi/sbi_ecall.c +0
-0@@ -42,16 +42,18 @@ static SBI_LIST_HEAD(ecall_exts_list);?
?structsbi_ecall_extension*sbi_ecall_find_extension(unsigned long extid) ?{
-???????structsbi_ecall_extension*t, *ret = NULL;
+???????structsbi_ecall_extension*t;
+???????unsigned longout_val;
?
????????sbi_list_for_each_entry(t,&ecall_exts_list, head) { ????????????????if (t->extid_start<= extid && extid <=t->extid_end) { -???????????????????????ret = t;
-???????????????????????break;
+???????????????????????if (t->probe&& (t->probe(extid, &out_val)|| !out_val)) +???????????????????????????????return NULL; +???????????????????????return t;
????????????????}
????????}
?
-???????return ret;
+???????return NULL;
?}
?
?intsbi_ecall_register_extension(struct sbi_ecall_extension *ext)Quoted 2 lines
Thanks,
drewGood. If so modified we can modify
sbi_ecall_base_probeas well. As follows:lib/sbi/sbi_ecall_base.c +0
-3@@ -20,15 +20,7 @@ static int sbi_ecall_base_probe(unsigned long extid, unsigned long *out_val)struct sbi_ecall_extension *ext; ext =sbi_ecall_find_extension (extid); -if (!ext) { - *out_val =0 ; -return 0 ;
lib/sbi/sbi_ecall_base.c static int sbi_ecall_base_probe(unsigned long extid, unsigned long *out_val) · line 26
Quoted 10 lines
lib/sbi/sbi_ecall_base.c +1
-6- } - -if (ext->probe) -return ext->probe (extid, out_val); - - *out_val =1 ; + *out_val = ext ?1 :0 ;return 0 ; }
If we do this, then we lose the return value of the extension's probe function, which is currently getting propagated all the way back to the SBI caller. sbi_ecall_base_probe() isn't in a fast path, so even though probe will get invoked twice, I guess we can afford it.
Thanks,
drew
PATCH 1/1 lib: sbi: Ensure SBI extension is available
? 2023-04-27???? 10:01 +0200?Andrew Jones???
Quoted 82 lines · Xiang W
Quoted 72 lines
? 2023-04-27???? 09:00 +0200?Andrew Jones???
Quoted 48 lines · Xiang W
Quoted 11 lines
? 2023-04-26???? 19:27 +0200?Andrew Jones???
Quoted 6 lines
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Adding this state may cause errors. For example: the id numberof an extension is from
extid_starttoextid_end, which has an unused ID. Searching for an ID in this extension will return NULL
if an unused ID is searched first.Hi Xiang,
You're right. Either
sbi_ecall_extension::statusneeds to be a function of extid, like probe is, or we should just always call probe (when it's available) and leave any optimizations to the individual probe functions. How about this?lib/sbi/sbi_ecall.c +0
-0@@ -42,16 +42,18 @@ static SBI_LIST_HEAD(ecall_exts_list);?
?structsbi_ecall_extension*sbi_ecall_find_extension(unsigned long extid) ?{
-???????structsbi_ecall_extension*t, *ret = NULL;
+???????structsbi_ecall_extension*t;
+???????unsigned longout_val;
?
????????sbi_list_for_each_entry(t,&ecall_exts_list, head) { ????????????????if (t->extid_start<= extid && extid <=t->extid_end) { -???????????????????????ret = t;
-???????????????????????break;
+???????????????????????if (t->probe&& (t->probe(extid, &out_val)|| !out_val)) +???????????????????????????????return NULL; +???????????????????????return t;
????????????????}
????????}
?
-???????return ret;
+???????return NULL;
?}
?
?intsbi_ecall_register_extension(struct sbi_ecall_extension *ext)Quoted 2 lines
Thanks,
drewGood. If so modified we can modify
sbi_ecall_base_probeas well. As follows:lib/sbi/sbi_ecall_base.c +0
-0@@ -20,15 +20,7 @@ static int sbi_ecall_base_probe(unsigned long extid, unsigned long *out_val) ??????? struct sbi_ecall_extension *ext;?
??????? ext =sbi_ecall_find_extension(extid);
-?????? if (!ext) {
-??????????????*out_val= 0;
-?????????????? return 0;
-?????? }lib/sbi/sbi_ecall_base.c +1
-5- -?????? if (ext->probe) -?????????????? return ext->probe(extid, out_val); - -?????? *out_val = 1; +?????? *out_val = ext ? 1 : 0;Quoted 3 lines
??????? return 0;
?}If we do this, then we lose the return value of the extension's probe function, which is currently getting propagated all the way back to the SBI caller.
sbi_ecall_base_probe()isn't in a fast path, so even though probe will get invoked twice, I guess we can afford it.
Quoted 2 lines
Thanks,
drew
If you don't modify sbi_ecall_base_probe, I don't think we need to modify sbi_ecall_find_extension. sbi_ecall_find_extension just find the structure of extension. If there is a gap in the id number, the handle and probe will definitely handle it.
Regards,
Xiang W
PATCH 1/1 lib: sbi: Ensure SBI extension is available
Quoted 83 lines · Xiang W
? 2023-04-27???? 10:01 +0200?Andrew Jones???
Quoted 82 lines · Xiang W
Quoted 72 lines
? 2023-04-27???? 09:00 +0200?Andrew Jones???
Quoted 48 lines · Xiang W
Quoted 11 lines
? 2023-04-26???? 19:27 +0200?Andrew Jones???
Quoted 6 lines
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Adding this state may cause errors. For example: the id numberof an extension is from
extid_starttoextid_end, which has an unused ID. Searching for an ID in this extension will return NULL
if an unused ID is searched first.Hi Xiang,
You're right. Either
sbi_ecall_extension::statusneeds to be a function of extid, like probe is, or we should just always call probe (when it's available) and leave any optimizations to the individual probe functions. How about this?lib/sbi/sbi_ecall.c +0
-0@@ -42,16 +42,18 @@ static SBI_LIST_HEAD(ecall_exts_list);?
?structsbi_ecall_extension*sbi_ecall_find_extension(unsigned long extid) ?{
-???????structsbi_ecall_extension*t, *ret = NULL;
+???????structsbi_ecall_extension*t;
+???????unsigned longout_val;
?
????????sbi_list_for_each_entry(t,&ecall_exts_list, head) { ????????????????if (t->extid_start<= extid && extid <=t->extid_end) { -???????????????????????ret = t;
-???????????????????????break;
+???????????????????????if (t->probe&& (t->probe(extid, &out_val)|| !out_val)) +???????????????????????????????return NULL; +???????????????????????return t;
????????????????}
????????}
?
-???????return ret;
+???????return NULL;
?}
?
?intsbi_ecall_register_extension(struct sbi_ecall_extension *ext)Quoted 2 lines
Thanks,
drewGood. If so modified we can modify
sbi_ecall_base_probeas well. As follows:lib/sbi/sbi_ecall_base.c +0
-0@@ -20,15 +20,7 @@ static int sbi_ecall_base_probe(unsigned long extid, unsigned long *out_val) ??????? struct sbi_ecall_extension *ext;?
??????? ext =sbi_ecall_find_extension(extid);
-?????? if (!ext) {
-??????????????*out_val= 0;
-?????????????? return 0;
-?????? }lib/sbi/sbi_ecall_base.c +1
-5- -?????? if (ext->probe) -?????????????? return ext->probe(extid, out_val); - -?????? *out_val = 1; +?????? *out_val = ext ? 1 : 0;Quoted 3 lines
??????? return 0;
?}If we do this, then we lose the return value of the extension's probe function, which is currently getting propagated all the way back to the SBI caller.
sbi_ecall_base_probe()isn't in a fast path, so even though probe will get invoked twice, I guess we can afford it.
Quoted 7 lines
Quoted 2 lines
Thanks,
drewIf you don't modify
sbi_ecall_base_probe, I don't think we need to modifysbi_ecall_find_extension.sbi_ecall_find_extensionjust find the structure of extension. If there is a gap in the id number, the handle and probe will definitely handle it.
I'm not sure I follow. The main goal of modifying sbi_ecall_find_extension is to ensure that we don't call handle unless probe says we can.
Thanks,
drew
PATCH 1/1 lib: sbi: Ensure SBI extension is available
In reply to Andrew JonesQuoted 47 lines · On Wed, Apr 26, 2023 at 10:58?PM Andrew Jones <[email protected]> wrote:
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Signed-off-by Andrew Jones <[email protected]>--- include/sbi/sbi_ecall.h | 6 ++++++ lib/sbi/sbi_ecall.c | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-)include/sbi/sbi_ecall.h +6
-0@@ -20,10 +20,16 @@struct sbi_trap_regs ;struct sbi_trap_info ; +enum sbi_ext_status { +SBI_EXT_AVAILABLE =1 , +SBI_EXT_UNAVAILABLE , +}; +struct sbi_ecall_extension {struct sbi_dlist head;unsigned long extid_start;unsigned long extid_end; +enum sbi_ext_status status;int (* probe)(unsigned long extid,unsigned long *out_val); int (* handle)(unsigned long extid, unsigned long funcid, const struct sbi_trap_regs *regs,lib/sbi/sbi_ecall.c +2
-1@@ -42,16 +42,26 @@ static SBI_LIST_HEAD(ecall_exts_list);struct sbi_ecall_extension *sbi_ecall_find_extension (unsigned long extid) { -struct sbi_ecall_extension *t, *ret =NULL ; +struct sbi_ecall_extension *t; +unsigned long out_val;
lib/sbi/sbi_ecall.c static SBI_LIST_HEAD(ecall_exts_list); · line 55
Quoted 10 lines
lib/sbi/sbi_ecall.c +6
-2sbi_list_for_each_entry (t, &ecall_exts_list, head) {if (t->extid_start <= extid && extid <= t->extid_end) { - ret = t; -break ; +if (t->status ==SBI_EXT_AVAILABLE ) +return t; +if (t->status ==SBI_EXT_UNAVAILABLE ) +return NULL ; +if (t->probe && (t->probe (extid, &out_val) || !out_val)) { + t->status =SBI_EXT_UNAVAILABLE ;
Setting this status flag is a bit racey because two HARTs can end-up in sbi_ecall_find_extension() at the same time.
I suggest an alternate approach whereby we don't need the status flag and sbi_ecall_find_extension() also does not need to change:
1) Rename probe() callback to init() callback
2) Remove sbi_ecall_register_extension() call from sbi_ecall_init() and instead call init() for each extension
3) Implement init() for each extension such that each extension explicitly register itself using sbi_ecall_register_extension() only if the extension is actually available.
In other words, the alternate approach treats SBI extensions are driver instances which need to be registered by some init() call.
Regards,
Anup
Quoted 21 lines
lib/sbi/sbi_ecall.c +6
-2+return NULL ; + } + + t->status =SBI_EXT_AVAILABLE ; +return t; } } -return ret; +return NULL ; }int sbi_ecall_register_extension (struct sbi_ecall_extension *ext) --2.39.2
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH 1/1 lib: sbi: Ensure SBI extension is available
Quoted 48 lines · Anup Patel
Quoted 47 lines · On Wed, Apr 26, 2023 at 10:58?PM Andrew Jones <[email protected]> wrote:
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Signed-off-by Andrew Jones <[email protected]>--- include/sbi/sbi_ecall.h | 6 ++++++ lib/sbi/sbi_ecall.c | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-)include/sbi/sbi_ecall.h +6
-0@@ -20,10 +20,16 @@struct sbi_trap_regs ;struct sbi_trap_info ; +enum sbi_ext_status { +SBI_EXT_AVAILABLE =1 , +SBI_EXT_UNAVAILABLE , +}; +struct sbi_ecall_extension {struct sbi_dlist head;unsigned long extid_start;unsigned long extid_end; +enum sbi_ext_status status;int (* probe)(unsigned long extid,unsigned long *out_val); int (* handle)(unsigned long extid, unsigned long funcid, const struct sbi_trap_regs *regs,lib/sbi/sbi_ecall.c +2
-1@@ -42,16 +42,26 @@ static SBI_LIST_HEAD(ecall_exts_list);struct sbi_ecall_extension *sbi_ecall_find_extension (unsigned long extid) { -struct sbi_ecall_extension *t, *ret =NULL ; +struct sbi_ecall_extension *t; +unsigned long out_val;
Quoted 13 lines
lib/sbi/sbi_ecall.c static SBI_LIST_HEAD(ecall_exts_list); · line 55
Quoted 10 lines
lib/sbi/sbi_ecall.c +6
-2sbi_list_for_each_entry (t, &ecall_exts_list, head) {if (t->extid_start <= extid && extid <= t->extid_end) { - ret = t; -break ; +if (t->status ==SBI_EXT_AVAILABLE ) +return t; +if (t->status ==SBI_EXT_UNAVAILABLE ) +return NULL ; +if (t->probe && (t->probe (extid, &out_val) || !out_val)) { + t->status =SBI_EXT_UNAVAILABLE ;Setting this
statusflag is a bit racey because two HARTs can end-up insbi_ecall_find_extension()at the same time.
It's racy, but it shouldn't matter, since the result will be the same whether a second hart ends up calling probe itself or not.
Quoted 11 lines
I suggest an alternate approach whereby we don't need the
statusflag andsbi_ecall_find_extension()also does not need to change:1) Rename
probe()callback toinit()callback
2) Removesbi_ecall_register_extension()call fromsbi_ecall_init()and instead callinit()for each extension
3) Implementinit()for each extension such that each extension explicitly register itself usingsbi_ecall_register_extension()only if the extension is actually available.
Quoted 2 lines
In other words, the alternate approach treats SBI extensions are driver instances which need to be registered by some
init()call.
I mostly like that, but probe is specified as "Returns 0 if the given SBI extension ID (EID) is not available, or 1 if it is available unless defined as any other non-zero value by the implementation.", which means a future extension may want to return some nonzero value from its probe function to inform the caller of something. So, maybe we keep probe, add init, and call probe from init to decide if we register the extension?
Thanks,
drew
PATCH 1/1 lib: sbi: Ensure SBI extension is available
Quoted 81 lines · On Fri, Apr 28, 2023 at 4:21?PM Andrew Jones <[email protected]> wrote:
Quoted 48 lines · Anup Patel
Quoted 47 lines · On Wed, Apr 26, 2023 at 10:58?PM Andrew Jones <[email protected]> wrote:
Ensure attempts to invoke SBI extension functions fail with
SBI_ERR_NOT_SUPPORTEDwhen the extension's probe function has reported that the extension is not available. By adding a new status member to the extension which has three states (uninitialized, available, unavailable), we ensure that the probe function is only invoked once, lazily, upon first use.Signed-off-by Andrew Jones <[email protected]>--- include/sbi/sbi_ecall.h | 6 ++++++ lib/sbi/sbi_ecall.c | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-)include/sbi/sbi_ecall.h +6
-0@@ -20,10 +20,16 @@struct sbi_trap_regs ;struct sbi_trap_info ; +enum sbi_ext_status { +SBI_EXT_AVAILABLE =1 , +SBI_EXT_UNAVAILABLE , +}; +struct sbi_ecall_extension {struct sbi_dlist head;unsigned long extid_start;unsigned long extid_end; +enum sbi_ext_status status;int (* probe)(unsigned long extid,unsigned long *out_val); int (* handle)(unsigned long extid, unsigned long funcid, const struct sbi_trap_regs *regs,lib/sbi/sbi_ecall.c +2
-1@@ -42,16 +42,26 @@ static SBI_LIST_HEAD(ecall_exts_list);struct sbi_ecall_extension *sbi_ecall_find_extension (unsigned long extid) { -struct sbi_ecall_extension *t, *ret =NULL ; +struct sbi_ecall_extension *t; +unsigned long out_val;Quoted 13 lines
lib/sbi/sbi_ecall.c static SBI_LIST_HEAD(ecall_exts_list); · line 55
Quoted 10 lines
lib/sbi/sbi_ecall.c +6
-2sbi_list_for_each_entry (t, &ecall_exts_list, head) {if (t->extid_start <= extid && extid <= t->extid_end) { - ret = t; -break ; +if (t->status ==SBI_EXT_AVAILABLE ) +return t; +if (t->status ==SBI_EXT_UNAVAILABLE ) +return NULL ; +if (t->probe && (t->probe (extid, &out_val) || !out_val)) { + t->status =SBI_EXT_UNAVAILABLE ;Setting this
statusflag is a bit racey because two HARTs can end-up insbi_ecall_find_extension()at the same time.It's racy, but it shouldn't matter, since the result will be the same whether a second hart ends up calling probe itself or not.
Quoted 11 lines
I suggest an alternate approach whereby we don't need the
statusflag andsbi_ecall_find_extension()also does not need to change:1) Rename
probe()callback toinit()callback
2) Removesbi_ecall_register_extension()call fromsbi_ecall_init()and instead callinit()for each extension
3) Implementinit()for each extension such that each extension explicitly register itself usingsbi_ecall_register_extension()only if the extension is actually available.Quoted 2 lines
In other words, the alternate approach treats SBI extensions are driver instances which need to be registered by some
init()call.
Quoted 7 lines
I mostly like that, but probe is specified as "Returns 0 if the given SBI extension ID (EID) is not available, or 1 if it is available unless defined as any other non-zero value by the implementation.", which means a future extension may want to return some nonzero value from its probe function to inform the caller of something. So, maybe we keep probe, add init, and call probe from init to decide if we register the extension?
I am okay to keep the probe() callback but currently none of the extensions have custom probe return value.
Regards,
Anup