From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Lars Persson <larper@axis.com>,
linux-mips@linux-mips.org, paul.burton@imgtec.com,
Ralf Baechle <ralf@linux-mips.org>
Subject: [PATCH 4.0 11/72] MIPS: Fix race condition in lazy cache flushing.
Date: Mon, 11 May 2015 10:54:17 -0700 [thread overview]
Message-ID: <20150511175437.449164531@linuxfoundation.org> (raw)
In-Reply-To: <20150511175437.112151861@linuxfoundation.org>
4.0-stable review patch. If anyone has any objections, please let me know.
------------------
From: Lars Persson <lars.persson@axis.com>
Commit 4d46a67a3eb827ccf1125959936fd51ba318dabc upstream.
The lazy cache flushing implemented in the MIPS kernel suffers from a
race condition that is exposed by do_set_pte() in mm/memory.c.
A pre-condition is a file-system that writes to the page from the CPU
in its readpage method and then calls flush_dcache_page(). One example
is ubifs. Another pre-condition is that the dcache flush is postponed
in __flush_dcache_page().
Upon a page fault for an executable mapping not existing in the
page-cache, the following will happen:
1. Write to the page
2. flush_dcache_page
3. flush_icache_page
4. set_pte_at
5. update_mmu_cache (commits the flush of a dcache-dirty page)
Between steps 4 and 5 another thread can hit the same page and it will
encounter a valid pte. Because the data still is in the L1 dcache the CPU
will fetch stale data from L2 into the icache and execute garbage.
This fix moves the commit of the cache flush to step 3 to close the
race window. It also reduces the amount of flushes on non-executable
mappings because we never enter __flush_dcache_page() for non-aliasing
CPUs.
Regressions can occur in drivers that mistakenly relies on the
flush_dcache_page() in get_user_pages() for DMA operations.
[ralf@linux-mips.org: Folded in patch 9346 to fix highmem issue.]
Signed-off-by: Lars Persson <larper@axis.com>
Cc: linux-mips@linux-mips.org
Cc: paul.burton@imgtec.com
Cc: linux-kernel@vger.kernel.org
Patchwork: https://patchwork.linux-mips.org/patch/9346/
Patchwork: https://patchwork.linux-mips.org/patch/9738/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
arch/mips/include/asm/cacheflush.h | 38 ++++++++++++++++++++++---------------
arch/mips/mm/cache.c | 12 +++++++++++
2 files changed, 35 insertions(+), 15 deletions(-)
--- a/arch/mips/include/asm/cacheflush.h
+++ b/arch/mips/include/asm/cacheflush.h
@@ -29,6 +29,20 @@
* - flush_icache_all() flush the entire instruction cache
* - flush_data_cache_page() flushes a page from the data cache
*/
+
+ /*
+ * This flag is used to indicate that the page pointed to by a pte
+ * is dirty and requires cleaning before returning it to the user.
+ */
+#define PG_dcache_dirty PG_arch_1
+
+#define Page_dcache_dirty(page) \
+ test_bit(PG_dcache_dirty, &(page)->flags)
+#define SetPageDcacheDirty(page) \
+ set_bit(PG_dcache_dirty, &(page)->flags)
+#define ClearPageDcacheDirty(page) \
+ clear_bit(PG_dcache_dirty, &(page)->flags)
+
extern void (*flush_cache_all)(void);
extern void (*__flush_cache_all)(void);
extern void (*flush_cache_mm)(struct mm_struct *mm);
@@ -37,13 +51,15 @@ extern void (*flush_cache_range)(struct
unsigned long start, unsigned long end);
extern void (*flush_cache_page)(struct vm_area_struct *vma, unsigned long page, unsigned long pfn);
extern void __flush_dcache_page(struct page *page);
+extern void __flush_icache_page(struct vm_area_struct *vma, struct page *page);
#define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
static inline void flush_dcache_page(struct page *page)
{
- if (cpu_has_dc_aliases || !cpu_has_ic_fills_f_dc)
+ if (cpu_has_dc_aliases)
__flush_dcache_page(page);
-
+ else if (!cpu_has_ic_fills_f_dc)
+ SetPageDcacheDirty(page);
}
#define flush_dcache_mmap_lock(mapping) do { } while (0)
@@ -61,6 +77,11 @@ static inline void flush_anon_page(struc
static inline void flush_icache_page(struct vm_area_struct *vma,
struct page *page)
{
+ if (!cpu_has_ic_fills_f_dc && (vma->vm_flags & VM_EXEC) &&
+ Page_dcache_dirty(page)) {
+ __flush_icache_page(vma, page);
+ ClearPageDcacheDirty(page);
+ }
}
extern void (*flush_icache_range)(unsigned long start, unsigned long end);
@@ -95,19 +116,6 @@ extern void (*flush_icache_all)(void);
extern void (*local_flush_data_cache_page)(void * addr);
extern void (*flush_data_cache_page)(unsigned long addr);
-/*
- * This flag is used to indicate that the page pointed to by a pte
- * is dirty and requires cleaning before returning it to the user.
- */
-#define PG_dcache_dirty PG_arch_1
-
-#define Page_dcache_dirty(page) \
- test_bit(PG_dcache_dirty, &(page)->flags)
-#define SetPageDcacheDirty(page) \
- set_bit(PG_dcache_dirty, &(page)->flags)
-#define ClearPageDcacheDirty(page) \
- clear_bit(PG_dcache_dirty, &(page)->flags)
-
/* Run kernel code uncached, useful for cache probing functions. */
unsigned long run_uncached(void *func);
--- a/arch/mips/mm/cache.c
+++ b/arch/mips/mm/cache.c
@@ -119,6 +119,18 @@ void __flush_anon_page(struct page *page
EXPORT_SYMBOL(__flush_anon_page);
+void __flush_icache_page(struct vm_area_struct *vma, struct page *page)
+{
+ unsigned long addr;
+
+ if (PageHighMem(page))
+ return;
+
+ addr = (unsigned long) page_address(page);
+ flush_data_cache_page(addr);
+}
+EXPORT_SYMBOL_GPL(__flush_icache_page);
+
void __update_cache(struct vm_area_struct *vma, unsigned long address,
pte_t pte)
{
next prev parent reply other threads:[~2015-05-11 17:54 UTC|newest]
Thread overview: 70+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-11 17:54 [PATCH 4.0 00/72] 4.0.3-stable review Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 01/72] bpf: fix 64-bit divide Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 02/72] route: Use ipv4_mtu instead of raw rt_pmtu Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 03/72] mlx4: Fix tx ring affinity_mask creation Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 04/72] cxgb4: Fix MC1 memory offset calculation Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 05/72] net/mlx4_en: Schedule napi when RX buffers allocation fails Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 06/72] ipv4: Missing sk_nulls_node_init() in ping_unhash() Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 07/72] MIPS: BCM63xx: Move bcm63xx_gpio_init() to bcm63xx_register_devices() Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 08/72] MIPS: OCTEON: dma-octeon: fix OHCI USB config check Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 09/72] MIPS: OCTEON: Use correct CSR to soft reset Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 10/72] Revert "MIPS: Remove race window in page fault handling" Greg Kroah-Hartman
2015-05-11 17:54 ` Greg Kroah-Hartman [this message]
2015-05-11 17:54 ` [PATCH 4.0 12/72] MIPS: Octeon: Remove udelay() causing huge IRQ latency Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 13/72] MIPS: OCTEON: fix PCI interrupt mapping for D-Link DSR-1000N Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 14/72] MIPS: Netlogic: Fix for SATA PHY init Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 15/72] MIPS: Kconfig: Fix typo for the r2-to-r6 emulator kernel parameter Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 16/72] MIPS: r4kcache: Use correct base register for MIPS R6 cache flushes Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 17/72] MIPS: asm: spinlock: Fix addiu instruction for R10000_LLSC_WAR case Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 18/72] MIPS: kernel: entry.S: Set correct ISA level for mips_ihb Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 19/72] MIPS: Fix cpu_has_mips_r2_exec_hazard Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 20/72] MIPS: Octeon: Delete override of cpu_has_mips_r2_exec_hazard Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 21/72] Revert "MIPS: Avoid pipeline stalls on some MIPS32R2 cores." Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 22/72] SSB: fix Kconfig dependencies Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 23/72] MIPS: ralink: Fix bad config symbol in PCI makefile Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 24/72] MIPS: ralink: add missing symbol for RALINK_ILL_ACC Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 25/72] MIPS: smp-cps: cpu_set FPU mask if FPU present Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 26/72] MIPS: Kconfig: Disable SMP/CPS for 64-bit Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 28/72] MIPS: asm: elf: Set O32 default FPU flags Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 29/72] MIPS: Makefile: Fix MIPS ASE detection code Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 30/72] ALSA: emux: Fix mutex deadlock at unloading Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 31/72] ALSA: emux: Fix mutex deadlock in OSS emulation Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 32/72] ALSA: emu10k1: Fix card shortname string buffer overflow Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 33/72] ALSA: emu10k1: Emu10k2 32 bit DMA mode Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 36/72] cdc-acm: prevent infinite loop when parsing CDC headers Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 37/72] serial: of-serial: Remove device_type = "serial" registration Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 38/72] serial: xilinx: Use platform_get_irq to get irq description structure Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 39/72] arm64: dma-mapping: always clear allocated buffers Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 40/72] arm64: add missing PAGE_ALIGN() to __dma_free() Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 41/72] usb: chipidea: otg: remove mutex unlock and lock while stop and start role Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 42/72] ASoC: samsung: s3c24xx-i2s: Fix return value check in s3c24xx_iis_dev_probe() Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 43/72] ASoC: tfa9879: Fix return value check in tfa9879_i2c_probe() Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 44/72] ASoC: rt5677: add register patch for PLL Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 45/72] ASoC: dapm: Enable autodisable on SOC_DAPM_SINGLE_TLV_AUTODISABLE Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 46/72] ASoC: rt5677: fixed wrong DMIC ref clock Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 47/72] btrfs: unlock i_mutex after attempting to delete subvolume during send Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 48/72] ACPI / SBS: Enable battery manager when present Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 49/72] tty/serial: at91: maxburst was missing for dma transfers Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 50/72] rbd: end I/O the entire obj_request on error Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 51/72] uas: Allow uas_use_uas_driver to return usb-storage flags Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 52/72] uas: Add US_FL_MAX_SECTORS_240 flag Greg Kroah-Hartman
2015-05-11 17:54 ` [PATCH 4.0 53/72] uas: Set max_sectors_240 quirk for ASM1053 devices Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 54/72] ext4: fix data corruption caused by unwritten and delayed extents Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 55/72] ext4: move check under lock scope to close a race Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 56/72] SCSI: add 1024 max sectors black list flag Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 57/72] 3w-xxxx: fix command completion race Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 58/72] 3w-9xxx: " Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 59/72] 3w-sas: " Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 60/72] drm/radeon: fix ordering of AVI packet setup Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 61/72] drm/radeon: drop dce6_dp_enable Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 62/72] drm/radeon/audio: dont enable packets until the end Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 63/72] drm/radeon: only mark audio as connected if the monitor supports it (v3) Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 64/72] drm/radeon: only enable audio streams if the monitor supports it Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 66/72] drm/radeon: adjust pll when audio is not enabled Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 67/72] drm/radeon: add SI DPM quirk for Sapphire R9 270 Dual-X 2G GDDR5 Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 71/72] hfsplus: dont store special "osx" xattr prefix on-disk Greg Kroah-Hartman
2015-05-11 17:55 ` [PATCH 4.0 72/72] Drivers: hv: vmbus: Dont wait after requesting offers Greg Kroah-Hartman
2015-05-11 20:14 ` [PATCH 4.0 00/72] 4.0.3-stable review Guenter Roeck
2015-05-11 20:16 ` Greg Kroah-Hartman
2015-05-11 23:40 ` Shuah Khan
2015-05-12 4:08 ` Greg Kroah-Hartman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150511175437.449164531@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=larper@axis.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@linux-mips.org \
--cc=paul.burton@imgtec.com \
--cc=ralf@linux-mips.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).