1 From 3e471b58fb766ed5b45ff8b560231da4704c946d Mon Sep 17 00:00:00 2001
2 From: Sugizaki Yukimasa <i.can.speak.c.and.basic@gmail.com>
3 Date: Wed, 10 Jan 2018 06:25:51 +0900
4 Subject: [PATCH 156/454] vcsm: Revert to do page-table-walk-based cache
5 manipulating on some ioctl calls
7 On FLUSH, INVALID, CLEAN_INVALID ioctl calls, cache operations based on
8 page table walk were used in case that the buffer of the cache is not
9 pinned. So reverted to do page-table-based cache manipulating.
11 Signed-off-by: Sugizaki Yukimasa <i.can.speak.c.and.basic@gmail.com>
13 drivers/char/broadcom/vc_sm/vmcs_sm.c | 141 ++++++++++++++++++++------
14 1 file changed, 110 insertions(+), 31 deletions(-)
16 --- a/drivers/char/broadcom/vc_sm/vmcs_sm.c
17 +++ b/drivers/char/broadcom/vc_sm/vmcs_sm.c
18 @@ -1256,7 +1256,33 @@ static const struct vm_operations_struct
19 .fault = vcsm_vma_fault,
22 -static int clean_invalid_mem_2d(const void __user *addr,
23 +/* Converts VCSM_CACHE_OP_* to an operating function. */
24 +static void (*cache_op_to_func(const unsigned cache_op))
25 + (const void*, const void*)
28 + case VCSM_CACHE_OP_NOP:
31 + case VCSM_CACHE_OP_INV:
32 + return dmac_inv_range;
34 + case VCSM_CACHE_OP_CLEAN:
35 + return dmac_clean_range;
37 + case VCSM_CACHE_OP_FLUSH:
38 + return dmac_flush_range;
41 + pr_err("[%s]: Invalid cache_op: 0x%08x\n", __func__, cache_op);
47 + * Clean/invalid/flush cache of which buffer is already pinned (i.e. accessed).
49 +static int clean_invalid_contiguous_mem_2d(const void __user *addr,
50 const size_t block_count, const size_t block_size, const size_t stride,
51 const unsigned cache_op)
53 @@ -1268,22 +1294,9 @@ static int clean_invalid_mem_2d(const vo
58 - case VCSM_CACHE_OP_NOP:
60 - case VCSM_CACHE_OP_INV:
61 - op_fn = dmac_inv_range;
63 - case VCSM_CACHE_OP_CLEAN:
64 - op_fn = dmac_clean_range;
66 - case VCSM_CACHE_OP_FLUSH:
67 - op_fn = dmac_flush_range;
70 - pr_err("[%s]: Invalid cache_op: 0x%08x\n", __func__, cache_op);
71 + op_fn = cache_op_to_func(cache_op);
76 for (i = 0; i < block_count; i ++, addr += stride)
77 op_fn(addr, addr + block_size);
78 @@ -1291,14 +1304,73 @@ static int clean_invalid_mem_2d(const vo
82 -static int clean_invalid_mem(const void __user *addr, const size_t size,
83 +/* Clean/invalid/flush cache of which buffer may be non-pinned. */
84 +/* The caller must lock current->mm->mmap_sem for read. */
85 +static int clean_invalid_mem_walk(unsigned long addr, const size_t size,
86 const unsigned cache_op)
88 - return clean_invalid_mem_2d(addr, 1, size, 0, cache_op);
93 + unsigned long pgd_next, pud_next, pmd_next;
94 + const unsigned long end = ALIGN(addr + size, PAGE_SIZE);
95 + void (*op_fn)(const void*, const void*);
102 + op_fn = cache_op_to_func(cache_op);
107 + pgd = pgd_offset(current->mm, addr);
109 + pgd_next = pgd_addr_end(addr, end);
111 + if (pgd_none(*pgd) || pgd_bad(*pgd))
115 + pud = pud_offset(pgd, addr);
117 + pud_next = pud_addr_end(addr, pgd_next);
118 + if (pud_none(*pud) || pud_bad(*pud))
122 + pmd = pmd_offset(pud, addr);
124 + pmd_next = pmd_addr_end(addr, pud_next);
125 + if (pmd_none(*pmd) || pmd_bad(*pmd))
129 + pte = pte_offset_map(pmd, addr);
131 + if (pte_none(*pte) || !pte_present(*pte))
134 + op_fn((const void __user*) addr,
135 + (const void __user*) (addr + PAGE_SIZE));
136 + } while (pte++, addr += PAGE_SIZE, addr != pmd_next);
139 + } while (pmd++, addr = pmd_next, addr != pud_next);
141 + } while (pud++, addr = pud_next, addr != pgd_next);
143 + } while (pgd++, addr = pgd_next, addr != end);
148 -static int clean_invalid_resource(const void __user *addr, const size_t size,
149 - const unsigned cache_op, const int usr_hdl,
150 +/* Clean/invalid/flush cache of buffer in resource */
151 +static int clean_invalid_resource_walk(const void __user *addr,
152 + const size_t size, const unsigned cache_op, const int usr_hdl,
153 struct sm_resource_t *resource)
156 @@ -1355,7 +1427,10 @@ static int clean_invalid_resource(const
160 - err = clean_invalid_mem(addr, size, cache_op);
161 + down_read(¤t->mm->mmap_sem);
162 + err = clean_invalid_mem_walk((unsigned long) addr, size, cache_op);
163 + up_read(¤t->mm->mmap_sem);
166 resource->res_stats[stat_failure]++;
168 @@ -2004,7 +2079,7 @@ static int vc_sm_ioctl_unlock(struct sm_
169 const unsigned long start = map->vma->vm_start;
170 const unsigned long end = map->vma->vm_end;
172 - ret = clean_invalid_mem((void __user*) start, end - start,
173 + ret = clean_invalid_mem_walk(start, end - start,
174 VCSM_CACHE_OP_FLUSH);
177 @@ -2886,7 +2961,7 @@ static long vc_sm_ioctl(struct file *fil
181 - ret = clean_invalid_resource((void __user*) ioparam.addr,
182 + ret = clean_invalid_resource_walk((void __user*) ioparam.addr,
183 ioparam.size, VCSM_CACHE_OP_FLUSH, ioparam.handle,
185 vmcs_sm_release_resource(resource, 0);
186 @@ -2917,7 +2992,7 @@ static long vc_sm_ioctl(struct file *fil
190 - ret = clean_invalid_resource((void __user*) ioparam.addr,
191 + ret = clean_invalid_resource_walk((void __user*) ioparam.addr,
192 ioparam.size, VCSM_CACHE_OP_INV, ioparam.handle, resource);
193 vmcs_sm_release_resource(resource, 0);
195 @@ -2951,16 +3026,19 @@ static long vc_sm_ioctl(struct file *fil
199 - ret = clean_invalid_resource((void __user*) ioparam.s[i].addr,
200 - ioparam.s[i].size, ioparam.s[i].cmd,
201 - ioparam.s[i].handle, resource);
202 + ret = clean_invalid_resource_walk(
203 + (void __user*) ioparam.s[i].addr, ioparam.s[i].size,
204 + ioparam.s[i].cmd, ioparam.s[i].handle, resource);
205 vmcs_sm_release_resource(resource, 0);
211 - /* Flush/Invalidate the cache for a given mapping. */
213 + * Flush/Invalidate the cache for a given mapping.
214 + * Blocks must be pinned (i.e. accessed) before this call.
216 case VMCS_SM_CMD_CLEAN_INVALID2:
219 @@ -2993,12 +3071,13 @@ static long vc_sm_ioctl(struct file *fil
220 for (i = 0; i < ioparam.op_count; i++) {
221 const struct vmcs_sm_ioctl_clean_invalid_block * const op = block + i;
223 - ret = clean_invalid_mem_2d((void __user*) op->start_address,
224 - op->block_count, op->block_size,
225 - op->inter_block_stride, op->invalidate_mode);
226 if (op->invalidate_mode == VCSM_CACHE_OP_NOP)
229 + ret = clean_invalid_contiguous_mem_2d(
230 + (void __user*) op->start_address, op->block_count,
231 + op->block_size, op->inter_block_stride,
232 + op->invalidate_mode);