Skip to contentSkip to conversation

iommu/exynos: allocate lv2 page table from own slab

PATCH Superseded Cho KyongHo 4 participants 9 messages Aug 8, 2013 – Aug 16, 2013

Subsystems (3)

v9 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v9 4/16 iommu/exynos: allocate lv2 page table from own slab

Since kmalloc() does not guarantee that the allignment of 1KiB when it allocates 1KiB, it is required to allocate lv2 page table from own slab that guarantees alignment of 1KiB

---
 drivers/iommu/exynos-iommu.c |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)
drivers/iommu/exynos-iommu.c +20 -4
index d90e6fa..a318049 100644
@@ -100,6 +100,8 @@
 #define REG_PB1_SADDR		0x054
 #define REG_PB1_EADDR		0x058
 
+static struct kmem_cache *lv2table_kmem_cache;
+
 static unsigned long *section_entry(unsigned long *pgtable, unsigned long iova)
 {
 	return pgtable + lv1ent_offset(iova);
@@ -765,7 +767,8 @@ static void exynos_iommu_domain_destroy(struct iommu_domain *domain)
 
 	for (i = 0; i < NUM_LV1ENTRIES; i++)
 		if (lv1ent_page(priv->pgtable + i))
-			kfree(__va(lv2table_base(priv->pgtable + i)));
+			kmem_cache_free(lv2table_kmem_cache,
+					__va(lv2table_base(priv->pgtable + i)));
 
 	free_pages((unsigned long)priv->pgtable, 2);
 	free_pages((unsigned long)priv->lv2entcnt, 1);
@@ -861,7 +864,7 @@ static unsigned long *alloc_lv2entry(unsigned long *sent, unsigned long iova,
 	if (lv1ent_fault(sent)) {
 		unsigned long *pent;
 
-		pent = kzalloc(LV2TABLE_SIZE, GFP_ATOMIC);
+		pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
 		BUG_ON((unsigned long)pent & (LV2TABLE_SIZE - 1));
 		if (!pent)
 			return ERR_PTR(-ENOMEM);
@@ -881,7 +884,7 @@ static int lv1set_section(unsigned long *sent, phys_addr_t paddr, short *pgcnt)
 
 	if (lv1ent_page(sent)) {
 		BUG_ON(*pgcnt != NUM_LV2ENTRIES);
-		kfree(page_entry(sent, 0));
+		kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
 		*pgcnt = 0;
 	}
 
@@ -1082,10 +1085,23 @@ static int __init exynos_iommu_init(void)
 {
 	int ret;
 
+	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
+				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
+	if (!lv2table_kmem_cache) {
+		pr_err("%s: Failed to create kmem cache\n", __func__);
+		return -ENOMEM;
+	}
+
 	ret = platform_driver_register(&exynos_sysmmu_driver);
 
 	if (ret == 0)
-		bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+		ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+
+	if (ret) {
+		pr_err("%s: Failed to register exynos-iommu driver.\n",
+								__func__);
+		kmem_cache_destroy(lv2table_kmem_cache);
+	}
 
 	return ret;
 }
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 64 lines · On Thursday 08 of August 2013 18:38:04 Cho KyongHo wrote:

Since kmalloc() does not guarantee that the allignment of 1KiB when it allocates 1KiB, it is required to allocate lv2 page table from own slab that guarantees alignment of 1KiB

---
 drivers/iommu/exynos-iommu.c |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)
drivers/iommu/exynos-iommu.c +4 -1
index d90e6fa..a318049 100644
@@ -100,6 +100,8 @@
 #define REG_PB1_SADDR		0x054
 #define REG_PB1_EADDR		0x058
 
+static struct kmem_cache *lv2table_kmem_cache;
+
 static unsigned long *section_entry(unsigned long *pgtable, unsigned
 long iova) {
 	return pgtable + lv1ent_offset(iova);
@@ -765,7 +767,8 @@ static void exynos_iommu_domain_destroy(struct
 iommu_domain *domain)
 
 	for (i = 0; i < NUM_LV1ENTRIES; i++)
 		if (lv1ent_page(priv->pgtable + i))
-			kfree(__va(lv2table_base(priv->pgtable + i)));
+			kmem_cache_free(lv2table_kmem_cache,
+					__va(lv2table_base(priv->pgtable + i)));
 
 	free_pages((unsigned long)priv->pgtable, 2);
drivers/iommu/exynos-iommu.c +9 -2
 	free_pages((unsigned long)priv->lv2entcnt, 1);
@@ -861,7 +864,7 @@ static unsigned long *alloc_lv2entry(unsigned long
 *sent, unsigned long iova, if (lv1ent_fault(sent)) {
 		unsigned long *pent;
 
-		pent = kzalloc(LV2TABLE_SIZE, GFP_ATOMIC);
+		pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
 		BUG_ON((unsigned long)pent & (LV2TABLE_SIZE - 1));
 		if (!pent)
 			return ERR_PTR(-ENOMEM);
@@ -881,7 +884,7 @@ static int lv1set_section(unsigned long *sent,
 phys_addr_t paddr, short *pgcnt)
 
 	if (lv1ent_page(sent)) {
 		BUG_ON(*pgcnt != NUM_LV2ENTRIES);
-		kfree(page_entry(sent, 0));
+		kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
 		*pgcnt = 0;
 	}
@@ -1082,10 +1085,23 @@ static int __init exynos_iommu_init(void)
 {
 	int ret;
 
+	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
+				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
+	if (!lv2table_kmem_cache) {
+		pr_err("%s: Failed to create kmem cache\n", __func__);
+		return -ENOMEM;
+	}
+
 	ret = platform_driver_register(&exynos_sysmmu_driver);

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void) · line 1104

Quoted 10 lines
drivers/iommu/exynos-iommu.c +7 -1
 
 	if (ret == 0)
-		bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+		ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+
+	if (ret) {
+		pr_err("%s: Failed to register exynos-iommu driver.\n",
+								__func__);
+		kmem_cache_destroy(lv2table_kmem_cache);
+	}

What about making the return value handling here cleaner? For example:

	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
	if (!lv2table_kmem_cache) {
		...
		return -ENOMEM;
	}

	ret = platform_driver_register(&exynos_sysmmu_driver);
	if (ret) {
		...
		goto err_destroy_kmem_cache;
	}

	ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
	if (ret) {
		...
		goto err_platform_unregister;
	}

	return 0;

err_platform_unregister:

	...
err_destroy_kmem_cache:
	...
	return ret;
}

Best regards,
Tomasz

#2 lore raw
Quoted 99 lines · Tomasz Figa
Quoted 64 lines · On Thursday 08 of August 2013 18:38:04 Cho KyongHo wrote:

Since kmalloc() does not guarantee that the allignment of 1KiB when it allocates 1KiB, it is required to allocate lv2 page table from own slab that guarantees alignment of 1KiB

---
 drivers/iommu/exynos-iommu.c |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)
drivers/iommu/exynos-iommu.c +4 -1
index d90e6fa..a318049 100644
@@ -100,6 +100,8 @@
 #define REG_PB1_SADDR		0x054
 #define REG_PB1_EADDR		0x058
 
+static struct kmem_cache *lv2table_kmem_cache;
+
 static unsigned long *section_entry(unsigned long *pgtable, unsigned
 long iova) {
 	return pgtable + lv1ent_offset(iova);
@@ -765,7 +767,8 @@ static void exynos_iommu_domain_destroy(struct
 iommu_domain *domain)
 
 	for (i = 0; i < NUM_LV1ENTRIES; i++)
 		if (lv1ent_page(priv->pgtable + i))
-			kfree(__va(lv2table_base(priv->pgtable + i)));
+			kmem_cache_free(lv2table_kmem_cache,
+					__va(lv2table_base(priv->pgtable + i)));
 
 	free_pages((unsigned long)priv->pgtable, 2);
drivers/iommu/exynos-iommu.c +9 -2
 	free_pages((unsigned long)priv->lv2entcnt, 1);
@@ -861,7 +864,7 @@ static unsigned long *alloc_lv2entry(unsigned long
 *sent, unsigned long iova, if (lv1ent_fault(sent)) {
 		unsigned long *pent;
 
-		pent = kzalloc(LV2TABLE_SIZE, GFP_ATOMIC);
+		pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
 		BUG_ON((unsigned long)pent & (LV2TABLE_SIZE - 1));
 		if (!pent)
 			return ERR_PTR(-ENOMEM);
@@ -881,7 +884,7 @@ static int lv1set_section(unsigned long *sent,
 phys_addr_t paddr, short *pgcnt)
 
 	if (lv1ent_page(sent)) {
 		BUG_ON(*pgcnt != NUM_LV2ENTRIES);
-		kfree(page_entry(sent, 0));
+		kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
 		*pgcnt = 0;
 	}
@@ -1082,10 +1085,23 @@ static int __init exynos_iommu_init(void)
 {
 	int ret;
 
+	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
+				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
+	if (!lv2table_kmem_cache) {
+		pr_err("%s: Failed to create kmem cache\n", __func__);
+		return -ENOMEM;
+	}
+
 	ret = platform_driver_register(&exynos_sysmmu_driver);

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void) · line 1104

Quoted 10 lines
drivers/iommu/exynos-iommu.c +7 -1
 
 	if (ret == 0)
-		bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+		ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+
+	if (ret) {
+		pr_err("%s: Failed to register exynos-iommu driver.\n",
+								__func__);
+		kmem_cache_destroy(lv2table_kmem_cache);
+	}

What about making the return value handling here cleaner? For example:

	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
	if (!lv2table_kmem_cache) {
		...
		return -ENOMEM;
	}

	ret = platform_driver_register(&exynos_sysmmu_driver);
	if (ret) {
		...
		goto err_destroy_kmem_cache;
	}

	ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
	if (ret) {
		...
		goto err_platform_unregister;
	}

	return 0;
Quoted 7 lines

err_platform_unregister:

	...
err_destroy_kmem_cache:
	...
	return ret;
}

Thank you for suggestion.
I think you are worrying about missing the information who makes 'ret' non-zero. Ok. I will process it separately.

thanks

KyongHo.

Quoted 3 lines

Best regards,
Tomasz

#3 lore raw

Hi KyongHo,

Quoted 35 lines · On Friday 09 of August 2013 14:58:49 Cho KyongHo wrote:
Quoted 34 lines · Tomasz Figa
Quoted 33 lines · On Thursday 08 of August 2013 18:38:04 Cho KyongHo wrote:

Since kmalloc() does not guarantee that the allignment of 1KiB when it
allocates 1KiB, it is required to allocate lv2 page table from own slab that guarantees alignment of 1KiB

---

 drivers/iommu/exynos-iommu.c |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iommu/exynos-iommu.c index d90e6fa..a318049 100644

drivers/iommu/exynos-iommu.c +2 -0
@@ -100,6 +100,8 @@
 
 #define REG_PB1_SADDR		0x054
 #define REG_PB1_EADDR		0x058
 
+static struct kmem_cache *lv2table_kmem_cache;
+
 
 static unsigned long *section_entry(unsigned long *pgtable,

unsigned

long iova) {

drivers/iommu/exynos-iommu.c +0 -0
 	return pgtable + lv1ent_offset(iova);
@@ -765,7 +767,8 @@ static void exynos_iommu_domain_destroy(struct
 iommu_domain *domain)
 
Quoted 7 lines
Quoted 7 lines

drivers/iommu/exynos-iommu.c static void exynos_iommu_domain_destroy(struct · line 774

Quoted 7 lines
drivers/iommu/exynos-iommu.c +2 -1
 	for (i = 0; i < NUM_LV1ENTRIES; i++)
 
 		if (lv1ent_page(priv->pgtable + i))
 
-			kfree(__va(lv2table_base(priv->pgtable + i)));
+			kmem_cache_free(lv2table_kmem_cache,
+					__va(lv2table_base(priv->pgtable + 

i)));

Quoted 46 lines
Quoted 46 lines
Quoted 46 lines
drivers/iommu/exynos-iommu.c +0 -0
 	free_pages((unsigned long)priv->pgtable, 2);
 	free_pages((unsigned long)priv->lv2entcnt, 1);
@@ -861,7 +864,7 @@ static unsigned long *alloc_lv2entry(unsigned

long
*sent, unsigned long iova, if (lv1ent_fault(sent)) {

drivers/iommu/exynos-iommu.c +2 -2
 		unsigned long *pent;
 
-		pent = kzalloc(LV2TABLE_SIZE, GFP_ATOMIC);
+		pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
 
 		BUG_ON((unsigned long)pent & (LV2TABLE_SIZE - 1));
 		if (!pent)
 
 			return ERR_PTR(-ENOMEM);
@@ -881,7 +884,7 @@ static int lv1set_section(unsigned long *sent,
 phys_addr_t paddr, short *pgcnt)
 
 	if (lv1ent_page(sent)) {
 
 		BUG_ON(*pgcnt != NUM_LV2ENTRIES);
 
-		kfree(page_entry(sent, 0));
+		kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
drivers/iommu/exynos-iommu.c +7 -0
 		*pgcnt = 0;
 
 	}
@@ -1082,10 +1085,23 @@ static int __init exynos_iommu_init(void)
 
 {
 
 	int ret;
 
+	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
+				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
+	if (!lv2table_kmem_cache) {
+		pr_err("%s: Failed to create kmem cache\n", __func__);
+		return -ENOMEM;
+	}
+
 
Quoted 6 lines
Quoted 6 lines

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void)

Quoted 6 lines
drivers/iommu/exynos-iommu.c +1 -1
 	ret = platform_driver_register(&exynos_sysmmu_driver);
 
 	if (ret == 0)
 
-		bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+		ret = bus_set_iommu(&platform_bus_type, 

&exynos_iommu_ops);

Quoted 44 lines
Quoted 44 lines

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void) · line 1104

Quoted 6 lines
drivers/iommu/exynos-iommu.c +6 -0
+
+	if (ret) {
+		pr_err("%s: Failed to register exynos-iommu driver.\n",
+								__func__);
+		kmem_cache_destroy(lv2table_kmem_cache);
+	}

What about making the return value handling here cleaner? For example:

	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
	
				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
	
	if (!lv2table_kmem_cache) {
	
		...
		return -ENOMEM;
	
	}
	
	ret = platform_driver_register(&exynos_sysmmu_driver);
	if (ret) {
	
		...
		goto err_destroy_kmem_cache;
	
	}
	
	ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
	if (ret) {
	
		...
		goto err_platform_unregister;
	
	}
	
	return 0;

err_platform_unregister:

	...

err_destroy_kmem_cache:

	...
	return ret;
Quoted 5 lines
Quoted 1 line

}

Thank you for suggestion.
I think you are worrying about missing the information who makes 'ret' non-zero.

Oh, this is a valid point, but it was more a nitpick about the coding style. Single path error handling (with goto) is widely used in the kernel in cases when more than one thing has to be undone and so I suggested this method of error handling here as well.

Quoted 1 line

Ok. I will process it separately.

Since this patch adds most of the error handling to this function, I think it should be fine to do it as a part of this patch.

Best regards,
Tomasz

#4 lore raw
Quoted 154 lines · Tomasz Figa

Hi KyongHo,

Quoted 35 lines · On Friday 09 of August 2013 14:58:49 Cho KyongHo wrote:
Quoted 34 lines · Tomasz Figa
Quoted 33 lines · On Thursday 08 of August 2013 18:38:04 Cho KyongHo wrote:

Since kmalloc() does not guarantee that the allignment of 1KiB when it
allocates 1KiB, it is required to allocate lv2 page table from own slab that guarantees alignment of 1KiB

---

 drivers/iommu/exynos-iommu.c |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iommu/exynos-iommu.c index d90e6fa..a318049 100644

drivers/iommu/exynos-iommu.c +2 -0
@@ -100,6 +100,8 @@
 
 #define REG_PB1_SADDR		0x054
 #define REG_PB1_EADDR		0x058
 
+static struct kmem_cache *lv2table_kmem_cache;
+
 
 static unsigned long *section_entry(unsigned long *pgtable,

unsigned

long iova) {

drivers/iommu/exynos-iommu.c +0 -0
 	return pgtable + lv1ent_offset(iova);
@@ -765,7 +767,8 @@ static void exynos_iommu_domain_destroy(struct
 iommu_domain *domain)
 
Quoted 7 lines
Quoted 7 lines

drivers/iommu/exynos-iommu.c static void exynos_iommu_domain_destroy(struct · line 774

Quoted 7 lines
drivers/iommu/exynos-iommu.c +2 -1
 	for (i = 0; i < NUM_LV1ENTRIES; i++)
 
 		if (lv1ent_page(priv->pgtable + i))
 
-			kfree(__va(lv2table_base(priv->pgtable + i)));
+			kmem_cache_free(lv2table_kmem_cache,
+					__va(lv2table_base(priv->pgtable + 

i)));

Quoted 46 lines
Quoted 46 lines
Quoted 46 lines
drivers/iommu/exynos-iommu.c +0 -0
 	free_pages((unsigned long)priv->pgtable, 2);
 	free_pages((unsigned long)priv->lv2entcnt, 1);
@@ -861,7 +864,7 @@ static unsigned long *alloc_lv2entry(unsigned

long
*sent, unsigned long iova, if (lv1ent_fault(sent)) {

drivers/iommu/exynos-iommu.c +2 -2
 		unsigned long *pent;
 
-		pent = kzalloc(LV2TABLE_SIZE, GFP_ATOMIC);
+		pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
 
 		BUG_ON((unsigned long)pent & (LV2TABLE_SIZE - 1));
 		if (!pent)
 
 			return ERR_PTR(-ENOMEM);
@@ -881,7 +884,7 @@ static int lv1set_section(unsigned long *sent,
 phys_addr_t paddr, short *pgcnt)
 
 	if (lv1ent_page(sent)) {
 
 		BUG_ON(*pgcnt != NUM_LV2ENTRIES);
 
-		kfree(page_entry(sent, 0));
+		kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
drivers/iommu/exynos-iommu.c +7 -0
 		*pgcnt = 0;
 
 	}
@@ -1082,10 +1085,23 @@ static int __init exynos_iommu_init(void)
 
 {
 
 	int ret;
 
+	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
+				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
+	if (!lv2table_kmem_cache) {
+		pr_err("%s: Failed to create kmem cache\n", __func__);
+		return -ENOMEM;
+	}
+
 
Quoted 6 lines
Quoted 6 lines

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void)

Quoted 6 lines
drivers/iommu/exynos-iommu.c +1 -1
 	ret = platform_driver_register(&exynos_sysmmu_driver);
 
 	if (ret == 0)
 
-		bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+		ret = bus_set_iommu(&platform_bus_type, 

&exynos_iommu_ops);

Quoted 44 lines
Quoted 44 lines

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void) · line 1104

Quoted 6 lines
drivers/iommu/exynos-iommu.c +6 -0
+
+	if (ret) {
+		pr_err("%s: Failed to register exynos-iommu driver.\n",
+								__func__);
+		kmem_cache_destroy(lv2table_kmem_cache);
+	}

What about making the return value handling here cleaner? For example:

	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
	
				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
	
	if (!lv2table_kmem_cache) {
	
		...
		return -ENOMEM;
	
	}
	
	ret = platform_driver_register(&exynos_sysmmu_driver);
	if (ret) {
	
		...
		goto err_destroy_kmem_cache;
	
	}
	
	ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
	if (ret) {
	
		...
		goto err_platform_unregister;
	
	}
	
	return 0;

err_platform_unregister:

	...

err_destroy_kmem_cache:

	...
	return ret;
Quoted 5 lines
Quoted 1 line

}

Thank you for suggestion.
I think you are worrying about missing the information who makes 'ret' non-zero.

Oh, this is a valid point, but it was more a nitpick about the coding style. Single path error handling (with goto) is widely used in the kernel in cases when more than one thing has to be undone and so I suggested this method of error handling here as well.

Quoted 5 lines
Quoted 1 line

Ok. I will process it separately.

Since this patch adds most of the error handling to this function, I think it should be fine to do it as a part of this patch.

I meant 'separately' that checking all return values each. I think it can be simpler without goto.
There are just 2 cases to rollback previous changes in that function.

Quoted 3 lines

Best regards,
Tomasz

#5 lore raw
Quoted 108 lines · On Friday 09 of August 2013 17:51:56 Cho KyongHo wrote:
Quoted 107 lines · Tomasz Figa

Hi KyongHo,

Quoted 38 lines · On Friday 09 of August 2013 14:58:49 Cho KyongHo wrote:
Quoted 37 lines · Tomasz Figa
Quoted 36 lines · On Thursday 08 of August 2013 18:38:04 Cho KyongHo wrote:

Since kmalloc() does not guarantee that the allignment of 1KiB when
it
allocates 1KiB, it is required to allocate lv2 page table from own
slab that guarantees alignment of 1KiB

---

 drivers/iommu/exynos-iommu.c |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iommu/exynos-iommu.c index d90e6fa..a318049 100644

drivers/iommu/exynos-iommu.c +2 -0
@@ -100,6 +100,8 @@
 
 #define REG_PB1_SADDR		0x054
 #define REG_PB1_EADDR		0x058
 
+static struct kmem_cache *lv2table_kmem_cache;
+
 
 static unsigned long *section_entry(unsigned long *pgtable,

unsigned

long iova) {

drivers/iommu/exynos-iommu.c +0 -0
 	return pgtable + lv1ent_offset(iova);
@@ -765,7 +767,8 @@ static void

exynos_iommu_domain_destroy(struct
iommu_domain *domain)

Quoted 7 lines
Quoted 7 lines

drivers/iommu/exynos-iommu.c

Quoted 7 lines
drivers/iommu/exynos-iommu.c +2 -1
 	for (i = 0; i < NUM_LV1ENTRIES; i++)
 
 		if (lv1ent_page(priv->pgtable + i))
 
-			kfree(__va(lv2table_base(priv->pgtable + i)));
+			kmem_cache_free(lv2table_kmem_cache,
+					__va(lv2table_base(priv->pgtable +

i)));

Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
drivers/iommu/exynos-iommu.c +0 -0
 	free_pages((unsigned long)priv->pgtable, 2);
 	free_pages((unsigned long)priv->lv2entcnt, 1);
@@ -861,7 +864,7 @@ static unsigned long *alloc_lv2entry(unsigned

long
*sent, unsigned long iova, if (lv1ent_fault(sent)) {

drivers/iommu/exynos-iommu.c +9 -2
 		unsigned long *pent;
 
-		pent = kzalloc(LV2TABLE_SIZE, GFP_ATOMIC);
+		pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
 
 		BUG_ON((unsigned long)pent & (LV2TABLE_SIZE - 1));
 		if (!pent)
 
 			return ERR_PTR(-ENOMEM);
@@ -881,7 +884,7 @@ static int lv1set_section(unsigned long
 *sent,
 phys_addr_t paddr, short *pgcnt)
 
 	if (lv1ent_page(sent)) {
 
 		BUG_ON(*pgcnt != NUM_LV2ENTRIES);
 
-		kfree(page_entry(sent, 0));
+		kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
 
 		*pgcnt = 0;
 
 	}
@@ -1082,10 +1085,23 @@ static int __init exynos_iommu_init(void)
 
 {
 
 	int ret;
 
+	lv2table_kmem_cache =
 kmem_cache_create("exynos-iommu-lv2table",
+				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
+	if (!lv2table_kmem_cache) {
+		pr_err("%s: Failed to create kmem cache\n", __func__);
+		return -ENOMEM;
+	}
+
 
Quoted 6 lines
Quoted 6 lines

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void)

Quoted 6 lines
drivers/iommu/exynos-iommu.c +1 -1
 	ret = platform_driver_register(&exynos_sysmmu_driver);
 
 	if (ret == 0)
 
-		bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+		ret = bus_set_iommu(&platform_bus_type,

&exynos_iommu_ops);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void) · line 1104

Quoted 6 lines
drivers/iommu/exynos-iommu.c +6 -0
+
+	if (ret) {
+		pr_err("%s: Failed to register exynos-iommu driver.\n",
+								__func__);
+		kmem_cache_destroy(lv2table_kmem_cache);
+	}

What about making the return value handling here cleaner? For

example:

Quoted 50 lines
Quoted 50 lines
Quoted 36 lines
Quoted 36 lines
	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
	
				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
	
	if (!lv2table_kmem_cache) {
	
		...
		return -ENOMEM;
	
	}
	
	ret = platform_driver_register(&exynos_sysmmu_driver);
	if (ret) {
	
		...
		goto err_destroy_kmem_cache;
	
	}
	
	ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
	if (ret) {
	
		...
		goto err_platform_unregister;
	
	}
	
	return 0;

err_platform_unregister:

	...

err_destroy_kmem_cache:

	...
	return ret;
Quoted 6 lines
Quoted 1 line

}

Thank you for suggestion.
I think you are worrying about missing the information who makes 'ret'
non-zero.

Oh, this is a valid point, but it was more a nitpick about the coding style. Single path error handling (with goto) is widely used in the kernel in cases when more than one thing has to be undone and so I suggested this method of error handling here as well.

Quoted 1 line

Ok. I will process it separately.

Quoted 7 lines
Quoted 2 lines

Since this patch adds most of the error handling to this function, I think it should be fine to do it as a part of this patch.

I meant 'separately' that checking all return values each. I think it can be simpler without goto.

There are just 2 cases to rollback previous changes in that function.

It might end up with less lines, but having roll back in single place is more readable. That's why it's the preferred error handling way in Linux kernel, if you have more than one action to undo on your error path.

Best regards,
Tomasz

#6 lore raw
Quoted 171 lines · Tomasz Figa
Quoted 108 lines · On Friday 09 of August 2013 17:51:56 Cho KyongHo wrote:
Quoted 107 lines · Tomasz Figa

Hi KyongHo,

Quoted 38 lines · On Friday 09 of August 2013 14:58:49 Cho KyongHo wrote:
Quoted 37 lines · Tomasz Figa
Quoted 36 lines · On Thursday 08 of August 2013 18:38:04 Cho KyongHo wrote:

Since kmalloc() does not guarantee that the allignment of 1KiB when
it
allocates 1KiB, it is required to allocate lv2 page table from own
slab that guarantees alignment of 1KiB

---

 drivers/iommu/exynos-iommu.c |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/iommu/exynos-iommu.c index d90e6fa..a318049 100644

drivers/iommu/exynos-iommu.c +2 -0
@@ -100,6 +100,8 @@
 
 #define REG_PB1_SADDR		0x054
 #define REG_PB1_EADDR		0x058
 
+static struct kmem_cache *lv2table_kmem_cache;
+
 
 static unsigned long *section_entry(unsigned long *pgtable,

unsigned

long iova) {

drivers/iommu/exynos-iommu.c +0 -0
 	return pgtable + lv1ent_offset(iova);
@@ -765,7 +767,8 @@ static void

exynos_iommu_domain_destroy(struct
iommu_domain *domain)

Quoted 7 lines
Quoted 7 lines

drivers/iommu/exynos-iommu.c

Quoted 7 lines
drivers/iommu/exynos-iommu.c +2 -1
 	for (i = 0; i < NUM_LV1ENTRIES; i++)
 
 		if (lv1ent_page(priv->pgtable + i))
 
-			kfree(__va(lv2table_base(priv->pgtable + i)));
+			kmem_cache_free(lv2table_kmem_cache,
+					__va(lv2table_base(priv->pgtable +

i)));

Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
drivers/iommu/exynos-iommu.c +0 -0
 	free_pages((unsigned long)priv->pgtable, 2);
 	free_pages((unsigned long)priv->lv2entcnt, 1);
@@ -861,7 +864,7 @@ static unsigned long *alloc_lv2entry(unsigned

long
*sent, unsigned long iova, if (lv1ent_fault(sent)) {

drivers/iommu/exynos-iommu.c +9 -2
 		unsigned long *pent;
 
-		pent = kzalloc(LV2TABLE_SIZE, GFP_ATOMIC);
+		pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
 
 		BUG_ON((unsigned long)pent & (LV2TABLE_SIZE - 1));
 		if (!pent)
 
 			return ERR_PTR(-ENOMEM);
@@ -881,7 +884,7 @@ static int lv1set_section(unsigned long
 *sent,
 phys_addr_t paddr, short *pgcnt)
 
 	if (lv1ent_page(sent)) {
 
 		BUG_ON(*pgcnt != NUM_LV2ENTRIES);
 
-		kfree(page_entry(sent, 0));
+		kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
 
 		*pgcnt = 0;
 
 	}
@@ -1082,10 +1085,23 @@ static int __init exynos_iommu_init(void)
 
 {
 
 	int ret;
 
+	lv2table_kmem_cache =
 kmem_cache_create("exynos-iommu-lv2table",
+				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
+	if (!lv2table_kmem_cache) {
+		pr_err("%s: Failed to create kmem cache\n", __func__);
+		return -ENOMEM;
+	}
+
 
Quoted 6 lines
Quoted 6 lines

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void)

Quoted 6 lines
drivers/iommu/exynos-iommu.c +1 -1
 	ret = platform_driver_register(&exynos_sysmmu_driver);
 
 	if (ret == 0)
 
-		bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
+		ret = bus_set_iommu(&platform_bus_type,

&exynos_iommu_ops);

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iommu/exynos-iommu.c static int __init exynos_iommu_init(void) · line 1104

Quoted 6 lines
drivers/iommu/exynos-iommu.c +6 -0
+
+	if (ret) {
+		pr_err("%s: Failed to register exynos-iommu driver.\n",
+								__func__);
+		kmem_cache_destroy(lv2table_kmem_cache);
+	}

What about making the return value handling here cleaner? For

example:

Quoted 53 lines
Quoted 52 lines
Quoted 36 lines
Quoted 36 lines
	lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
	
				LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
	
	if (!lv2table_kmem_cache) {
	
		...
		return -ENOMEM;
	
	}
	
	ret = platform_driver_register(&exynos_sysmmu_driver);
	if (ret) {
	
		...
		goto err_destroy_kmem_cache;
	
	}
	
	ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
	if (ret) {
	
		...
		goto err_platform_unregister;
	
	}
	
	return 0;

err_platform_unregister:

	...

err_destroy_kmem_cache:

	...
	return ret;
Quoted 6 lines
Quoted 1 line

}

Thank you for suggestion.
I think you are worrying about missing the information who makes 'ret'
non-zero.

Oh, this is a valid point, but it was more a nitpick about the coding style. Single path error handling (with goto) is widely used in the kernel in cases when more than one thing has to be undone and so I suggested this method of error handling here as well.

Quoted 1 line

Ok. I will process it separately.

Since this patch adds most of the error handling to this function, I think it should be fine to do it as a part of this patch.

Quoted 9 lines
Quoted 4 lines

I meant 'separately' that checking all return values each. I think it can be simpler without goto.

There are just 2 cases to rollback previous changes in that function.

It might end up with less lines, but having roll back in single place is more readable. That's why it's the preferred error handling way in Linux kernel, if you have more than one action to undo on your error path.

I fully understand what you are concerning about and I also agree. Actually it is simple choice without any side effect. It is just a preference.
I will change it into 'goto'.
Then we can determine what looks better :)

Thank you.

KyongHo.

Quoted 3 lines

Best regards,
Tomasz

maintainer
#7 lore raw
In reply to Tomasz Figa
Quoted 4 lines · Tomasz Figa

Oh, this is a valid point, but it was more a nitpick about the coding style. Single path error handling (with goto) is widely used in the kernel in cases when more than one thing has to be undone and so I suggested this method of error handling here as well.

I agree with that review from Tomasz, the error-handling should look similar to all the other places in the kernel and use goto.

Joerg

#8 lore raw
Quoted 9 lines · Joerg Roedel
Quoted 4 lines · Tomasz Figa

Oh, this is a valid point, but it was more a nitpick about the coding style. Single path error handling (with goto) is widely used in the kernel in cases when more than one thing has to be undone and so I suggested this method of error handling here as well.

I agree with that review from Tomasz, the error-handling should look similar to all the other places in the kernel and use goto.

Ok.

Thanks.

KyongHo.

Quoted 4 lines

Joerg