Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3131335pxj; Mon, 17 May 2021 18:36:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTFo0ylsltV9UpIxFYWYXArbj7nFAlnNFIo3uC5e4JDIQDc2SSN/Yw/tbpFCqzkrerp8nP X-Received: by 2002:a05:6e02:20c2:: with SMTP id 2mr2283516ilq.120.1621301791368; Mon, 17 May 2021 18:36:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621301791; cv=none; d=google.com; s=arc-20160816; b=JupUYiUfSxvhndJF6pq70LTyj6TjpfYqB8lK4hDnAbRauIWLqP+HLdSn8KViZ9LrqX Z3CgTjn0r8wydy37AltgjUKBaPsaIG6LNvh15V1GnMv/7/AriWA52xkM5ViXrRCV8Z53 4cMWOp7dgigbOk2M6GaZNEcmNik1eEJAwU0EYcTT9jMylo+WXH8fQDciRUSOpD+njJdb ecw4KXA5jvaw7TAZRgUbWdhzKi/t1Rb+hVvWtVHXZZuH4eX/PGGdVvzr1orP9JHS0RKk ZxxTygSLw/wFd0031jNtAXGrnBpfA3w85EVtAIFjyEpnKnyvzJT7f4EHX1wnoyKQXfQ2 GyPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=hzrl4z2QMxvZCTonFqYi0sIC+xfYQC5jGAjMPf72fo4=; b=Hx6PujB4N8aT0PhhyhcVETzYVEzimPMU1hoczkgd+04AJvfk1MwLxUVeClPacw9ksL Hy1IhJWN8M5uul0JvvI33orTQp1YmVNa/Bc2mAOeKz7FS+uLmchZfoix0D/ODatNlAlR G7GEZFYJAokMSAy1dpVUZzsdY1FcZsDVSIe21/0zh6Bws5VSAlpSnfKBEFk/hLwRSBme cwAjvszQJcM9nZbNKW9rkVWvoZ7XUcaIv20KIiKTeW8lDFFWpUpdWxy6iJNSComSwAgx tVdy3l3MODru9U7sFN1e0M9akbhmh+Y8veDiyhLHniSJbVdupakk4ItqTb/SOkLDK6Wv 3+fA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ePOtFEGy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z8si18579834ilh.46.2021.05.17.18.36.18; Mon, 17 May 2021 18:36:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ePOtFEGy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240693AbhEQOfE (ORCPT + 99 others); Mon, 17 May 2021 10:35:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:43786 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239997AbhEQO3u (ORCPT ); Mon, 17 May 2021 10:29:50 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id AE7A7611ED; Mon, 17 May 2021 14:14:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621260900; bh=pnKSpHyz9pQmxPkhijylRsJgz8hvUcVRIQ3sz0mB/KM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ePOtFEGyoDUHqOWrqAplyR+cvsM2ekRaqkIzLL+F47VkCGw2nxxBW+dSp2vkJoLJr 3v6hErZin0UTRsfznlkaj3GlFGiwumLRI4p7oPfzyQ0ZOB3RQhWhgW9Z1IN6zxm02S ELxeUu9bqgou9FrekNFqXgNsZlFMJ4E2DNhHRpww= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Catalin Marinas , Will Deacon , Steven Price Subject: [PATCH 5.12 263/363] arm64: Fix race condition on PG_dcache_clean in __sync_icache_dcache() Date: Mon, 17 May 2021 16:02:09 +0200 Message-Id: <20210517140311.497540449@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140302.508966430@linuxfoundation.org> References: <20210517140302.508966430@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Catalin Marinas commit 588a513d34257fdde95a9f0df0202e31998e85c6 upstream. To ensure that instructions are observable in a new mapping, the arm64 set_pte_at() implementation cleans the D-cache and invalidates the I-cache to the PoU. As an optimisation, this is only done on executable mappings and the PG_dcache_clean page flag is set to avoid future cache maintenance on the same page. When two different processes map the same page (e.g. private executable file or shared mapping) there's a potential race on checking and setting PG_dcache_clean via set_pte_at() -> __sync_icache_dcache(). While on the fault paths the page is locked (PG_locked), mprotect() does not take the page lock. The result is that one process may see the PG_dcache_clean flag set but the I/D cache maintenance not yet performed. Avoid test_and_set_bit(PG_dcache_clean) in favour of separate test_bit() and set_bit(). In the rare event of a race, the cache maintenance is done twice. Signed-off-by: Catalin Marinas Cc: Cc: Will Deacon Cc: Steven Price Reviewed-by: Steven Price Acked-by: Will Deacon Link: https://lore.kernel.org/r/20210514095001.13236-1-catalin.marinas@arm.com Signed-off-by: Catalin Marinas Signed-off-by: Greg Kroah-Hartman --- arch/arm64/mm/flush.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) --- a/arch/arm64/mm/flush.c +++ b/arch/arm64/mm/flush.c @@ -55,8 +55,10 @@ void __sync_icache_dcache(pte_t pte) { struct page *page = pte_page(pte); - if (!test_and_set_bit(PG_dcache_clean, &page->flags)) + if (!test_bit(PG_dcache_clean, &page->flags)) { sync_icache_aliases(page_address(page), page_size(page)); + set_bit(PG_dcache_clean, &page->flags); + } } EXPORT_SYMBOL_GPL(__sync_icache_dcache);