Skip to contentSkip to conversation

a few cleanup and bugfixes about shmem

PATCH Superseded Chen Wandun 4 participants 10 messages Jun 1, 2022 – Jun 5, 2022

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover a few cleanup and bugfixes about shmem

Chen Wandun (4):
mm/shmem: check return value of shmem_init_inodecache mm/shmem: return -EINVAL for addr not PAGE_SIZE aligned mm/shmem: return error code directly
mm/shmem: rework calculation of inflated_addr in
shmem_get_unmapped_area

 mm/shmem.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 mm/shmem: check return value of shmem_init_inodecache

It will result in null pointer access if shmem_init_inodecache fail, so check return value of shmem_init_inodecache

---
 mm/shmem.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
mm/shmem.c +7 -2
index d55dd972023a..80c361c3d82c 100644
@@ -3776,11 +3776,13 @@ static void shmem_init_inode(void *foo)
 	inode_init_once(&info->vfs_inode);
 }
 
-static void shmem_init_inodecache(void)
+static struct kmem_cache *shmem_init_inodecache(void)
 {
 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
 				sizeof(struct shmem_inode_info),
 				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
+
+	return shmem_inode_cachep;
 }
 
 static void shmem_destroy_inodecache(void)
@@ -3924,7 +3926,10 @@ void __init shmem_init(void)
 {
 	int error;
 
-	shmem_init_inodecache();
+	if (!shmem_init_inodecache()) {
+		error = -ENOMEM;
+		goto out2;
+	}
 
 	error = register_filesystem(&shmem_fs_type);
 	if (error) {
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/4] mm/shmem: check return value of shmem_init_inodecache

Quoted 12 lines · Chen Wandun
mm/shmem.c +3 -1
-static void shmem_init_inodecache(void)
+static struct kmem_cache *shmem_init_inodecache(void)
 {
 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
 				sizeof(struct shmem_inode_info),
 				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
+
+	return shmem_inode_cachep;
 }
 
 static void shmem_destroy_inodecache(void)

mm/shmem.c void __init shmem_init(void) · line 3932

Quoted 8 lines
mm/shmem.c +4 -1
@@ -3924,7 +3926,10 @@ void __init shmem_init(void)
 {
 	int error;
 
-	shmem_init_inodecache();
+	if (!shmem_init_inodecache()) {
+		error = -ENOMEM;
+		goto out2;
+	}

better to return the errno from shmem_init_inodecache():

	error = shmem_init_inodecache();
	if (error)
		goto out2;
#3 lore raw

Re: [PATCH 1/4] mm/shmem: check return value of shmem_init_inodecache

Quoted 16 lines · Matthew Wilcox
Quoted 15 lines · Chen Wandun
mm/shmem.c +3 -1
-static void shmem_init_inodecache(void)
+static struct kmem_cache *shmem_init_inodecache(void)
  {
  	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  				sizeof(struct shmem_inode_info),
  				0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
+
+	return shmem_inode_cachep;
  }
 
  static void shmem_destroy_inodecache(void)
@@ -3924,7 +3926,10 @@ void __init shmem_init(void)
  {
  	int error;
 
Quoted 8 lines

mm/shmem.c void __init shmem_init(void) · line 3932

Quoted 5 lines
mm/shmem.c +4 -1
-	shmem_init_inodecache();
+	if (!shmem_init_inodecache()) {
+		error = -ENOMEM;
+		goto out2;
+	}

better to return the errno from shmem_init_inodecache():

	error = shmem_init_inodecache();

kmem_cache_create return a pointer to the cache on success, NULL on failure, so error = -ENOMEM; is right :)

Quoted 4 lines
	if (error)
		goto out2;

.

#4 lore raw

PATCH 2/4 mm/shmem: return -EINVAL for addr not PAGE_SIZE aligned

In reply to Chen Wandun

If addr is not PAGE_SIZE aligned, return -EINVAL directly. Besides, use macro offset_in_page to check addr is not PAGE_SIZE aligned case.

---
 mm/shmem.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/shmem.c +3 -3
index 80c361c3d82c..1136dd7da9e5 100644
@@ -2143,8 +2143,8 @@ unsigned long shmem_get_unmapped_area(struct file *file,
 		return addr;
 	if (IS_ERR_VALUE(addr))
 		return addr;
-	if (addr & ~PAGE_MASK)
-		return addr;
+	if (offset_in_page(addr))
+		return -EINVAL;
 	if (addr > TASK_SIZE - len)
 		return addr;
 
@@ -2197,7 +2197,7 @@ unsigned long shmem_get_unmapped_area(struct file *file,
 	inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags);
 	if (IS_ERR_VALUE(inflated_addr))
 		return addr;
-	if (inflated_addr & ~PAGE_MASK)
+	if (offset_in_page(inflated_addr))
 		return addr;
 
 	inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH 2/4] mm/shmem: return -EINVAL for addr not PAGE_SIZE aligned

Quoted 1 line · Chen Wandun

If addr is not PAGE_SIZE aligned, return -EINVAL directly.

Why is this one to be treated in a special way compared to all of the other related checks?

Quoted 2 lines

Besides, use macro offset_in_page to check addr is not PAGE_SIZE aligned case.

Using offset_in_page() LGTM.

Quoted 30 lines
---
 mm/shmem.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/shmem.c +3 -3
index 80c361c3d82c..1136dd7da9e5 100644
@@ -2143,8 +2143,8 @@ unsigned long shmem_get_unmapped_area(struct file *file,
 		return addr;
 	if (IS_ERR_VALUE(addr))
 		return addr;
-	if (addr & ~PAGE_MASK)
-		return addr;
+	if (offset_in_page(addr))
+		return -EINVAL;
 	if (addr > TASK_SIZE - len)
 		return addr;
 
@@ -2197,7 +2197,7 @@ unsigned long shmem_get_unmapped_area(struct file *file,
 	inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags);
 	if (IS_ERR_VALUE(inflated_addr))
 		return addr;
-	if (inflated_addr & ~PAGE_MASK)
+	if (offset_in_page(inflated_addr))
 		return addr;
 
 	inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
Signature · 3 lines
Thanks,

David / dhildenb
#6 lore raw

Re: [PATCH 2/4] mm/shmem: return -EINVAL for addr not PAGE_SIZE aligned

Quoted 5 lines · David Hildenbrand
Quoted 1 line · Chen Wandun

If addr is not PAGE_SIZE aligned, return -EINVAL directly.

Why is this one to be treated in a special way compared to all of the other related checks?

Ah, I see you modify other places in other patches. Maybe just combine all these return value changes into a single patch? That makes it look less "special".

Signature · 3 lines
Thanks,

David / dhildenb
#7 lore raw

Re: [PATCH 2/4] mm/shmem: return -EINVAL for addr not PAGE_SIZE aligned

Quoted 8 lines · David Hildenbrand
Quoted 4 lines · David Hildenbrand
Quoted 1 line · Chen Wandun

If addr is not PAGE_SIZE aligned, return -EINVAL directly.

Why is this one to be treated in a special way compared to all of the other related checks?

Ah, I see you modify other places in other patches. Maybe just combine all these return value changes into a single patch? That makes it look less "special".

OK, patch 2 and patch 3 are used to return error value directly, I will combine these patch to a single patch in next version.

Thanks.

Quoted 3 lines
#8 lore raw

PATCH 3/4 mm/shmem: return error code directly

In reply to Chen Wandun

If [addr, addr + len) beyond TASK_SIZE, return error code directly. No need to check this case in caller.

---
 mm/shmem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/shmem.c +1 -1
index 1136dd7da9e5..ca04f3975a8a 100644
@@ -2146,7 +2146,7 @@ unsigned long shmem_get_unmapped_area(struct file *file,
 	if (offset_in_page(addr))
 		return -EINVAL;
 	if (addr > TASK_SIZE - len)
-		return addr;
+		return -ENOMEM;
 
 	if (shmem_huge == SHMEM_HUGE_DENY)
 		return addr;
Signature · 1 line
2.25.1
#9 lore raw

PATCH 4/4 mm/shmem: rework calculation of inflated_addr in shmem_get_unmapped_area

In reply to Chen Wandun

In function shmem_get_unmapped_area, inflated_offset and offset are unsigned long, it will result in underflow when offset below inflated_offset, a little confusing, no functional change.

---
 mm/shmem.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/shmem.c +5 -2
index ca04f3975a8a..f12163bd0f69 100644
@@ -2201,9 +2201,12 @@ unsigned long shmem_get_unmapped_area(struct file *file,
 		return addr;
 
 	inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
-	inflated_addr += offset - inflated_offset;
-	if (inflated_offset > offset)
+	if (offset > inflated_offset)
+		inflated_addr += offset - inflated_offset;
+	else if (offset < inflated_offset) {
+		inflated_addr -= inflated_offset - offset;
 		inflated_addr += HPAGE_PMD_SIZE;
+	}
 
 	if (inflated_addr > TASK_SIZE - len)
 		return addr;
Signature · 1 line
2.25.1