Skip to contentSkip to conversation

tpm_crb: Remove dead code from crb_map_res()

PATCH Superseded Jarkko Sakkinen 2 participants 6 messages Sep 1 – Sep 23

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH 1/1 tpm_crb: Remove dead code from crb_map_res()

In all the pre-existing call sites both @iomem and @iobase_ptr are either NULL or non-NULL.

Thus, add invariant for this and remove a dead basic block.

---
 drivers/char/tpm/tpm_crb.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/char/tpm/tpm_crb.c +3 -6
index ceb4100ba400..e7a61f36c58b 100644
@@ -570,15 +570,12 @@ static void __iomem *crb_map_res(struct device *dev, struct resource *iores,
 	if (start != new_res.start)
 		return IOMEM_ERR_PTR(-EINVAL);
 
+	if ((iores == NULL) != (iobase_ptr == NULL))
+		return IOMEM_ERR_PTR(-EINVAL);
+
 	if (!iores)
 		return devm_ioremap_resource(dev, &new_res);
 
-	if (!*iobase_ptr) {
-		*iobase_ptr = devm_ioremap_resource(dev, iores);
-		if (IS_ERR(*iobase_ptr))
-			return *iobase_ptr;
-	}
-
 	return *iobase_ptr + (new_res.start - iores->start);
 }
 
Signature · 1 line
2.47.3
#1 lore raw
Quoted 2 lines · Jarkko Sakkinen

In all the pre-existing call sites both @iomem and @iobase_ptr are either NULL or non-NULL.

I don't know this code, but I'm a bit worried about iobase_ptr and *iobase_ptr. IIUC it is true that iobase_ptr and iores are either NULL or non-NULL, but here we are removing the case where *iobase_ptr is NULL.

Now looking at crb_map_io(), IIUC iobase_array is initialized with NULL pointers and the code we are removing was the only one initializing those pointers IIUC, or am I missing something?

Thanks,
Stefano

Quoted 35 lines

Thus, add invariant for this and remove a dead basic block.

---
drivers/char/tpm/tpm_crb.c | 9 +++------

1 file changed, 3 insertions(+), 6 deletions(-)
drivers/char/tpm/tpm_crb.c +3 -6
index ceb4100ba400..e7a61f36c58b 100644
@@ -570,15 +570,12 @@ static void __iomem *crb_map_res(struct device *dev, struct resource *iores,
 	if (start != new_res.start)
 		return IOMEM_ERR_PTR(-EINVAL);
 
+	if ((iores == NULL) != (iobase_ptr == NULL))
+		return IOMEM_ERR_PTR(-EINVAL);
+
 	if (!iores)
 		return devm_ioremap_resource(dev, &new_res);
 
-	if (!*iobase_ptr) {
-		*iobase_ptr = devm_ioremap_resource(dev, iores);
-		if (IS_ERR(*iobase_ptr))
-			return *iobase_ptr;
-	}
-
 	return *iobase_ptr + (new_res.start - iores->start);
 }
 
Signature · 1 line
2.47.3
author maintainer
#2 lore raw
Quoted 11 lines · Stefano Garzarella
Quoted 2 lines · Jarkko Sakkinen

In all the pre-existing call sites both @iomem and @iobase_ptr are either NULL or non-NULL.

I don't know this code, but I'm a bit worried about iobase_ptr and *iobase_ptr. IIUC it is true that iobase_ptr and iores are either NULL or non-NULL, but here we are removing the case where *iobase_ptr is NULL.

Now looking at crb_map_io(), IIUC iobase_array is initialized with NULL pointers and the code we are removing was the only one initializing those pointers IIUC, or am I missing something?

crb_map_io() sets both to non-NULL value, or leaves both as NULL.

crb_map_pluton() explicitly calls both explicitly with NULL.

If anything else will arrive too crb_map_res, that'd be unexpected input, which without this patch will go unnoticed and will lead to undefined behavior.

Not sure what is the argument here really.

Quoted 3 lines

Thanks,
Stefano

BR, Jarkko

#3 lore raw
Quoted 13 lines · Jarkko Sakkinen
Quoted 11 lines · Stefano Garzarella
Quoted 2 lines · Jarkko Sakkinen

In all the pre-existing call sites both @iomem and @iobase_ptr are either NULL or non-NULL.

I don't know this code, but I'm a bit worried about iobase_ptr and *iobase_ptr. IIUC it is true that iobase_ptr and iores are either NULL or non-NULL, but here we are removing the case where *iobase_ptr is NULL.

Now looking at crb_map_io(), IIUC iobase_array is initialized with NULL pointers and the code we are removing was the only one initializing those pointers IIUC, or am I missing something?

Quoted 7 lines

crb_map_io() sets both to non-NULL value, or leaves both as NULL.

crb_map_pluton() explicitly calls both explicitly with NULL.

If anything else will arrive too crb_map_res, that'd be unexpected input, which without this patch will go unnoticed and will lead to undefined behavior.

This is clear, and it's what is done in the first hunk, what is not clear to me is why removing the second hunk.

Quoted 2 lines

Not sure what is the argument here really.

Sorry, I should have commented in the diff:

drivers/char/tpm/tpm_crb.c static void __iomem *crb_map_res(struct device *dev, struct resource *iores, · line 575

Quoted 11 lines
drivers/char/tpm/tpm_crb.c +3 -0
index ceb4100ba400..e7a61f36c58b 100644
@@ -570,15 +570,12 @@ static void __iomem *crb_map_res(struct device *dev, struct resource *iores,
 	if (start != new_res.start)
 		return IOMEM_ERR_PTR(-EINVAL);
 
+	if ((iores == NULL) != (iobase_ptr == NULL))
+		return IOMEM_ERR_PTR(-EINVAL);
+

This makes sense to me.

drivers/char/tpm/tpm_crb.c static void __iomem *crb_map_res(struct device *dev, struct resource *iores, · line 581

Quoted 9 lines
drivers/char/tpm/tpm_crb.c +0 -6
 	if (!iores)
 		return devm_ioremap_resource(dev, &new_res);
 
-	if (!*iobase_ptr) {
-		*iobase_ptr = devm_ioremap_resource(dev, iores);
-		if (IS_ERR(*iobase_ptr))
-			return *iobase_ptr;
-	}
-

This is unclear to me, here we are checking if the value stored in iobase_ptr is NULL (so something different from the check we are adding above, but appropriate because it only makes sense when both are non-NULL). If the value stored in the pointer is NULL we are setting it. Looking at the code, I can't see any other point where that values (iobase_array[]) are initialized, but again, I don't know this code, so I may missing something.

Stefano

Quoted 2 lines
drivers/char/tpm/tpm_crb.c +0 -0
 	return *iobase_ptr + (new_res.start - iores->start);
 }
author maintainer
#4 lore raw
Quoted 62 lines · Stefano Garzarella
Quoted 13 lines · Jarkko Sakkinen
Quoted 11 lines · Stefano Garzarella
Quoted 2 lines · Jarkko Sakkinen

In all the pre-existing call sites both @iomem and @iobase_ptr are either NULL or non-NULL.

I don't know this code, but I'm a bit worried about iobase_ptr and *iobase_ptr. IIUC it is true that iobase_ptr and iores are either NULL or non-NULL, but here we are removing the case where *iobase_ptr is NULL.

Now looking at crb_map_io(), IIUC iobase_array is initialized with NULL pointers and the code we are removing was the only one initializing those pointers IIUC, or am I missing something?

Quoted 7 lines

crb_map_io() sets both to non-NULL value, or leaves both as NULL.

crb_map_pluton() explicitly calls both explicitly with NULL.

If anything else will arrive too crb_map_res, that'd be unexpected input, which without this patch will go unnoticed and will lead to undefined behavior.

This is clear, and it's what is done in the first hunk, what is not clear to me is why removing the second hunk.

Quoted 2 lines

Not sure what is the argument here really.

Sorry, I should have commented in the diff:

drivers/char/tpm/tpm_crb.c static void __iomem *crb_map_res(struct device *dev, struct resource *iores, · line 575

Quoted 11 lines
drivers/char/tpm/tpm_crb.c +3 -0
index ceb4100ba400..e7a61f36c58b 100644
@@ -570,15 +570,12 @@ static void __iomem *crb_map_res(struct device *dev, struct resource *iores,
 	if (start != new_res.start)
 		return IOMEM_ERR_PTR(-EINVAL);
 
+	if ((iores == NULL) != (iobase_ptr == NULL))
+		return IOMEM_ERR_PTR(-EINVAL);
+

This makes sense to me.

drivers/char/tpm/tpm_crb.c static void __iomem *crb_map_res(struct device *dev, struct resource *iores, · line 581

Quoted 9 lines
drivers/char/tpm/tpm_crb.c +0 -6
 	if (!iores)
 		return devm_ioremap_resource(dev, &new_res);
 
-	if (!*iobase_ptr) {
-		*iobase_ptr = devm_ioremap_resource(dev, iores);
-		if (IS_ERR(*iobase_ptr))
-			return *iobase_ptr;
-	}
-

This is unclear to me, here we are checking if the value stored in iobase_ptr is NULL (so something different from the check we are adding above, but appropriate because it only makes sense when both are non-NULL). If the value stored in the pointer is NULL we are setting it. Looking at the code, I can't see any other point where that values (iobase_array[]) are initialized, but again, I don't know this code, so I may missing something.

Quoted 5 lines

Stefano

Quoted 2 lines
drivers/char/tpm/tpm_crb.c +0 -0
 	return *iobase_ptr + (new_res.start - iores->start);
 }

Thanks for the remarks and ack for receiving this ;-)

I'll move this to my TODO-folder and read it with thought some days from now.

BR, Jarkko

author maintainer
#5 lore raw
In reply to Stefano Garzarella
Quoted 47 lines · Stefano Garzarella
Quoted 13 lines · Jarkko Sakkinen
Quoted 11 lines · Stefano Garzarella
Quoted 2 lines · Jarkko Sakkinen

In all the pre-existing call sites both @iomem and @iobase_ptr are either NULL or non-NULL.

I don't know this code, but I'm a bit worried about iobase_ptr and *iobase_ptr. IIUC it is true that iobase_ptr and iores are either NULL or non-NULL, but here we are removing the case where *iobase_ptr is NULL.

Now looking at crb_map_io(), IIUC iobase_array is initialized with NULL pointers and the code we are removing was the only one initializing those pointers IIUC, or am I missing something?

Quoted 7 lines

crb_map_io() sets both to non-NULL value, or leaves both as NULL.

crb_map_pluton() explicitly calls both explicitly with NULL.

If anything else will arrive too crb_map_res, that'd be unexpected input, which without this patch will go unnoticed and will lead to undefined behavior.

This is clear, and it's what is done in the first hunk, what is not clear to me is why removing the second hunk.

Quoted 2 lines

Not sure what is the argument here really.

Sorry, I should have commented in the diff:

drivers/char/tpm/tpm_crb.c static void __iomem *crb_map_res(struct device *dev, struct resource *iores, · line 575

Quoted 11 lines
drivers/char/tpm/tpm_crb.c +3 -0
index ceb4100ba400..e7a61f36c58b 100644
@@ -570,15 +570,12 @@ static void __iomem *crb_map_res(struct device *dev, struct resource *iores,
 	if (start != new_res.start)
 		return IOMEM_ERR_PTR(-EINVAL);
 
+	if ((iores == NULL) != (iobase_ptr == NULL))
+		return IOMEM_ERR_PTR(-EINVAL);
+

This makes sense to me.

Quoted 3 lines
drivers/char/tpm/tpm_crb.c +0 -0
 	if (!iores)
 		return devm_ioremap_resource(dev, &new_res);
 
Quoted 14 lines

drivers/char/tpm/tpm_crb.c static void __iomem *crb_map_res(struct device *dev, struct resource *iores, · line 581

Quoted 6 lines
drivers/char/tpm/tpm_crb.c +0 -6
-	if (!*iobase_ptr) {
-		*iobase_ptr = devm_ioremap_resource(dev, iores);
-		if (IS_ERR(*iobase_ptr))
-			return *iobase_ptr;
-	}
-

This is unclear to me, here we are checking if the value stored in iobase_ptr is NULL (so something different from the check we are adding above, but appropriate because it only makes sense when both are non-NULL). If the value stored in the pointer is NULL we are setting it. Looking at the code, I can't see any other point where that values (iobase_array[]) are initialized, but again, I don't know this code, so I may missing something.

If iobase_ptr would be NULL, so would iores given previous guards.

Call sites for crb_map_res:

- crb_map_io: both are non-NULL
- crb_map_pluton: both are NULL

Quoted 2 lines

Stefano

Br, Jarkko