Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1735004rdg; Sat, 14 Oct 2023 15:26:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFioOAdmRiM9TFcqaII2neMMxkYIaUQyvLQWnLDr/rfRzJnCdEStZWxInrPPySef92FSmAo X-Received: by 2002:a81:4ed6:0:b0:5a7:ba17:15ac with SMTP id c205-20020a814ed6000000b005a7ba1715acmr16184769ywb.41.1697322376436; Sat, 14 Oct 2023 15:26:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697322376; cv=none; d=google.com; s=arc-20160816; b=j4LxMwDbKF7tjWxaNooGPP8aWAhvtTkW34k6RiqSoecqi3DMjkK1C2PRwqmDHNiW6R X8VdKPoX8ZHyVaPRZEYXV46HyNlDbC+rlAN7qoScOPUHZMsMl4PWecO2bsN8pLP0wW37 NAKxbtuDB5kQCwE5xSfU6gOO6qoO23GKHsdnPp3ZymMJoKrnEATzco0BghV0Yz9yzJ6K p+s2fpYPy7x2NEzGB/CTBGfJIFBHArhjLKKpLpXqEcDFjd8/YA4k9Kh5v9ZssQO8OVJT F7GZrAjWBl0IEFpctxzBIlDbJ23OBXx+4ufszpGqHR/CnY7vAROyAg1+ZmaXg9sBdoWm OUGQ== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=IHN9ShU+67DVGl3KhwsMlAJsXh4v2A8r732M+gTXbqc=; fh=Om5/TqpEMqGhcqCtjmI3FBo0gR5f/NJxQcXmL6ynlUs=; b=Y9zgKhKCKIf2NSKQhJ4cFZiYOwrNUi0J1oIHScrZAanHbWw7gIR5JvzJeBMOPn7zOT nsjT3PmYWpkxmbcs83PXbMpsn8orvHvzuqxS6PA5RGpfOTgoeZN7Ea9zJ9Cs66XUCTSE Rq3M7phTDbNeYTAQ/FnaQfiopEDHvapDjDz+TYhktBzMBPTj1jr7lsMxykNP5uYpdCmr gp9NMpeKCJkzUXjMCNW8sqzLL5n7J2vnafPXICXbhbEeR3gwSaEiIWhTlNZn78B9HwEU mrm6xtWsIt9+EYjAiBDUl7gKc2X7KB2bAn+DPSCEvFH+pDfKYAyBpy58zc5OW1fYkN/q LVBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=eg3LLDdu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id mh17-20020a17090b4ad100b00279866aa14csi3406287pjb.16.2023.10.14.15.26.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Oct 2023 15:26:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=eg3LLDdu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id F2CD580968A9; Sat, 14 Oct 2023 15:25:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229867AbjJNWZo (ORCPT + 99 others); Sat, 14 Oct 2023 18:25:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229735AbjJNWZo (ORCPT ); Sat, 14 Oct 2023 18:25:44 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB7D6DA for ; Sat, 14 Oct 2023 15:25:42 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 677B5C433C7; Sat, 14 Oct 2023 22:25:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1697322342; bh=DVFXu995Ek/eydKdbc/Uxs7E/ribT7x6PLE1LvWtWMs=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=eg3LLDduGAPpZPmH+cwbNSSyYRkJV++4EQMhHvF2yYXau3PoRoqK881SxtlKd1zyF waCMDDEMx8joqUXDlJZcLXCmOyG3PY1S9ww0g5hYFh5BQ7RkHc6WqHt7oA1cQz5fS7 iHZrAoYDVmAd0THWA9jzKLQ3A6zRH38Ph+JNYzjQ= Date: Sat, 14 Oct 2023 15:25:32 -0700 From: Andrew Morton To: Charan Teja Kalla Cc: , , , , , , , Subject: Re: [PATCH] mm/sparsemem: fix race in accessing memory_section->usage Message-Id: <20231014152532.5f3dca7838c2567a1a9ca9c6@linux-foundation.org> In-Reply-To: <1697202267-23600-1-git-send-email-quic_charante@quicinc.com> References: <1697202267-23600-1-git-send-email-quic_charante@quicinc.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Sat, 14 Oct 2023 15:25:53 -0700 (PDT) On Fri, 13 Oct 2023 18:34:27 +0530 Charan Teja Kalla wrote: > The below race is observed on a PFN which falls into the device memory > region with the system memory configuration where PFN's are such that > [ZONE_NORMAL ZONE_DEVICE ZONE_NORMAL]. Since normal zone start and > end pfn contains the device memory PFN's as well, the compaction > triggered will try on the device memory PFN's too though they end up in > NOP(because pfn_to_online_page() returns NULL for ZONE_DEVICE memory > sections). When from other core, the section mappings are being removed > for the ZONE_DEVICE region, that the PFN in question belongs to, > on which compaction is currently being operated is resulting into the > kernel crash with CONFIG_SPASEMEM_VMEMAP enabled. Seems this bug is four years old, yes? It must be quite hard to hit. When people review this, please offer opinions on whether a fix should be backported into -stable kernels, thanks. > compact_zone() memunmap_page > ------------- --------------- > __pageblock_pfn_to_page > ...... > (a)pfn_valid(): > valid_section()//return true > (b)__remove_pages()-> > sparse_remove_section()-> > section_deactivate(): > [Free the array ms->usage and set > ms->usage = NULL] > pfn_section_valid() > [Access ms->usage which > is NULL] > > NOTE: From the above it can be said that the race is reduced to between > the pfn_valid()/pfn_section_valid() and the section deactivate with > SPASEMEM_VMEMAP enabled. > > The commit b943f045a9af("mm/sparse: fix kernel crash with > pfn_section_valid check") tried to address the same problem by clearing > the SECTION_HAS_MEM_MAP with the expectation of valid_section() returns > false thus ms->usage is not accessed. > > Fix this issue by the below steps: > a) Clear SECTION_HAS_MEM_MAP before freeing the ->usage. > b) RCU protected read side critical section will either return NULL when > SECTION_HAS_MEM_MAP is cleared or can successfully access ->usage. > c) Synchronize the rcu on the write side and free the ->usage. No > attempt will be made to access ->usage after this as the > SECTION_HAS_MEM_MAP is cleared thus valid_section() return false. > > Since the section_deactivate() is a rare operation and will come in the > hot remove path, impact of synchronize_rcu() should be negligble. > > Fixes: f46edbd1b151 ("mm/sparsemem: add helpers track active portions of a section at boot")