Skip to content

Commit f2d5776

Browse files
xp4ns3gregkh
authored andcommitted
firmware_loader: Replace kmap() with kmap_local_page()
The use of kmap() is being deprecated in favor of kmap_local_page(). Two main problems with kmap(): (1) It comes with an overhead as mapping space is restricted and protected by a global lock for synchronization and (2) kmap() also requires global TLB invalidation when the kmap’s pool wraps and it might block when the mapping space is fully utilized until a slot becomes available. kmap_local_page() is preferred over kmap() and kmap_atomic(). Where it cannot mechanically replace the latters, code refactor should be considered (special care must be taken if kernel virtual addresses are aliases in different contexts). With kmap_local_page() the mappings are per thread, CPU local, can take page faults, and can be called from any context (including interrupts). Call kmap_local_page() in firmware_loader wherever kmap() is currently used. In firmware_rw() use the helpers copy_{from,to}_page() instead of open coding the local mappings + memcpy(). Successfully tested with "firmware" selftests on a QEMU/KVM 32-bits VM with 4GB RAM, booting a kernel with HIGHMEM64GB enabled. Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Luis Chamberlain <mcgrof@kernel.org> Suggested-by: Ira Weiny <ira.weiny@intel.com> Reviewed-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com> Link: https://lore.kernel.org/r/20220714235030.12732-1-fmdefrancesco@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b18ee4a commit f2d5776

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

drivers/base/firmware_loader/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,11 @@ static int fw_decompress_xz_pages(struct device *dev, struct fw_priv *fw_priv,
435435

436436
/* decompress onto the new allocated page */
437437
page = fw_priv->pages[fw_priv->nr_pages - 1];
438-
xz_buf.out = kmap(page);
438+
xz_buf.out = kmap_local_page(page);
439439
xz_buf.out_pos = 0;
440440
xz_buf.out_size = PAGE_SIZE;
441441
xz_ret = xz_dec_run(xz_dec, &xz_buf);
442-
kunmap(page);
442+
kunmap_local(xz_buf.out);
443443
fw_priv->size += xz_buf.out_pos;
444444
/* partial decompression means either end or error */
445445
if (xz_buf.out_pos != PAGE_SIZE)

drivers/base/firmware_loader/sysfs.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,17 @@ static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
242242
loff_t offset, size_t count, bool read)
243243
{
244244
while (count) {
245-
void *page_data;
246245
int page_nr = offset >> PAGE_SHIFT;
247246
int page_ofs = offset & (PAGE_SIZE - 1);
248247
int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
249248

250-
page_data = kmap(fw_priv->pages[page_nr]);
251-
252249
if (read)
253-
memcpy(buffer, page_data + page_ofs, page_cnt);
250+
memcpy_from_page(buffer, fw_priv->pages[page_nr],
251+
page_ofs, page_cnt);
254252
else
255-
memcpy(page_data + page_ofs, buffer, page_cnt);
253+
memcpy_to_page(fw_priv->pages[page_nr], page_ofs,
254+
buffer, page_cnt);
256255

257-
kunmap(fw_priv->pages[page_nr]);
258256
buffer += page_cnt;
259257
offset += page_cnt;
260258
count -= page_cnt;

0 commit comments

Comments
 (0)