Add the new vma_set_file() function to allow changing
vma->vm_file with the necessary refcount dance.
Signed-off-by: Christian König <[email protected]>
---
drivers/dma-buf/dma-buf.c | 16 +++++-----------
include/linux/mm.h | 2 ++
mm/mmap.c | 16 ++++++++++++++++
3 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 1699a8e309ef..672f3525ba74 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -1163,20 +1163,14 @@ int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
return -EINVAL;
/* readjust the vma */
- get_file(dmabuf->file);
- oldfile = vma->vm_file;
- vma->vm_file = dmabuf->file;
+ oldfile = vma_set_file(vma, dmabuf->file);
vma->vm_pgoff = pgoff;
ret = dmabuf->ops->mmap(dmabuf, vma);
- if (ret) {
- /* restore old parameters on failure */
- vma->vm_file = oldfile;
- fput(dmabuf->file);
- } else {
- if (oldfile)
- fput(oldfile);
- }
+ /* restore old parameters on failure */
+ if (ret)
+ vma_set_file(vma, oldfile);
+
return ret;
}
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 1983e08f5906..398a6fdaad1e 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2688,6 +2688,8 @@ static inline void vma_set_page_prot(struct vm_area_struct *vma)
}
#endif
+struct file *vma_set_file(struct vm_area_struct *vma, struct file *file);
+
#ifdef CONFIG_NUMA_BALANCING
unsigned long change_prot_numa(struct vm_area_struct *vma,
unsigned long start, unsigned long end);
diff --git a/mm/mmap.c b/mm/mmap.c
index 40248d84ad5f..d3c3c510f643 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -136,6 +136,22 @@ void vma_set_page_prot(struct vm_area_struct *vma)
WRITE_ONCE(vma->vm_page_prot, vm_page_prot);
}
+/*
+ * Change backing file, only valid to use during initial VMA setup.
+ */
+struct file *vma_set_file(struct vm_area_struct *vma, struct file *file)
+{
+ if (file)
+ get_file(file);
+
+ swap(vma->vm_file, file);
+
+ if (file)
+ fput(file);
+
+ return file;
+}
+
/*
* Requires inode->i_mapping->i_mmap_rwsem
*/
--
2.17.1
Hi "Christian,
I love your patch! Yet something to improve:
[auto build test ERROR on hnaz-linux-mm/master]
[also build test ERROR on drm-intel/for-linux-next drm-tip/drm-tip linus/master v5.9-rc5 next-20200914]
[cannot apply to tegra-drm/drm/tegra/for-next drm-exynos/exynos-drm-next drm/drm-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Christian-K-nig/drm-shmem-helpers-revert-Redirect-mmap-for-imported-dma-buf/20200914-222921
base: https://github.com/hnaz/linux-mm master
config: h8300-randconfig-r023-20200914 (attached as .config)
compiler: h8300-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=h8300
If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>
All errors (new ones prefixed by >>):
h8300-linux-ld: drivers/dma-buf/dma-buf.o: in function `dma_buf_mmap':
>> drivers/dma-buf/dma-buf.c:1166: undefined reference to `vma_set_file'
>> h8300-linux-ld: drivers/dma-buf/dma-buf.c:1172: undefined reference to `vma_set_file'
h8300-linux-ld: drivers/leds/leds-lp55xx-common.o: in function `devm_led_classdev_register':
include/linux/leds.h:200: undefined reference to `devm_led_classdev_register_ext'
# https://github.com/0day-ci/linux/commit/c558278651bbea7cb67487890a983608764cc7f4
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Christian-K-nig/drm-shmem-helpers-revert-Redirect-mmap-for-imported-dma-buf/20200914-222921
git checkout c558278651bbea7cb67487890a983608764cc7f4
vim +1166 drivers/dma-buf/dma-buf.c
1127
1128
1129 /**
1130 * dma_buf_mmap - Setup up a userspace mmap with the given vma
1131 * @dmabuf: [in] buffer that should back the vma
1132 * @vma: [in] vma for the mmap
1133 * @pgoff: [in] offset in pages where this mmap should start within the
1134 * dma-buf buffer.
1135 *
1136 * This function adjusts the passed in vma so that it points at the file of the
1137 * dma_buf operation. It also adjusts the starting pgoff and does bounds
1138 * checking on the size of the vma. Then it calls the exporters mmap function to
1139 * set up the mapping.
1140 *
1141 * Can return negative error values, returns 0 on success.
1142 */
1143 int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
1144 unsigned long pgoff)
1145 {
1146 struct file *oldfile;
1147 int ret;
1148
1149 if (WARN_ON(!dmabuf || !vma))
1150 return -EINVAL;
1151
1152 /* check if buffer supports mmap */
1153 if (!dmabuf->ops->mmap)
1154 return -EINVAL;
1155
1156 /* check for offset overflow */
1157 if (pgoff + vma_pages(vma) < pgoff)
1158 return -EOVERFLOW;
1159
1160 /* check for overflowing the buffer's size */
1161 if (pgoff + vma_pages(vma) >
1162 dmabuf->size >> PAGE_SHIFT)
1163 return -EINVAL;
1164
1165 /* readjust the vma */
> 1166 oldfile = vma_set_file(vma, dmabuf->file);
1167 vma->vm_pgoff = pgoff;
1168
1169 ret = dmabuf->ops->mmap(dmabuf, vma);
1170 /* restore old parameters on failure */
1171 if (ret)
> 1172 vma_set_file(vma, oldfile);
1173
1174 return ret;
1175
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]
Hi "Christian,
I love your patch! Yet something to improve:
[auto build test ERROR on hnaz-linux-mm/master]
[also build test ERROR on drm-intel/for-linux-next drm-tip/drm-tip linus/master v5.9-rc5 next-20200915]
[cannot apply to tegra-drm/drm/tegra/for-next drm-exynos/exynos-drm-next drm/drm-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Christian-K-nig/drm-shmem-helpers-revert-Redirect-mmap-for-imported-dma-buf/20200914-222921
base: https://github.com/hnaz/linux-mm master
config: sh-allmodconfig (attached as .config)
compiler: sh4-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh
If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>
All errors (new ones prefixed by >>):
sh4-linux-ld: drivers/dma-buf/dma-buf.o: in function `dma_buf_mmap':
>> (.text+0x8dc): undefined reference to `vma_set_file'
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]