Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp117837rwe; Fri, 26 Aug 2022 01:40:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR5gthlkd0oJqyvpvFHClGz/rPr3gRGg4cOqYbphZIbm/v0QdAgLKW4A3XL8B2GC0m66niE8 X-Received: by 2002:a63:1c0b:0:b0:429:f504:e9a1 with SMTP id c11-20020a631c0b000000b00429f504e9a1mr2490878pgc.319.1661503236880; Fri, 26 Aug 2022 01:40:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661503236; cv=none; d=google.com; s=arc-20160816; b=FtSlvvtuPxnGbrnvqt0nPkNkRlYCQM8nuX3sxQce2UEARl/tetYVIueYCrMRhbikqe DO2tn+A425sMFY+wdF/mzHTg8QwgT0egeWEDY2eFzWjMv/+jNMXePo5ohyaOSItIMpBX x/TxLkr+Ottqpi+8o7FlEpyuBWon4JHqV14R9SPLWKnx6xWLW6HySkY078AxiC2aM1Qq gUxjkfMiZCww9DJhueblUDIMA2AZ29xnzclw8OpBMDAlO/Jp9+prCsEMo9FdEspj5mlX yGcWdL0ytbfMTpz/ydHzE++Vmm9K+1wthUIeT0KonxxRQKweB1/2Qvs0LOj+ZkEd4q+t b0MA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=0BsyNMBraTf2zRdBdEtcVEbOWZN+F6n2yPd2BDqPgFc=; b=YvHi1sVqiBvbhBj44ft0K3hf0RcmSbYAQIHUS+nUGkBDx4EEnm8yR3exY/vYiRZx7G XJTRjyFuWyJVNwpP5igKXCbYbDFpVzRlqK1qaaDP7LrrAU3B+IIwk48JS/G9yxpGGPbn LLqVEN2tLQCbMdQuC8svSzf3qor9rtFbgJjqxQXJIlJVPqGF4isxj32GQ2IaVTwJAacN aR75kiNctecoVxy+lHn8Ymw7UQLoCFhvNuzhvxaLk92bIJa9s9d8OqO2xy91X1cpF9i1 Z8ck556cPt1dOoHGvKRh5gb4IGfm08rCtISjaOfB64gKAuB3+mLDuMXr5z5rSMCXTo50 6NAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LYxs4GeY; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q2-20020a056a00150200b0052bceb2fe4esi1331370pfu.221.2022.08.26.01.40.25; Fri, 26 Aug 2022 01:40:36 -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=@redhat.com header.s=mimecast20190719 header.b=LYxs4GeY; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245692AbiHZHxD (ORCPT + 99 others); Fri, 26 Aug 2022 03:53:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245656AbiHZHwz (ORCPT ); Fri, 26 Aug 2022 03:52:55 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27947D3E7A for ; Fri, 26 Aug 2022 00:52:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661500373; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0BsyNMBraTf2zRdBdEtcVEbOWZN+F6n2yPd2BDqPgFc=; b=LYxs4GeYm9+8utnjFIqj4ejLTpWxIdG4mjxhE1Orp1kHZzYyzqJvWp7uU5jptmL7g+S0m0 xxaQzOV2YRfH3UwhqhO1CkgCMl4RdE6q3MLNRwb3Z8o92sVMnUnV/IEJyb8wUphCxXzKko R7qf+TdkKNWLy1yourVMxJPwKw0PGjg= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-668-rgStA0ErPiGW0UhyN8KFAA-1; Fri, 26 Aug 2022 03:52:50 -0400 X-MC-Unique: rgStA0ErPiGW0UhyN8KFAA-1 Received: by mail-wr1-f72.google.com with SMTP id e14-20020adf9bce000000b002254afda62aso73167wrc.18 for ; Fri, 26 Aug 2022 00:52:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc; bh=0BsyNMBraTf2zRdBdEtcVEbOWZN+F6n2yPd2BDqPgFc=; b=IwwurRoAKjP4pUkv02m+jWTM6D5Clv7KHOTu18WzMIeflj6qKWQ5pVSJambo2VRbAW dNtcDIpyrZrLXnIfaIPOaoZqZcNrrP2AZ89ev0nlPiPT/T2XZEQd+bF0e5MyZaaVnFK9 ET+N8n/ZpMKCOHu8rG7QoqSN+1mXPJBH9gqvUcuT52PAsrM/86/6QUNNbQp2axL3NHR9 qrtjVzqNFzU/Tzwpmgr5XPMA2Zqii1Yr0bFnGIHXlb84eSMSJHeupNp8mr/2TA63iflr 7qCHNEx+f4B66JK+9CVnGmsMOy3oPss/eOJeBt0UarCR4pE8RiQVqb3auiBdbiJdTvOd Zv9w== X-Gm-Message-State: ACgBeo0l5JWoo8aBPtVbIhoy0UMlOert4PH7F9DbKw/YIG9+Iv3S61vp JldSeKJ76XKT+G1hicgona+cIqi59KCBPeu23vwqMaqAB5zk6CbaOz7szlzjA3dgdTBBXFNduSf qMYXY/GRekqYhDxQ+dM2sp/ZF X-Received: by 2002:a1c:7315:0:b0:3a5:ff61:4080 with SMTP id d21-20020a1c7315000000b003a5ff614080mr4358187wmb.196.1661500369355; Fri, 26 Aug 2022 00:52:49 -0700 (PDT) X-Received: by 2002:a1c:7315:0:b0:3a5:ff61:4080 with SMTP id d21-20020a1c7315000000b003a5ff614080mr4358174wmb.196.1661500369061; Fri, 26 Aug 2022 00:52:49 -0700 (PDT) Received: from ?IPV6:2003:cb:c708:f600:abad:360:c840:33fa? (p200300cbc708f600abad0360c84033fa.dip0.t-ipconnect.de. [2003:cb:c708:f600:abad:360:c840:33fa]) by smtp.gmail.com with ESMTPSA id e16-20020a05600c219000b003a5537bb2besm1564624wme.25.2022.08.26.00.52.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 Aug 2022 00:52:48 -0700 (PDT) Message-ID: <3ba51e8c-52f5-29b6-4727-52f00b0d74d3@redhat.com> Date: Fri, 26 Aug 2022 09:52:47 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH V5] mm: fix use-after free of page_ext after race with memory-offline Content-Language: en-US To: Charan Teja Kalla , akpm@linux-foundation.org, mhocko@suse.com, vbabka@suse.cz, pasha.tatashin@soleen.com, shakeelb@google.com, sieberf@amazon.com, sjpark@amazon.de, william.kucharski@oracle.com, willy@infradead.org, quic_pkondeti@quicinc.com, minchan@google.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <1661496993-11473-1-git-send-email-quic_charante@quicinc.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <1661496993-11473-1-git-send-email-quic_charante@quicinc.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 On 26.08.22 08:56, Charan Teja Kalla wrote: > The below is one path where race between page_ext and offline of the > respective memory blocks will cause use-after-free on the access of > page_ext structure. > > process1 process2 > --------- --------- > a)doing /proc/page_owner doing memory offline > through offline_pages. > > b)PageBuddy check is failed > thus proceed to get the > page_owner information > through page_ext access. > page_ext = lookup_page_ext(page); > > migrate_pages(); > ................. > Since all pages are successfully > migrated as part of the offline > operation,send MEM_OFFLINE notification > where for page_ext it calls: > offline_page_ext()--> > __free_page_ext()--> > free_page_ext()--> > vfree(ms->page_ext) > mem_section->page_ext = NULL > > c) Check for the PAGE_EXT flags > in the page_ext->flags access > results into the use-after-free(leading > to the translation faults). > > As mentioned above, there is really no synchronization between page_ext > access and its freeing in the memory_offline. > > The memory offline steps(roughly) on a memory block is as below: > 1) Isolate all the pages > 2) while(1) > try free the pages to buddy.(->free_list[MIGRATE_ISOLATE]) > 3) delete the pages from this buddy list. > 4) Then free page_ext.(Note: The struct page is still alive as it is > freed only during hot remove of the memory which frees the memmap, which > steps the user might not perform). > > This design leads to the state where struct page is alive but the struct > page_ext is freed, where the later is ideally part of the former which > just representing the page_flags (check [3] for why this design is > chosen). > > 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())__. > > Fix all the paths where offline races with page_ext access by > maintaining synchronization with rcu lock and is achieved in 3 steps: > 1) Invalidate all the page_ext's of the sections of a memory block by > storing a flag in the LSB of mem_section->page_ext. > > 2) Wait till all the existing readers to finish working with the > ->page_ext's with synchronize_rcu(). Any parallel process that starts > after this call will not get page_ext, through lookup_page_ext(), for > the block parallel offline operation is being performed. > > 3) Now safely free all sections ->page_ext's of the block on which > offline operation is being performed. > > Note: If synchronize_rcu() takes time then optimizations can be done in > this path through call_rcu()[2]. > > Thanks to David Hildenbrand for his views/suggestions on the initial > discussion[1] and Pavan kondeti for various inputs on this patch. > > [1] https://lore.kernel.org/linux-mm/59edde13-4167-8550-86f0-11fc67882107@quicinc.com/ > [2] https://lore.kernel.org/all/a26ce299-aed1-b8ad-711e-a49e82bdd180@quicinc.com/T/#u > [3] https://lore.kernel.org/all/6fa6b7aa-731e-891c-3efb-a03d6a700efa@redhat.com/ > > Suggested-by: David Hildenbrand > Suggested-by: Michal Hocko > Signed-off-by: Charan Teja Kalla > --- Acked-by: David Hildenbrand Thanks! -- Thanks, David / dhildenb