Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp5244247imw; Wed, 20 Jul 2022 01:45:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v9XW/tKNZ6b+/ClIQcX9AxQEE0olk9Zkf0KThmlSZuouqWccPrWuF5c7YaH2LZJfLpfUHH X-Received: by 2002:a05:6402:2497:b0:437:a341:9286 with SMTP id q23-20020a056402249700b00437a3419286mr49430926eda.156.1658306740477; Wed, 20 Jul 2022 01:45:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658306740; cv=none; d=google.com; s=arc-20160816; b=Q0nQWzSdjYnhGakMBF7ZzlSIS/F5EkEh7hIsBRduZtAmOiE2tr2Iv9CKlfqKOPl9Rj UXS9KYpxg+ZY6UMSy2fuOASkc3n0RmcaTlMlFZyVL/w/H2lTPX/DjHLULG+tebBEosQQ vPlVcOUA5DA1568hgvjaAXfSaa3ZVNPnlU3WC5xPzzN/7NurSA6iaA5Z+l3E8k6o5K/g 3/NIUQ9Zj4mys1XGecpBZm/PAe/P61W+4GHkd8nTrR41Uc8U8jpnakFHjZUOm253sp/u QeeS6K9Jw02ymIGrZlXLvvcTqBUhpzFIpIkJnAE/P1G+J2iJ5q/2nLqrwwAW7AahajDh v7Xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=DyE1S6/M8cxx8TUOX92GSOdQu0Ro177cj6dL0LTcF6U=; b=akldxSd6rDghS6tcXqxewsLsu0pVEe7kvkVwzAOAgB9vy8Xrk2wZyYGt9UqLgpBi0C fRD1DI9vAvsf1iGP05V7wfF2gPffIHMvbr7CSj2YVcR3facFVkngfeTtZnHK5Fj/8w2e hTgxuCC7m6XJFx9b9lpJUxwoubXzGnh2FHu0mY7PwIEUM5zY24A+nUqSdZJUavEQaA8s FHLmkg6U/66/fa++mb//YvA7HtbXvKhqTUJzL7OCfwxdTcfo9sDnVl57HcA7rUR1IJyz nKeix7qMVgZUBkLcI1gST2UvBVI570X+VBEIAE7zu/jhFPnvRMARvoiLCypN5PBZM3yF +1cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcdkim header.b="EzLT/G5s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lf3-20020a170907174300b0072b4151c094si17388097ejc.879.2022.07.20.01.45.15; Wed, 20 Jul 2022 01:45:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcdkim header.b="EzLT/G5s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239089AbiGTIV0 (ORCPT + 99 others); Wed, 20 Jul 2022 04:21:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229686AbiGTIVY (ORCPT ); Wed, 20 Jul 2022 04:21:24 -0400 Received: from alexa-out-sd-02.qualcomm.com (alexa-out-sd-02.qualcomm.com [199.106.114.39]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 613CADFA9 for ; Wed, 20 Jul 2022 01:21:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; i=@quicinc.com; q=dns/txt; s=qcdkim; t=1658305283; x=1689841283; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=DyE1S6/M8cxx8TUOX92GSOdQu0Ro177cj6dL0LTcF6U=; b=EzLT/G5s6g+tXOdyPGMxoZOeDnlDbU/80bGw79tchS7t1rZevNXTTmkC gMTdFpGmt8MkRPAHRaUUrNAIvjhss8hYJTuaBGOduAHB4OBwv002D73DW 34yPfSODvbhtHmwr/LWINCpobpgg8EcYAR8nb2+REK6HxEK1grYe8Po3i M=; Received: from unknown (HELO ironmsg-SD-alpha.qualcomm.com) ([10.53.140.30]) by alexa-out-sd-02.qualcomm.com with ESMTP; 20 Jul 2022 01:21:23 -0700 X-QCInternal: smtphost Received: from nasanex01c.na.qualcomm.com ([10.47.97.222]) by ironmsg-SD-alpha.qualcomm.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2022 01:21:22 -0700 Received: from nalasex01a.na.qualcomm.com (10.47.209.196) by nasanex01c.na.qualcomm.com (10.47.97.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Wed, 20 Jul 2022 01:21:21 -0700 Received: from hu-pkondeti-hyd.qualcomm.com (10.80.80.8) by nalasex01a.na.qualcomm.com (10.47.209.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Wed, 20 Jul 2022 01:21:16 -0700 Date: Wed, 20 Jul 2022 13:51:12 +0530 From: Pavan Kondeti To: Charan Teja Kalla CC: Michal Hocko , , , , , , , , , , , , , "iamjoonsoo.kim@lge.com" , Pavan Kondeti Subject: Re: [PATCH] mm: fix use-after free of page_ext after race with memory-offline Message-ID: <20220720082112.GA14437@hu-pkondeti-hyd.qualcomm.com> References: <1657810063-28938-1-git-send-email-quic_charante@quicinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nalasex01a.na.qualcomm.com (10.47.209.196) X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Charan, On Tue, Jul 19, 2022 at 08:42:42PM +0530, Charan Teja Kalla wrote: > Thanks Michal!! > > On 7/18/2022 8:24 PM, Michal Hocko wrote: > >>>> The above mentioned race is just one example __but the problem persists > >>>> in the other paths too involving page_ext->flags access(eg: > >>>> page_is_idle())__. Since offline waits till the last reference on the > >>>> page goes down i.e. any path that took the refcount on the page can make > >>>> the memory offline operation to wait. Eg: In the migrate_pages() > >>>> operation, we do take the extra refcount on the pages that are under > >>>> migration and then we do copy page_owner by accessing page_ext. For > >>>> > >>>> Fix those paths where offline races with page_ext access by maintaining > >>>> synchronization with rcu lock. > >>> Please be much more specific about the synchronization. How does RCU > >>> actually synchronize the offlining and access? Higher level description > >>> of all the actors would be very helpful not only for the review but also > >>> for future readers. > >> I will improve the commit message about this synchronization change > >> using RCU's. > > Thanks! The most imporant part is how the exclusion is actual achieved > > because that is not really clear at first sight > > > > CPU1 CPU2 > > lookup_page_ext(PageA) offlining > > offline_page_ext > > __free_page_ext(addrA) > > get_entry(addrA) > > ms->page_ext = NULL > > synchronize_rcu() > > free_page_ext > > free_pages_exact (now addrA is unusable) > > > > rcu_read_lock() > > entryA = get_entry(addrA) > > base + page_ext_size * index # an address not invalidated by the freeing path > > do_something(entryA) > > rcu_read_unlock() > > > > CPU1 never checks ms->page_ext so it cannot bail out early when the > > thing is torn down. Or maybe I am missing something. I am not familiar > > with page_ext much. > > > Thanks a lot for catching this Michal. You are correct that the proposed > code from me is still racy. I Will correct this along with the proper > commit message in the next version of this patch. > Trying to understand your discussion with Michal. What part is still racy? We do check for mem_section::page_ext and bail out early from lookup_page_ext(), no? Also to make this scheme explicit, we can annotate page_ext member with __rcu and use rcu_assign_pointer() on the writer side. struct page_ext *lookup_page_ext(const struct page *page) { unsigned long pfn = page_to_pfn(page); struct mem_section *section = __pfn_to_section(pfn); /* * The sanity checks the page allocator does upon freeing a * page can reach here before the page_ext arrays are * allocated when feeding a range of pages to the allocator * for the first time during bootup or memory hotplug. */ if (!section->page_ext) return NULL; return get_entry(section->page_ext, pfn); } Thanks, Pavan