diff mbox series

[RFC] mm: block swap in mtd partition

Message ID CP4P284MB0802F5877A519540C6BB9FE9C43B9@CP4P284MB0802.BRAP284.PROD.OUTLOOK.COM (mailing list archive)
State New
Headers show
Series [RFC] mm: block swap in mtd partition | expand

Commit Message

Lucas Martins Alves Nov. 4, 2022, 11:28 a.m. UTC
Hello,

I'm enabling swap in kernel version 4.9.206 for an embedded system, however I want users of that kernel to not be able to use mtd partitions for swap, so I made the change below. Is there a better way to do this within the kernel? Can this change have undesirable effects on the kernel?
diff mbox series

Patch

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 855f62ab8c1b..92fed4eb7a97 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2450,6 +2450,22 @@  SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
      mapping = swap_file->f_mapping;
      inode = mapping->host;

+     if( S_ISBLK(inode->i_mode) )
+           if( imajor(inode) == MTD_BLOCK_MAJOR )
+           {
+                 pr_warn("It is not allowed to use internal flash partitions for swap.\n");
+                 error = -EINVAL;
+                 goto bad_swap;
+           }
+
+     if( S_ISREG(inode->i_mode) )
+           if( MAJOR(inode->i_sb->s_dev) == MTD_BLOCK_MAJOR )
+           {
+                 pr_warn("It is not allowed to use files in internal flash for swap.\n");
+                 error = -EINVAL;
+                 goto bad_swap;
+           }
+
      /* If S_ISREG(inode->i_mode) will do inode_lock(inode); */
      error = claim_swapfile(p, inode);
      if (unlikely(error))