[PATCH 01/12] mm/shmem: add `const` to lots of pointer parameters

Max Kellermann posted 12 patches 1 month ago
There is a newer version of this series
[PATCH 01/12] mm/shmem: add `const` to lots of pointer parameters
Posted by Max Kellermann 1 month ago
For improved const-correctness.

Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
 include/linux/mm.h       | 8 ++++----
 include/linux/shmem_fs.h | 4 ++--
 mm/shmem.c               | 6 +++---
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 00c8a54127d3..a40a3c42c904 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -979,11 +979,11 @@ static inline void vma_iter_set(struct vma_iterator *vmi, unsigned long addr)
  * The vma_is_shmem is not inline because it is used only by slow
  * paths in userfault.
  */
-bool vma_is_shmem(struct vm_area_struct *vma);
-bool vma_is_anon_shmem(struct vm_area_struct *vma);
+bool vma_is_shmem(const struct vm_area_struct *vma);
+bool vma_is_anon_shmem(const struct vm_area_struct *vma);
 #else
-static inline bool vma_is_shmem(struct vm_area_struct *vma) { return false; }
-static inline bool vma_is_anon_shmem(struct vm_area_struct *vma) { return false; }
+static inline bool vma_is_shmem(const struct vm_area_struct *vma) { return false; }
+static inline bool vma_is_anon_shmem(const struct vm_area_struct *vma) { return false; }
 #endif
 
 int vma_is_stack_for_current(struct vm_area_struct *vma);
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index 6d0f9c599ff7..0e47465ef0fd 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -99,9 +99,9 @@ extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
 		unsigned long len, unsigned long pgoff, unsigned long flags);
 extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);
 #ifdef CONFIG_SHMEM
-bool shmem_mapping(struct address_space *mapping);
+bool shmem_mapping(const struct address_space *mapping);
 #else
-static inline bool shmem_mapping(struct address_space *mapping)
+static inline bool shmem_mapping(const struct address_space *mapping)
 {
 	return false;
 }
diff --git a/mm/shmem.c b/mm/shmem.c
index 13cc51df3893..2a4476e223a3 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -275,18 +275,18 @@ static const struct vm_operations_struct shmem_vm_ops;
 static const struct vm_operations_struct shmem_anon_vm_ops;
 static struct file_system_type shmem_fs_type;
 
-bool shmem_mapping(struct address_space *mapping)
+bool shmem_mapping(const struct address_space *mapping)
 {
 	return mapping->a_ops == &shmem_aops;
 }
 EXPORT_SYMBOL_GPL(shmem_mapping);
 
-bool vma_is_anon_shmem(struct vm_area_struct *vma)
+bool vma_is_anon_shmem(const struct vm_area_struct *vma)
 {
 	return vma->vm_ops == &shmem_anon_vm_ops;
 }
 
-bool vma_is_shmem(struct vm_area_struct *vma)
+bool vma_is_shmem(const struct vm_area_struct *vma)
 {
 	return vma_is_anon_shmem(vma) || vma->vm_ops == &shmem_vm_ops;
 }
-- 
2.47.2
Re: [PATCH 01/12] mm/shmem: add `const` to lots of pointer parameters
Posted by Vishal Moola (Oracle) 1 month ago
On Fri, Aug 29, 2025 at 08:31:48PM +0200, Max Kellermann wrote:
> For improved const-correctness.
> 
> Signed-off-by: Max Kellermann <max.kellermann@ionos.com>

Reviewed-by: Vishal Moola (Oracle) <vishal.moola@gmail.com>
Re: [PATCH 01/12] mm/shmem: add `const` to lots of pointer parameters
Posted by Yuanchu Xie 1 month ago
On Fri, Aug 29, 2025 at 1:32 PM Max Kellermann <max.kellermann@ionos.com> wrote:
>
> For improved const-correctness.
>
> Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
Reviewed-by: Yuanchu Xie <yuanchu@google.com>

> ...
>-bool shmem_mapping(struct address_space *mapping)
>+bool shmem_mapping(const struct address_space *mapping)
>{
>       return mapping->a_ops == &shmem_aops;
>}
>EXPORT_SYMBOL_GPL(shmem_mapping);
The exported symbol is being changed, but this doesn't seem like it
would break anything.

Appreciate the work. On a side note, Andrew previously mentioned[1]
making the actual parameter value const (which is different from
adding the const qualifier to the pointer). Longer function
readability would benefit from that, but it's IMO infeasible to do so
everywhere.

[1] https://lore.kernel.org/lkml/20250827144832.87d2f1692fe61325628710f4@linux-foundation.org/#r
Yuanchu
Re: [PATCH 01/12] mm/shmem: add `const` to lots of pointer parameters
Posted by Vishal Moola (Oracle) 1 month ago
On Fri, Aug 29, 2025 at 03:44:12PM -0500, Yuanchu Xie wrote:
> On Fri, Aug 29, 2025 at 1:32 PM Max Kellermann <max.kellermann@ionos.com> wrote:
> >
> > For improved const-correctness.
> >
> > Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
> Reviewed-by: Yuanchu Xie <yuanchu@google.com>
> 
> Appreciate the work. On a side note, Andrew previously mentioned[1]
> making the actual parameter value const (which is different from
> adding the const qualifier to the pointer). Longer function
> readability would benefit from that, but it's IMO infeasible to do so
> everywhere.

+1

> [1] https://lore.kernel.org/lkml/20250827144832.87d2f1692fe61325628710f4@linux-foundation.org/#r

Imo the lack of these value const's isn't a blocker, but it'd be nice to
do that "For improved const-correctness". Especially when you're changing the
line anyways (see mapping_tagged() or folio_contains()).