Skip to contentSkip to conversation

x86: refactor psr implementation in hypervisor.

PATCH Superseded Yi Sun 3 participants 13 messages Aug 25, 2016 – Sep 12, 2016

v1 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Yi Sun

author
#0 lore raw

PATCH 1/3 x86: refactor psr implementation in hypervisor.

Current psr.c is designed for supporting L3 CAT/CDP. It has many limitations to add new feature. Considering to support more PSR features, we need refactor PSR implementation to make it more flexible and fulfill the principle, open for extension but closed for modification.

The core of the refactoring is to abstract the common actions and encapsulate them into "struct feat_ops". The detailed steps to add a new feature are described at the head of psr.c.

---
 xen/arch/x86/domctl.c     |   36 +-
 xen/arch/x86/psr.c        | 1121 +++++++++++++++++++++++++++++++++++----------
 xen/arch/x86/sysctl.c     |    9 +-
 xen/include/asm-x86/psr.h |   21 +-
 4 files changed, 912 insertions(+), 275 deletions(-)
xen/arch/x86/domctl.c +18 -18
index bed70aa..a51ed2c 100644
@@ -1358,41 +1358,41 @@ long arch_do_domctl(
         switch ( domctl->u.psr_cat_op.cmd )
         {
         case XEN_DOMCTL_PSR_CAT_OP_SET_L3_CBM:
-            ret = psr_set_l3_cbm(d, domctl->u.psr_cat_op.target,
-                                 domctl->u.psr_cat_op.data,
-                                 PSR_CBM_TYPE_L3);
+            ret = psr_set_val(d, domctl->u.psr_cat_op.target,
+                              domctl->u.psr_cat_op.data,
+                              PSR_MASK_TYPE_L3_CBM);
             break;
 
         case XEN_DOMCTL_PSR_CAT_OP_SET_L3_CODE:
-            ret = psr_set_l3_cbm(d, domctl->u.psr_cat_op.target,
-                                 domctl->u.psr_cat_op.data,
-                                 PSR_CBM_TYPE_L3_CODE);
+            ret = psr_set_val(d, domctl->u.psr_cat_op.target,
+                              domctl->u.psr_cat_op.data,
+                              PSR_MASK_TYPE_L3_CODE);
             break;
 
         case XEN_DOMCTL_PSR_CAT_OP_SET_L3_DATA:
-            ret = psr_set_l3_cbm(d, domctl->u.psr_cat_op.target,
-                                 domctl->u.psr_cat_op.data,
-                                 PSR_CBM_TYPE_L3_DATA);
+            ret = psr_set_val(d, domctl->u.psr_cat_op.target,
+                              domctl->u.psr_cat_op.data,
+                              PSR_MASK_TYPE_L3_DATA);
             break;
 
         case XEN_DOMCTL_PSR_CAT_OP_GET_L3_CBM:
-            ret = psr_get_l3_cbm(d, domctl->u.psr_cat_op.target,
-                                 &domctl->u.psr_cat_op.data,
-                                 PSR_CBM_TYPE_L3);
+            ret = psr_get_val(d, domctl->u.psr_cat_op.target,
+                              &domctl->u.psr_cat_op.data,
+                              PSR_MASK_TYPE_L3_CBM);
             copyback = 1;
             break;
 
         case XEN_DOMCTL_PSR_CAT_OP_GET_L3_CODE:
-            ret = psr_get_l3_cbm(d, domctl->u.psr_cat_op.target,
-                                 &domctl->u.psr_cat_op.data,
-                                 PSR_CBM_TYPE_L3_CODE);
+            ret = psr_get_val(d, domctl->u.psr_cat_op.target,
+                              &domctl->u.psr_cat_op.data,
+                              PSR_MASK_TYPE_L3_CODE);
             copyback = 1;
             break;
 
         case XEN_DOMCTL_PSR_CAT_OP_GET_L3_DATA:
-            ret = psr_get_l3_cbm(d, domctl->u.psr_cat_op.target,
-                                 &domctl->u.psr_cat_op.data,
-                                 PSR_CBM_TYPE_L3_DATA);
+            ret = psr_get_val(d, domctl->u.psr_cat_op.target,
+                              &domctl->u.psr_cat_op.data,
+                              PSR_MASK_TYPE_L3_DATA);
             copyback = 1;
             break;
 
xen/arch/x86/psr.c +878 -243

1,381 lines not on this page · raw message

xen/arch/x86/sysctl.c +5 -4
index 14e7dc7..a143e7a 100644
@@ -176,10 +176,11 @@ long arch_do_sysctl(
         switch ( sysctl->u.psr_cat_op.cmd )
         {
         case XEN_SYSCTL_PSR_CAT_get_l3_info:
-            ret = psr_get_cat_l3_info(sysctl->u.psr_cat_op.target,
-                                      &sysctl->u.psr_cat_op.u.l3_info.cbm_len,
-                                      &sysctl->u.psr_cat_op.u.l3_info.cos_max,
-                                      &sysctl->u.psr_cat_op.u.l3_info.flags);
+            ret = psr_get_info(sysctl->u.psr_cat_op.target,
+                               PSR_MASK_TYPE_L3_CBM,
+                               &sysctl->u.psr_cat_op.u.l3_info.cbm_len,
+                               &sysctl->u.psr_cat_op.u.l3_info.cos_max,
+                               &sysctl->u.psr_cat_op.u.l3_info.flags);
 
             if ( !ret && __copy_field_to_guest(u_sysctl, sysctl, u.psr_cat_op) )
                 ret = -EFAULT;
xen/include/asm-x86/psr.h +11 -10
index 57f47e9..0993863 100644
@@ -46,10 +46,10 @@ struct psr_cmt {
     struct psr_cmt_l3 l3;
 };
 
-enum cbm_type {
-    PSR_CBM_TYPE_L3,
-    PSR_CBM_TYPE_L3_CODE,
-    PSR_CBM_TYPE_L3_DATA,
+enum mask_type {
+    PSR_MASK_TYPE_L3_CBM,
+    PSR_MASK_TYPE_L3_CODE,
+    PSR_MASK_TYPE_L3_DATA,
 };
 
 extern struct psr_cmt *psr_cmt;
@@ -63,12 +63,13 @@ int psr_alloc_rmid(struct domain *d);
 void psr_free_rmid(struct domain *d);
 void psr_ctxt_switch_to(struct domain *d);
 
-int psr_get_cat_l3_info(unsigned int socket, uint32_t *cbm_len,
-                        uint32_t *cos_max, uint32_t *flags);
-int psr_get_l3_cbm(struct domain *d, unsigned int socket,
-                   uint64_t *cbm, enum cbm_type type);
-int psr_set_l3_cbm(struct domain *d, unsigned int socket,
-                   uint64_t cbm, enum cbm_type type);
+int psr_get_info(unsigned int socket, enum mask_type type,
+                 uint32_t *dat0, uint32_t *dat1,
+                 uint32_t *dat2);
+int psr_get_val(struct domain *d, unsigned int socket,
+                uint64_t *val, enum mask_type type);
+int psr_set_val(struct domain *d, unsigned int socket,
+                uint64_t val, enum mask_type type);
 
 int psr_domain_init(struct domain *d);
 void psr_domain_free(struct domain *d);
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 25.08.16 at 07:22, <[email protected]> wrote:
xen/arch/x86/psr.c +14 -8
@@ -23,22 +23,116 @@
 #define PSR_CAT        (1<<1)
 #define PSR_CDP        (1<<2)
 
-struct psr_cat_cbm {
-    union {
-        uint64_t cbm;
-        struct {
-            uint64_t code;
-            uint64_t data;
-        };
-    };
+/*
+ * To add a new PSR feature, please follow below steps:
+ * 1. Implement feature specific callback functions listed in
+ *    "struct feat_ops";
+ * 2. Implement a feature specific "struct feat_ops" object and register
+ *    feature specific callback functions into it;
+ * 3. Delcare feat_list object for the feature and malloc memory for it
+ *    in internal_cpu_prepare(). Correspondingly, free them in
+ *    free_feature();
+ * 4. Add feature initialization codes in internal_cpu_init().
+ */
+
+struct feat_list;
+struct psr_socket_alloc_info;

xen/arch/x86/psr.c line 49

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+
+/* Every feature enabled should implement such ops and callback functions. */
+struct feat_ops {
+    /*
+     * init_feature is used in cpu initialization process to do feature
+     * specific initialization works.
+     */
+    void (*init_feature)(unsigned int eax, unsigned int ebx,
+                         unsigned int ecx, unsigned int edx,
+                         struct feat_list *pFeat,

Here and below - we don't normally use identifiers with capital letters in the middle.

xen/arch/x86/psr.c line 54

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+                         struct psr_socket_alloc_info *info);
+    /*
+     * get_old_set_new is used in set value process to get all features'
+     * COS registers values according to original cos id of the domain.
+     * Then, assemble them into an mask array as feature list order.

This sentence in particular doesn't make any sense to me. What follows below also looks like it is in need of improvement.

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+     * Then, set the new feature value into feature corresponding position
+     * in the array. This function is used to pair with compare_mask.
+     */
+    int (*get_old_set_new)(uint64_t *mask,
+                           struct feat_list *pFeat,

xen/arch/x86/psr.c line 69

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+                           unsigned int old_cos,
+                           enum mask_type type,
+                           uint64_t m);
+    /*
+     * compare_mask is used to in set value process to compare if the
+     * input mask array can match all the features' COS registers values
+     * according to input cos id.
+     */
+    int (*compare_mask)(uint64_t *mask, struct feat_list *pFeat,
+                        unsigned int cos, bool_t *found);

For a "compare" function I'd expect a word on the meaning of the return value, and I'd expect most if not all pointer parameters to be pointers to const.

Also plain bool please.

xen/arch/x86/psr.c line 75

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+    /*
+     * get_cos_max_as_type is used to get the cos_max value of the feature
+     * according to input mask_type.
+     */
+    unsigned int (*get_cos_max_as_type)(struct feat_list *pFeat,
+                                        enum mask_type type);

DYM mean "get_cos_max_from_type()"?

xen/arch/x86/psr.c line 85

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+    /*
+     * exceed_range is used to check if the input cos id exceeds the
+     * feature's cos_max and if the input mask value is not the default one.
+     * Even if the associated cos exceeds the cos_max, HW can work as default
+     * value. That is the reason we need check is mask value is default one.
+     * If both criteria are fulfilled, that means the input exceeds the
+     * range.
+     */
+    unsigned int (*exceed_range)(uint64_t *mask, struct feat_list *pFeat,
+                                 unsigned int cos);

According to the comment this is kind of a predicate, which seems unlikely to return an unsigned value. In fact without a word on the return value I'd expect such to return bool. And I'd also expect the name to reflect the purpose, i.e. "exceeds_name()". Plus just like for compare above I wonder whether come or all of the parameters should be pointers to const (please go over the entire patch and do constification wherever possible/sensible).

xen/arch/x86/psr.c line 88

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+    /*
+     * write_msr is used to write feature specific MSR registers.
+     */

This is a single line comment.

xen/arch/x86/psr.c line 90

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+    int (*write_msr)(unsigned int cos, uint64_t *mask,
+                     struct feat_list *pFeat);

Such write operations don't normally alter the value to be written. If that's different here, that's perhaps worth a word in the comment. If it isn't, then I don't see why the address of the value gets passed instead of the value itself.

Oh, having gone further in the patch I see this is to update multiple MSRs. I guess this would best be reflected by making the parameter const uint64_t masks[] (albeit it's also not clear to me why this necessarily have to be masks kind items, and not just arbitrary values - proper choice of variable and parameter names helps the understanding).

xen/arch/x86/psr.c line 109

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+#define MAX_FEAT_INFO_SIZE 8
+#define MAX_COS_REG_NUM  128

Are these numbers arbitrary, or based on something?

xen/arch/x86/psr.c line 127

Quoted 1 line
xen/arch/x86/psr.c +1 -0
+struct psr_socket_alloc_info {

I've yet to see whether the "alloc" in the name really makes sense.

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 155

Quoted 8 lines
xen/arch/x86/psr.c +3 -1
@@ -58,8 +150,427 @@ static unsigned int __read_mostly opt_cos_max = 255;
 static uint64_t rmid_mask;
 static DEFINE_PER_CPU(struct psr_assoc, psr_assoc);
 
-static struct psr_cat_cbm *temp_cos_to_cbm;
+static struct psr_ref *temp_cos_ref;
+/* Every feature has its own object. */
+struct feat_list *pL3CAT;

static. And a better name please, more in line with the other variable or similar purpose.

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 167

Quoted 11 lines
xen/arch/x86/psr.c +11 -0
+/* Common functions for supporting feature callback functions. */
+static void add_feature(struct feat_list *pHead, struct feat_list *pTmp)
+{
+    if ( NULL == pHead || NULL == pTmp )
+        return;
+
+    while ( pHead->pNext )
+        pHead = pHead->pNext;
+
+    pHead->pNext = pTmp;
+}

Do you really need custom list management here?

Quoted 11 lines
xen/arch/x86/psr.c +11 -0
+static void free_feature(struct psr_socket_alloc_info *info)
+{
+    struct feat_list *pTmp;
+    struct feat_list *pPrev;
+
+    if ( NULL == info )
+        return;
+
+    if ( NULL == info->pFeat )
+        return;
+

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 189

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        pPrev = pTmp;
+        pTmp = pTmp->pNext;
+        clear_bit(pPrev->feature, &(info->features));
+        xfree(pPrev);
+        pPrev = NULL;
+    }
+}

Why does info->pFeat not get freed here?

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 234

Quoted 11 lines
xen/arch/x86/psr.c +11 -0
+static void l3_cat_init_feature(unsigned int eax, unsigned int ebx,
+                                unsigned int ecx, unsigned int edx,
+                                struct feat_list *pFeat,
+                                struct psr_socket_alloc_info *info)
+{
+    struct psr_cat_lvl_info l3_cat;
+    unsigned int socket;
+    uint64_t val;
+
+    if ( MAX_FEAT_INFO_SIZE < sizeof(struct psr_cat_lvl_info) )
+        return;

DYM BUILD_BUG_ON()? Also the MAX_FEAT_INFO_SIZE dimensioned array is one with unsigned int as the base type, which doesn't get reflected here.

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 247

Quoted 12 lines
xen/arch/x86/psr.c +11 -0
+    /* No valid value so do not enable feature. */
+    if ( 0 == eax || 0 == edx )
+        return;
+
+    l3_cat.cbm_len = (eax & 0x1f) + 1;
+    l3_cat.cos_max = min(opt_cos_max, edx & 0xffff);
 
+    /* cos=0 is reserved as default cbm(all ones). */
+    pFeat->cos_reg_val[0] = (1ull << l3_cat.cbm_len) - 1;
+
+    pFeat->feature = PSR_SOCKET_L3_CAT;
+    set_bit(PSR_SOCKET_L3_CAT, &(info->features));

Does this need to be set_bit(), or would __set_bit() suffice? Also there are many cases of this strange &() construct - the parentheses are pointless there.

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+    if ( (ecx & PSR_CAT_CDP_CAPABILITY) && (opt_psr & PSR_CDP) &&
+         !test_bit(PSR_SOCKET_L3_CDP, &(info->features)) )
+    {
+        /* Data */
+        pFeat->cos_reg_val[0] = (1ull << l3_cat.cbm_len) - 1;
+        /* Code */
+        pFeat->cos_reg_val[1] = (1ull << l3_cat.cbm_len) - 1;
+
+        /* We only write mask1 since mask0 is always all ones by default. */
+        wrmsrl(MSR_IA32_PSR_L3_MASK(1),

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 268

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+               (1ull << l3_cat.cbm_len) - 1);
+        rdmsrl(MSR_IA32_PSR_L3_QOS_CFG, val);
+        wrmsrl(MSR_IA32_PSR_L3_QOS_CFG, val | (1 << PSR_L3_QOS_CDP_ENABLE_BIT));
+
+        /* Cut half of cos_max when CDP is enabled. */
+        l3_cat.cos_max >>= 1;
+
+        pFeat->feature = PSR_SOCKET_L3_CDP;
+        set_bit(PSR_SOCKET_L3_CDP, &(info->features));
+        clear_bit(PSR_SOCKET_L3_CAT, &(info->features));

A few lines up you set this bit, and now you clear it again. Please instead set it in an else branch below.

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 271

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+    }
+
+    memcpy(pFeat->feat_info, &l3_cat, sizeof(struct psr_cat_lvl_info));

Aforementioned BUILD_BUG_ON() would probably bets be placed right ahead of this memcpy().

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 290

Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+static int l3_cat_compare_mask(uint64_t *mask, struct feat_list *pFeat,
+                               unsigned int cos, bool_t *found)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));

Already here I think this memcpy()ing gets unwieldy. Can't you simply make the structure field a union of all types of interest?

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 301

Quoted 11 lines
xen/arch/x86/psr.c +11 -0
+    l3_def_cbm = (1ull << cat_info.cbm_len) - 1;
+
+    /* CDP */
+    if ( pFeat->feature == PSR_SOCKET_L3_CDP )
+    {
+        if ( cos > cat_info.cos_max )
+        {
+            if ( mask[0] != l3_def_cbm ||
+                 mask[1] != l3_def_cbm )
+            {
+                *found = 0;

false and true for literal boolean values please.

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 302

Quoted 1 line
xen/arch/x86/psr.c +1 -0
+                printk(XENLOG_ERR "CDP exceed cos max.\n");

I haven't figured yet at what times this function will get called, but I suspect it's inappropriate for it to log messages.

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 306

Quoted 4 lines
xen/arch/x86/psr.c +4 -0
+                return -ENOENT;
+            }
+            *found = 1;
+            return 2;

Who or what is 2?

Quoted 18 lines
xen/arch/x86/psr.c +18 -0
+        }
+
+        if ( mask[0] == pFeat->cos_reg_val[cos * 2] &&
+             mask[1] == pFeat->cos_reg_val[cos * 2 + 1])
+            *found = 1;
+        else
+            *found = 0;
+
+        return 2;
+    }
+
+    /* CAT */
+    if ( cos > cat_info.cos_max )
+    {
+        if ( mask[0] != l3_def_cbm )
+        {
+            *found = 0;
+            printk(XENLOG_ERR "CAT exceed cos max.\n");

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 334

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+            return -ENOENT;
+        }
+        *found = 1;
+        return 1;
+    }
+
+    if ( mask[0] == pFeat->cos_reg_val[cos] )
+        *found = 1;
+    else
+        *found = 0;

Please use a simple assignment in cases like this, instead of if/else.

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+static unsigned int l3_cat_exceed_range(uint64_t *mask, struct feat_list *pFeat,
+                                        unsigned int cos)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 367

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));
+    l3_def_cbm = (1ull << cat_info.cbm_len) - 1;
+
+    /* CDP */
+    if ( pFeat->feature == PSR_SOCKET_L3_CDP )
+    {
+        if ( cos > cat_info.cos_max )
+            if ( mask[0] != l3_def_cbm ||
+                 mask[1] != l3_def_cbm )

Please combine such if()s.

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 374

Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+                /*
+                 * Exceed cos_max and value to set is not default,
+                 * return error.
+                 */
+                return 0;
+
+        return 2;

Same question as above - who or what is 2?

Quoted 12 lines
xen/arch/x86/psr.c +12 -0
+static int l3_cat_get_old_set_new(uint64_t *mask,
+                                  struct feat_list *pFeat,
+                                  unsigned int old_cos,
+                                  enum mask_type type,
+                                  uint64_t m)
+{
+    struct psr_cat_lvl_info cat_info;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));
+
+    if ( type == PSR_MASK_TYPE_L3_DATA ||
+         type == PSR_MASK_TYPE_L3_CODE ||

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 446

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+         type == PSR_MASK_TYPE_L3_CBM )
+    {
+        if ( !psr_check_cbm(cat_info.cbm_len, m) )
+            return -EINVAL;
+    }
+
+    if ( ( type == PSR_MASK_TYPE_L3_DATA ||
+         type == PSR_MASK_TYPE_L3_CODE ) &&
+         pFeat->feature != PSR_SOCKET_L3_CDP )
+        return -ENXIO;

Please either combine the two if()s into a single switch() (with appropriate fall through behavior) or, perhaps better, move this second if() - suitably shrunk - past the following one. This is to avoid redundant code, which makes things more difficult to follow.

Quoted 22 lines
xen/arch/x86/psr.c +22 -0
+    /* CDP */
+    if ( pFeat->feature == PSR_SOCKET_L3_CDP )
+    {
+        if ( old_cos > cat_info.cos_max )
+        {
+            /* Data */
+            mask[0] = pFeat->cos_reg_val[0];
+            /* Code */
+            mask[1] = pFeat->cos_reg_val[1];
+        }
+        else
+        {
+            /* Data */
+            mask[0] = pFeat->cos_reg_val[old_cos * 2];
+            /* Code */
+            mask[1] = pFeat->cos_reg_val[old_cos * 2 + 1];
+        }
+
+        /* Set new mask */
+        if ( type == PSR_MASK_TYPE_L3_DATA )
+            mask[0] = m;
+        if ( type == PSR_MASK_TYPE_L3_CODE )

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 479

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+            mask[1] = m;
+
+        return 2;
+    }
+
+    /* CAT */
+    if ( old_cos > cat_info.cos_max )
+        mask[0] =  pFeat->cos_reg_val[0];
+    else
+        mask[0] =  pFeat->cos_reg_val[old_cos];

Across the entire function - wouldn't it be easier to range check old_cos once up front, forcing it to zero if the check fails? That would reduce code size to a better readable amount. (And just to avoid any misunderstanding - comments like this also may apply to other functions; I'm not going to repeat them there.)

Also note that there are undue double blanks in here.

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 482

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+    if ( type == PSR_MASK_TYPE_L3_CBM )
+        mask[0] = m;

This overwriting behavior also looks quite strange to me. What's the purpose? And if this really is meant to be that way, why is this not (leaving aside the other suggested adjustment)

    if ( type == PSR_MASK_TYPE_L3_CBM )
        mask[0] = m;
    else if ( old_cos > cat_info.cos_max )
        mask[0] = pFeat->cos_reg_val[0];
    else
        mask[0] = pFeat->cos_reg_val[old_cos];

?

Quoted 13 lines
xen/arch/x86/psr.c +13 -0
+static int l3_cat_get_feat_info(struct feat_list *pFeat, enum mask_type type,
+                                uint32_t *dat0, uint32_t *dat1,
+                                uint32_t *dat2)
+{
+    struct psr_cat_lvl_info cat_info;
+
+    if ( type != PSR_MASK_TYPE_L3_DATA &&
+         type != PSR_MASK_TYPE_L3_CODE &&
+         type != PSR_MASK_TYPE_L3_CBM )
+        return 0;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));
+

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 548

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+    *dat0 = cat_info.cbm_len;
+    *dat1 = cat_info.cos_max;
+
+    if ( pFeat->feature == PSR_SOCKET_L3_CDP )
+        *dat2 |= XEN_SYSCTL_PSR_CAT_L3_CDP;
+    else
+        *dat2 = 0;
+
+    return 1;
+}

The latest here it becomes clear that you want to use switch() much more widely.

xen/arch/x86/psr.c void psr_free_rmid(struct domain *d) · line 750

Quoted 12 lines
xen/arch/x86/psr.c +5 -2
 static inline void psr_assoc_init(void)
 {
     struct psr_assoc *psra = &this_cpu(psr_assoc);
+    struct psr_socket_alloc_info *info;
+    unsigned int cos_max;
+    unsigned int socket;
 
-    if ( cat_socket_info )
+    if ( socket_alloc_info )
     {
-        unsigned int socket = cpu_to_socket(smp_processor_id());
+        socket = cpu_to_socket(smp_processor_id());

What's the point of moving the variable declaration? Quite the other way around, the new variable you add could - afaict - all live in this more narrow scope. And info could presumably become a pointer to const.

Quoted 15 lines
xen/arch/x86/psr.c +5 -5
-int psr_get_cat_l3_info(unsigned int socket, uint32_t *cbm_len,
-                        uint32_t *cos_max, uint32_t *flags)
+int psr_get_info(unsigned int socket, enum mask_type type,
+                 uint32_t *dat0, uint32_t *dat1,
+                 uint32_t *dat2)
 {
-    struct psr_cat_socket_info *info = get_cat_socket_info(socket);
+    struct psr_socket_alloc_info *info = get_socket_alloc_info(socket);
+    struct feat_list *pTmp;
 
     if ( IS_ERR(info) )
         return PTR_ERR(info);
 
-    *cbm_len = info->cbm_len;
-    *cos_max = info->cos_max;

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 827

Quoted 10 lines
xen/arch/x86/psr.c +6 -3
+    if ( NULL == info->pFeat || NULL == info->pFeat->pNext)
+       return -ENODEV;
 
-    *flags = 0;
-    if ( cdp_is_enabled(socket) )
-        *flags |= XEN_SYSCTL_PSR_CAT_L3_CDP;
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        if ( pTmp->ops.get_feat_info(pTmp, type, dat0, dat1, dat2) )

Considering that this is one of the generic ops: What guarantees that 3 elements will suffice going forward? I.e. why is this not an array?

Quoted 24 lines
xen/arch/x86/psr.c +8 -14
-int psr_get_l3_cbm(struct domain *d, unsigned int socket,
-                   uint64_t *cbm, enum cbm_type type)
+static int get_old_set_new(uint64_t *mask,
+                           struct psr_socket_alloc_info *info,
+                           unsigned int old_cos,
+                           enum mask_type type,
+                           uint64_t m)
 {
-    struct psr_cat_socket_info *info = get_cat_socket_info(socket);
-    bool_t cdp_enabled = cdp_is_enabled(socket);
-
-    if ( IS_ERR(info) )
-        return PTR_ERR(info);
+    struct feat_list *pTmp;
+    int ret;
 
-    switch ( type )
-    {
-    case PSR_CBM_TYPE_L3:
-        if ( cdp_enabled )
-            return -EXDEV;
-        *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].cbm;
-        break;
+    if ( !mask )

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 849

Quoted 10 lines
xen/arch/x86/psr.c +3 -6
+        return -EINVAL;
 
-    case PSR_CBM_TYPE_L3_CODE:
-        if ( !cdp_enabled )
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].cbm;
-        else
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].code;
-        break;
+    if ( !info || !info->pFeat )
+        return -ENODEV;

Earlier on you used (slightly oddly arranged) checks against NULL. Please be consistent (and I'd prefer if you used ! across the board).

Quoted 7 lines
xen/arch/x86/psr.c +1 -6
-    case PSR_CBM_TYPE_L3_DATA:
-        if ( !cdp_enabled )
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].cbm;
-        else
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].data;
-        break;
+    pTmp = info->pFeat->pNext;

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 859

Quoted 10 lines
xen/arch/x86/psr.c +7 -2
+    while ( pTmp )
+    {
+        /* mask getting and setting order is same as feature list */
+        ret = pTmp->ops.get_old_set_new(mask, pTmp, old_cos, type, m);
+        if ( ret < 0 )
+            return ret;
 
-    default:
-        ASSERT_UNREACHABLE();
+        mask += ret;

With this - what ensures the array isn't going to be overrun?

And I'm also having some difficulty seeing how an array of outputs matches up with exactly one input (m). Can you explain this please?

Quoted 25 lines
xen/arch/x86/psr.c +10 -11
-static bool_t psr_check_cbm(unsigned int cbm_len, uint64_t cbm)
+int psr_get_val(struct domain *d, unsigned int socket,
+                uint64_t *val, enum mask_type type)
 {
-    unsigned int first_bit, zero_bit;
-
-    /* Set bits should only in the range of [0, cbm_len). */
-    if ( cbm & (~0ull << cbm_len) )
-        return 0;
+    struct psr_socket_alloc_info *info = get_socket_alloc_info(socket);
+    unsigned int cos = d->arch.psr_cos_ids[socket];
+    struct feat_list *pTmp;
+    int ret;
 
-    /* At least one bit need to be set. */
-    if ( cbm == 0 )
-        return 0;
+    if ( IS_ERR(info) )
+        return PTR_ERR(info);
 
-    first_bit = find_first_bit(&cbm, cbm_len);
-    zero_bit = find_next_zero_bit(&cbm, cbm_len, first_bit);
+    if ( NULL == info->pFeat || NULL == info->pFeat->pNext )
+        return -ENODEV;
 

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 885

Quoted 10 lines
xen/arch/x86/psr.c +6 -4
-    /* Set bits should be contiguous. */
-    if ( zero_bit < cbm_len &&
-         find_next_bit(&cbm, cbm_len, zero_bit) < cbm_len )
-        return 0;
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        ret = pTmp->ops.get_val(pTmp, cos, type, val);
+        if ( ret < 0 )
+            return -EINVAL;

Why don't you return "ret" here?

Quoted 1 line

+ else if ( ret > 0 )

Pointless "else".

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 903

Quoted 2 lines
xen/arch/x86/psr.c +1 -1
-static void do_write_l3_cbm(void *data)
+static void do_write_psr_ref(void *data)

What does "_ref" stand for?

Quoted 50 lines
xen/arch/x86/psr.c +39 -6
-static int find_cos(struct psr_cat_cbm *map, unsigned int cos_max,
-                    uint64_t cbm_code, uint64_t cbm_data, bool_t cdp_enabled)
+static int find_cos(uint64_t *mask, enum mask_type type,
+                    struct psr_socket_alloc_info *info)
 {
     unsigned int cos;
+    struct psr_ref *map = info->cos_ref;
+    struct feat_list *pTmp;
+    uint64_t *pMask = mask;
+    int ret;
+    bool_t found = 0;
+    unsigned int cos_max = 0;
+
+    /* cos_max is the max COS of the feature to be set. */
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        cos_max = pTmp->ops.get_cos_max_as_type(pTmp, type);
+        if ( cos_max > 0 )
+            break;
+
+        pTmp = pTmp->pNext;
+    }
 
+    pTmp = info->pFeat->pNext;
     for ( cos = 0; cos <= cos_max; cos++ )
     {
-        if ( (map[cos].ref || cos == 0) &&
-             ((!cdp_enabled && map[cos].cbm == cbm_code) ||
-              (cdp_enabled && map[cos].code == cbm_code &&
-                              map[cos].data == cbm_data)) )
+        if ( 0 != cos && 0 == map[cos].ref )
+            continue;
+
+        while ( pTmp )
+        {
+            /*
+             * Compare mask according to feature list order.
+             * We must follow this order because mask is assembled
+             * as this order in get_old_set_new().
+             */
+            ret = pTmp->ops.compare_mask(pMask, pTmp, cos, &found);
+            if ( ret < 0 )
+                return ret;
+
+            /* If fail to match, go to next cos to compare. */
+            if ( !found )
+                break;
+
+            pMask += ret;

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 1006

Quoted 10 lines
xen/arch/x86/psr.c +9 -0
+            pTmp = pTmp->pNext;
+        }
+
+        /* Every feature can match, this cos is what we find. */
+        if ( found )
             return cos;
+
+        /* Not found, need find again from beginning. */
+        pTmp = info->pFeat->pNext;
+        pMask = mask;

Please do once (at the appropriate place inside the outer loop) what you have done prior to the loop and repeat here. As already alluded to - limiting the scope of variables also helps ensuring that things work as intended.

Quoted 3 lines
     }

+ printk(XENLOG_INFO "%s: cannot find cos.\n", __func__);

Again a questionable log message with a questionable use of __func__. In any case log messages should normally not have a full stop at their end.

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 1016

Quoted 6 lines
xen/arch/x86/psr.c +3 -2
-static int pick_avail_cos(struct psr_cat_cbm *map, unsigned int cos_max,
-                          unsigned int old_cos)
+static int check_exceed_range(uint64_t *mask, struct feat_list *pTmp,
+                              unsigned int cos)
 {
+    unsigned int ret = 0;

Pointless initializer.

Quoted 26 lines
xen/arch/x86/psr.c +23 -0
+static int alloc_new_cos(struct psr_socket_alloc_info *info,
+                         uint64_t *mask, unsigned int old_cos,
+                         enum mask_type type)
+{
+    unsigned int cos_max = 0;
+    struct feat_list *pTmp;
     unsigned int cos;
+    struct psr_ref *map = info->cos_ref;
+
+    /*
+     * cos_max is the max COS of the feature to be set.
+     */
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        cos_max = pTmp->ops.get_cos_max_as_type(pTmp, type);
+        if ( cos_max > 0 )
+            break;
+
+        pTmp = pTmp->pNext;
+    }
+
+    if ( !cos_max )
+        return -ENOENT;
 
     /* If old cos is referred only by the domain, then use it. */

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 1066

Quoted 10 lines
xen/arch/x86/psr.c +7 -0
     if ( map[old_cos].ref == 1 && old_cos != 0 )
+    {
+        pTmp = info->pFeat->pNext;
+        if ( check_exceed_range(mask, pTmp, old_cos) )
+            goto find_new;
+
         return old_cos;
+    }
 
+find_new:

Labels indented by at least one blank please. In this case, though, it looks like you could easily do without any label and goto.

Quoted 43 lines
xen/arch/x86/psr.c +7 -29
-int psr_set_l3_cbm(struct domain *d, unsigned int socket,
-                   uint64_t cbm, enum cbm_type type)
+int psr_set_val(struct domain *d, unsigned int socket,
+                uint64_t val, enum mask_type type)
 {
-    unsigned int old_cos, cos_max;
+    unsigned int old_cos;
     int cos, ret;
-    uint64_t cbm_data, cbm_code;
-    bool_t cdp_enabled = cdp_is_enabled(socket);
-    struct psr_cat_cbm *map;
-    struct psr_cat_socket_info *info = get_cat_socket_info(socket);
+    struct psr_ref *map;
+    uint64_t *mask;
+    struct psr_socket_alloc_info *info = get_socket_alloc_info(socket);
 
     if ( IS_ERR(info) )
         return PTR_ERR(info);
 
-    if ( !psr_check_cbm(info->cbm_len, cbm) )
-        return -EINVAL;
-
-    if ( !cdp_enabled && (type == PSR_CBM_TYPE_L3_CODE ||
-                          type == PSR_CBM_TYPE_L3_DATA) )
-        return -ENXIO;
-
-    cos_max = info->cos_max;
+    map = info->cos_ref;
     old_cos = d->arch.psr_cos_ids[socket];
-    map = info->cos_to_cbm;
-
-    switch ( type )
-    {
-    case PSR_CBM_TYPE_L3:
-        cbm_code = cbm;
-        cbm_data = cbm;
-        break;
-
-    case PSR_CBM_TYPE_L3_CODE:
-        cbm_code = cbm;
-        cbm_data = map[old_cos].data;
-        break;
-

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 1101

Quoted 10 lines
xen/arch/x86/psr.c +1 -8
-    case PSR_CBM_TYPE_L3_DATA:
-        cbm_code = map[old_cos].code;
-        cbm_data = cbm;
-        break;
 
-    default:
-        ASSERT_UNREACHABLE();
-        return -EINVAL;
-    }
+    /* Considering CDP liking features */

DYM "like"? And what do you want to say with this comment?

Quoted 1 line

+ mask = xzalloc_array(uint64_t, info->nr_feat * 2);

Perhaps it's related to the doubling of nr_feat here? If so - what precludes another future feature to require 3 or 4 slots per element? I think this needs abstracting out, now that you create an abstract framework.

Quoted 1 line

+ if ( (ret = get_old_set_new(mask, info, old_cos, type, val)) != 0 )

What if the xzalloc_array() returned NULL?

Quoted 51 lines
xen/arch/x86/psr.c +14 -17
+        return ret;
 
-    spin_lock(&info->cbm_lock);
-    cos = find_cos(map, cos_max, cbm_code, cbm_data, cdp_enabled);
+    spin_lock(&info->mask_lock);
+    cos = find_cos(mask, type, info);
     if ( cos >= 0 )
     {
         if ( cos == old_cos )
         {
-            spin_unlock(&info->cbm_lock);
+            spin_unlock(&info->mask_lock);
+            xfree(mask);
             return 0;
         }
     }
     else
     {
-        cos = pick_avail_cos(map, cos_max, old_cos);
+        cos = alloc_new_cos(info, mask, old_cos, type);
         if ( cos < 0 )
         {
-            spin_unlock(&info->cbm_lock);
+            spin_unlock(&info->mask_lock);
+            xfree(mask);
             return cos;
         }
 
-        /* We try to avoid writing MSR. */
-        if ( (cdp_enabled &&
-             (map[cos].code != cbm_code || map[cos].data != cbm_data)) ||
-             (!cdp_enabled && map[cos].cbm != cbm_code) )
+        /* Write all features mask MSRs corresponding to the COS */
+        ret = write_psr_ref(socket, cos, mask);
+        if ( ret )
         {
-            ret = write_l3_cbm(socket, cos, cbm_code, cbm_data, cdp_enabled);
-            if ( ret )
-            {
-                spin_unlock(&info->cbm_lock);
-                return ret;
-            }
-            map[cos].code = cbm_code;
-            map[cos].data = cbm_data;
+            spin_unlock(&info->mask_lock);
+            xfree(mask);
+            return ret;
         }
     }
 
     map[cos].ref++;

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 1146

Quoted 10 lines
xen/arch/x86/psr.c +4 -1
     map[old_cos].ref--;
-    spin_unlock(&info->cbm_lock);
+
+    spin_unlock(&info->mask_lock);
 
     d->arch.psr_cos_ids[socket] = cos;
+    xfree(mask);
+    mask = NULL;
 
     return 0;

I don't think setting mask to NULL right ahead of a return statement is very helpful.

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1213

Quoted 2 lines
xen/arch/x86/psr.c +1 -1
-static void cat_cpu_init(void)
+static void internal_cpu_init(void)

Is "internal_" really a useful prefix here?

Quoted 24 lines
 {
     unsigned int eax, ebx, ecx, edx;
xen/arch/x86/psr.c +4 -5
-    struct psr_cat_socket_info *info;
+    struct psr_socket_alloc_info *info;
     unsigned int socket;
     unsigned int cpu = smp_processor_id();
-    uint64_t val;
     const struct cpuinfo_x86 *c = cpu_data + cpu;
+    struct feat_list *pTmp;
 
     if ( !cpu_has(c, X86_FEATURE_PQE) || c->cpuid_level < PSR_CPUID_LEVEL_CAT )
         return;
 
     socket = cpu_to_socket(cpu);
-    if ( test_bit(socket, cat_socket_enable) )
+    info = socket_alloc_info + socket;
+    if ( info->features )
         return;
 
     cpuid_count(PSR_CPUID_LEVEL_CAT, 0, &eax, &ebx, &ecx, &edx);
     if ( ebx & PSR_RESOURCE_TYPE_L3 )
     {
-        cpuid_count(PSR_CPUID_LEVEL_CAT, 1, &eax, &ebx, &ecx, &edx);
-        info = cat_socket_info + socket;

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1235

Quoted 10 lines
xen/arch/x86/psr.c +3 -7
-        info->cbm_len = (eax & 0x1f) + 1;
-        info->cos_max = min(opt_cos_max, edx & 0xffff);
-
-        info->cos_to_cbm = temp_cos_to_cbm;
-        temp_cos_to_cbm = NULL;
-        /* cos=0 is reserved as default cbm(all ones). */
-        info->cos_to_cbm[0].cbm = (1ull << info->cbm_len) - 1;
+        pTmp = pL3CAT;
+        if ( !pTmp )
+            return;

You check the allocation result in internal_cpu_prepare() - can you really get here with this being NULL? If you can't, drop the if() (or make it an ASSERT()).

Quoted 20 lines
xen/arch/x86/psr.c +1 -18
+        pL3CAT = NULL;
 
-        spin_lock_init(&info->cbm_lock);
-
-        set_bit(socket, cat_socket_enable);
-
-        if ( (ecx & PSR_CAT_CDP_CAPABILITY) && (opt_psr & PSR_CDP) &&
-             cdp_socket_enable && !test_bit(socket, cdp_socket_enable) )
-        {
-            info->cos_to_cbm[0].code = (1ull << info->cbm_len) - 1;
-            info->cos_to_cbm[0].data = (1ull << info->cbm_len) - 1;
-
-            /* We only write mask1 since mask0 is always all ones by default. */
-            wrmsrl(MSR_IA32_PSR_L3_MASK(1), (1ull << info->cbm_len) - 1);
-
-            rdmsrl(MSR_IA32_PSR_L3_QOS_CFG, val);
-            wrmsrl(MSR_IA32_PSR_L3_QOS_CFG, val | (1 << PSR_L3_QOS_CDP_ENABLE_BIT));
-
-            /* Cut half of cos_max when CDP is enabled. */
-            info->cos_max >>= 1;

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1241

Quoted 10 lines
xen/arch/x86/psr.c +4 -6
-
-            set_bit(socket, cdp_socket_enable);
-        }
-        printk(XENLOG_INFO "CAT: enabled on socket %u, cos_max:%u, cbm_len:%u, CDP:%s\n",
-               socket, info->cos_max, info->cbm_len,
-               cdp_is_enabled(socket) ? "on" : "off");
+        /* Initialize this feature according to CPUID. */
+        cpuid_count(PSR_CPUID_LEVEL_CAT, 1, &eax, &ebx, &ecx, &edx);
+        pTmp->ops = l3_cat_ops;
+        pTmp->ops.init_feature(eax, ebx, ecx, edx, pTmp, info);

This function can return without having consumed pTmp, which means you'd leak the memory in that case.

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1265

Quoted 8 lines
xen/arch/x86/psr.c +2 -5
-static void __init psr_cat_free(void)
+static void __init psr_free(void)
 {
-    xfree(cat_socket_enable);
-    cat_socket_enable = NULL;
-    xfree(cat_socket_info);
-    cat_socket_info = NULL;
+    int i;

unsigned int

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1280

Quoted 4 lines
xen/arch/x86/psr.c +2 -1
-static void __init init_psr_cat(void)
+static void __init init_psr(void)
 {
+    int i;

Again.

Quoted 13 lines
xen/arch/x86/psr.c +4 -3
+
     if ( opt_cos_max < 1 )
     {
         printk(XENLOG_INFO "CAT: disabled, cos_max is too small\n");
         return;
     }
 
-    cat_socket_enable = xzalloc_array(unsigned long, BITS_TO_LONGS(nr_sockets));
-    cat_socket_info = xzalloc_array(struct psr_cat_socket_info, nr_sockets);
-    cdp_socket_enable = xzalloc_array(unsigned long, BITS_TO_LONGS(nr_sockets));
+    socket_alloc_info = xzalloc_array(struct psr_socket_alloc_info, nr_sockets);
+
+    if ( !socket_alloc_info )

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1298

Quoted 10 lines
xen/arch/x86/psr.c +7 -2
+    {
+        printk(XENLOG_INFO "Fail to alloc socket_alloc_info!\n");
+        return;
+    }
 
-    if ( !cat_socket_enable || !cat_socket_info )
-        psr_cat_free();
+    for ( i = 0; i < nr_sockets; i++ )
+    {
+        socket_alloc_info[i].pFeat =  xzalloc(struct feat_list);

What's the point of this dummy list element?

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1301

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+        if ( NULL == socket_alloc_info[i].pFeat )
+        {
+            printk(XENLOG_INFO "Fail to alloc pFeat!\n");

How would the reader of the log easily associate such a message with something having gone wrong in PSR initialization?

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1304

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+            return;
+        }
+        socket_alloc_info[i].pFeat->pNext = NULL;

This is redundant with the use of xzalloc().

Quoted 6 lines

static void psr_cpu_init(void)
{

xen/arch/x86/psr.c +2 -2
-    if ( cat_socket_info )
-        cat_cpu_init();
+    unsigned int socket;
+    struct psr_socket_alloc_info *info;

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1326

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+
+    if ( socket_alloc_info )
+    {
+        socket = cpu_to_socket(smp_processor_id());
+        info = socket_alloc_info + socket;
+        info->cos_ref = temp_cos_ref;
+        temp_cos_ref = NULL;
+        spin_lock_init(&info->mask_lock);
+
+        internal_cpu_init();

Why is everything ahead of this call not part of that function?

Quoted 5 lines
xen/include/asm-x86/psr.h +0 -0
@@ -46,10 +46,10 @@ struct psr_cmt {
     struct psr_cmt_l3 l3;
 };

xen/include/asm-x86/psr.h struct psr_cmt { · line 53

Quoted 10 lines
xen/include/asm-x86/psr.h +4 -4
 
-enum cbm_type {
-    PSR_CBM_TYPE_L3,
-    PSR_CBM_TYPE_L3_CODE,
-    PSR_CBM_TYPE_L3_DATA,
+enum mask_type {
+    PSR_MASK_TYPE_L3_CBM,
+    PSR_MASK_TYPE_L3_CODE,
+    PSR_MASK_TYPE_L3_DATA,
 };

The enum tag is now too generic a name - please make this psr_mask_type or something similar.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#2 lore raw

Hi, Jan,

Thank you very much for reviewing my patches in details! Please check my comments below.

Quoted 34 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 25.08.16 at 07:22, <[email protected]> wrote:
xen/arch/x86/psr.c +19 -8
@@ -23,22 +23,116 @@
 #define PSR_CAT        (1<<1)
 #define PSR_CDP        (1<<2)
 
-struct psr_cat_cbm {
-    union {
-        uint64_t cbm;
-        struct {
-            uint64_t code;
-            uint64_t data;
-        };
-    };
+/*
+ * To add a new PSR feature, please follow below steps:
+ * 1. Implement feature specific callback functions listed in
+ *    "struct feat_ops";
+ * 2. Implement a feature specific "struct feat_ops" object and register
+ *    feature specific callback functions into it;
+ * 3. Delcare feat_list object for the feature and malloc memory for it
+ *    in internal_cpu_prepare(). Correspondingly, free them in
+ *    free_feature();
+ * 4. Add feature initialization codes in internal_cpu_init().
+ */
+
+struct feat_list;
+struct psr_socket_alloc_info;
+
+/* Every feature enabled should implement such ops and callback functions. */
+struct feat_ops {
+    /*
+     * init_feature is used in cpu initialization process to do feature
Quoted 9 lines

xen/arch/x86/psr.c line 49

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+     * specific initialization works.
+     */
+    void (*init_feature)(unsigned int eax, unsigned int ebx,
+                         unsigned int ecx, unsigned int edx,
+                         struct feat_list *pFeat,

Here and below - we don't normally use identifiers with capital letters in the middle.

Thanks! I will fix it.

Quoted 9 lines

xen/arch/x86/psr.c line 54

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+                         struct psr_socket_alloc_info *info);
+    /*
+     * get_old_set_new is used in set value process to get all features'
+     * COS registers values according to original cos id of the domain.
+     * Then, assemble them into an mask array as feature list order.

This sentence in particular doesn't make any sense to me. What follows below also looks like it is in need of improvement.

Do you mean the comments are not accurate? How about below description?

get_old_set_new will traverse all features in list. It is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

All the values are set into mask array according the traversal order, meaning the same order of feature list members.

For example, old_cos of the domain is 1. User wants to set L3 CAT CBM to 0x1ff. The original COS registers like below.

        -----------------------
        | COS 0 | COS 1 | ... |
        -----------------------
L3 CAT  | 0x7ff | 0x3ff | ... |
        -----------------------
L2 CAT  | 0xff  | 0x3f  | ... |
        -----------------------

Then, mask array should be:
mask[0]: 0x1ff
mask[1]: 0x3f

Then, we can use this mask array to find if there is matching COS through compare_mask().

Quoted 16 lines
Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+     * Then, set the new feature value into feature corresponding position
+     * in the array. This function is used to pair with compare_mask.
+     */
+    int (*get_old_set_new)(uint64_t *mask,
+                           struct feat_list *pFeat,

xen/arch/x86/psr.c line 69

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+                           unsigned int old_cos,
+                           enum mask_type type,
+                           uint64_t m);
+    /*
+     * compare_mask is used to in set value process to compare if the
+     * input mask array can match all the features' COS registers values
+     * according to input cos id.
+     */
+    int (*compare_mask)(uint64_t *mask, struct feat_list *pFeat,
+                        unsigned int cos, bool_t *found);
Quoted 4 lines

For a "compare" function I'd expect a word on the meaning of the return value, and I'd expect most if not all pointer parameters to be pointers to const.

Thanks! I will explain the return value.

Quoted 2 lines

Also plain bool please.

Thanks! I will fix it.

Quoted 9 lines

xen/arch/x86/psr.c line 75

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+    /*
+     * get_cos_max_as_type is used to get the cos_max value of the feature
+     * according to input mask_type.
+     */
+    unsigned int (*get_cos_max_as_type)(struct feat_list *pFeat,
+                                        enum mask_type type);

DYM mean "get_cos_max_from_type()"?

Yes, correct. I will revise the function name.

Quoted 11 lines

xen/arch/x86/psr.c line 85

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+    /*
+     * exceed_range is used to check if the input cos id exceeds the
+     * feature's cos_max and if the input mask value is not the default one.
+     * Even if the associated cos exceeds the cos_max, HW can work as default
+     * value. That is the reason we need check is mask value is default one.
+     * If both criteria are fulfilled, that means the input exceeds the
+     * range.
+     */
+    unsigned int (*exceed_range)(uint64_t *mask, struct feat_list *pFeat,
+                                 unsigned int cos);
Quoted 8 lines

According to the comment this is kind of a predicate, which seems unlikely to return an unsigned value. In fact without a word on the return value I'd expect such to return bool. And I'd also expect the name to reflect the purpose, i.e. "exceeds_name()". Plus just like for compare above I wonder whether come or all of the parameters should be pointers to const (please go over the entire patch and do constification wherever possible/sensible).

Yes, you are right. I will change the function type to bool and add const for not changed input pointers.

This function is used to check if the input cos id exceeds the cos_max. If yes and the set value is not default value, we should return error. So, I think to change the function name to exceed_cos_max(). How do you think?

Quoted 6 lines

xen/arch/x86/psr.c line 88

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+    /*
+     * write_msr is used to write feature specific MSR registers.
+     */

This is a single line comment.

Yes, will change the format. Thanks!

Quoted 8 lines

xen/arch/x86/psr.c line 90

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+    int (*write_msr)(unsigned int cos, uint64_t *mask,
+                     struct feat_list *pFeat);

Such write operations don't normally alter the value to be written. If that's different here, that's perhaps worth a word in the comment. If it isn't, then I don't see why the address of the value gets passed instead of the value itself.

Quoted 7 lines

Oh, having gone further in the patch I see this is to update multiple MSRs. I guess this would best be reflected by making the parameter const uint64_t masks[] (albeit it's also not clear to me why this necessarily have to be masks kind items, and not just arbitrary values - proper choice of variable and parameter names helps the understanding).

Thanks for your suggestion! Will change it to uint64_t val[].

Quoted 5 lines

xen/arch/x86/psr.c line 109

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+#define MAX_FEAT_INFO_SIZE 8
+#define MAX_COS_REG_NUM  128

Are these numbers arbitrary, or based on something?

MAX_FEAT_INFO_SIZE is got from the sizeof(struct psr_cat_lvl_info) and consider the extension for future feature.

MAX_COS_REG_NUM is got from spec that the max COS registers number is 128 for all PSR features so far.

Quoted 4 lines

xen/arch/x86/psr.c line 127

Quoted 1 line
xen/arch/x86/psr.c +1 -0
+struct psr_socket_alloc_info {

I've yet to see whether the "alloc" in the name really makes sense.

Thanks! Will change it.

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/psr.c +0 -0
@@ -58,8 +150,427 @@ static unsigned int __read_mostly opt_cos_max = 255;
 static uint64_t rmid_mask;
 static DEFINE_PER_CPU(struct psr_assoc, psr_assoc);
 
Quoted 8 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 155

Quoted 4 lines
xen/arch/x86/psr.c +3 -1
-static struct psr_cat_cbm *temp_cos_to_cbm;
+static struct psr_ref *temp_cos_ref;
+/* Every feature has its own object. */
+struct feat_list *pL3CAT;

static. And a better name please, more in line with the other variable or similar purpose.

Thanks! Will modify it.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+/* Common functions for supporting feature callback functions. */
+static void add_feature(struct feat_list *pHead, struct feat_list *pTmp)
+{
+    if ( NULL == pHead || NULL == pTmp )
+        return;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 167

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+
+    while ( pHead->pNext )
+        pHead = pHead->pNext;
+
+    pHead->pNext = pTmp;
+}

Do you really need custom list management here?

It seems xen list interfaces require the input list be a double linked list but my list is a single linked list. Furthermore, I only need simple add to tail function and free function. So I create custom list management functions.

Quoted 15 lines
Quoted 15 lines
xen/arch/x86/psr.c +15 -0
+static void free_feature(struct psr_socket_alloc_info *info)
+{
+    struct feat_list *pTmp;
+    struct feat_list *pPrev;
+
+    if ( NULL == info )
+        return;
+
+    if ( NULL == info->pFeat )
+        return;
+
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        pPrev = pTmp;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 189

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+        pTmp = pTmp->pNext;
+        clear_bit(pPrev->feature, &(info->features));
+        xfree(pPrev);
+        pPrev = NULL;
+    }
+}

Why does info->pFeat not get freed here?

The info->pFeat is a redundant list head to facilitate list operations. It is only freed when socket info is freed.

With this list head, the advantage is that we can insert/delete the first element same as others without special operations.

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+static void l3_cat_init_feature(unsigned int eax, unsigned int ebx,
+                                unsigned int ecx, unsigned int edx,
+                                struct feat_list *pFeat,
+                                struct psr_socket_alloc_info *info)
+{
+    struct psr_cat_lvl_info l3_cat;
+    unsigned int socket;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 234

Quoted 4 lines
xen/arch/x86/psr.c +4 -0
+    uint64_t val;
+
+    if ( MAX_FEAT_INFO_SIZE < sizeof(struct psr_cat_lvl_info) )
+        return;

DYM BUILD_BUG_ON()? Also the MAX_FEAT_INFO_SIZE dimensioned array is one with unsigned int as the base type, which doesn't get reflected here.

I will use BUILD_BUG_ON() for the error case.

As my explanation for MAX_FEAT_INFO_SIZE above, it reflects a size more than struct psr_cat_lvl_info. So, if MAX_FEAT_INFO_SIZE is bigger than the sizeof(struct psr_cat_lvl_info), the following operations on feat_info[] should be good.

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/psr.c +6 -0
+    /* No valid value so do not enable feature. */
+    if ( 0 == eax || 0 == edx )
+        return;
+
+    l3_cat.cbm_len = (eax & 0x1f) + 1;
+    l3_cat.cos_max = min(opt_cos_max, edx & 0xffff);
 
Quoted 10 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 247

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+    /* cos=0 is reserved as default cbm(all ones). */
+    pFeat->cos_reg_val[0] = (1ull << l3_cat.cbm_len) - 1;
+
+    pFeat->feature = PSR_SOCKET_L3_CAT;
+    set_bit(PSR_SOCKET_L3_CAT, &(info->features));

Does this need to be set_bit(), or would __set_bit() suffice? Also there are many cases of this strange &() construct - the parentheses are pointless there.

This is inherited from original codes. After checking codes, I think this can be replaced by non-atomic operation __set_bit(). Thanks!

Will remove unnecessary parentheses.

Quoted 15 lines
Quoted 15 lines
xen/arch/x86/psr.c +15 -0
+    if ( (ecx & PSR_CAT_CDP_CAPABILITY) && (opt_psr & PSR_CDP) &&
+         !test_bit(PSR_SOCKET_L3_CDP, &(info->features)) )
+    {
+        /* Data */
+        pFeat->cos_reg_val[0] = (1ull << l3_cat.cbm_len) - 1;
+        /* Code */
+        pFeat->cos_reg_val[1] = (1ull << l3_cat.cbm_len) - 1;
+
+        /* We only write mask1 since mask0 is always all ones by default. */
+        wrmsrl(MSR_IA32_PSR_L3_MASK(1),
+               (1ull << l3_cat.cbm_len) - 1);
+        rdmsrl(MSR_IA32_PSR_L3_QOS_CFG, val);
+        wrmsrl(MSR_IA32_PSR_L3_QOS_CFG, val | (1 << PSR_L3_QOS_CDP_ENABLE_BIT));
+
+        /* Cut half of cos_max when CDP is enabled. */
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 268

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+        l3_cat.cos_max >>= 1;
+
+        pFeat->feature = PSR_SOCKET_L3_CDP;
+        set_bit(PSR_SOCKET_L3_CDP, &(info->features));
+        clear_bit(PSR_SOCKET_L3_CAT, &(info->features));

A few lines up you set this bit, and now you clear it again. Please instead set it in an else branch below.

Ok, thanks for the suggestion!

Quoted 7 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 271

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+    }
+
+    memcpy(pFeat->feat_info, &l3_cat, sizeof(struct psr_cat_lvl_info));

Aforementioned BUILD_BUG_ON() would probably bets be placed right ahead of this memcpy().

Can we call BUILD_BUG_ON() at the top of the function to avoid executing above process if the size is not right?

Quoted 11 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 290

Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+static int l3_cat_compare_mask(uint64_t *mask, struct feat_list *pFeat,
+                               unsigned int cos, bool_t *found)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));

Already here I think this memcpy()ing gets unwieldy. Can't you simply make the structure field a union of all types of interest?

Sorry that I am not very clear about your meaning to make a union. Do you mean make feat_info a union? If so, it will lose the universality to cover all features. Future feature may have different info.

I think I can replace the memcpy() to directly assign value to cat_info.

Quoted 12 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 301

Quoted 11 lines
xen/arch/x86/psr.c +11 -0
+    l3_def_cbm = (1ull << cat_info.cbm_len) - 1;
+
+    /* CDP */
+    if ( pFeat->feature == PSR_SOCKET_L3_CDP )
+    {
+        if ( cos > cat_info.cos_max )
+        {
+            if ( mask[0] != l3_def_cbm ||
+                 mask[1] != l3_def_cbm )
+            {
+                *found = 0;
Quoted 2 lines

false and true for literal boolean values please.

Thanks! Will do it.

Quoted 5 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 302

Quoted 1 line
xen/arch/x86/psr.c +1 -0
+                printk(XENLOG_ERR "CDP exceed cos max.\n");

I haven't figured yet at what times this function will get called, but I suspect it's inappropriate for it to log messages.

Ok, will remove the log. Thanks!

Quoted 7 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 306

Quoted 4 lines
xen/arch/x86/psr.c +4 -0
+                return -ENOENT;
+            }
+            *found = 1;
+            return 2;

Who or what is 2?

CDP occupies 2 members in mask[] array. So, it returns 2 to make codes outside the function to skip to next feature's mask value.

Quoted 22 lines
Quoted 22 lines
xen/arch/x86/psr.c +22 -0
+        }
+
+        if ( mask[0] == pFeat->cos_reg_val[cos * 2] &&
+             mask[1] == pFeat->cos_reg_val[cos * 2 + 1])
+            *found = 1;
+        else
+            *found = 0;
+
+        return 2;
+    }
+
+    /* CAT */
+    if ( cos > cat_info.cos_max )
+    {
+        if ( mask[0] != l3_def_cbm )
+        {
+            *found = 0;
+            printk(XENLOG_ERR "CAT exceed cos max.\n");
+            return -ENOENT;
+        }
+        *found = 1;
+        return 1;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 334

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+    }
+
+    if ( mask[0] == pFeat->cos_reg_val[cos] )
+        *found = 1;
+    else
+        *found = 0;

Please use a simple assignment in cases like this, instead of if/else.

Do you mean codes like below?

*found = 0;
if ( mask[0] == pFeat->cos_reg_val[cos] )

    *found = 1;
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/psr.c +9 -0
+static unsigned int l3_cat_exceed_range(uint64_t *mask, struct feat_list *pFeat,
+                                        unsigned int cos)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));
+    l3_def_cbm = (1ull << cat_info.cbm_len) - 1;
+
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 299

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+    /* CDP */
+    if ( pFeat->feature == PSR_SOCKET_L3_CDP )
+    {
+        if ( cos > cat_info.cos_max )
+            if ( mask[0] != l3_def_cbm ||
+                 mask[1] != l3_def_cbm )

Please combine such if()s.

Sure. Thanks!

Quoted 10 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 374

Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+                /*
+                 * Exceed cos_max and value to set is not default,
+                 * return error.
+                 */
+                return 0;
+
+        return 2;

Same question as above - who or what is 2?

Please check above comment.

Quoted 20 lines
Quoted 20 lines
xen/arch/x86/psr.c +20 -0
+static int l3_cat_get_old_set_new(uint64_t *mask,
+                                  struct feat_list *pFeat,
+                                  unsigned int old_cos,
+                                  enum mask_type type,
+                                  uint64_t m)
+{
+    struct psr_cat_lvl_info cat_info;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));
+
+    if ( type == PSR_MASK_TYPE_L3_DATA ||
+         type == PSR_MASK_TYPE_L3_CODE ||
+         type == PSR_MASK_TYPE_L3_CBM )
+    {
+        if ( !psr_check_cbm(cat_info.cbm_len, m) )
+            return -EINVAL;
+    }
+
+    if ( ( type == PSR_MASK_TYPE_L3_DATA ||
+         type == PSR_MASK_TYPE_L3_CODE ) &&
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 328

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+         pFeat->feature != PSR_SOCKET_L3_CDP )
+        return -ENXIO;

Please either combine the two if()s into a single switch() (with appropriate fall through behavior) or, perhaps better, move this second if() - suitably shrunk - past the following one. This is to avoid redundant code, which makes things more difficult to follow.

Thanks! Will make modification.

Quoted 30 lines
Quoted 30 lines
xen/arch/x86/psr.c +30 -0
+    /* CDP */
+    if ( pFeat->feature == PSR_SOCKET_L3_CDP )
+    {
+        if ( old_cos > cat_info.cos_max )
+        {
+            /* Data */
+            mask[0] = pFeat->cos_reg_val[0];
+            /* Code */
+            mask[1] = pFeat->cos_reg_val[1];
+        }
+        else
+        {
+            /* Data */
+            mask[0] = pFeat->cos_reg_val[old_cos * 2];
+            /* Code */
+            mask[1] = pFeat->cos_reg_val[old_cos * 2 + 1];
+        }
+
+        /* Set new mask */
+        if ( type == PSR_MASK_TYPE_L3_DATA )
+            mask[0] = m;
+        if ( type == PSR_MASK_TYPE_L3_CODE )
+            mask[1] = m;
+
+        return 2;
+    }
+
+    /* CAT */
+    if ( old_cos > cat_info.cos_max )
+        mask[0] =  pFeat->cos_reg_val[0];
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 360

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+    else
+        mask[0] =  pFeat->cos_reg_val[old_cos];

Across the entire function - wouldn't it be easier to range check old_cos once up front, forcing it to zero if the check fails? That would reduce code size to a better readable amount. (And just to avoid any misunderstanding - comments like this also may apply to other functions; I'm not going to repeat them there.)

Good idea! Thanks for the suggestion. I will check other functions like this.

Quoted 2 lines

Also note that there are undue double blanks in here.

Do you mean two spaces? I cannot see it in my original patch and codes.

Quoted 7 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 482

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+    if ( type == PSR_MASK_TYPE_L3_CBM )
+        mask[0] = m;

This overwriting behavior also looks quite strange to me. What's the purpose? And if this really is meant to be that way, why is this not (leaving aside the other suggested adjustment)

Quoted 9 lines
    if ( type == PSR_MASK_TYPE_L3_CBM )
        mask[0] = m;
    else if ( old_cos > cat_info.cos_max )
        mask[0] = pFeat->cos_reg_val[0];
    else
        mask[0] = pFeat->cos_reg_val[old_cos];

?

get_old_set_new() is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

So, if the appointed feature is L3 CAT, we should set input vallue for it here.

Quoted 18 lines
Quoted 18 lines
xen/arch/x86/psr.c +18 -0
+static int l3_cat_get_feat_info(struct feat_list *pFeat, enum mask_type type,
+                                uint32_t *dat0, uint32_t *dat1,
+                                uint32_t *dat2)
+{
+    struct psr_cat_lvl_info cat_info;
+
+    if ( type != PSR_MASK_TYPE_L3_DATA &&
+         type != PSR_MASK_TYPE_L3_CODE &&
+         type != PSR_MASK_TYPE_L3_CBM )
+        return 0;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));
+
+    *dat0 = cat_info.cbm_len;
+    *dat1 = cat_info.cos_max;
+
+    if ( pFeat->feature == PSR_SOCKET_L3_CDP )
+        *dat2 |= XEN_SYSCTL_PSR_CAT_L3_CDP;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 548

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+    else
+        *dat2 = 0;
+
+    return 1;
+}

The latest here it becomes clear that you want to use switch() much more widely.

Ok, will consider it carefully.

Quoted 13 lines

xen/arch/x86/psr.c void psr_free_rmid(struct domain *d) · line 750

Quoted 12 lines
xen/arch/x86/psr.c +5 -2
 static inline void psr_assoc_init(void)
 {
     struct psr_assoc *psra = &this_cpu(psr_assoc);
+    struct psr_socket_alloc_info *info;
+    unsigned int cos_max;
+    unsigned int socket;
 
-    if ( cat_socket_info )
+    if ( socket_alloc_info )
     {
-        unsigned int socket = cpu_to_socket(smp_processor_id());
+        socket = cpu_to_socket(smp_processor_id());
Quoted 5 lines

What's the point of moving the variable declaration? Quite the other way around, the new variable you add could - afaict - all live in this more narrow scope. And info could presumably become a pointer to const.

I remember it reports warning or error when compiling old codes.

Quoted 18 lines
Quoted 18 lines
xen/arch/x86/psr.c +7 -5
-int psr_get_cat_l3_info(unsigned int socket, uint32_t *cbm_len,
-                        uint32_t *cos_max, uint32_t *flags)
+int psr_get_info(unsigned int socket, enum mask_type type,
+                 uint32_t *dat0, uint32_t *dat1,
+                 uint32_t *dat2)
 {
-    struct psr_cat_socket_info *info = get_cat_socket_info(socket);
+    struct psr_socket_alloc_info *info = get_socket_alloc_info(socket);
+    struct feat_list *pTmp;
 
     if ( IS_ERR(info) )
         return PTR_ERR(info);
 
-    *cbm_len = info->cbm_len;
-    *cos_max = info->cos_max;
+    if ( NULL == info->pFeat || NULL == info->pFeat->pNext)
+       return -ENODEV;
Quoted 12 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 827

Quoted 7 lines
xen/arch/x86/psr.c +4 -3
-    *flags = 0;
-    if ( cdp_is_enabled(socket) )
-        *flags |= XEN_SYSCTL_PSR_CAT_L3_CDP;
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        if ( pTmp->ops.get_feat_info(pTmp, type, dat0, dat1, dat2) )

Considering that this is one of the generic ops: What guarantees that 3 elements will suffice going forward? I.e. why is this not an array?

Good point. I consider this too. Per all features we know, three parameters are sufficient. To simplify codes, I write the function like this. I think we can make change if future feature has more requirements.

If you like to get things done one time, I will make change.

Quoted 26 lines
Quoted 26 lines
xen/arch/x86/psr.c +9 -14
-int psr_get_l3_cbm(struct domain *d, unsigned int socket,
-                   uint64_t *cbm, enum cbm_type type)
+static int get_old_set_new(uint64_t *mask,
+                           struct psr_socket_alloc_info *info,
+                           unsigned int old_cos,
+                           enum mask_type type,
+                           uint64_t m)
 {
-    struct psr_cat_socket_info *info = get_cat_socket_info(socket);
-    bool_t cdp_enabled = cdp_is_enabled(socket);
-
-    if ( IS_ERR(info) )
-        return PTR_ERR(info);
+    struct feat_list *pTmp;
+    int ret;
 
-    switch ( type )
-    {
-    case PSR_CBM_TYPE_L3:
-        if ( cdp_enabled )
-            return -EXDEV;
-        *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].cbm;
-        break;
+    if ( !mask )
+        return -EINVAL;
Quoted 12 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 849

Quoted 8 lines
xen/arch/x86/psr.c +2 -6
-    case PSR_CBM_TYPE_L3_CODE:
-        if ( !cdp_enabled )
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].cbm;
-        else
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].code;
-        break;
+    if ( !info || !info->pFeat )
+        return -ENODEV;

Earlier on you used (slightly oddly arranged) checks against NULL. Please be consistent (and I'd prefer if you used ! across the board).

Got it, thanks!

Quoted 14 lines
Quoted 14 lines
xen/arch/x86/psr.c +7 -6
-    case PSR_CBM_TYPE_L3_DATA:
-        if ( !cdp_enabled )
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].cbm;
-        else
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].data;
-        break;
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        /* mask getting and setting order is same as feature list */
+        ret = pTmp->ops.get_old_set_new(mask, pTmp, old_cos, type, m);
+        if ( ret < 0 )
+            return ret;
Quoted 6 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 859

Quoted 3 lines
xen/arch/x86/psr.c +1 -2
-    default:
-        ASSERT_UNREACHABLE();
+        mask += ret;

With this - what ensures the array isn't going to be overrun?

It seems you missed one line below. The while() loop traverse all features in list. It will exit when the last member has been accessed.

+ pTmp = pTmp->pNext;

Quoted 3 lines

And I'm also having some difficulty seeing how an array of outputs matches up with exactly one input (m). Can you explain this please?

This while loop will traverse all features in list. It will get all features old_cos value back. If the feature is the one to set new value, the input new value (m) will replace the old_cos value. All the value are set into mask array according the traversal order, meaning the same order of feature list members, like L3 CAT as the first and L2 CAT as the second.

For example, old_cos of the domain is 1. User wants to set L3 CAT CBM to 0x1ff. The original COS registers like below.

        -----------------------
        | COS 0 | COS 1 | ... |
        -----------------------
L3 CAT  | 0x7ff | 0x3ff | ... |
        -----------------------
L2 CAT  | 0xff  | 0x3f  | ... |
        -----------------------

Then, mask array should be:
mask[0]: 0x1ff
mask[1]: 0x3f

Then, we can use this mask array to find if there is matching COS through compare_mask().

Quoted 25 lines
Quoted 25 lines
xen/arch/x86/psr.c +10 -11
-static bool_t psr_check_cbm(unsigned int cbm_len, uint64_t cbm)
+int psr_get_val(struct domain *d, unsigned int socket,
+                uint64_t *val, enum mask_type type)
 {
-    unsigned int first_bit, zero_bit;
-
-    /* Set bits should only in the range of [0, cbm_len). */
-    if ( cbm & (~0ull << cbm_len) )
-        return 0;
+    struct psr_socket_alloc_info *info = get_socket_alloc_info(socket);
+    unsigned int cos = d->arch.psr_cos_ids[socket];
+    struct feat_list *pTmp;
+    int ret;
 
-    /* At least one bit need to be set. */
-    if ( cbm == 0 )
-        return 0;
+    if ( IS_ERR(info) )
+        return PTR_ERR(info);
 
-    first_bit = find_first_bit(&cbm, cbm_len);
-    zero_bit = find_next_zero_bit(&cbm, cbm_len, first_bit);
+    if ( NULL == info->pFeat || NULL == info->pFeat->pNext )
+        return -ENODEV;
Quoted 13 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 885

Quoted 10 lines
xen/arch/x86/psr.c +6 -4
-    /* Set bits should be contiguous. */
-    if ( zero_bit < cbm_len &&
-         find_next_bit(&cbm, cbm_len, zero_bit) < cbm_len )
-        return 0;
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        ret = pTmp->ops.get_val(pTmp, cos, type, val);
+        if ( ret < 0 )
+            return -EINVAL;

Why don't you return "ret" here?

This is an unnecessary check. Will remove it.

Quoted 3 lines
Quoted 1 line

+ else if ( ret > 0 )

Pointless "else".

Will remove it.

Quoted 6 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 903

Quoted 3 lines
xen/arch/x86/psr.c +1 -1
-static void do_write_l3_cbm(void *data)
+static void do_write_psr_ref(void *data)

What does "_ref" stand for?

Sorry, a mistake.

Quoted 57 lines
Quoted 57 lines
xen/arch/x86/psr.c +45 -6
-static int find_cos(struct psr_cat_cbm *map, unsigned int cos_max,
-                    uint64_t cbm_code, uint64_t cbm_data, bool_t cdp_enabled)
+static int find_cos(uint64_t *mask, enum mask_type type,
+                    struct psr_socket_alloc_info *info)
 {
     unsigned int cos;
+    struct psr_ref *map = info->cos_ref;
+    struct feat_list *pTmp;
+    uint64_t *pMask = mask;
+    int ret;
+    bool_t found = 0;
+    unsigned int cos_max = 0;
+
+    /* cos_max is the max COS of the feature to be set. */
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        cos_max = pTmp->ops.get_cos_max_as_type(pTmp, type);
+        if ( cos_max > 0 )
+            break;
+
+        pTmp = pTmp->pNext;
+    }
 
+    pTmp = info->pFeat->pNext;
     for ( cos = 0; cos <= cos_max; cos++ )
     {
-        if ( (map[cos].ref || cos == 0) &&
-             ((!cdp_enabled && map[cos].cbm == cbm_code) ||
-              (cdp_enabled && map[cos].code == cbm_code &&
-                              map[cos].data == cbm_data)) )
+        if ( 0 != cos && 0 == map[cos].ref )
+            continue;
+
+        while ( pTmp )
+        {
+            /*
+             * Compare mask according to feature list order.
+             * We must follow this order because mask is assembled
+             * as this order in get_old_set_new().
+             */
+            ret = pTmp->ops.compare_mask(pMask, pTmp, cos, &found);
+            if ( ret < 0 )
+                return ret;
+
+            /* If fail to match, go to next cos to compare. */
+            if ( !found )
+                break;
+
+            pMask += ret;
+            pTmp = pTmp->pNext;
+        }
+
+        /* Every feature can match, this cos is what we find. */
+        if ( found )
             return cos;
+
Quoted 9 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 1006

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+        /* Not found, need find again from beginning. */
+        pTmp = info->pFeat->pNext;
+        pMask = mask;

Please do once (at the appropriate place inside the outer loop) what you have done prior to the loop and repeat here. As already alluded to - limiting the scope of variables also helps ensuring that things work as intended.

Good suggestion, thanks!

Quoted 8 lines
Quoted 3 lines
     }

+ printk(XENLOG_INFO "%s: cannot find cos.\n", __func__);

Again a questionable log message with a questionable use of __func__. In any case log messages should normally not have a full stop at their end.

Will remove this log.

Quoted 9 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 1016

Quoted 6 lines
xen/arch/x86/psr.c +3 -2
-static int pick_avail_cos(struct psr_cat_cbm *map, unsigned int cos_max,
-                          unsigned int old_cos)
+static int check_exceed_range(uint64_t *mask, struct feat_list *pTmp,
+                              unsigned int cos)
 {
+    unsigned int ret = 0;

Pointless initializer.

Will remove it.

Quoted 35 lines
Quoted 35 lines
xen/arch/x86/psr.c +29 -0
+static int alloc_new_cos(struct psr_socket_alloc_info *info,
+                         uint64_t *mask, unsigned int old_cos,
+                         enum mask_type type)
+{
+    unsigned int cos_max = 0;
+    struct feat_list *pTmp;
     unsigned int cos;
+    struct psr_ref *map = info->cos_ref;
+
+    /*
+     * cos_max is the max COS of the feature to be set.
+     */
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        cos_max = pTmp->ops.get_cos_max_as_type(pTmp, type);
+        if ( cos_max > 0 )
+            break;
+
+        pTmp = pTmp->pNext;
+    }
+
+    if ( !cos_max )
+        return -ENOENT;
 
     /* If old cos is referred only by the domain, then use it. */
     if ( map[old_cos].ref == 1 && old_cos != 0 )
+    {
+        pTmp = info->pFeat->pNext;
+        if ( check_exceed_range(mask, pTmp, old_cos) )
+            goto find_new;
+
         return old_cos;
+    }
Quoted 5 lines
Quoted 1 line

+find_new:

Labels indented by at least one blank please. In this case, though, it looks like you could easily do without any label and goto.

Ok, I will consider not to use goto and label here. At least, will add one blank. Thanks!

Quoted 48 lines
Quoted 48 lines
xen/arch/x86/psr.c +7 -33
-int psr_set_l3_cbm(struct domain *d, unsigned int socket,
-                   uint64_t cbm, enum cbm_type type)
+int psr_set_val(struct domain *d, unsigned int socket,
+                uint64_t val, enum mask_type type)
 {
-    unsigned int old_cos, cos_max;
+    unsigned int old_cos;
     int cos, ret;
-    uint64_t cbm_data, cbm_code;
-    bool_t cdp_enabled = cdp_is_enabled(socket);
-    struct psr_cat_cbm *map;
-    struct psr_cat_socket_info *info = get_cat_socket_info(socket);
+    struct psr_ref *map;
+    uint64_t *mask;
+    struct psr_socket_alloc_info *info = get_socket_alloc_info(socket);
 
     if ( IS_ERR(info) )
         return PTR_ERR(info);
 
-    if ( !psr_check_cbm(info->cbm_len, cbm) )
-        return -EINVAL;
-
-    if ( !cdp_enabled && (type == PSR_CBM_TYPE_L3_CODE ||
-                          type == PSR_CBM_TYPE_L3_DATA) )
-        return -ENXIO;
-
-    cos_max = info->cos_max;
+    map = info->cos_ref;
     old_cos = d->arch.psr_cos_ids[socket];
-    map = info->cos_to_cbm;
-
-    switch ( type )
-    {
-    case PSR_CBM_TYPE_L3:
-        cbm_code = cbm;
-        cbm_data = cbm;
-        break;
-
-    case PSR_CBM_TYPE_L3_CODE:
-        cbm_code = cbm;
-        cbm_data = map[old_cos].data;
-        break;
-
-    case PSR_CBM_TYPE_L3_DATA:
-        cbm_code = map[old_cos].code;
-        cbm_data = cbm;
-        break;
Quoted 8 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 1101

Quoted 5 lines
xen/arch/x86/psr.c +1 -4
-    default:
-        ASSERT_UNREACHABLE();
-        return -EINVAL;
-    }
+    /* Considering CDP liking features */

DYM "like"? And what do you want to say with this comment?

Yes, "like" should be. CDP needs occupy two members in mask array. So, I make nr_feat multiply 2 as the array size.

Quoted 7 lines
Quoted 1 line

+ mask = xzalloc_array(uint64_t, info->nr_feat * 2);

Perhaps it's related to the doubling of nr_feat here? If so - what precludes another future feature to require 3 or 4 slots per element? I think this needs abstracting out, now that you create an abstract framework.

Good point. I will consider it.

Quoted 4 lines
Quoted 1 line

+ if ( (ret = get_old_set_new(mask, info, old_cos, type, val)) != 0 )

What if the xzalloc_array() returned NULL?

Sorry, missed the check.

Quoted 56 lines
Quoted 56 lines
xen/arch/x86/psr.c +16 -18
+        return ret;
 
-    spin_lock(&info->cbm_lock);
-    cos = find_cos(map, cos_max, cbm_code, cbm_data, cdp_enabled);
+    spin_lock(&info->mask_lock);
+    cos = find_cos(mask, type, info);
     if ( cos >= 0 )
     {
         if ( cos == old_cos )
         {
-            spin_unlock(&info->cbm_lock);
+            spin_unlock(&info->mask_lock);
+            xfree(mask);
             return 0;
         }
     }
     else
     {
-        cos = pick_avail_cos(map, cos_max, old_cos);
+        cos = alloc_new_cos(info, mask, old_cos, type);
         if ( cos < 0 )
         {
-            spin_unlock(&info->cbm_lock);
+            spin_unlock(&info->mask_lock);
+            xfree(mask);
             return cos;
         }
 
-        /* We try to avoid writing MSR. */
-        if ( (cdp_enabled &&
-             (map[cos].code != cbm_code || map[cos].data != cbm_data)) ||
-             (!cdp_enabled && map[cos].cbm != cbm_code) )
+        /* Write all features mask MSRs corresponding to the COS */
+        ret = write_psr_ref(socket, cos, mask);
+        if ( ret )
         {
-            ret = write_l3_cbm(socket, cos, cbm_code, cbm_data, cdp_enabled);
-            if ( ret )
-            {
-                spin_unlock(&info->cbm_lock);
-                return ret;
-            }
-            map[cos].code = cbm_code;
-            map[cos].data = cbm_data;
+            spin_unlock(&info->mask_lock);
+            xfree(mask);
+            return ret;
         }
     }
 
     map[cos].ref++;
     map[old_cos].ref--;
-    spin_unlock(&info->cbm_lock);
+
+    spin_unlock(&info->mask_lock);
Quoted 9 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 1146

Quoted 5 lines
     d->arch.psr_cos_ids[socket] = cos;
xen/arch/x86/psr.c +2 -0
+    xfree(mask);
+    mask = NULL;
 
     return 0;

I don't think setting mask to NULL right ahead of a return statement is very helpful.

Yes, this sentence can be removed.

Quoted 5 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1213

Quoted 2 lines
xen/arch/x86/psr.c +1 -1
-static void cat_cpu_init(void)
+static void internal_cpu_init(void)

Is "internal_" really a useful prefix here?

I wanted to express this is an internal function in psr.c to separate it from psr_cpu_init().

Quoted 35 lines
Quoted 24 lines
 {
     unsigned int eax, ebx, ecx, edx;
xen/arch/x86/psr.c +4 -5
-    struct psr_cat_socket_info *info;
+    struct psr_socket_alloc_info *info;
     unsigned int socket;
     unsigned int cpu = smp_processor_id();
-    uint64_t val;
     const struct cpuinfo_x86 *c = cpu_data + cpu;
+    struct feat_list *pTmp;
 
     if ( !cpu_has(c, X86_FEATURE_PQE) || c->cpuid_level < PSR_CPUID_LEVEL_CAT )
         return;
 
     socket = cpu_to_socket(cpu);
-    if ( test_bit(socket, cat_socket_enable) )
+    info = socket_alloc_info + socket;
+    if ( info->features )
         return;
 
     cpuid_count(PSR_CPUID_LEVEL_CAT, 0, &eax, &ebx, &ecx, &edx);
     if ( ebx & PSR_RESOURCE_TYPE_L3 )
     {
-        cpuid_count(PSR_CPUID_LEVEL_CAT, 1, &eax, &ebx, &ecx, &edx);
-        info = cat_socket_info + socket;

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1235

Quoted 10 lines
xen/arch/x86/psr.c +3 -7
-        info->cbm_len = (eax & 0x1f) + 1;
-        info->cos_max = min(opt_cos_max, edx & 0xffff);
-
-        info->cos_to_cbm = temp_cos_to_cbm;
-        temp_cos_to_cbm = NULL;
-        /* cos=0 is reserved as default cbm(all ones). */
-        info->cos_to_cbm[0].cbm = (1ull << info->cbm_len) - 1;
+        pTmp = pL3CAT;
+        if ( !pTmp )
+            return;
Quoted 4 lines

You check the allocation result in internal_cpu_prepare() - can you really get here with this being NULL? If you can't, drop the if() (or make it an ASSERT()).

Ok, thanks!

Quoted 25 lines
Quoted 25 lines
xen/arch/x86/psr.c +1 -23
+        pL3CAT = NULL;
 
-        spin_lock_init(&info->cbm_lock);
-
-        set_bit(socket, cat_socket_enable);
-
-        if ( (ecx & PSR_CAT_CDP_CAPABILITY) && (opt_psr & PSR_CDP) &&
-             cdp_socket_enable && !test_bit(socket, cdp_socket_enable) )
-        {
-            info->cos_to_cbm[0].code = (1ull << info->cbm_len) - 1;
-            info->cos_to_cbm[0].data = (1ull << info->cbm_len) - 1;
-
-            /* We only write mask1 since mask0 is always all ones by default. */
-            wrmsrl(MSR_IA32_PSR_L3_MASK(1), (1ull << info->cbm_len) - 1);
-
-            rdmsrl(MSR_IA32_PSR_L3_QOS_CFG, val);
-            wrmsrl(MSR_IA32_PSR_L3_QOS_CFG, val | (1 << PSR_L3_QOS_CDP_ENABLE_BIT));
-
-            /* Cut half of cos_max when CDP is enabled. */
-            info->cos_max >>= 1;
-
-            set_bit(socket, cdp_socket_enable);
-        }
-        printk(XENLOG_INFO "CAT: enabled on socket %u, cos_max:%u, cbm_len:%u, CDP:%s\n",
-               socket, info->cos_max, info->cbm_len,
Quoted 9 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1241

Quoted 5 lines
xen/arch/x86/psr.c +4 -1
-               cdp_is_enabled(socket) ? "on" : "off");
+        /* Initialize this feature according to CPUID. */
+        cpuid_count(PSR_CPUID_LEVEL_CAT, 1, &eax, &ebx, &ecx, &edx);
+        pTmp->ops = l3_cat_ops;
+        pTmp->ops.init_feature(eax, ebx, ecx, edx, pTmp, info);

This function can return without having consumed pTmp, which means you'd leak the memory in that case.

Yes, right, this is by design. pL3CAT/pL2CAT are allocated in internal_cpu_prepare(). If this CPU does not support L2 CAT for example, the pL2CAT will not be added into feature list. But when CPU on other socket up, internal_cpu_prepare() will be called again and the pL2CAT will not be allocated again (check if it is NULL). We can use the buffer has been allocated. That means we maintain one redundant buffer to simplify the codes.

Quoted 11 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1265

Quoted 8 lines
xen/arch/x86/psr.c +2 -5
-static void __init psr_cat_free(void)
+static void __init psr_free(void)
 {
-    xfree(cat_socket_enable);
-    cat_socket_enable = NULL;
-    xfree(cat_socket_info);
-    cat_socket_info = NULL;
+    int i;

unsigned int

Will change it. Thanks!

Quoted 7 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1280

Quoted 4 lines
xen/arch/x86/psr.c +2 -1
-static void __init init_psr_cat(void)
+static void __init init_psr(void)
 {
+    int i;

Again.

Will change it. Thanks!

Quoted 18 lines
Quoted 18 lines
xen/arch/x86/psr.c +8 -3
+
     if ( opt_cos_max < 1 )
     {
         printk(XENLOG_INFO "CAT: disabled, cos_max is too small\n");
         return;
     }
 
-    cat_socket_enable = xzalloc_array(unsigned long, BITS_TO_LONGS(nr_sockets));
-    cat_socket_info = xzalloc_array(struct psr_cat_socket_info, nr_sockets);
-    cdp_socket_enable = xzalloc_array(unsigned long, BITS_TO_LONGS(nr_sockets));
+    socket_alloc_info = xzalloc_array(struct psr_socket_alloc_info, nr_sockets);
+
+    if ( !socket_alloc_info )
+    {
+        printk(XENLOG_INFO "Fail to alloc socket_alloc_info!\n");
+        return;
+    }
Quoted 8 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1298

Quoted 5 lines
xen/arch/x86/psr.c +3 -2
-    if ( !cat_socket_enable || !cat_socket_info )
-        psr_cat_free();
+    for ( i = 0; i < nr_sockets; i++ )
+    {
+        socket_alloc_info[i].pFeat =  xzalloc(struct feat_list);

What's the point of this dummy list element?

The pFeat is a redundant list head to facilitate list operations. With this list head, the advantage is that we can insert/delete the first element same as others without special operations.

Quoted 7 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1301

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+        if ( NULL == socket_alloc_info[i].pFeat )
+        {
+            printk(XENLOG_INFO "Fail to alloc pFeat!\n");

How would the reader of the log easily associate such a message with something having gone wrong in PSR initialization?

Yes, seems not necessary. Will remove it.

Quoted 6 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1304

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+            return;
+        }
+        socket_alloc_info[i].pFeat->pNext = NULL;

This is redundant with the use of xzalloc().

Will remove it, thanks!

Quoted 10 lines
Quoted 10 lines

static void psr_cpu_init(void)
{

xen/arch/x86/psr.c +6 -2
-    if ( cat_socket_info )
-        cat_cpu_init();
+    unsigned int socket;
+    struct psr_socket_alloc_info *info;
+
+    if ( socket_alloc_info )
+    {
+        socket = cpu_to_socket(smp_processor_id());
Quoted 9 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1326

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+        info = socket_alloc_info + socket;
+        info->cos_ref = temp_cos_ref;
+        temp_cos_ref = NULL;
+        spin_lock_init(&info->mask_lock);
+
+        internal_cpu_init();

Why is everything ahead of this call not part of that function?

The original purpose is to make internal_cpu_init() be simpler. After considering it again, move these codes into the function may be better. Thanks!

Quoted 6 lines
Quoted 6 lines
xen/include/asm-x86/psr.h +0 -0
@@ -46,10 +46,10 @@ struct psr_cmt {
     struct psr_cmt_l3 l3;
 };
 
Quoted 13 lines

xen/include/asm-x86/psr.h struct psr_cmt { · line 53

Quoted 9 lines
xen/include/asm-x86/psr.h +4 -4
-enum cbm_type {
-    PSR_CBM_TYPE_L3,
-    PSR_CBM_TYPE_L3_CODE,
-    PSR_CBM_TYPE_L3_DATA,
+enum mask_type {
+    PSR_MASK_TYPE_L3_CBM,
+    PSR_MASK_TYPE_L3_CODE,
+    PSR_MASK_TYPE_L3_DATA,
 };

The enum tag is now too generic a name - please make this psr_mask_type or something similar.

Ok, thanks!

Quoted 1 line

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
Quoted 11 lines
Quoted 10 lines

xen/arch/x86/psr.c line 54

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 25.08.16 at 07:22, <[email protected]> wrote:
xen/arch/x86/psr.c +5 -0
+                         struct psr_socket_alloc_info *info);
+    /*
+     * get_old_set_new is used in set value process to get all features'
+     * COS registers values according to original cos id of the domain.
+     * Then, assemble them into an mask array as feature list order.

This sentence in particular doesn't make any sense to me. What follows below also looks like it is in need of improvement.

Do you mean the comments are not accurate?

I simply wasn't able to tell, because of not being able to interpret the sentence.

Quoted 9 lines

How about below description?

get_old_set_new will traverse all features in list. It is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

All the values are set into mask array according the traversal order, meaning the same order of feature list members.

Sounds reasonable. I suppose the example that you gave right next wasn't meant to go into the comment.

Quoted 19 lines
Quoted 11 lines

xen/arch/x86/psr.c line 85

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+    /*
+     * exceed_range is used to check if the input cos id exceeds the
+     * feature's cos_max and if the input mask value is not the default one.
+     * Even if the associated cos exceeds the cos_max, HW can work as default
+     * value. That is the reason we need check is mask value is default one.
+     * If both criteria are fulfilled, that means the input exceeds the
+     * range.
+     */
+    unsigned int (*exceed_range)(uint64_t *mask, struct feat_list *pFeat,
+                                 unsigned int cos);
Quoted 8 lines

According to the comment this is kind of a predicate, which seems unlikely to return an unsigned value. In fact without a word on the return value I'd expect such to return bool. And I'd also expect the name to reflect the purpose, i.e. "exceeds_name()". Plus just like for compare above I wonder whether come or all of the parameters should be pointers to const (please go over the entire patch and do constification wherever possible/sensible).

Quoted 6 lines

Yes, you are right. I will change the function type to bool and add const for not changed input pointers.

This function is used to check if the input cos id exceeds the cos_max. If yes and the set value is not default value, we should return error. So, I think to change the function name to exceed_cos_max(). How do you think?

Okay, except that I continue to think you mean "exceeds". "exceed_cos_max" to me is kind of a directive, not a predicate.

Quoted 7 lines
Quoted 5 lines

xen/arch/x86/psr.c line 109

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+#define MAX_FEAT_INFO_SIZE 8
+#define MAX_COS_REG_NUM  128

Are these numbers arbitrary, or based on something?

MAX_FEAT_INFO_SIZE is got from the sizeof(struct psr_cat_lvl_info) and consider the extension for future feature.

In that case please use that sizeof() in the expression here.

Quoted 2 lines

MAX_COS_REG_NUM is got from spec that the max COS registers number is 128 for all PSR features so far.

"So far" makes me still wonder: Is this an architectural limit or one resulting from current (hardware) implementations. In the former case I don't think a comment is strictly needed, but in the latter case the choice should be explained.

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

+struct psr_socket_alloc_info {

I've yet to see whether the "alloc" in the name really makes sense.

And btw., having seen the entire patch I don't think this alloc_ infix is warranted both here and in the variable name.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+/* Common functions for supporting feature callback functions. */
+static void add_feature(struct feat_list *pHead, struct feat_list *pTmp)
+{
+    if ( NULL == pHead || NULL == pTmp )
+        return;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 167

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+
+    while ( pHead->pNext )
+        pHead = pHead->pNext;
+
+    pHead->pNext = pTmp;
+}

Do you really need custom list management here?

Quoted 3 lines

It seems xen list interfaces require the input list be a double linked list but my list is a single linked list. Furthermore, I only need simple add to tail function and free function. So I create custom list management functions.

Unless there's a strong need, I'd like you to go with what is there, or introduce _generic_ singly linked list management.

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/psr.c +15 -0
+static void free_feature(struct psr_socket_alloc_info *info)
+{
+    struct feat_list *pTmp;
+    struct feat_list *pPrev;
+
+    if ( NULL == info )
+        return;
+
+    if ( NULL == info->pFeat )
+        return;
+
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        pPrev = pTmp;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 189

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+        pTmp = pTmp->pNext;
+        clear_bit(pPrev->feature, &(info->features));
+        xfree(pPrev);
+        pPrev = NULL;
+    }
+}

Why does info->pFeat not get freed here?

Quoted 5 lines

The info->pFeat is a redundant list head to facilitate list operations. It is only freed when socket info is freed.

With this list head, the advantage is that we can insert/delete the first element same as others without special operations.

But it results in less obvious code (and a basically pointless extra allocation). One more reason to use generic list management primitives.

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+static void l3_cat_init_feature(unsigned int eax, unsigned int ebx,
+                                unsigned int ecx, unsigned int edx,
+                                struct feat_list *pFeat,
+                                struct psr_socket_alloc_info *info)
+{
+    struct psr_cat_lvl_info l3_cat;
+    unsigned int socket;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 234

Quoted 4 lines
xen/arch/x86/psr.c +4 -0
+    uint64_t val;
+
+    if ( MAX_FEAT_INFO_SIZE < sizeof(struct psr_cat_lvl_info) )
+        return;

DYM BUILD_BUG_ON()? Also the MAX_FEAT_INFO_SIZE dimensioned array is one with unsigned int as the base type, which doesn't get reflected here.

Quoted 6 lines

I will use BUILD_BUG_ON() for the error case.

As my explanation for MAX_FEAT_INFO_SIZE above, it reflects a size more than struct psr_cat_lvl_info. So, if MAX_FEAT_INFO_SIZE is bigger than the sizeof(struct psr_cat_lvl_info), the following operations on feat_info[] should be good.

In fact the BUILD_BUG_ON() may not be needed if you follow the suggestion above.

Quoted 9 lines
Quoted 7 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 271

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+    }
+
+    memcpy(pFeat->feat_info, &l3_cat, sizeof(struct psr_cat_lvl_info));

Aforementioned BUILD_BUG_ON() would probably bets be placed right ahead of this memcpy().

Can we call BUILD_BUG_ON() at the top of the function to avoid executing above process if the size is not right?

Well, the purpose of BUILD_BUG_ON() is what its name implies: It fails the _build_ if violated, so execution can't possibly come here (or in fact anywhere) in that case.

Quoted 11 lines
Quoted 11 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 290

Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+static int l3_cat_compare_mask(uint64_t *mask, struct feat_list *pFeat,
+                               unsigned int cos, bool_t *found)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));

Already here I think this memcpy()ing gets unwieldy. Can't you simply make the structure field a union of all types of interest?

Quoted 3 lines

Sorry that I am not very clear about your meaning to make a union. Do you mean make feat_info a union? If so, it will lose the universality to cover all features. Future feature may have different info.

Which is the purpose of a union - you'd simply add a new member then.

Quoted 1 line

I think I can replace the memcpy() to directly assign value to cat_info.

No, this copying (done in _many_ places) really should go away.

Quoted 10 lines
Quoted 10 lines
Quoted 1 line

+ printk(XENLOG_ERR "CDP exceed cos max.\n");

I haven't figured yet at what times this function will get called, but I suspect it's inappropriate for it to log messages.

Quoted 4 lines
xen/arch/x86/psr.c +4 -0
+                return -ENOENT;
+            }
+            *found = 1;
+            return 2;
Quoted 4 lines
Quoted 2 lines

Who or what is 2?

CDP occupies 2 members in mask[] array. So, it returns 2 to make codes outside the function to skip to next feature's mask value.

I guess that's going to be understandable once the return values of the hooks get properly explained in their comments.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
xen/arch/x86/psr.c +4 -0
+    if ( mask[0] == pFeat->cos_reg_val[cos] )
+        *found = 1;
+    else
+        *found = 0;
Quoted 7 lines
Quoted 2 lines

Please use a simple assignment in cases like this, instead of if/else.

Do you mean codes like below?

*found = 0;
if ( mask[0] == pFeat->cos_reg_val[cos] )

    *found = 1;

No.

    *found = (mask[0] == pFeat->cos_reg_val[cos]);
Quoted 12 lines
Quoted 6 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 479

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+    /* CAT */
+    if ( old_cos > cat_info.cos_max )
+        mask[0] =  pFeat->cos_reg_val[0];
+    else
+        mask[0] =  pFeat->cos_reg_val[old_cos];
Quoted 6 lines

Across the entire function - wouldn't it be easier to range check old_cos once up front, forcing it to zero if the check fails? That would reduce code size to a better readable amount. (And just to avoid any misunderstanding - comments like this also may apply to other functions; I'm not going to repeat them there.)

Quoted 6 lines

Good idea! Thanks for the suggestion. I will check other functions like this.

Quoted 2 lines

Also note that there are undue double blanks in here.

Do you mean two spaces? I cannot see it in my original patch and codes.

It's even still visible in the quoted text above.

Quoted 14 lines
Quoted 14 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 482

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+    if ( type == PSR_MASK_TYPE_L3_CBM )
+        mask[0] = m;

This overwriting behavior also looks quite strange to me. What's the purpose? And if this really is meant to be that way, why is this not (leaving aside the other suggested adjustment)

    if ( type == PSR_MASK_TYPE_L3_CBM )
        mask[0] = m;
    else if ( old_cos > cat_info.cos_max )
        mask[0] = pFeat->cos_reg_val[0];
    else
        mask[0] = pFeat->cos_reg_val[old_cos];
Quoted 8 lines
Quoted 2 lines

?

get_old_set_new() is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

So, if the appointed feature is L3 CAT, we should set input vallue for it here.

Okay, that answers the "what" aspect, but leaves open _why_ it needs to be that way.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

static inline void psr_assoc_init(void)

 {
     struct psr_assoc *psra = &this_cpu(psr_assoc);
xen/arch/x86/psr.c +5 -2
+    struct psr_socket_alloc_info *info;
+    unsigned int cos_max;
+    unsigned int socket;
 
-    if ( cat_socket_info )
+    if ( socket_alloc_info )
     {
-        unsigned int socket = cpu_to_socket(smp_processor_id());
+        socket = cpu_to_socket(smp_processor_id());
Quoted 6 lines
Quoted 5 lines

What's the point of moving the variable declaration? Quite the other way around, the new variable you add could - afaict - all live in this more narrow scope. And info could presumably become a pointer to const.

I remember it reports warning or error when compiling old codes.

I don't see why it would.

Quoted 30 lines
Quoted 18 lines
Quoted 18 lines
xen/arch/x86/psr.c +7 -5
-int psr_get_cat_l3_info(unsigned int socket, uint32_t *cbm_len,
-                        uint32_t *cos_max, uint32_t *flags)
+int psr_get_info(unsigned int socket, enum mask_type type,
+                 uint32_t *dat0, uint32_t *dat1,
+                 uint32_t *dat2)
 {
-    struct psr_cat_socket_info *info = get_cat_socket_info(socket);
+    struct psr_socket_alloc_info *info = get_socket_alloc_info(socket);
+    struct feat_list *pTmp;
 
     if ( IS_ERR(info) )
         return PTR_ERR(info);
 
-    *cbm_len = info->cbm_len;
-    *cos_max = info->cos_max;
+    if ( NULL == info->pFeat || NULL == info->pFeat->pNext)
+       return -ENODEV;
Quoted 12 lines

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 827

Quoted 7 lines
xen/arch/x86/psr.c +4 -3
-    *flags = 0;
-    if ( cdp_is_enabled(socket) )
-        *flags |= XEN_SYSCTL_PSR_CAT_L3_CDP;
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        if ( pTmp->ops.get_feat_info(pTmp, type, dat0, dat1, dat2) )

Considering that this is one of the generic ops: What guarantees that 3 elements will suffice going forward? I.e. why is this not an array?

Quoted 3 lines

Good point. I consider this too. Per all features we know, three parameters are sufficient. To simplify codes, I write the function like this. I think we can make change if future feature has more requirements.

If you introduce an abstraction layer, you should design it with abstract requirements in mind, not with the requirements of currently known implementations.

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
xen/arch/x86/psr.c +8 -8
-    case PSR_CBM_TYPE_L3_DATA:
-        if ( !cdp_enabled )
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].cbm;
-        else
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].data;
-        break;
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        /* mask getting and setting order is same as feature list */
+        ret = pTmp->ops.get_old_set_new(mask, pTmp, old_cos, type, m);
+        if ( ret < 0 )
+            return ret;
 
-    default:
-        ASSERT_UNREACHABLE();
+        mask += ret;
Quoted 6 lines
Quoted 2 lines

With this - what ensures the array isn't going to be overrun?

It seems you missed one line below. The while() loop traverse all features in list. It will exit when the last member has been accessed.

+ pTmp = pTmp->pNext;

No, I didn't miss any line. I'm asking because there's nothing but programmer discipline keeping array size and list length match up. That's fragile.

Quoted 8 lines
Quoted 3 lines

And I'm also having some difficulty seeing how an array of outputs matches up with exactly one input (m). Can you explain this please?

This while loop will traverse all features in list. It will get all features old_cos value back. If the feature is the one to set new value, the input new value (m) will replace the old_cos value. All the value are set into mask array according the traversal order, meaning the same order of feature list members, like L3 CAT as the first and L2 CAT as the second.

This gets us back to the question of _why_ you need this "get all but set only one" functionality. Plus, wasn't it that CDP requires two values per feature?

Quoted 7 lines
Quoted 5 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1213

Quoted 2 lines
xen/arch/x86/psr.c +1 -1
-static void cat_cpu_init(void)
+static void internal_cpu_init(void)

Is "internal_" really a useful prefix here?

I wanted to express this is an internal function in psr.c to separate it from psr_cpu_init().

The static keyword already says it's an internal one.

Quoted 34 lines
Quoted 25 lines
Quoted 25 lines
xen/arch/x86/psr.c +1 -23
+        pL3CAT = NULL;
 
-        spin_lock_init(&info->cbm_lock);
-
-        set_bit(socket, cat_socket_enable);
-
-        if ( (ecx & PSR_CAT_CDP_CAPABILITY) && (opt_psr & PSR_CDP) &&
-             cdp_socket_enable && !test_bit(socket, cdp_socket_enable) )
-        {
-            info->cos_to_cbm[0].code = (1ull << info->cbm_len) - 1;
-            info->cos_to_cbm[0].data = (1ull << info->cbm_len) - 1;
-
-            /* We only write mask1 since mask0 is always all ones by default. */
-            wrmsrl(MSR_IA32_PSR_L3_MASK(1), (1ull << info->cbm_len) - 1);
-
-            rdmsrl(MSR_IA32_PSR_L3_QOS_CFG, val);
-            wrmsrl(MSR_IA32_PSR_L3_QOS_CFG, val | (1 << PSR_L3_QOS_CDP_ENABLE_BIT));
-
-            /* Cut half of cos_max when CDP is enabled. */
-            info->cos_max >>= 1;
-
-            set_bit(socket, cdp_socket_enable);
-        }
-        printk(XENLOG_INFO "CAT: enabled on socket %u, cos_max:%u, cbm_len:%u, CDP:%s\n",
-               socket, info->cos_max, info->cbm_len,
Quoted 9 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1241

Quoted 5 lines
xen/arch/x86/psr.c +4 -1
-               cdp_is_enabled(socket) ? "on" : "off");
+        /* Initialize this feature according to CPUID. */
+        cpuid_count(PSR_CPUID_LEVEL_CAT, 1, &eax, &ebx, &ecx, &edx);
+        pTmp->ops = l3_cat_ops;
+        pTmp->ops.init_feature(eax, ebx, ecx, edx, pTmp, info);

This function can return without having consumed pTmp, which means you'd leak the memory in that case.

Quoted 1 line

Yes, right, this is by design.

I hope not. You shouldn't design in memory leaks.

Quoted 6 lines

pL3CAT/pL2CAT are allocated in
internal_cpu_prepare(). If this CPU does not support L2 CAT for example, the pL2CAT will not be added into feature list. But when CPU on other socket up, internal_cpu_prepare() will be called again and the pL2CAT will not be allocated again (check if it is NULL). We can use the buffer has been allocated. That means we maintain one redundant buffer to simplify the codes.

I don't think that's the case - pL3CAT gets set to NULL further up. But with you switching to generic list management, this bogus extra list element will go away anyway, and so will the extra allocation.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#4 lore raw
Quoted 22 lines · Jan Beulich
Quoted 11 lines
Quoted 10 lines

xen/arch/x86/psr.c line 54

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 25.08.16 at 07:22, <[email protected]> wrote:
xen/arch/x86/psr.c +5 -0
+                         struct psr_socket_alloc_info *info);
+    /*
+     * get_old_set_new is used in set value process to get all features'
+     * COS registers values according to original cos id of the domain.
+     * Then, assemble them into an mask array as feature list order.

This sentence in particular doesn't make any sense to me. What follows below also looks like it is in need of improvement.

Do you mean the comments are not accurate?

I simply wasn't able to tell, because of not being able to interpret the sentence.

Quoted 7 lines

How about below description?

get_old_set_new will traverse all features in list. It is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

Quoted 6 lines
Quoted 2 lines

All the values are set into mask array according the traversal order, meaning the same order of feature list members.

Sounds reasonable. I suppose the example that you gave right next wasn't meant to go into the comment.

Great, will replace the comments with the new sentences. The example will not go in.

Quoted 22 lines
Quoted 22 lines
Quoted 11 lines

xen/arch/x86/psr.c line 85

Quoted 10 lines
xen/arch/x86/psr.c +10 -0
+    /*
+     * exceed_range is used to check if the input cos id exceeds the
+     * feature's cos_max and if the input mask value is not the default one.
+     * Even if the associated cos exceeds the cos_max, HW can work as default
+     * value. That is the reason we need check is mask value is default one.
+     * If both criteria are fulfilled, that means the input exceeds the
+     * range.
+     */
+    unsigned int (*exceed_range)(uint64_t *mask, struct feat_list *pFeat,
+                                 unsigned int cos);
Quoted 8 lines

According to the comment this is kind of a predicate, which seems unlikely to return an unsigned value. In fact without a word on the return value I'd expect such to return bool. And I'd also expect the name to reflect the purpose, i.e. "exceeds_name()". Plus just like for compare above I wonder whether come or all of the parameters should be pointers to const (please go over the entire patch and do constification wherever possible/sensible).

Yes, you are right. I will change the function type to bool and add const for not changed input pointers.

Quoted 7 lines
Quoted 3 lines

This function is used to check if the input cos id exceeds the cos_max. If yes and the set value is not default value, we should return error. So, I think to change the function name to exceed_cos_max(). How do you think?

Okay, except that I continue to think you mean "exceeds". "exceed_cos_max" to me is kind of a directive, not a predicate.

How about "beyond"?

Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

xen/arch/x86/psr.c line 109

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+#define MAX_FEAT_INFO_SIZE 8
+#define MAX_COS_REG_NUM  128

Are these numbers arbitrary, or based on something?

MAX_FEAT_INFO_SIZE is got from the sizeof(struct psr_cat_lvl_info) and consider the extension for future feature.

In that case please use that sizeof() in the expression here.

Sure. Thanks!

Quoted 8 lines
Quoted 2 lines

MAX_COS_REG_NUM is got from spec that the max COS registers number is 128 for all PSR features so far.

"So far" makes me still wonder: Is this an architectural limit or one resulting from current (hardware) implementations. In the former case I don't think a comment is strictly needed, but in the latter case the choice should be explained.

It is the latter case. I will add comment to explain it. Thanks!

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

+struct psr_socket_alloc_info {

I've yet to see whether the "alloc" in the name really makes sense.

And btw., having seen the entire patch I don't think this alloc_ infix is warranted both here and in the variable name.

Ok, will consider to remove it in codes. Thanks!

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 167

Quoted 11 lines
xen/arch/x86/psr.c +11 -0
+/* Common functions for supporting feature callback functions. */
+static void add_feature(struct feat_list *pHead, struct feat_list *pTmp)
+{
+    if ( NULL == pHead || NULL == pTmp )
+        return;
+
+    while ( pHead->pNext )
+        pHead = pHead->pNext;
+
+    pHead->pNext = pTmp;
+}

Do you really need custom list management here?

Quoted 7 lines
Quoted 3 lines

It seems xen list interfaces require the input list be a double linked list but my list is a single linked list. Furthermore, I only need simple add to tail function and free function. So I create custom list management functions.

Unless there's a strong need, I'd like you to go with what is there, or introduce _generic_ singly linked list management.

I will check below list management interfaces to see if I can reuse them. Thanks!
xen/include/xen/list.h

Quoted 27 lines
Quoted 27 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/x86/psr.c +15 -0
+static void free_feature(struct psr_socket_alloc_info *info)
+{
+    struct feat_list *pTmp;
+    struct feat_list *pPrev;
+
+    if ( NULL == info )
+        return;
+
+    if ( NULL == info->pFeat )
+        return;
+
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        pPrev = pTmp;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 189

Quoted 6 lines
xen/arch/x86/psr.c +6 -0
+        pTmp = pTmp->pNext;
+        clear_bit(pPrev->feature, &(info->features));
+        xfree(pPrev);
+        pPrev = NULL;
+    }
+}

Why does info->pFeat not get freed here?

The info->pFeat is a redundant list head to facilitate list operations. It is only freed when socket info is freed.

Quoted 7 lines
Quoted 2 lines

With this list head, the advantage is that we can insert/delete the first element same as others without special operations.

But it results in less obvious code (and a basically pointless extra allocation). One more reason to use generic list management primitives.

I will check below list management interfaces to see if I can reuse them. Thanks!
xen/include/xen/list.h

Quoted 18 lines
Quoted 18 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+static void l3_cat_init_feature(unsigned int eax, unsigned int ebx,
+                                unsigned int ecx, unsigned int edx,
+                                struct feat_list *pFeat,
+                                struct psr_socket_alloc_info *info)
+{
+    struct psr_cat_lvl_info l3_cat;
+    unsigned int socket;
Quoted 9 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 234

Quoted 4 lines
xen/arch/x86/psr.c +4 -0
+    uint64_t val;
+
+    if ( MAX_FEAT_INFO_SIZE < sizeof(struct psr_cat_lvl_info) )
+        return;

DYM BUILD_BUG_ON()? Also the MAX_FEAT_INFO_SIZE dimensioned array is one with unsigned int as the base type, which doesn't get reflected here.

I will use BUILD_BUG_ON() for the error case.

Quoted 8 lines
Quoted 4 lines

As my explanation for MAX_FEAT_INFO_SIZE above, it reflects a size more than struct psr_cat_lvl_info. So, if MAX_FEAT_INFO_SIZE is bigger than the sizeof(struct psr_cat_lvl_info), the following operations on feat_info[] should be good.

In fact the BUILD_BUG_ON() may not be needed if you follow the suggestion above.

Ok, thanks!

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 271

Quoted 3 lines
xen/arch/x86/psr.c +3 -0
+    }
+
+    memcpy(pFeat->feat_info, &l3_cat, sizeof(struct psr_cat_lvl_info));

Aforementioned BUILD_BUG_ON() would probably bets be placed right ahead of this memcpy().

Quoted 7 lines
Quoted 2 lines

Can we call BUILD_BUG_ON() at the top of the function to avoid executing above process if the size is not right?

Well, the purpose of BUILD_BUG_ON() is what its name implies: It fails the _build_ if violated, so execution can't possibly come here (or in fact anywhere) in that case.

Thanks for the explanation!

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 290

Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+static int l3_cat_compare_mask(uint64_t *mask, struct feat_list *pFeat,
+                               unsigned int cos, bool_t *found)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));

Already here I think this memcpy()ing gets unwieldy. Can't you simply make the structure field a union of all types of interest?

Quoted 7 lines
Quoted 3 lines

Sorry that I am not very clear about your meaning to make a union. Do you mean make feat_info a union? If so, it will lose the universality to cover all features. Future feature may have different info.

Which is the purpose of a union - you'd simply add a new member then.

I guess your idea likes below. Right?

union {
    struct l3_info {
        union {
            uint64_t cbm;
            struct {
                uint64_t code;
                uint64_t data;
            };
        };

    };

    struct l2_info {
        uint64_t cbm;
    };
};

My original design is to use this feat_info cover all features and eliminate the feature's specific properties. If using above union, we still need to know the current feature is which when handles feat_info. That loses the abstraction.

If my thought is not right, please correct me. Thanks!

Quoted 4 lines
Quoted 1 line

I think I can replace the memcpy() to directly assign value to cat_info.

No, this copying (done in _many_ places) really should go away.

I want to replace memcpy() to below codes.
cat_info.cbm_len = feat_info[0];
cat_info.cos_max = feat_info[1];

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 1 line

+ printk(XENLOG_ERR "CDP exceed cos max.\n");

I haven't figured yet at what times this function will get called, but I suspect it's inappropriate for it to log messages.

Quoted 4 lines
xen/arch/x86/psr.c +4 -0
+                return -ENOENT;
+            }
+            *found = 1;
+            return 2;
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Who or what is 2?

CDP occupies 2 members in mask[] array. So, it returns 2 to make codes outside the function to skip to next feature's mask value.

I guess that's going to be understandable once the return values of the hooks get properly explained in their comments.

Yes, I will explain return value by adding comments.

Quoted 9 lines
Quoted 9 lines
Quoted 7 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 334

Quoted 4 lines
xen/arch/x86/psr.c +4 -0
+    if ( mask[0] == pFeat->cos_reg_val[cos] )
+        *found = 1;
+    else
+        *found = 0;

Please use a simple assignment in cases like this, instead of if/else.

Do you mean codes like below?

Quoted 8 lines
Quoted 3 lines

*found = 0;
if ( mask[0] == pFeat->cos_reg_val[cos] )

    *found = 1;

No.

    *found = (mask[0] == pFeat->cos_reg_val[cos]);

Thank you!

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 479

Quoted 5 lines
xen/arch/x86/psr.c +5 -0
+    /* CAT */
+    if ( old_cos > cat_info.cos_max )
+        mask[0] =  pFeat->cos_reg_val[0];
+    else
+        mask[0] =  pFeat->cos_reg_val[old_cos];

Across the entire function - wouldn't it be easier to range check old_cos once up front, forcing it to zero if the check fails? That would reduce code size to a better readable amount. (And just to avoid any misunderstanding - comments like this also may apply to other functions; I'm not going to repeat them there.)

Quoted 9 lines
Quoted 6 lines

Good idea! Thanks for the suggestion. I will check other functions like this.

Quoted 2 lines

Also note that there are undue double blanks in here.

Do you mean two spaces? I cannot see it in my original patch and codes.

It's even still visible in the quoted text above.

Oh, really strange. Anyway, I need submit a new version. I will check this carefully. Thanks!

Quoted 20 lines
Quoted 20 lines
Quoted 7 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 482

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+    if ( type == PSR_MASK_TYPE_L3_CBM )
+        mask[0] = m;

This overwriting behavior also looks quite strange to me. What's the purpose? And if this really is meant to be that way, why is this not (leaving aside the other suggested adjustment)

Quoted 9 lines
    if ( type == PSR_MASK_TYPE_L3_CBM )
        mask[0] = m;
    else if ( old_cos > cat_info.cos_max )
        mask[0] = pFeat->cos_reg_val[0];
    else
        mask[0] = pFeat->cos_reg_val[old_cos];

?

get_old_set_new() is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

Quoted 6 lines
Quoted 2 lines

So, if the appointed feature is L3 CAT, we should set input vallue for it here.

Okay, that answers the "what" aspect, but leaves open _why_ it needs to be that way.

A scenario here to help to understand _why_.

Like the example for explaining get_old_set_new(), old_cos of the domain is 1. Then, User wants to set L3 CAT CBM to 0x1ff and L2 CAT 0x3f. The original COS registers like below.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x3ff | 0x1ff | ... |
        -------------------------------
L2 CAT  | 0xff  | 0x3f  | 0x3f  | ... |
        -------------------------------

Then, mask array should be assembled in get_old_set_new() like below:
mask[0]: 0x1ff
mask[1]: 0x3f

Then, we can use this mask array to find if there is matching COS through compare_mask(). We can find COS 2 is the matching one.

If there is already a COS registers combination (e.g. L3 COS 2 and L2 COS 2) same as the mask array, we can reuse this combination directly but not to allocate a new COS ID. By this way, we can save the COS registers. You know, there is limitation on COS registers number.

Of course, if we cannot find it in existing combinations, we will call alloc_new_cos() to allocate a new COS to use.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

static inline void psr_assoc_init(void)

 {
     struct psr_assoc *psra = &this_cpu(psr_assoc);
xen/arch/x86/psr.c +5 -2
+    struct psr_socket_alloc_info *info;
+    unsigned int cos_max;
+    unsigned int socket;
 
-    if ( cat_socket_info )
+    if ( socket_alloc_info )
     {
-        unsigned int socket = cpu_to_socket(smp_processor_id());
+        socket = cpu_to_socket(smp_processor_id());
Quoted 9 lines
Quoted 6 lines
Quoted 5 lines

What's the point of moving the variable declaration? Quite the other way around, the new variable you add could - afaict - all live in this more narrow scope. And info could presumably become a pointer to const.

I remember it reports warning or error when compiling old codes.

I don't see why it would.

Hmm, I will check this again. Thanks!

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
Quoted 15 lines
xen/arch/x86/psr.c +5 -5
-int psr_get_cat_l3_info(unsigned int socket, uint32_t *cbm_len,
-                        uint32_t *cos_max, uint32_t *flags)
+int psr_get_info(unsigned int socket, enum mask_type type,
+                 uint32_t *dat0, uint32_t *dat1,
+                 uint32_t *dat2)
 {
-    struct psr_cat_socket_info *info = get_cat_socket_info(socket);
+    struct psr_socket_alloc_info *info = get_socket_alloc_info(socket);
+    struct feat_list *pTmp;
 
     if ( IS_ERR(info) )
         return PTR_ERR(info);
 
-    *cbm_len = info->cbm_len;
-    *cos_max = info->cos_max;

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 827

Quoted 10 lines
xen/arch/x86/psr.c +6 -3
+    if ( NULL == info->pFeat || NULL == info->pFeat->pNext)
+       return -ENODEV;
 
-    *flags = 0;
-    if ( cdp_is_enabled(socket) )
-        *flags |= XEN_SYSCTL_PSR_CAT_L3_CDP;
+    pTmp = info->pFeat->pNext;
+    while ( pTmp )
+    {
+        if ( pTmp->ops.get_feat_info(pTmp, type, dat0, dat1, dat2) )

Considering that this is one of the generic ops: What guarantees that 3 elements will suffice going forward? I.e. why is this not an array?

Quoted 8 lines
Quoted 3 lines

Good point. I consider this too. Per all features we know, three parameters are sufficient. To simplify codes, I write the function like this. I think we can make change if future feature has more requirements.

If you introduce an abstraction layer, you should design it with abstract requirements in mind, not with the requirements of currently known implementations.

Ok, will consider to make a general interface.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 7 lines
xen/arch/x86/psr.c +1 -6
-    case PSR_CBM_TYPE_L3_DATA:
-        if ( !cdp_enabled )
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].cbm;
-        else
-            *cbm = info->cos_to_cbm[d->arch.psr_cos_ids[socket]].data;
-        break;
+    pTmp = info->pFeat->pNext;

xen/arch/x86/psr.c void psr_ctxt_switch_to(struct domain *d) · line 859

Quoted 10 lines
xen/arch/x86/psr.c +7 -2
+    while ( pTmp )
+    {
+        /* mask getting and setting order is same as feature list */
+        ret = pTmp->ops.get_old_set_new(mask, pTmp, old_cos, type, m);
+        if ( ret < 0 )
+            return ret;
 
-    default:
-        ASSERT_UNREACHABLE();
+        mask += ret;

With this - what ensures the array isn't going to be overrun?

Quoted 9 lines
Quoted 4 lines

It seems you missed one line below. The while() loop traverse all features in list. It will exit when the last member has been accessed.

+ pTmp = pTmp->pNext;

No, I didn't miss any line. I'm asking because there's nothing but programmer discipline keeping array size and list length match up. That's fragile.

Sorry, I misunderstood your meaning. I will add a mechanism to check mask array size. Thanks for pointing this out!

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

And I'm also having some difficulty seeing how an array of outputs matches up with exactly one input (m). Can you explain this please?

This while loop will traverse all features in list. It will get all features old_cos value back. If the feature is the one to set new value, the input new value (m) will replace the old_cos value. All the value are set into mask array according the traversal order, meaning the same order of feature list members, like L3 CAT as the first and L2 CAT as the second.

Quoted 4 lines

This gets us back to the question of _why_ you need this "get all but set only one" functionality. Plus, wasn't it that CDP requires two values per feature?

Please check above comment about "_why_".

After operation in get_old_set_new(), CDP occupies two elements of mask array.

Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1213

Quoted 2 lines
xen/arch/x86/psr.c +1 -1
-static void cat_cpu_init(void)
+static void internal_cpu_init(void)

Is "internal_" really a useful prefix here?

I wanted to express this is an internal function in psr.c to separate it from psr_cpu_init().

The static keyword already says it's an internal one.

Yes, I did not figure out a better function name so I used 'internal'. Will consider another one if you think this is not appropriate.

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 20 lines
xen/arch/x86/psr.c +1 -18
+        pL3CAT = NULL;
 
-        spin_lock_init(&info->cbm_lock);
-
-        set_bit(socket, cat_socket_enable);
-
-        if ( (ecx & PSR_CAT_CDP_CAPABILITY) && (opt_psr & PSR_CDP) &&
-             cdp_socket_enable && !test_bit(socket, cdp_socket_enable) )
-        {
-            info->cos_to_cbm[0].code = (1ull << info->cbm_len) - 1;
-            info->cos_to_cbm[0].data = (1ull << info->cbm_len) - 1;
-
-            /* We only write mask1 since mask0 is always all ones by default. */
-            wrmsrl(MSR_IA32_PSR_L3_MASK(1), (1ull << info->cbm_len) - 1);
-
-            rdmsrl(MSR_IA32_PSR_L3_QOS_CFG, val);
-            wrmsrl(MSR_IA32_PSR_L3_QOS_CFG, val | (1 << PSR_L3_QOS_CDP_ENABLE_BIT));
-
-            /* Cut half of cos_max when CDP is enabled. */
-            info->cos_max >>= 1;

xen/arch/x86/psr.c void psr_domain_free(struct domain *d) · line 1241

Quoted 10 lines
xen/arch/x86/psr.c +4 -6
-
-            set_bit(socket, cdp_socket_enable);
-        }
-        printk(XENLOG_INFO "CAT: enabled on socket %u, cos_max:%u, cbm_len:%u, CDP:%s\n",
-               socket, info->cos_max, info->cbm_len,
-               cdp_is_enabled(socket) ? "on" : "off");
+        /* Initialize this feature according to CPUID. */
+        cpuid_count(PSR_CPUID_LEVEL_CAT, 1, &eax, &ebx, &ecx, &edx);
+        pTmp->ops = l3_cat_ops;
+        pTmp->ops.init_feature(eax, ebx, ecx, edx, pTmp, info);

This function can return without having consumed pTmp, which means you'd leak the memory in that case.

Quoted 4 lines
Quoted 1 line

Yes, right, this is by design.

I hope not. You shouldn't design in memory leaks.

Ok, I will free the redundant buffer in free_feature(). Thanks!

Quoted 11 lines
Quoted 6 lines

pL3CAT/pL2CAT are allocated in
internal_cpu_prepare(). If this CPU does not support L2 CAT for example, the pL2CAT will not be added into feature list. But when CPU on other socket up, internal_cpu_prepare() will be called again and the pL2CAT will not be allocated again (check if it is NULL). We can use the buffer has been allocated. That means we maintain one redundant buffer to simplify the codes.

I don't think that's the case - pL3CAT gets set to NULL further up. But with you switching to generic list management, this bogus extra list element will go away anyway, and so will the extra allocation.

Yes, you are right. For above scenario I described, pL3CAT will be allocated again because it has been added into feature list and set to NULL. Finally, it will be freed in free_feature(). So, pL3CAT is not a memory leak.

I am trying to explain the memory leak case, pL2CAT. Because CPU on socket0 does not support L2 CAT, it is not added into feature list and not set to NULL. But if CPU on socket1 up and it supports L2 CAT, the pL2CAT allocated before can be used directly without allocating a new buffer. Of course, if there is no such case, e.g. all CPUs only support L3 CAT, the pL2CAT is leaked.

Per your suggestion that not allowing any memory leak, I will free it in free_feature().

Quoted 1 line

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 08.09.16 at 09:28, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 4 lines

xen/arch/x86/psr.c line 85

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.08.16 at 07:22, <[email protected]> wrote:
xen/arch/x86/psr.c +2 -0
+    unsigned int (*exceed_range)(uint64_t *mask, struct feat_list *pFeat,
+                                 unsigned int cos);
Quoted 8 lines

According to the comment this is kind of a predicate, which seems unlikely to return an unsigned value. In fact without a word on the return value I'd expect such to return bool. And I'd also expect the name to reflect the purpose, i.e. "exceeds_name()". Plus just like for compare above I wonder whether come or all of the parameters should be pointers to const (please go over the entire patch and do constification wherever possible/sensible).

Yes, you are right. I will change the function type to bool and add const for not changed input pointers.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

This function is used to check if the input cos id exceeds the cos_max. If yes and the set value is not default value, we should return error. So, I think to change the function name to exceed_cos_max(). How do you think?

Okay, except that I continue to think you mean "exceeds". "exceed_cos_max" to me is kind of a directive, not a predicate.

How about "beyond"?

What's wrong with "exceeds"?

Quoted 36 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 290

Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+static int l3_cat_compare_mask(uint64_t *mask, struct feat_list *pFeat,
+                               unsigned int cos, bool_t *found)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));

Already here I think this memcpy()ing gets unwieldy. Can't you simply make the structure field a union of all types of interest?

Quoted 7 lines
Quoted 3 lines

Sorry that I am not very clear about your meaning to make a union. Do you mean make feat_info a union? If so, it will lose the universality to cover all features. Future feature may have different info.

Which is the purpose of a union - you'd simply add a new member then.

I guess your idea likes below. Right?

union {
    struct l3_info {
        union {
            uint64_t cbm;
            struct {
                uint64_t code;
                uint64_t data;
            };
        };

    };

    struct l2_info {
        uint64_t cbm;
    };
};
Quoted 6 lines

My original design is to use this feat_info cover all features and eliminate the feature's specific properties. If using above union, we still need to know the current feature is which when handles feat_info. That loses the abstraction.

If my thought is not right, please correct me. Thanks!

I don't understand what abstraction you would lose with the above layout. The memcpy()int you currently do is, I'm sorry to say that, horrible.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I think I can replace the memcpy() to directly assign value to cat_info.

No, this copying (done in _many_ places) really should go away.

I want to replace memcpy() to below codes.
cat_info.cbm_len = feat_info[0];
cat_info.cos_max = feat_info[1];

And again do that in a dozen places? No, please don't.

Quoted 48 lines
Quoted 20 lines
Quoted 20 lines
Quoted 7 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 482

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+    if ( type == PSR_MASK_TYPE_L3_CBM )
+        mask[0] = m;

This overwriting behavior also looks quite strange to me. What's the purpose? And if this really is meant to be that way, why is this not (leaving aside the other suggested adjustment)

Quoted 9 lines
    if ( type == PSR_MASK_TYPE_L3_CBM )
        mask[0] = m;
    else if ( old_cos > cat_info.cos_max )
        mask[0] = pFeat->cos_reg_val[0];
    else
        mask[0] = pFeat->cos_reg_val[old_cos];

?

get_old_set_new() is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

Quoted 6 lines
Quoted 2 lines

So, if the appointed feature is L3 CAT, we should set input vallue for it here.

Okay, that answers the "what" aspect, but leaves open _why_ it needs to be that way.

A scenario here to help to understand _why_.

Like the example for explaining get_old_set_new(), old_cos of the domain is 1.
Then, User wants to set L3 CAT CBM to 0x1ff and L2 CAT 0x3f. The original COS registers like below.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x3ff | 0x1ff | ... |
        -------------------------------
L2 CAT  | 0xff  | 0x3f  | 0x3f  | ... |
        -------------------------------

Then, mask array should be assembled in get_old_set_new() like below:
mask[0]: 0x1ff
mask[1]: 0x3f

Then, we can use this mask array to find if there is matching COS through compare_mask(). We can find COS 2 is the matching one.

Quoted 7 lines

If there is already a COS registers combination (e.g. L3 COS 2 and L2 COS 2) same as the mask array, we can reuse this combination directly but not to allocate a new COS ID. By this way, we can save the COS registers. You know, there is limitation on COS registers number.

Of course, if we cannot find it in existing combinations, we will call alloc_new_cos() to allocate a new COS to use.

I think I begin to see the purpose, but then again I still can't get this explanation in line with there just being a single new value to be set (m). Perhaps it would help if you split the lookup from the setting or a new value.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#6 lore raw
Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 08.09.16 at 09:28, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 18 lines
Quoted 4 lines

xen/arch/x86/psr.c line 85

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.08.16 at 07:22, <[email protected]> wrote:
xen/arch/x86/psr.c +2 -0
+    unsigned int (*exceed_range)(uint64_t *mask, struct feat_list *pFeat,
+                                 unsigned int cos);
Quoted 8 lines

According to the comment this is kind of a predicate, which seems unlikely to return an unsigned value. In fact without a word on the return value I'd expect such to return bool. And I'd also expect the name to reflect the purpose, i.e. "exceeds_name()". Plus just like for compare above I wonder whether come or all of the parameters should be pointers to const (please go over the entire patch and do constification wherever possible/sensible).

Yes, you are right. I will change the function type to bool and add const for not changed input pointers.

This function is used to check if the input cos id exceeds the cos_max. If yes and the set value is not default value, we should return error. So, I think to change the function name to exceed_cos_max(). How do you think?

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Okay, except that I continue to think you mean "exceeds". "exceed_cos_max" to me is kind of a directive, not a predicate.

How about "beyond"?

What's wrong with "exceeds"?

Sorry, I may misunderstand you. Maybe "check_exceed_cos_max" is a good name?

Quoted 41 lines
Quoted 41 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 290

Quoted 7 lines
xen/arch/x86/psr.c +7 -0
+static int l3_cat_compare_mask(uint64_t *mask, struct feat_list *pFeat,
+                               unsigned int cos, bool_t *found)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));

Already here I think this memcpy()ing gets unwieldy. Can't you simply make the structure field a union of all types of interest?

Quoted 7 lines
Quoted 3 lines

Sorry that I am not very clear about your meaning to make a union. Do you mean make feat_info a union? If so, it will lose the universality to cover all features. Future feature may have different info.

Which is the purpose of a union - you'd simply add a new member then.

I guess your idea likes below. Right?

union {
    struct l3_info {
        union {
            uint64_t cbm;
            struct {
                uint64_t code;
                uint64_t data;
            };
        };

    };

    struct l2_info {
        uint64_t cbm;
    };
};

My original design is to use this feat_info cover all features and eliminate the feature's specific properties. If using above union, we still need to know the current feature is which when handles feat_info. That loses the abstraction.

Quoted 6 lines
Quoted 1 line

If my thought is not right, please correct me. Thanks!

I don't understand what abstraction you would lose with the above layout. The memcpy()int you currently do is, I'm sorry to say that, horrible.

Sorry to make you feel bad. I will avoid to use memcpy() in the codes.

I think I have got your mean. I will construct a union for feat_info in next version. Different features can use different members in union. Then, I can directly assign the struct value without memcpy. Thanks for the guidance!

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I think I can replace the memcpy() to directly assign value to cat_info.

No, this copying (done in _many_ places) really should go away.

I want to replace memcpy() to below codes.
cat_info.cbm_len = feat_info[0];
cat_info.cos_max = feat_info[1];

And again do that in a dozen places? No, please don't.

Sure.

Quoted 56 lines
Quoted 48 lines
Quoted 20 lines
Quoted 20 lines
Quoted 7 lines

xen/arch/x86/psr.c static unsigned int __read_mostly opt_cos_max = 255; · line 482

Quoted 2 lines
xen/arch/x86/psr.c +2 -0
+    if ( type == PSR_MASK_TYPE_L3_CBM )
+        mask[0] = m;

This overwriting behavior also looks quite strange to me. What's the purpose? And if this really is meant to be that way, why is this not (leaving aside the other suggested adjustment)

Quoted 9 lines
    if ( type == PSR_MASK_TYPE_L3_CBM )
        mask[0] = m;
    else if ( old_cos > cat_info.cos_max )
        mask[0] = pFeat->cos_reg_val[0];
    else
        mask[0] = pFeat->cos_reg_val[old_cos];

?

get_old_set_new() is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

Quoted 6 lines
Quoted 2 lines

So, if the appointed feature is L3 CAT, we should set input vallue for it here.

Okay, that answers the "what" aspect, but leaves open _why_ it needs to be that way.

A scenario here to help to understand _why_.

Like the example for explaining get_old_set_new(), old_cos of the domain is 1.
Then, User wants to set L3 CAT CBM to 0x1ff and L2 CAT 0x3f. The original COS registers like below.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x3ff | 0x1ff | ... |
        -------------------------------
L2 CAT  | 0xff  | 0x3f  | 0x3f  | ... |
        -------------------------------

Then, mask array should be assembled in get_old_set_new() like below:
mask[0]: 0x1ff
mask[1]: 0x3f

Then, we can use this mask array to find if there is matching COS through compare_mask(). We can find COS 2 is the matching one.

Quoted 7 lines

If there is already a COS registers combination (e.g. L3 COS 2 and L2 COS 2) same as the mask array, we can reuse this combination directly but not to allocate a new COS ID. By this way, we can save the COS registers. You know, there is limitation on COS registers number.

Of course, if we cannot find it in existing combinations, we will call alloc_new_cos() to allocate a new COS to use.

Quoted 6 lines

I think I begin to see the purpose, but then again I still can't get this explanation in line with there just being a single new value to be set (m). Perhaps it would help if you split the lookup from the setting or a new value.

Jan

Sure, let me introduce it from beginning.

First time, user wants to set L3 CAT of Dom1 to 0x1ff for example. The old_cos of Dom1 is 0. L3 CAT is the first element of feature list. The COS registers values are below at this time.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | ...   | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | ...   | ...   | ... |
        -------------------------------

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

It cannot find a matching COS so allocate COS 1 to store the value set. The COS registers values are changed to below now.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x1ff | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | 0xff  | ...   | ... |
        -------------------------------

Then, user wants to set L3 CAT of Dom2 to 0x1ff too. The old_cos of Dom2 is 0 too.

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

So, it can find a matching COS, COS 1. Then, it can reuse COS 1 for Dom2.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#7 lore raw
Quoted 23 lines
Quoted 1 line
Quoted 1 line · On 09.09.16 at 10:09, <[email protected]> wrote:
Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 08.09.16 at 09:28, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 18 lines
Quoted 4 lines

Review

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.08.16 at 07:22, <[email protected]> wrote:
(quoted diff) +2 -0
+    unsigned int (*exceed_range)(uint64_t *mask, struct feat_list *pFeat,
+                                 unsigned int cos);
Quoted 8 lines

According to the comment this is kind of a predicate, which seems unlikely to return an unsigned value. In fact without a word on the return value I'd expect such to return bool. And I'd also expect the name to reflect the purpose, i.e. "exceeds_name()". Plus just like for compare above I wonder whether come or all of the parameters should be pointers to const (please go over the entire patch and do constification wherever possible/sensible).

Yes, you are right. I will change the function type to bool and add const for not changed input pointers.

This function is used to check if the input cos id exceeds the cos_max. If yes and the set value is not default value, we should return error. So, I think to change the function name to exceed_cos_max(). How do you think?

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Okay, except that I continue to think you mean "exceeds". "exceed_cos_max" to me is kind of a directive, not a predicate.

How about "beyond"?

What's wrong with "exceeds"?

Sorry, I may misunderstand you. Maybe "check_exceed_cos_max" is a good name?

According to my knowledge of English this would still need to be "check_exceeds_cos_max", and then the check ends up being quite redundant.

Quoted 47 lines
Quoted 41 lines
Quoted 41 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static int l3_cat_compare_mask(uint64_t *mask, struct feat_list *pFeat,
+                               unsigned int cos, bool_t *found)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));

Already here I think this memcpy()ing gets unwieldy. Can't you simply make the structure field a union of all types of interest?

Quoted 7 lines
Quoted 3 lines

Sorry that I am not very clear about your meaning to make a union. Do you mean make feat_info a union? If so, it will lose the universality to cover all features. Future feature may have different info.

Which is the purpose of a union - you'd simply add a new member then.

I guess your idea likes below. Right?

union {
    struct l3_info {
        union {
            uint64_t cbm;
            struct {
                uint64_t code;
                uint64_t data;
            };
        };

    };

    struct l2_info {
        uint64_t cbm;
    };
};

My original design is to use this feat_info cover all features and eliminate the feature's specific properties. If using above union, we still need to know the current feature is which when handles feat_info. That loses the abstraction.

Quoted 6 lines
Quoted 1 line

If my thought is not right, please correct me. Thanks!

I don't understand what abstraction you would lose with the above layout. The memcpy()int you currently do is, I'm sorry to say that, horrible.

Quoted 5 lines

Sorry to make you feel bad. I will avoid to use memcpy() in the codes.

I think I have got your mean. I will construct a union for feat_info in next version. Different features can use different members in union. Then, I can directly assign the struct value without memcpy. Thanks for the guidance!

You shouldn't need to do such assignments in the vast majority of places you do the memcpy()ing in right now. (And don't forget, structure assignment in the end is only a type safe variant of memcpy().)

Quoted 96 lines
Quoted 42 lines
Quoted 42 lines
Quoted 20 lines
Quoted 20 lines
Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    if ( type == PSR_MASK_TYPE_L3_CBM )
+        mask[0] = m;

This overwriting behavior also looks quite strange to me. What's the purpose? And if this really is meant to be that way, why is this not (leaving aside the other suggested adjustment)

Quoted 9 lines
    if ( type == PSR_MASK_TYPE_L3_CBM )
        mask[0] = m;
    else if ( old_cos > cat_info.cos_max )
        mask[0] = pFeat->cos_reg_val[0];
    else
        mask[0] = pFeat->cos_reg_val[old_cos];

?

get_old_set_new() is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

Quoted 6 lines
Quoted 2 lines

So, if the appointed feature is L3 CAT, we should set input vallue for it here.

Okay, that answers the "what" aspect, but leaves open _why_ it needs to be that way.

A scenario here to help to understand _why_.

Like the example for explaining get_old_set_new(), old_cos of the domain is

1.
Then, User wants to set L3 CAT CBM to 0x1ff and L2 CAT 0x3f. The original COS registers like below.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x3ff | 0x1ff | ... |
        -------------------------------
L2 CAT  | 0xff  | 0x3f  | 0x3f  | ... |
        -------------------------------
Quoted 8 lines
Quoted 8 lines

Then, mask array should be assembled in get_old_set_new() like below:
mask[0]: 0x1ff
mask[1]: 0x3f

Then, we can use this mask array to find if there is matching COS through compare_mask(). We can find COS 2 is the matching one.

If there is already a COS registers combination (e.g. L3 COS 2 and L2 COS

2)

Quoted 2 lines
Quoted 2 lines

same as the mask array, we can reuse this combination directly but not to allocate a new COS ID. By this way, we can save the COS registers. You

know,

Quoted 11 lines
Quoted 4 lines

there is limitation on COS registers number.

Of course, if we cannot find it in existing combinations, we will call alloc_new_cos() to allocate a new COS to use.

I think I begin to see the purpose, but then again I still can't get this explanation in line with there just being a single new value to be set (m). Perhaps it would help if you split the lookup from the setting or a new value.

Jan

Sure, let me introduce it from beginning.

First time, user wants to set L3 CAT of Dom1 to 0x1ff for example. The old_cos
of Dom1 is 0. L3 CAT is the first element of feature list. The COS registers values are below at this time.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | ...   | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | ...   | ...   | ... |
        -------------------------------

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

It cannot find a matching COS so allocate COS 1 to store the value set. The COS registers values are changed to below now.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x1ff | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | 0xff  | ...   | ... |
        -------------------------------
Quoted 8 lines

Then, user wants to set L3 CAT of Dom2 to 0x1ff too. The old_cos of Dom2 is 0 too.

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

So, it can find a matching COS, COS 1. Then, it can reuse COS 1 for Dom2.

So the lookup is then to find an entry with all current values except for the one intended to be changed. If that's a correct understanding of mine, then I think the primitives should be more primitive, making the overall operation
1) get all current values into an array,
2) update the one array slot that's meant to get changed,
3) look for matching entries,
4) allocate new entry if none found.
And with that I'm then not even sure how many of these steps actually need hooks, as most if not all of this looks pretty independent of the aspects of the specific CAT variant. Hooks should be introduced solely for cases where behavior necessarily differs between variants.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#8 lore raw

Jan Beulich writes ("Re: [PATCH 1/3] x86: refactor psr implementation in hypervisor."):

Quoted 6 lines · On 09.09.16 at 10:09, <[email protected]> wrote:
Quoted 1 line

Sorry, I may misunderstand you. Maybe "check_exceed_cos_max" is a good name?

According to my knowledge of English this would still need to be "check_exceeds_cos_max", and then the check ends up being quite redundant.

In the hope of helping to end this subthread: I'm a native speaker of English and Jan is right.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#10 lore raw
In reply to Jan Beulich
Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 09.09.16 at 10:09, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 08.09.16 at 09:28, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 4 lines

Review

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 25.08.16 at 07:22, <[email protected]> wrote:
(quoted diff) +2 -0
+    unsigned int (*exceed_range)(uint64_t *mask, struct feat_list *pFeat,
+                                 unsigned int cos);
Quoted 8 lines

According to the comment this is kind of a predicate, which seems unlikely to return an unsigned value. In fact without a word on the return value I'd expect such to return bool. And I'd also expect the name to reflect the purpose, i.e. "exceeds_name()". Plus just like for compare above I wonder whether come or all of the parameters should be pointers to const (please go over the entire patch and do constification wherever possible/sensible).

Yes, you are right. I will change the function type to bool and add const for not changed input pointers.

Quoted 7 lines
Quoted 3 lines

This function is used to check if the input cos id exceeds the cos_max. If yes and the set value is not default value, we should return error. So, I think to change the function name to exceed_cos_max(). How do you think?

Okay, except that I continue to think you mean "exceeds". "exceed_cos_max" to me is kind of a directive, not a predicate.

How about "beyond"?

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

What's wrong with "exceeds"?

Sorry, I may misunderstand you. Maybe "check_exceed_cos_max" is a good name?

According to my knowledge of English this would still need to be "check_exceeds_cos_max", and then the check ends up being quite redundant.

Ok, got it. Thanks!

Quoted 49 lines
Quoted 49 lines
Quoted 41 lines
Quoted 41 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+static int l3_cat_compare_mask(uint64_t *mask, struct feat_list *pFeat,
+                               unsigned int cos, bool_t *found)
+{
+    struct psr_cat_lvl_info cat_info;
+    uint64_t l3_def_cbm;
+
+    memcpy(&cat_info, pFeat->feat_info, sizeof(struct psr_cat_lvl_info));

Already here I think this memcpy()ing gets unwieldy. Can't you simply make the structure field a union of all types of interest?

Quoted 7 lines
Quoted 3 lines

Sorry that I am not very clear about your meaning to make a union. Do you mean make feat_info a union? If so, it will lose the universality to cover all features. Future feature may have different info.

Which is the purpose of a union - you'd simply add a new member then.

I guess your idea likes below. Right?

union {
    struct l3_info {
        union {
            uint64_t cbm;
            struct {
                uint64_t code;
                uint64_t data;
            };
        };

    };

    struct l2_info {
        uint64_t cbm;
    };
};

My original design is to use this feat_info cover all features and eliminate the feature's specific properties. If using above union, we still need to know the current feature is which when handles feat_info. That loses the abstraction.

Quoted 6 lines
Quoted 1 line

If my thought is not right, please correct me. Thanks!

I don't understand what abstraction you would lose with the above layout. The memcpy()int you currently do is, I'm sorry to say that, horrible.

Sorry to make you feel bad. I will avoid to use memcpy() in the codes.

Quoted 9 lines
Quoted 3 lines

I think I have got your mean. I will construct a union for feat_info in next version. Different features can use different members in union. Then, I can directly assign the struct value without memcpy. Thanks for the guidance!

You shouldn't need to do such assignments in the vast majority of places you do the memcpy()ing in right now. (And don't forget, structure assignment in the end is only a type safe variant of memcpy().)

Got it, thanks!

Quoted 119 lines
Quoted 96 lines
Quoted 42 lines
Quoted 42 lines
Quoted 20 lines
Quoted 20 lines
Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    if ( type == PSR_MASK_TYPE_L3_CBM )
+        mask[0] = m;

This overwriting behavior also looks quite strange to me. What's the purpose? And if this really is meant to be that way, why is this not (leaving aside the other suggested adjustment)

Quoted 9 lines
    if ( type == PSR_MASK_TYPE_L3_CBM )
        mask[0] = m;
    else if ( old_cos > cat_info.cos_max )
        mask[0] = pFeat->cos_reg_val[0];
    else
        mask[0] = pFeat->cos_reg_val[old_cos];

?

get_old_set_new() is used to do below two things:
1. get old_cos register value of all supported features and
2. set the new value for appointed feature.

Quoted 6 lines
Quoted 2 lines

So, if the appointed feature is L3 CAT, we should set input vallue for it here.

Okay, that answers the "what" aspect, but leaves open _why_ it needs to be that way.

A scenario here to help to understand _why_.

Like the example for explaining get_old_set_new(), old_cos of the domain is

1.
Then, User wants to set L3 CAT CBM to 0x1ff and L2 CAT 0x3f. The original COS registers like below.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x3ff | 0x1ff | ... |
        -------------------------------
L2 CAT  | 0xff  | 0x3f  | 0x3f  | ... |
        -------------------------------
Quoted 8 lines
Quoted 8 lines

Then, mask array should be assembled in get_old_set_new() like below:
mask[0]: 0x1ff
mask[1]: 0x3f

Then, we can use this mask array to find if there is matching COS through compare_mask(). We can find COS 2 is the matching one.

If there is already a COS registers combination (e.g. L3 COS 2 and L2 COS

2)

Quoted 2 lines
Quoted 2 lines

same as the mask array, we can reuse this combination directly but not to allocate a new COS ID. By this way, we can save the COS registers. You

know,

Quoted 11 lines
Quoted 4 lines

there is limitation on COS registers number.

Of course, if we cannot find it in existing combinations, we will call alloc_new_cos() to allocate a new COS to use.

I think I begin to see the purpose, but then again I still can't get this explanation in line with there just being a single new value to be set (m). Perhaps it would help if you split the lookup from the setting or a new value.

Jan

Sure, let me introduce it from beginning.

First time, user wants to set L3 CAT of Dom1 to 0x1ff for example. The old_cos
of Dom1 is 0. L3 CAT is the first element of feature list. The COS registers values are below at this time.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | ...   | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | ...   | ...   | ... |
        -------------------------------

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

It cannot find a matching COS so allocate COS 1 to store the value set. The COS registers values are changed to below now.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x1ff | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | 0xff  | ...   | ... |
        -------------------------------
Quoted 8 lines

Then, user wants to set L3 CAT of Dom2 to 0x1ff too. The old_cos of Dom2 is 0 too.

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

So, it can find a matching COS, COS 1. Then, it can reuse COS 1 for Dom2.

So the lookup is then to find an entry with all current values except for the one intended to be changed. If that's a correct understanding of mine, then I think the primitives should be more primitive, making the overall operation
1) get all current values into an array,
2) update the one array slot that's meant to get changed,
3) look for matching entries,
4) allocate new entry if none found.
And with that I'm then not even sure how many of these steps actually need hooks, as most if not all of this looks pretty independent of the aspects of the specific CAT variant. Hooks should be introduced solely for cases where behavior necessarily differs between variants.

Quoted 1 line

Jan

By analyzing the behaviors and requirements of different CAT features, I divide COS find/allocate process to three steps.
1) get_old_set_new(): get all current values into an array and the value of the changed feature is replaced by the input value(m). Here, you get an array which contains all features old value and the new value for changed feature.
2) find_cos(): find if there is a matching COS combination which can match the input array.
3) alloc_new_cos(): if find_cos() cannot find a matching one, allocate a new COS id or reuse the one which is only occupied by current domain.

So, our ideas are almost same. Only one exception to split get_old_set_new() function into two parts. That increases one loop to traverse all features and most features "set new value" callback functions will return directly because the type cannot match. And, I think to put value array assembling work into one function is reasonable. What do you think? Thanks!

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#9 lore raw
In reply to Ian Jackson
Quoted 4 lines · Ian Jackson

Jan Beulich writes ("Re: [PATCH 1/3] x86: refactor psr implementation in hypervisor."):

Quoted 3 lines · On 09.09.16 at 10:09, <[email protected]> wrote:
Quoted 1 line

Sorry, I may misunderstand you. Maybe "check_exceed_cos_max" is a good name?

Quoted 8 lines
Quoted 3 lines

According to my knowledge of English this would still need to be "check_exceeds_cos_max", and then the check ends up being quite redundant.

In the hope of helping to end this subthread: I'm a native speaker of English and Jan is right.

Ian.

Thank you!

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#11 lore raw
In reply to Yi Sun
Quoted 66 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 05:26, <[email protected]> wrote:
Quoted 52 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 09.09.16 at 10:09, <[email protected]> wrote:

First time, user wants to set L3 CAT of Dom1 to 0x1ff for example. The old_cos
of Dom1 is 0. L3 CAT is the first element of feature list. The COS registers values are below at this time.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | ...   | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | ...   | ...   | ... |
        -------------------------------

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

It cannot find a matching COS so allocate COS 1 to store the value set. The COS registers values are changed to below now.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x1ff | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | 0xff  | ...   | ... |
        -------------------------------
Quoted 8 lines

Then, user wants to set L3 CAT of Dom2 to 0x1ff too. The old_cos of Dom2 is 0 too.

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

So, it can find a matching COS, COS 1. Then, it can reuse COS 1 for Dom2.

So the lookup is then to find an entry with all current values except for the one intended to be changed. If that's a correct understanding of mine, then I think the primitives should be more primitive, making the overall operation
1) get all current values into an array,
2) update the one array slot that's meant to get changed,
3) look for matching entries,
4) allocate new entry if none found.
And with that I'm then not even sure how many of these steps actually need hooks, as most if not all of this looks pretty independent of the aspects of the specific CAT variant. Hooks should be introduced solely for cases where behavior necessarily differs between variants.

Quoted 1 line

Jan

By analyzing the behaviors and requirements of different CAT features, I divide COS find/allocate process to three steps.
1) get_old_set_new(): get all current values into an array and the value of the changed feature is replaced by the input value(m). Here, you get an array
which contains all features old value and the new value for changed feature.
2) find_cos(): find if there is a matching COS combination which can match the input array.
3) alloc_new_cos(): if find_cos() cannot find a matching one, allocate a new COS id or reuse the one which is only occupied by current domain.

Quoted 5 lines

So, our ideas are almost same. Only one exception to split get_old_set_new() function into two parts. That increases one loop to traverse all features and most features "set new value" callback functions will return directly because the type cannot match. And, I think to put value array assembling work into one function is reasonable. What do you think? Thanks!

How many iterations do you expect these loops to have? If it's only a few, I think the simpler and more clear abstraction would outweigh the performance benefits, the more that I hope none of this ends up sitting on a performance critical path in the first place.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#12 lore raw
Quoted 72 lines · Jan Beulich
Quoted 66 lines
Quoted 1 line
Quoted 1 line · On 12.09.16 at 05:26, <[email protected]> wrote:
Quoted 52 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 09.09.16 at 10:09, <[email protected]> wrote:

First time, user wants to set L3 CAT of Dom1 to 0x1ff for example. The old_cos
of Dom1 is 0. L3 CAT is the first element of feature list. The COS registers values are below at this time.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | ...   | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | ...   | ...   | ... |
        -------------------------------

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

It cannot find a matching COS so allocate COS 1 to store the value set. The COS registers values are changed to below now.

        -------------------------------
        | COS 0 | COS 1 | COS 2 | ... |
        -------------------------------
L3 CAT  | 0x7ff | 0x1ff | ...   | ... |
        -------------------------------
L2 CAT  | 0xff  | 0xff  | ...   | ... |
        -------------------------------
Quoted 8 lines

Then, user wants to set L3 CAT of Dom2 to 0x1ff too. The old_cos of Dom2 is 0 too.

The mask array assembled in get_old_set_new() is:
mask[0]: 0x1ff
mask[1]: 0xff

So, it can find a matching COS, COS 1. Then, it can reuse COS 1 for Dom2.

So the lookup is then to find an entry with all current values except for the one intended to be changed. If that's a correct understanding of mine, then I think the primitives should be more primitive, making the overall operation
1) get all current values into an array,
2) update the one array slot that's meant to get changed,
3) look for matching entries,
4) allocate new entry if none found.
And with that I'm then not even sure how many of these steps actually need hooks, as most if not all of this looks pretty independent of the aspects of the specific CAT variant. Hooks should be introduced solely for cases where behavior necessarily differs between variants.

Quoted 1 line

Jan

By analyzing the behaviors and requirements of different CAT features, I divide COS find/allocate process to three steps.
1) get_old_set_new(): get all current values into an array and the value of the changed feature is replaced by the input value(m). Here, you get an array
which contains all features old value and the new value for changed feature.
2) find_cos(): find if there is a matching COS combination which can match the input array.
3) alloc_new_cos(): if find_cos() cannot find a matching one, allocate a new COS id or reuse the one which is only occupied by current domain.

Quoted 5 lines

So, our ideas are almost same. Only one exception to split get_old_set_new() function into two parts. That increases one loop to traverse all features and most features "set new value" callback functions will return directly because the type cannot match. And, I think to put value array assembling work into one function is reasonable. What do you think? Thanks!

Quoted 6 lines

How many iterations do you expect these loops to have? If it's only a few, I think the simpler and more clear abstraction would outweigh the performance benefits, the more that I hope none of this ends up sitting on a performance critical path in the first place.

Jan

So far, only two features and it would not be so many in the future. :)

Will add more callback function to set new value. Thanks!

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel