Sparse HART id improvements
cover Sparse HART id improvements
Modify last_hartindex_having_scratch/hartindex_to_hartid_table /hartindex_to_scratch_table as static variables to prevent accidental modification and reduce memory consumption
Xiang W (3):
lib: sbi: Refactor sbi_scratch_last_hartindex as a function
lib: sbi: Refactor sbi_hartindex_to_hartid as a function
lib: sbi: Refactor sbi_hartindex_to_scratch as a function
include/sbi/sbi_scratch.h | 35 ++++++++++++----------------------- lib/sbi/sbi_scratch.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 27 deletions(-)
Signature · 1 line
2.40.1
PATCH 1/3 lib: sbi: Refactor sbi_scratch_last_hartindex as a function
Refactor sbi_scratch_last_hartindex as a function. This prevents the last_hartindex_having_scratch variable from being corrupted elsewhere
--- include/sbi/sbi_scratch.h | 5 +---- lib/sbi/sbi_scratch.c | 7 ++++++- 2 files changed, 7 insertions(+), 5 deletions(-)
include/sbi/sbi_scratch.h +1 -4
@@ -202,11 +202,8 @@ do { \ = (__type)(__ptr); \ }while (0 ) -/** Last HART index having a sbi_scratch pointer */ -extern u32 last_hartindex_having_scratch; -/** Get last HART index having a sbi_scratch pointer */ -#define sbi_scratch_last_hartindex () last_hartindex_having_scratch +u32 sbi_scratch_last_hartindex(void);/** Check whether a particular HART index is valid or not */ #define sbi_hartindex_valid (__hartindex) \
lib/sbi/sbi_scratch.c +6 -1
@@ -14,13 +14,18 @@#include <sbi/sbi_scratch.h> #include <sbi/sbi_string.h> -u32 last_hartindex_having_scratch =0 ; +static u32 last_hartindex_having_scratch =0 ;u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS +1 ] = {-1U };struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS +1 ] = {0 };static spinlock_t extra_lock =SPIN_LOCK_INITIALIZER ;static unsigned long extra_offset =SBI_SCRATCH_EXTRA_SPACE_OFFSET ; +u32 sbi_scratch_last_hartindex (void ) +{ +return last_hartindex_having_scratch; +} +u32 sbi_hartid_to_hartindex (u32 hartid) {u32 i;
Signature · 1 line
2.40.1
PATCH 2/3 lib: sbi: Refactor sbi_hartindex_to_hartid as a function
In reply to Xiang WRefactor sbi_hartindex_to_hartid as a function. hartindex_to_hartid_table is modified to a pointer and points to plat->hart_index2id to reduce memory consumption.
--- include/sbi/sbi_scratch.h | 14 +++++--------- lib/sbi/sbi_scratch.c | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 11 deletions(-)
include/sbi/sbi_scratch.h +5 -9
@@ -209,15 +209,11 @@ u32 sbi_scratch_last_hartindex(void);#define sbi_hartindex_valid (__hartindex) \ (((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false) -/** HART index to HART id table */ -extern u32 hartindex_to_hartid_table[]; - -/** Get sbi_scratch from HART index */ -#define sbi_hartindex_to_hartid (__hartindex) \ -({ \ - ((__hartindex) <= sbi_scratch_last_hartindex()) ?\ - hartindex_to_hartid_table[__hartindex] : -1U; \ -}) +/** Get sbi_scratch from HART index +* @param hartindex hart index +* @return phtsical HART id, -1U indicate input is invalid +*/ +u32 sbi_hartindex_to_hartid (u32 hartindex);/** HART index to scratch table */ extern struct sbi_scratch *hartindex_to_scratch_table[];
lib/sbi/sbi_scratch.c +12 -2
@@ -15,7 +15,7 @@#include <sbi/sbi_string.h> static u32 last_hartindex_having_scratch =0 ; -u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS +1 ] = {-1U }; +static const u32 *hartindex_to_hartid_table =NULL ;struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS +1 ] = {0 };static spinlock_t extra_lock =SPIN_LOCK_INITIALIZER ;
@@ -37,6 +37,16 @@ u32 sbi_hartid_to_hartindex(u32 hartid)return -1U ; } +u32 sbi_hartindex_to_hartid (u32 hartindex) +{ +if (hartindex < last_hartindex_having_scratch) { +if (hartindex_to_hartid_table) +return hartindex_to_hartid_table[hartindex]; +return hartindex; + } +return -1U ; +} +typedef struct sbi_scratch *(*hartid2scratch )(ulong hartid,ulong hartindex);int sbi_scratch_init (struct sbi_scratch *scratch)
@@ -46,12 +56,12 @@ int sbi_scratch_init(struct sbi_scratch *scratch)for (i =0 ; i < plat->hart_count; i++) { h = (plat->hart_index2id) ? plat->hart_index2id[i] : i; - hartindex_to_hartid_table[i] = h; hartindex_to_scratch_table[i] = ((hartid2scratch )scratch->hartid_to_scratch)(h, i); } last_hartindex_having_scratch = plat->hart_count -1 ; + hartindex_to_hartid_table = plat->hart_index2id;return 0 ; }
Signature · 1 line
2.40.1
PATCH 3/3 lib: sbi: Refactor sbi_hartindex_to_scratch as a function
In reply to Xiang WRefactor sbi_hartindex_to_scratch as a function. This prevents the hartindex_to_scratch_table variable from being corrupted elsewhere.
--- include/sbi/sbi_scratch.h | 14 +++++--------- lib/sbi/sbi_scratch.c | 9 ++++++++- 2 files changed, 13 insertions(+), 10 deletions(-)
include/sbi/sbi_scratch.h +5 -9
@@ -215,15 +215,11 @@ u32 sbi_scratch_last_hartindex(void);*/ u32 sbi_hartindex_to_hartid (u32 hartindex); -/** HART index to scratch table */ -extern struct sbi_scratch *hartindex_to_scratch_table[]; - -/** Get sbi_scratch from HART index */ -#define sbi_hartindex_to_scratch (__hartindex) \ -({ \ - ((__hartindex) <= sbi_scratch_last_hartindex()) ?\ - hartindex_to_scratch_table[__hartindex] : NULL;\ -}) +/** Get sbi_scratch from HART index +* @param hartindex HART index +* @return scratch of HART +*/ +struct sbi_scratch *sbi_hartindex_to_scratch (u32 hartindex);/** * Get logical index for given HART id
lib/sbi/sbi_scratch.c +8 -1
@@ -16,7 +16,7 @@static u32 last_hartindex_having_scratch =0 ;static const u32 *hartindex_to_hartid_table =NULL ; -struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS +1 ] = {0 }; +static struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS +1 ] = {0 };static spinlock_t extra_lock =SPIN_LOCK_INITIALIZER ;static unsigned long extra_offset =SBI_SCRATCH_EXTRA_SPACE_OFFSET ;
@@ -47,6 +47,13 @@ u32 sbi_hartindex_to_hartid(u32 hartindex)return -1U ; } +struct sbi_scratch *sbi_hartindex_to_scratch (u32 hartindex) +{ +if (hartindex < last_hartindex_having_scratch) +return hartindex_to_scratch_table[hartindex]; +return NULL ; +} +typedef struct sbi_scratch *(*hartid2scratch )(ulong hartid,ulong hartindex);int sbi_scratch_init (struct sbi_scratch *scratch)
Signature · 1 line
2.40.1
cover Sparse HART id improvements
In reply to Xiang WQuoted 9 lines · On Sun, Sep 24, 2023 at 9:28?PM Xiang W <[email protected]> wrote:
Modify last_hartindex_having_scratch/hartindex_to_hartid_table /hartindex_to_scratch_table as static variables to prevent accidental modification and reduce memory consumption
Xiang W (3):
lib: sbi: Refactorsbi_scratch_last_hartindexas a function
lib: sbi: Refactorsbi_hartindex_to_hartidas a function
lib: sbi: Refactorsbi_hartindex_to_scratchas a function
All these are in hot-path at various places so introducing a function is certainly not helping.
Also, if some buggy code is accidentally modifying global data then that buggy code should be fixed rather than papering over the problem by introducing these functions.
Regards,
Anup
Quoted 13 lines
include/sbi/sbi_scratch.h | 35 ++++++++++++----------------------- lib/sbi/sbi_scratch.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 27 deletions(-)--
2.40.1--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
cover Sparse HART id improvements
? 2023-10-06???? 10:05 +0530?Anup Patel???
Quoted 11 lines · On Sun, Sep 24, 2023 at 9:28?PM Xiang W <[email protected]> wrote:
Quoted 9 lines
Modify last_hartindex_having_scratch/hartindex_to_hartid_table /hartindex_to_scratch_table as static variables to prevent accidental modification and reduce memory consumption
Xiang W (3):
? lib: sbi: Refactorsbi_scratch_last_hartindexas a function ? lib: sbi: Refactorsbi_hartindex_to_hartidas a function ? lib: sbi: Refactorsbi_hartindex_to_scratchas a function
Quoted 6 lines
All these are in hot-path at various places so introducing a function is certainly not helping.
Also, if some buggy code is accidentally modifying global data then that buggy code should be fixed rather than papering over the problem by introducing these functions.
PATCH1:
The original code is a bit strange in that there is already a global variable last_hartindex_having_scratch, but instead of using it outside of sbi_scratch.c, the macro sbi_scratch_last_hartindex is used. If don't want to modify this macro into a function, why not just change the name of the last_hartindex_having_scratch renamed to sbi_scratch_last_hartindex and then remove the macro?
PATCH2:
The arrays hartindex_to_hartid_table and plat->hart_index2id store similar content, and the extra length of the hartindex_to_hartid_table array is not used because there is a determination of whether the index is less than sbi_scratch_last_ hartindex. the array can be removed by adding just a little bit of checking.
Regards,
Xiang W
Quoted 17 lines
Regards,
AnupQuoted 13 lines
?include/sbi/sbi_scratch.h | 35 ++++++++++++----------------------- ?lib/sbi/sbi_scratch.c???? | 30 ++++++++++++++++++++++++++---- ?2 files changed, 38 insertions(+), 27 deletions(-)
--
2.40.1--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
cover Sparse HART id improvements
Quoted 19 lines · On Sat, Oct 7, 2023 at 9:19?AM Xiang W <[email protected]> wrote:
? 2023-10-06???? 10:05 +0530?Anup Patel???
Quoted 11 lines · On Sun, Sep 24, 2023 at 9:28?PM Xiang W <[email protected]> wrote:
Quoted 9 lines
Modify last_hartindex_having_scratch/hartindex_to_hartid_table /hartindex_to_scratch_table as static variables to prevent accidental modification and reduce memory consumption
Xiang W (3):
lib: sbi: Refactorsbi_scratch_last_hartindexas a function
lib: sbi: Refactorsbi_hartindex_to_hartidas a function
lib: sbi: Refactorsbi_hartindex_to_scratchas a functionQuoted 6 lines
All these are in hot-path at various places so introducing a function is certainly not helping.
Also, if some buggy code is accidentally modifying global data then that buggy code should be fixed rather than papering over the problem by introducing these functions.
Quoted 6 lines
PATCH1:
The original code is a bit strange in that there is already a global variablelast_hartindex_having_scratch, but instead of using it outside ofsbi_scratch.c, the macrosbi_scratch_last_hartindexis used. If don't want to modify this macro into a function, why not just change the name of thelast_hartindex_having_scratchrenamed tosbi_scratch_last_hartindexand then remove the macro?
The sbi_scratch_last_hartindex() macro allows adding instrumentation code for debugging purposes. The caller of sbi_scratch_last_hartindex() should not care about how it is implemented.
Quoted 6 lines
PATCH2:
The arrayshartindex_to_hartid_tableandplat->hart_index2idstore similar content, and the extra length of thehartindex_to_hartid_tablearray is not used because there is a determination of whether the index is less thansbi_scratch_last_hartindex. the array can be removed by adding just a little bit of checking.
The "plat->hart_index2id" is optional and can be NULL as well so we have to always use "if ()" before accessing "plat->hart_index2id".
Also, we always have to access "hart_index2id[]" via "plat" pointer which is slower compared to a global array like "hartindex_to_hartid_table[]".
Regards,
Anup
Quoted 21 lines
Regards,
Xiang WQuoted 17 lines
Regards,
AnupQuoted 13 lines
include/sbi/sbi_scratch.h | 35 ++++++++++++----------------------- lib/sbi/sbi_scratch.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 27 deletions(-)--
2.40.1--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
cover Sparse HART id improvements
? 2023-10-09???? 14:06 +0530?Anup Patel???
Quoted 38 lines · On Sat, Oct 7, 2023 at 9:19?AM Xiang W <[email protected]> wrote:
Quoted 19 lines
? 2023-10-06???? 10:05 +0530?Anup Patel???
Quoted 11 lines · On Sun, Sep 24, 2023 at 9:28?PM Xiang W <[email protected]> wrote:
Quoted 9 lines
Modify last_hartindex_having_scratch/hartindex_to_hartid_table /hartindex_to_scratch_table as static variables to prevent accidental modification and reduce memory consumption
Xiang W (3):
? lib: sbi: Refactorsbi_scratch_last_hartindexas a function ? lib: sbi: Refactorsbi_hartindex_to_hartidas a function ? lib: sbi: Refactorsbi_hartindex_to_scratchas a functionQuoted 6 lines
All these are in hot-path at various places so introducing a function is certainly not helping.
Also, if some buggy code is accidentally modifying global data then that buggy code should be fixed rather than papering over the problem by introducing these functions.
Quoted 6 lines
PATCH1:
The original code is a bit strange in that there is already a global variablelast_hartindex_having_scratch, but instead of using it outside ofsbi_scratch.c, the macrosbi_scratch_last_hartindexis used. If don't want to modify this macro into a function, why not just change the name of thelast_hartindex_having_scratchrenamed tosbi_scratch_last_hartindexand then remove the macro?The
sbi_scratch_last_hartindex()macro allows adding instrumentation code for debugging purposes. The caller ofsbi_scratch_last_hartindex()should not care about how it is implemented.Quoted 6 lines
PATCH2:
The arrayshartindex_to_hartid_tableandplat->hart_index2idstore similar content, and the extra length of thehartindex_to_hartid_tablearray is not used because there is a determination of whether the index is less thansbi_scratch_last_hartindex. the array can be removed by adding just a little bit of checking.
Quoted 5 lines
The "
plat->hart_index2id" is optional and can be NULL as well so we have to always use "if ()" before accessing "plat->hart_index2id".Also, we always have to access "hart_index2id[]" via "plat" pointer which is slower compared to a global array like "hartindex_to_hartid_table[]".
My expression may not be very accurate, please review my code.
https://lists.infradead.org/pipermail/opensbi/2023-October/005703.html
Regards,
Xiang W
Quoted 25 lines
Regards,
AnupQuoted 21 lines
Regards,
Xiang WQuoted 17 lines
Regards,
AnupQuoted 13 lines
?include/sbi/sbi_scratch.h | 35 ++++++++++++----------------------- ?lib/sbi/sbi_scratch.c???? | 30 ++++++++++++++++++++++++++---- ?2 files changed, 38 insertions(+), 27 deletions(-)
--
2.40.1--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi