Skip to contentSkip to conversation

x86/intel_rdt: Fixes for schemata parsing issues

PATCH In review Vikas Shivappa 4 participants 13 messages Apr 19, 2017 – Apr 20, 2017 x86

lore
Share & tools
lore
#0 lore raw

cover x86/intel_rdt: Fixes for schemata parsing issues

This series includes some minor fixes to RDT parsing of the schemata. Patches are based on tip x86/cpu/

[PATCH 1/3] x86/intel_rdt: Fix padding when resource is enabled via mount
[PATCH 2/3] x86/intel_rdt: Trim whitespace while parsing schemata [PATCH 3/3] x86/intel_rdt: Return error for incorrect resource names

#1 lore raw

PATCH 1/3 x86/intel_rdt: Fix padding when resource is enabled via mount

Currently max width of 'resource name' and 'resource data' is being initialized based on 'enabled resources' during boot. But the mount can enable different capable resources at a later time which upsets the tabular format of schemata. Fix this to be based on 'all capable' resources.

---
 arch/x86/kernel/cpu/intel_rdt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/intel_rdt.c +1 -1
index 731f70a..5b36646 100644
@@ -492,7 +492,7 @@ static __init void rdt_init_padding(void)
 	struct rdt_resource *r;
 	int cl;
 
-	for_each_enabled_rdt_resource(r) {
+	for_each_capable_rdt_resource(r) {
 		cl = strlen(r->name);
 		if (cl > max_name_width)
 			max_name_width = cl;
Signature · 1 line
1.9.1
#2 lore raw

[tip:x86/cpu] x86/intel_rdt: Fix padding when resource is enabled via mount

x86/intel_rdt: Fix padding when resource is enabled via mount
Commit-ID:  adcbdd70309dba5a12a9d8158deb6a62a6d5fc98
Gitweb:     http://git.kernel.org/tip/adcbdd70309dba5a12a9d8158deb6a62a6d5fc98
Author:     Vikas Shivappa <[email protected]>
AuthorDate: Wed, 19 Apr 2017 16:50:02 -0700
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 20 Apr 2017 15:57:59 +0200

x86/intel_rdt: Fix padding when resource is enabled via mount

Currently max width of 'resource name' and 'resource data' is being initialized based on 'enabled resources' during boot. But the mount can enable different capable resources at a later time which upsets the tabular format of schemata. Fix this to be based on 'all capable' resources.

---
 arch/x86/kernel/cpu/intel_rdt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/intel_rdt.c +1 -1
index 731f70a..5b36646 100644
@@ -492,7 +492,7 @@ static __init void rdt_init_padding(void)
 	struct rdt_resource *r;
 	int cl;
 
-	for_each_enabled_rdt_resource(r) {
+	for_each_capable_rdt_resource(r) {
 		cl = strlen(r->name);
 		if (cl > max_name_width)
 			max_name_width = cl;
#3 lore raw

PATCH 2/3 x86/intel_rdt: Trim whitespace while parsing schemata input

In reply to Vikas Shivappa

Schemata is displayed in tabular format which introduces some whitespace to show data in a tabular format. If user wants to input the same data that is displayed, the parsing fails. Trim the leading and trailing whitespace to help parse such data.

---
 arch/x86/kernel/cpu/intel_rdt_schemata.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/intel_rdt_schemata.c +2 -2
index 9467a00..3cfa1ca 100644
@@ -143,7 +143,7 @@ static int parse_line(char *line, struct rdt_resource *r)
 		return -EINVAL;
 	list_for_each_entry(d, &r->domains, list) {
 		if (d->id == dom_id) {
-			if (r->parse_ctrlval(dom, r, d))
+			if (r->parse_ctrlval(strim(dom), r, d))
 				return -EINVAL;
 			goto next;
 		}
@@ -220,7 +220,7 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 			goto out;
 		}
 		for_each_enabled_rdt_resource(r) {
-			if (!strcmp(resname, r->name) &&
+			if (!strcmp(strim(resname), r->name) &&
 			    closid < r->num_closid) {
 				ret = parse_line(tok, r);
 				if (ret)
Signature · 1 line
1.9.1
#4 lore raw

Re: [PATCH 2/3] x86/intel_rdt: Trim whitespace while parsing schemata input

Quoted 26 lines · Vikas Shivappa

Schemata is displayed in tabular format which introduces some whitespace to show data in a tabular format. If user wants to input the same data that is displayed, the parsing fails. Trim the leading and trailing whitespace to help parse such data.

---
 arch/x86/kernel/cpu/intel_rdt_schemata.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/intel_rdt_schemata.c +1 -1
index 9467a00..3cfa1ca 100644
@@ -143,7 +143,7 @@ static int parse_line(char *line, struct rdt_resource *r)
 		return -EINVAL;
 	list_for_each_entry(d, &r->domains, list) {
 		if (d->id == dom_id) {
-			if (r->parse_ctrlval(dom, r, d))
+			if (r->parse_ctrlval(strim(dom), r, d))
 				return -EINVAL;
 			goto next;
 		}

arch/x86/kernel/cpu/intel_rdt_schemata.c ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of, · line 223

Quoted 5 lines
arch/x86/kernel/cpu/intel_rdt_schemata.c +1 -1
@@ -220,7 +220,7 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 			goto out;
 		}
 		for_each_enabled_rdt_resource(r) {
-			if (!strcmp(resname, r->name) &&
+			if (!strcmp(strim(resname), r->name) &&

Both strims() invocations are just silly. They can be done before the loop once instead of doing them over and over again.

Thanks,

tglx

#5 lore raw

Re: [PATCH 2/3] x86/intel_rdt: Trim whitespace while parsing schemata input

Quoted 28 lines · Thomas Gleixner
Quoted 26 lines · Vikas Shivappa

Schemata is displayed in tabular format which introduces some whitespace to show data in a tabular format. If user wants to input the same data that is displayed, the parsing fails. Trim the leading and trailing whitespace to help parse such data.

---
 arch/x86/kernel/cpu/intel_rdt_schemata.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/intel_rdt_schemata.c +1 -1
index 9467a00..3cfa1ca 100644
@@ -143,7 +143,7 @@ static int parse_line(char *line, struct rdt_resource *r)
 		return -EINVAL;
 	list_for_each_entry(d, &r->domains, list) {
 		if (d->id == dom_id) {
-			if (r->parse_ctrlval(dom, r, d))
+			if (r->parse_ctrlval(strim(dom), r, d))
 				return -EINVAL;
 			goto next;
 		}
@@ -220,7 +220,7 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
Quoted 8 lines

arch/x86/kernel/cpu/intel_rdt_schemata.c ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of, · line 223

Quoted 5 lines
arch/x86/kernel/cpu/intel_rdt_schemata.c +1 -1
 			goto out;
 		}
 		for_each_enabled_rdt_resource(r) {
-			if (!strcmp(resname, r->name) &&
+			if (!strcmp(strim(resname), r->name) &&

Both strims() invocations are just silly. They can be done before the loop once instead of doing them over and over again.

Will fix. I went overboard not wanting to add a line

Thanks,
Vikas

Quoted 5 lines

Thanks,

tglx

#6 lore raw

Re: [PATCH 2/3] x86/intel_rdt: Trim whitespace while parsing schemata input

Quoted 1 line · Shivappa Vikas

Will fix. I went overboard not wanting to add a line

Finish reading the thread before you start :)

#7 lore raw

Re: [PATCH 2/3] x86/intel_rdt: Trim whitespace while parsing schemata input

Quoted 4 lines · Thomas Gleixner
Quoted 1 line · Shivappa Vikas

Will fix. I went overboard not wanting to add a line

Finish reading the thread before you start :)

Got it :) Had not seen the tip bot emails on my other email..

Quoted 1 line
#8 lore raw

[tip:x86/cpu] x86/intel_rdt: Trim whitespace while parsing schemata input

In reply to Vikas Shivappa
x86/intel_rdt: Trim whitespace while parsing schemata input
Commit-ID:  634b0e0491d6f6e882b922eb41c278d01a743bab
Gitweb:     http://git.kernel.org/tip/634b0e0491d6f6e882b922eb41c278d01a743bab
Author:     Vikas Shivappa <[email protected]>
AuthorDate: Wed, 19 Apr 2017 16:50:03 -0700
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 20 Apr 2017 15:57:59 +0200

x86/intel_rdt: Trim whitespace while parsing schemata input

Schemata is displayed in tabular format which introduces some whitespace to show data in a tabular format.

Writing back the same data fails as the parser does not handle the whitespace.

Trim the leading and trailing whitespace before parsing.

---
 arch/x86/kernel/cpu/intel_rdt_schemata.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/intel_rdt_schemata.c +2 -1
index 9467a00..e64b2cf 100644
@@ -141,6 +141,7 @@ next:
 	id = strsep(&dom, "=");
 	if (!dom || kstrtoul(id, 10, &dom_id))
 		return -EINVAL;
+	dom = strim(dom);
 	list_for_each_entry(d, &r->domains, list) {
 		if (d->id == dom_id) {
 			if (r->parse_ctrlval(dom, r, d))
@@ -214,7 +215,7 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 			dom->have_new_ctrl = false;
 
 	while ((tok = strsep(&buf, "\n")) != NULL) {
-		resname = strsep(&tok, ":");
+		resname = strim(strsep(&tok, ":"));
 		if (!tok) {
 			ret = -EINVAL;
 			goto out;
#9 lore raw

PATCH 3/3 x86/intel_rdt: Return error for incorrect resource names in schemata

In reply to Vikas Shivappa

When schemata parses the resource names, currently it may not return error for incorrect resource names and fail quietly without updating the control values.

This is because for_each_enabled_rdt_resource(r) leaves "r" pointing beyond the end of the rdt_resources_all[] array, and we check for !r->name which results in an out of bounds access and also may not be NULL, hence we may not detect that we did not find the name user supplied.

Update this to check (r == (rdt_resources_all + RDT_NUM_RESOURCES)) instead.

---
 arch/x86/kernel/cpu/intel_rdt_schemata.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/intel_rdt_schemata.c +1 -1
index 3cfa1ca..bb99bd8 100644
@@ -228,7 +228,7 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 				break;
 			}
 		}
-		if (!r->name) {
+		if (r == (rdt_resources_all + RDT_NUM_RESOURCES)) {
 			ret = -EINVAL;
 			goto out;
 		}
Signature · 1 line
1.9.1
#10 lore raw

Re: [PATCH 3/3] x86/intel_rdt: Return error for incorrect resource names in schemata

Quoted 25 lines · Vikas Shivappa

When schemata parses the resource names, currently it may not return error for incorrect resource names and fail quietly without updating the control values.

This is because for_each_enabled_rdt_resource(r) leaves "r" pointing beyond the end of the rdt_resources_all[] array, and we check for !r->name which results in an out of bounds access and also may not be NULL, hence we may not detect that we did not find the name user supplied.

Update this to check (r == (rdt_resources_all + RDT_NUM_RESOURCES)) instead.

---
 arch/x86/kernel/cpu/intel_rdt_schemata.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/intel_rdt_schemata.c +0 -0
index 3cfa1ca..bb99bd8 100644

arch/x86/kernel/cpu/intel_rdt_schemata.c ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of, · line 234

Quoted 8 lines
arch/x86/kernel/cpu/intel_rdt_schemata.c +1 -1
@@ -228,7 +228,7 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 				break;
 			}
 		}
-		if (!r->name) {
+		if (r == (rdt_resources_all + RDT_NUM_RESOURCES)) {
 			ret = -EINVAL;
 			goto out;
 		}

The resulting loop is just horrible to read. We can do better than that. Patch below.

Thanks,

tglx
8<-------------

arch/x86/kernel/cpu/intel_rdt_schemata.c +15 -13
@@ -187,6 +187,17 @@ static int update_domains(struct rdt_res
 	return 0;
 }
 
+static int rdtgroup_parse_resource(char *resname, char *tok, int closid)
+{
+	struct rdt_resource *r;
+
+	for_each_enabled_rdt_resource(r) {
+		if (!strcmp(resname, r->name) && closid < r->num_closid)
+			return parse_line(tok, r);
+	}
+	return -EINVAL;
+}
+
 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 				char *buf, size_t nbytes, loff_t off)
 {
@@ -209,9 +220,10 @@ ssize_t rdtgroup_schemata_write(struct k
 
 	closid = rdtgrp->closid;
 
-	for_each_enabled_rdt_resource(r)
+	for_each_enabled_rdt_resource(r) {
 		list_for_each_entry(dom, &r->domains, list)
 			dom->have_new_ctrl = false;
+	}
 
 	while ((tok = strsep(&buf, "\n")) != NULL) {
 		resname = strsep(&tok, ":");
@@ -219,19 +231,9 @@ ssize_t rdtgroup_schemata_write(struct k
 			ret = -EINVAL;
 			goto out;
 		}
-		for_each_enabled_rdt_resource(r) {
-			if (!strcmp(strim(resname), r->name) &&
-			    closid < r->num_closid) {
-				ret = parse_line(tok, r);
-				if (ret)
-					goto out;
-				break;
-			}
-		}
-		if (!r->name) {
-			ret = -EINVAL;
+		ret = rdtgroup_parse_resource(strim(resname), tok, closid);
+		if (ret)
 			goto out;
-		}
 	}
 
 	for_each_enabled_rdt_resource(r) {
#11 lore raw

Re: [PATCH 3/3] x86/intel_rdt: Return error for incorrect resource names in schemata

Quoted 54 lines · Thomas Gleixner
Quoted 1 line · Vikas Shivappa

}

The resulting loop is just horrible to read. We can do better than that. Patch below.

Thanks,

tglx
8<-------------

arch/x86/kernel/cpu/intel_rdt_schemata.c +13 -7
@@ -187,6 +187,17 @@ static int update_domains(struct rdt_res
 	return 0;
 }
 
+static int rdtgroup_parse_resource(char *resname, char *tok, int closid)
+{
+	struct rdt_resource *r;
+
+	for_each_enabled_rdt_resource(r) {
+		if (!strcmp(resname, r->name) && closid < r->num_closid)
+			return parse_line(tok, r);
+	}
+	return -EINVAL;
+}
+
 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 				char *buf, size_t nbytes, loff_t off)
 {
@@ -209,9 +220,10 @@ ssize_t rdtgroup_schemata_write(struct k
 
 	closid = rdtgrp->closid;
 
-	for_each_enabled_rdt_resource(r)
+	for_each_enabled_rdt_resource(r) {
 		list_for_each_entry(dom, &r->domains, list)
 			dom->have_new_ctrl = false;
+	}
 
 	while ((tok = strsep(&buf, "\n")) != NULL) {
 		resname = strsep(&tok, ":");
@@ -219,19 +231,9 @@ ssize_t rdtgroup_schemata_write(struct k
 			ret = -EINVAL;
 			goto out;
 		}
-		for_each_enabled_rdt_resource(r) {
-			if (!strcmp(strim(resname), r->name) &&
-			    closid < r->num_closid) {
-				ret = parse_line(tok, r);
-				if (ret)
-					goto out;

arch/x86/kernel/cpu/intel_rdt_schemata.c ssize_t rdtgroup_schemata_write(struct k · line 237

Quoted 10 lines
arch/x86/kernel/cpu/intel_rdt_schemata.c +2 -6
-				break;
-			}
-		}
-		if (!r->name) {
-			ret = -EINVAL;
+		ret = rdtgroup_parse_resource(strim(resname), tok, closid);
+		if (ret)
 			goto out;
-		}
 	}

Ok agree its better way. I was trying to add a NULL which Tony already said was Nacked, did not really want to use a pointer which may be out of bounds although we dont check the contents.

Thanks,
Vikas

#12 lore raw

[tip:x86/cpu] x86/intel_rdt: Return error for incorrect resource names in schemata

In reply to Vikas Shivappa
x86/intel_rdt: Return error for incorrect resource names in schemata
Commit-ID:  4797b7dfdfcf457075c36743d71e2b0feeaaa20f
Gitweb:     http://git.kernel.org/tip/4797b7dfdfcf457075c36743d71e2b0feeaaa20f
Author:     Vikas Shivappa <[email protected]>
AuthorDate: Wed, 19 Apr 2017 16:50:04 -0700
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 20 Apr 2017 15:57:59 +0200

x86/intel_rdt: Return error for incorrect resource names in schemata

When schemata parses the resource names it does not return an error if it detects incorrect resource names and fails quietly.

This happens because for_each_enabled_rdt_resource(r) leaves "r" pointing beyond the end of the rdt_resources_all[] array, and the check for !r->name results in an out of bounds access.

Split the resource parsing part into a helper function to avoid the issue.

[ tglx: Made it readable by splitting the parser loop out into a function ]

---
 arch/x86/kernel/cpu/intel_rdt_schemata.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/intel_rdt_schemata.c +15 -13
index e64b2cf..406d7a6 100644
@@ -188,6 +188,17 @@ done:
 	return 0;
 }
 
+static int rdtgroup_parse_resource(char *resname, char *tok, int closid)
+{
+	struct rdt_resource *r;
+
+	for_each_enabled_rdt_resource(r) {
+		if (!strcmp(resname, r->name) && closid < r->num_closid)
+			return parse_line(tok, r);
+	}
+	return -EINVAL;
+}
+
 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 				char *buf, size_t nbytes, loff_t off)
 {
@@ -210,9 +221,10 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 
 	closid = rdtgrp->closid;
 
-	for_each_enabled_rdt_resource(r)
+	for_each_enabled_rdt_resource(r) {
 		list_for_each_entry(dom, &r->domains, list)
 			dom->have_new_ctrl = false;
+	}
 
 	while ((tok = strsep(&buf, "\n")) != NULL) {
 		resname = strim(strsep(&tok, ":"));
@@ -220,19 +232,9 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
 			ret = -EINVAL;
 			goto out;
 		}
-		for_each_enabled_rdt_resource(r) {
-			if (!strcmp(resname, r->name) &&
-			    closid < r->num_closid) {
-				ret = parse_line(tok, r);
-				if (ret)
-					goto out;
-				break;
-			}
-		}
-		if (!r->name) {
-			ret = -EINVAL;
+		ret = rdtgroup_parse_resource(resname, tok, closid);
+		if (ret)
 			goto out;
-		}
 	}
 
 	for_each_enabled_rdt_resource(r) {