Skip to contentSkip to conversation

soc: qcom: smem: some refactoring

PATCH Superseded Alex Elder 1 participant 13 messages May 2, 2018

Subsystems (2)
lore
Share & tools
lore

Alex Elder

author
#0 lore raw

cover soc: qcom: smem: some refactoring

This series contains a few general cleanups in the Qualcomm SMEM code, reorganizing some things so memory partitions are treated more consistently, and to avoid some duplication. A few patches change existing behavior, as highlighted below. Each of these changes has been tested and verified to function with available hardware.

-Alex

Alex Elder (12):
soc: qcom: smem: rename variable in qcom_smem_get_global()
soc: qcom: smem: initialize region struct only when successful
These two are simple cleanups.

soc: qcom: smem: always ignore partitions with 0 offset or size
This patch makes partitions with a zero offset always be ignored. Previously this condition was flagged as an error in one place and ignored in another.

soc: qcom: smem: small refactor in qcom_smem_enumerate_partitions()
This just simplifies a little logic in one spot.

soc: qcom: smem: verify both host ids in partition header
This patch makes it so *both* IDs, not just one or the other, must use the special value SMEM_GLOBAL_HOST to signify the special global partition.

soc: qcom: smem: require order of host ids to match
A memory partition is described by an entry in a partition table, which identifies two hosts permitted to access the partition. A partition also embeds a header, which also identifies the hosts. Previously the host ids could be in any order; now the two host ids are required to be listed in the same order in the partition table and header.

soc: qcom: smem: introduce qcom_smem_partition_header()
soc: qcom: smem: verify partition header size
soc: qcom: smem: verify partition offset_free_uncached
soc: qcom: smem: small change in global entry loop
soc: qcom: smem: verify partition host ids match
These patches together introduce a single function to locate a partition header in memory, and to validate its contents. The common function is used in two place and removes some duplication. The patches build up the function in steps to facilitate review.

soc: qcom: smem: a few last cleanups
This patch gathers a small set of trivial cleanups.

 drivers/soc/qcom/smem.c | 173 +++++++++++++++++++++++-------------------------
 1 file changed, 82 insertions(+), 91 deletions(-)
Signature · 1 line
2.14.1
author
#1 lore raw

PATCH 1/12 soc: qcom: smem: rename variable in qcom_smem_get_global()

Rename the variable "area" to be "region" in qcom_smem_get_global(), so its name better matches its type.

---
 drivers/soc/qcom/smem.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/soc/qcom/smem.c +5 -5
index 70b2ee80d6bd..c46bb43c0f3d 100644
@@ -277,7 +277,7 @@ struct qcom_smem {
 	u32 item_count;
 
 	unsigned num_regions;
-	struct smem_region regions[0];
+	struct smem_region regions[];
 };
 
 static void *
@@ -494,7 +494,7 @@ static void *qcom_smem_get_global(struct qcom_smem *smem,
 				  size_t *size)
 {
 	struct smem_header *header;
-	struct smem_region *area;
+	struct smem_region *region;
 	struct smem_global_entry *entry;
 	u32 aux_base;
 	unsigned i;
@@ -507,12 +507,12 @@ static void *qcom_smem_get_global(struct qcom_smem *smem,
 	aux_base = le32_to_cpu(entry->aux_base) & AUX_BASE_MASK;
 
 	for (i = 0; i < smem->num_regions; i++) {
-		area = &smem->regions[i];
+		region = &smem->regions[i];
 
-		if (area->aux_base == aux_base || !aux_base) {
+		if (region->aux_base == aux_base || !aux_base) {
 			if (size != NULL)
 				*size = le32_to_cpu(entry->size);
-			return area->virt_base + le32_to_cpu(entry->offset);
+			return region->virt_base + le32_to_cpu(entry->offset);
 		}
 	}
 
Signature · 1 line
2.14.1
author
#2 lore raw

PATCH 2/12 soc: qcom: smem: initialize region struct only when successful

In reply to Alex Elder

Hold off initializing anything for the array entry representing a memory region in qcom_smem_map_memory() until we know we've successfully mapped it.

---
 drivers/soc/qcom/smem.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/soc/qcom/smem.c +5 -3
index c46bb43c0f3d..d8008bf480a4 100644
@@ -887,6 +887,7 @@ static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
 {
 	struct device_node *np;
 	struct resource r;
+	resource_size_t size;
 	int ret;
 
 	np = of_parse_phandle(dev->of_node, name, 0);
@@ -899,12 +900,13 @@ static int qcom_smem_map_memory(struct qcom_smem *smem, struct device *dev,
 	of_node_put(np);
 	if (ret)
 		return ret;
+	size = resource_size(&r);
 
-	smem->regions[i].aux_base = (u32)r.start;
-	smem->regions[i].size = resource_size(&r);
-	smem->regions[i].virt_base = devm_ioremap_wc(dev, r.start, resource_size(&r));
+	smem->regions[i].virt_base = devm_ioremap_wc(dev, r.start, size);
 	if (!smem->regions[i].virt_base)
 		return -ENOMEM;
+	smem->regions[i].aux_base = (u32)r.start;
+	smem->regions[i].size = size;
 
 	return 0;
 }
Signature · 1 line
2.14.1
author
#3 lore raw

PATCH 3/12 soc: qcom: smem: always ignore partitions with 0 offset or size

In reply to Alex Elder

In qcom_smem_enumerate_partitions(), any partition table entry having a zero offset or size field is ignored. Move those checks earlier in the loop, because there's no sense in examining the host fields for those entries.

Add the same checks in qcom_smem_set_global_partition(), so the scan for the global partition skips over these invalid entries. This allows a later check for zero size or offset once the global entry is found to be eliminated.

---
 drivers/soc/qcom/smem.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
drivers/soc/qcom/smem.c +10 -13
index d8008bf480a4..83ba549cfea0 100644
@@ -742,9 +742,13 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 
 	for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
 		entry = &ptable->entry[i];
+		if (!le32_to_cpu(entry->offset))
+			continue;
+		if (!le32_to_cpu(entry->size))
+			continue;
+
 		host0 = le16_to_cpu(entry->host0);
 		host1 = le16_to_cpu(entry->host1);
-
 		if (host0 == SMEM_GLOBAL_HOST && host0 == host1) {
 			found = true;
 			break;
@@ -756,11 +760,6 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 		return -EINVAL;
 	}
 
-	if (!le32_to_cpu(entry->offset) || !le32_to_cpu(entry->size)) {
-		dev_err(smem->dev, "Invalid entry for global partition\n");
-		return -EINVAL;
-	}
-
 	header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
 	host0 = le16_to_cpu(header->host0);
 	host1 = le16_to_cpu(header->host1);
@@ -809,18 +808,16 @@ static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
 
 	for (i = 0; i < le32_to_cpu(ptable->num_entries); i++) {
 		entry = &ptable->entry[i];
+		if (!le32_to_cpu(entry->offset))
+			continue;
+		if (!le32_to_cpu(entry->size))
+			continue;
+
 		host0 = le16_to_cpu(entry->host0);
 		host1 = le16_to_cpu(entry->host1);
-
 		if (host0 != local_host && host1 != local_host)
 			continue;
 
-		if (!le32_to_cpu(entry->offset))
-			continue;
-
-		if (!le32_to_cpu(entry->size))
-			continue;
-
 		if (host0 == local_host)
 			remote_host = host1;
 		else
Signature · 1 line
2.14.1
author
#4 lore raw

PATCH 4/12 soc: qcom: smem: small refactor in qcom_smem_enumerate_partitions()

In reply to Alex Elder

Combine the code that checks whether a partition table entry is associated with the local host with the assignment of the remote host id value.

---
 drivers/soc/qcom/smem.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/soc/qcom/smem.c +3 -4
index 83ba549cfea0..5d3ed510e54b 100644
@@ -815,13 +815,12 @@ static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
 
 		host0 = le16_to_cpu(entry->host0);
 		host1 = le16_to_cpu(entry->host1);
-		if (host0 != local_host && host1 != local_host)
-			continue;
-
 		if (host0 == local_host)
 			remote_host = host1;
-		else
+		else if (host1 == local_host)
 			remote_host = host0;
+		else
+			continue;
 
 		if (remote_host >= SMEM_HOST_COUNT) {
 			dev_err(smem->dev,
Signature · 1 line
2.14.1
author
#5 lore raw

PATCH 5/12 soc: qcom: smem: verify both host ids in partition header

In reply to Alex Elder

The global partition is indicated by having both host values in its table of contents entry equal SMEM_GLOBAL_HOST=0xfffe.

In qcom_smem_set_global_partition(), we check whether the header structure at the beginning of the partition contains that host value, but the check only verifies *one* of them. Change the check so the partition header must have SMEM_GLOBAL_HOST for *both* its host fields.

---
 drivers/soc/qcom/smem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soc/qcom/smem.c +1 -1
index 5d3ed510e54b..6931602d9a9e 100644
@@ -769,7 +769,7 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 		return -EINVAL;
 	}
 
-	if (host0 != SMEM_GLOBAL_HOST && host1 != SMEM_GLOBAL_HOST) {
+	if (host0 != SMEM_GLOBAL_HOST || host1 != SMEM_GLOBAL_HOST) {
 		dev_err(smem->dev, "Global partition hosts are invalid\n");
 		return -EINVAL;
 	}
Signature · 1 line
2.14.1
author
#6 lore raw

PATCH 6/12 soc: qcom: smem: require order of host ids to match

In reply to Alex Elder

In qcom_smem_enumerate_partitions(), we find all partitions that have a given local host id in either its host0 or its host1 field in the partition table entry. We then verify that the header structure at the start of each partition also contains the same two host ids as is found in the table of contents.

There is no requirement that the order of the two host ids be the same in the table of contents and in the partition header.

This patch changes that, requiring host0 to in the partition table entry to equal host0 in the partition header structure (and similar for the host1 values).

---
 drivers/soc/qcom/smem.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/soc/qcom/smem.c +3 -11
index 6931602d9a9e..9e20c6b1b4c2 100644
@@ -837,9 +837,6 @@ static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
 		}
 
 		header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
-		host0 = le16_to_cpu(header->host0);
-		host1 = le16_to_cpu(header->host1);
-
 		if (memcmp(header->magic, SMEM_PART_MAGIC,
 			    sizeof(header->magic))) {
 			dev_err(smem->dev,
@@ -847,15 +844,10 @@ static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
 			return -EINVAL;
 		}
 
-		if (host0 != local_host && host1 != local_host) {
+		if (host0 != le16_to_cpu(header->host0) ||
+				host1 != le16_to_cpu(header->host1)) {
 			dev_err(smem->dev,
-				"Partition %d hosts are invalid\n", i);
-			return -EINVAL;
-		}
-
-		if (host0 != remote_host && host1 != remote_host) {
-			dev_err(smem->dev,
-				"Partition %d hosts are invalid\n", i);
+				"Partition %d hosts don't match\n", i);
 			return -EINVAL;
 		}
 
Signature · 1 line
2.14.1
author
#7 lore raw

PATCH 7/12 soc: qcom: smem: introduce qcom_smem_partition_header()

In reply to Alex Elder

Create a new function qcom_smem_partition_header() to encapsulate validating the header information found in a partition. This will be built up over a few commits to make it more obvious how the common function is replacing duplicated code elsewhere. Initially it just verifies the header has the right magic number.

---
 drivers/soc/qcom/smem.c | 41 +++++++++++++++++++++++++++++------------
 1 file changed, 29 insertions(+), 12 deletions(-)
drivers/soc/qcom/smem.c +29 -12
index 9e20c6b1b4c2..d52a282f7338 100644
@@ -727,6 +727,29 @@ static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
 	return le16_to_cpu(info->num_items);
 }
 
+/*
+ * Validate the partition header for a partition whose partition
+ * table entry is supplied.  Returns a pointer to its header if
+ * valid, or a null pointer otherwise.
+ */
+static struct smem_partition_header *
+qcom_smem_partition_header(struct qcom_smem *smem,
+		struct smem_ptable_entry *entry)
+{
+	struct smem_partition_header *header;
+
+	header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
+
+	if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
+		dev_err(smem->dev, "bad partition magic %02x %02x %02x %02x\n",
+			header->magic[0], header->magic[1],
+			header->magic[2], header->magic[3]);
+		return NULL;
+	}
+
+	return header;
+}
+
 static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 {
 	struct smem_partition_header *header;
@@ -760,15 +783,13 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 		return -EINVAL;
 	}
 
-	header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
+	header = qcom_smem_partition_header(smem, entry);
+	if (!header)
+		return -EINVAL;
+
 	host0 = le16_to_cpu(header->host0);
 	host1 = le16_to_cpu(header->host1);
 
-	if (memcmp(header->magic, SMEM_PART_MAGIC, sizeof(header->magic))) {
-		dev_err(smem->dev, "Global partition has invalid magic\n");
-		return -EINVAL;
-	}
-
 	if (host0 != SMEM_GLOBAL_HOST || host1 != SMEM_GLOBAL_HOST) {
 		dev_err(smem->dev, "Global partition hosts are invalid\n");
 		return -EINVAL;
@@ -836,13 +857,9 @@ static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
 			return -EINVAL;
 		}
 
-		header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
-		if (memcmp(header->magic, SMEM_PART_MAGIC,
-			    sizeof(header->magic))) {
-			dev_err(smem->dev,
-				"Partition %d has invalid magic\n", i);
+		header = qcom_smem_partition_header(smem, entry);
+		if (!header)
 			return -EINVAL;
-		}
 
 		if (host0 != le16_to_cpu(header->host0) ||
 				host1 != le16_to_cpu(header->host1)) {
Signature · 1 line
2.14.1
author
#8 lore raw

PATCH 8/12 soc: qcom: smem: verify partition header size

In reply to Alex Elder

Add verification in qcom_smem_partition_header() that the size in a partition's header structure matches the size in its partition table entry.

---
 drivers/soc/qcom/smem.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)
drivers/soc/qcom/smem.c +8 -11
index d52a282f7338..f1e6e4609ccb 100644
@@ -737,6 +737,7 @@ qcom_smem_partition_header(struct qcom_smem *smem,
 		struct smem_ptable_entry *entry)
 {
 	struct smem_partition_header *header;
+	u32 size;
 
 	header = smem->regions[0].virt_base + le32_to_cpu(entry->offset);
 
@@ -747,6 +748,13 @@ qcom_smem_partition_header(struct qcom_smem *smem,
 		return NULL;
 	}
 
+	size = le32_to_cpu(header->size);
+	if (size != le32_to_cpu(entry->size)) {
+		dev_err(smem->dev, "bad partition size (%u != %u)\n",
+			size, le32_to_cpu(entry->size));
+		return NULL;
+	}
+
 	return header;
 }
 
@@ -795,11 +803,6 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 		return -EINVAL;
 	}
 
-	if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
-		dev_err(smem->dev, "Global partition has invalid size\n");
-		return -EINVAL;
-	}
-
 	size = le32_to_cpu(header->offset_free_uncached);
 	if (size > le32_to_cpu(header->size)) {
 		dev_err(smem->dev,
@@ -868,12 +871,6 @@ static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
 			return -EINVAL;
 		}
 
-		if (le32_to_cpu(header->size) != le32_to_cpu(entry->size)) {
-			dev_err(smem->dev,
-				"Partition %d has invalid size\n", i);
-			return -EINVAL;
-		}
-
 		if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
 			dev_err(smem->dev,
 				"Partition %d has invalid free pointer\n", i);
Signature · 1 line
2.14.1
author
#9 lore raw

PATCH 9/12 soc: qcom: smem: verify partition offset_free_uncached

In reply to Alex Elder

Add verification in qcom_smem_partition_header() that the offset_free_uncached field in a partition's header structure does not exceed the partition's size.

---
 drivers/soc/qcom/smem.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)
drivers/soc/qcom/smem.c +7 -14
index f1e6e4609ccb..7ef415c4d62e 100644
@@ -755,6 +755,12 @@ qcom_smem_partition_header(struct qcom_smem *smem,
 		return NULL;
 	}
 
+	if (le32_to_cpu(header->offset_free_uncached) > size) {
+		dev_err(smem->dev, "bad partition free uncached (%u > %u)\n",
+			le32_to_cpu(header->offset_free_uncached), size);
+		return NULL;
+	}
+
 	return header;
 }
 
@@ -763,7 +769,7 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 	struct smem_partition_header *header;
 	struct smem_ptable_entry *entry;
 	struct smem_ptable *ptable;
-	u32 host0, host1, size;
+	u32 host0, host1;
 	bool found = false;
 	int i;
 
@@ -803,13 +809,6 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 		return -EINVAL;
 	}
 
-	size = le32_to_cpu(header->offset_free_uncached);
-	if (size > le32_to_cpu(header->size)) {
-		dev_err(smem->dev,
-			"Global partition has invalid free pointer\n");
-		return -EINVAL;
-	}
-
 	smem->global_partition = header;
 	smem->global_cacheline = le32_to_cpu(entry->cacheline);
 
@@ -871,12 +870,6 @@ static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
 			return -EINVAL;
 		}
 
-		if (le32_to_cpu(header->offset_free_uncached) > le32_to_cpu(header->size)) {
-			dev_err(smem->dev,
-				"Partition %d has invalid free pointer\n", i);
-			return -EINVAL;
-		}
-
 		smem->partitions[remote_host] = header;
 		smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
 	}
Signature · 1 line
2.14.1
author
#10 lore raw

PATCH 10/12 soc: qcom: smem: small change in global entry loop

In reply to Alex Elder

Change the logic in the loop that finds that global host entry in the partition table not require the host0 and host1 local variables. The next patch will remove them.

---
 drivers/soc/qcom/smem.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/soc/qcom/smem.c +4 -3
index 7ef415c4d62e..54c504e41508 100644
@@ -784,9 +784,10 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 		if (!le32_to_cpu(entry->size))
 			continue;
 
-		host0 = le16_to_cpu(entry->host0);
-		host1 = le16_to_cpu(entry->host1);
-		if (host0 == SMEM_GLOBAL_HOST && host0 == host1) {
+		if (le16_to_cpu(entry->host0) != SMEM_GLOBAL_HOST)
+			continue;
+
+		if (le16_to_cpu(entry->host1) == SMEM_GLOBAL_HOST) {
 			found = true;
 			break;
 		}
Signature · 1 line
2.14.1
author
#11 lore raw

PATCH 11/12 soc: qcom: smem: verify partition host ids match

In reply to Alex Elder

Add verification in qcom_smem_partition_header() that the host ids found in a partition's header structure match those in its partition table entry.

---
 drivers/soc/qcom/smem.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)
drivers/soc/qcom/smem.c +15 -19
index 54c504e41508..7383a0e1b468 100644
@@ -734,7 +734,7 @@ static u32 qcom_smem_get_item_count(struct qcom_smem *smem)
  */
 static struct smem_partition_header *
 qcom_smem_partition_header(struct qcom_smem *smem,
-		struct smem_ptable_entry *entry)
+		struct smem_ptable_entry *entry, u16 host0, u16 host1)
 {
 	struct smem_partition_header *header;
 	u32 size;
@@ -748,6 +748,17 @@ qcom_smem_partition_header(struct qcom_smem *smem,
 		return NULL;
 	}
 
+	if (host0 != le16_to_cpu(header->host0)) {
+		dev_err(smem->dev, "bad host0 (%hu != %hu)\n",
+				host0, le16_to_cpu(header->host0));
+		return NULL;
+	}
+	if (host1 != le16_to_cpu(header->host1)) {
+		dev_err(smem->dev, "bad host1 (%hu != %hu)\n",
+				host1, le16_to_cpu(header->host1));
+		return NULL;
+	}
+
 	size = le32_to_cpu(header->size);
 	if (size != le32_to_cpu(entry->size)) {
 		dev_err(smem->dev, "bad partition size (%u != %u)\n",
@@ -769,7 +780,6 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 	struct smem_partition_header *header;
 	struct smem_ptable_entry *entry;
 	struct smem_ptable *ptable;
-	u32 host0, host1;
 	bool found = false;
 	int i;
 
@@ -798,18 +808,11 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 		return -EINVAL;
 	}
 
-	header = qcom_smem_partition_header(smem, entry);
+	header = qcom_smem_partition_header(smem, entry,
+				SMEM_GLOBAL_HOST, SMEM_GLOBAL_HOST);
 	if (!header)
 		return -EINVAL;
 
-	host0 = le16_to_cpu(header->host0);
-	host1 = le16_to_cpu(header->host1);
-
-	if (host0 != SMEM_GLOBAL_HOST || host1 != SMEM_GLOBAL_HOST) {
-		dev_err(smem->dev, "Global partition hosts are invalid\n");
-		return -EINVAL;
-	}
-
 	smem->global_partition = header;
 	smem->global_cacheline = le32_to_cpu(entry->cacheline);
 
@@ -860,17 +863,10 @@ static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
 			return -EINVAL;
 		}
 
-		header = qcom_smem_partition_header(smem, entry);
+		header = qcom_smem_partition_header(smem, entry, host0, host1);
 		if (!header)
 			return -EINVAL;
 
-		if (host0 != le16_to_cpu(header->host0) ||
-				host1 != le16_to_cpu(header->host1)) {
-			dev_err(smem->dev,
-				"Partition %d hosts don't match\n", i);
-			return -EINVAL;
-		}
-
 		smem->partitions[remote_host] = header;
 		smem->cacheline[remote_host] = le32_to_cpu(entry->cacheline);
 	}
Signature · 1 line
2.14.1
author
#12 lore raw

PATCH 12/12 soc: qcom: smem: a few last cleanups

In reply to Alex Elder

This patch contains several small cleanups:
- In qcom_smem_enumerate_partitions(), change the "local_host" argument to have 16 bit unsigned type
- Also in qcom_smem_enumerate_partitions(), change the type of the "host0" and "host1" local variables to be u16
- Fix error messages reporting host ids to use the right format specifier
- Shorten the error messages as well, to fit on one line
- Add a compile-time check to ensure the local host value passed to qcom_smem_enumerate_partitions() is in range

---
 drivers/soc/qcom/smem.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/soc/qcom/smem.c +6 -9
index 7383a0e1b468..0d7f369c3f7a 100644
@@ -819,14 +819,14 @@ static int qcom_smem_set_global_partition(struct qcom_smem *smem)
 	return 0;
 }
 
-static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
-					  unsigned int local_host)
+static int
+qcom_smem_enumerate_partitions(struct qcom_smem *smem, u16 local_host)
 {
 	struct smem_partition_header *header;
 	struct smem_ptable_entry *entry;
 	struct smem_ptable *ptable;
 	unsigned int remote_host;
-	u32 host0, host1;
+	u16 host0, host1;
 	int i;
 
 	ptable = qcom_smem_get_ptable(smem);
@@ -850,16 +850,12 @@ static int qcom_smem_enumerate_partitions(struct qcom_smem *smem,
 			continue;
 
 		if (remote_host >= SMEM_HOST_COUNT) {
-			dev_err(smem->dev,
-				"Invalid remote host %d\n",
-				remote_host);
+			dev_err(smem->dev, "bad host %hu\n", remote_host);
 			return -EINVAL;
 		}
 
 		if (smem->partitions[remote_host]) {
-			dev_err(smem->dev,
-				"Already found a partition for host %d\n",
-				remote_host);
+			dev_err(smem->dev, "duplicate host %hu\n", remote_host);
 			return -EINVAL;
 		}
 
@@ -956,6 +952,7 @@ static int qcom_smem_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
+	BUILD_BUG_ON(SMEM_HOST_APPS >= SMEM_HOST_COUNT);
 	ret = qcom_smem_enumerate_partitions(smem, SMEM_HOST_APPS);
 	if (ret < 0 && ret != -ENOENT)
 		return ret;
Signature · 1 line
2.14.1