From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54F8EECAAD2 for ; Fri, 26 Aug 2022 07:52:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D5F11940009; Fri, 26 Aug 2022 03:52:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D0FBB940007; Fri, 26 Aug 2022 03:52:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD6BF940009; Fri, 26 Aug 2022 03:52:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AFDE0940007 for ; Fri, 26 Aug 2022 03:52:54 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8458FC0720 for ; Fri, 26 Aug 2022 07:52:54 +0000 (UTC) X-FDA: 79840977468.08.A159100 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf16.hostedemail.com (Postfix) with ESMTP id DACF918000A for ; Fri, 26 Aug 2022 07:52:53 +0000 (UTC) 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-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-149-Hk0FpO_YMKqSBmGsp3YjBQ-1; Fri, 26 Aug 2022 03:52:50 -0400 X-MC-Unique: Hk0FpO_YMKqSBmGsp3YjBQ-1 Received: by mail-wr1-f70.google.com with SMTP id l25-20020adfa399000000b002252058bad2so75169wrb.11 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=U81BOE1heQGqZ3tFfPtpKVyhT3rkmury5SFFBIv56YMz1zPpvvTdSus92kskckhXhn iKcOiyS4yBmGhD2RgMd+D2zuGyBRI2qmNf5270VTs6s1OG7BKsXkLWQ8hZc1RQHBm6x7 gvDUw3phC7lblbkGIV6wg8wgkzv57IoZOC5Pqm84nchqPphH6qNEdZoagdZwnv2GoJcd ao4BaOt2PN1Ub8w8axTCDPpG5THXiy2tAFqPzjl33viP5yk3U2sHetXMRgmZ7cswm+b8 KHSwZLVAxKnm+fUeWigPl483EgTdRArDJH2ZlT8SXVJVHc3fgiMPcs3AtWs1v8FoD4cu f9BQ== X-Gm-Message-State: ACgBeo342zPG4GMs5a2X/lDedhyJX1VpAevneP7wLAu0Npom2dLMu7js ORw37Pdh1GJ04Qja1cezZ7zDcEPT+X3a4LjqqoKsHq0fiCHftMEQvaR4KtpqV6NMVcXsbC/owMY yQFHHm01Io44= X-Received: by 2002:a1c:7315:0:b0:3a5:ff61:4080 with SMTP id d21-20020a1c7315000000b003a5ff614080mr4358188wmb.196.1661500369355; Fri, 26 Aug 2022 00:52:49 -0700 (PDT) X-Google-Smtp-Source: AA6agR4VAAESIWC7QgaM7o6uZkV22WGTbUbGcDRIewk8Fd5LU+8TzRqdXa3r1xehrlBDRJhIhirevQ== 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 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> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661500374; h=from:from:sender: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:dkim-signature; bh=0BsyNMBraTf2zRdBdEtcVEbOWZN+F6n2yPd2BDqPgFc=; b=2Y1ijZp9Vx4FUqxTEnm7em/vwTuvcybZ2omrXIt6LkmVRlqVVGupaYvU/PzIffJH7nzKoH 4OpxibwySaNEmODiUhF+zydfPSz1POdSAkwzDw11jaHb0uQ+e/H6pXWn9aBRKJwMxfLKG8 b0SjLlNx2ceR8QVF/EmKJ5mtgAEA3oM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=LYxs4GeY; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661500374; a=rsa-sha256; cv=none; b=cRDD9wsD5FGUOIW3WTkTFalLH9K6EXx7yPc+PgLbj88UiWVtaUhXiQFK7mCszE+6ciG2uh CZsXOcElGwM3XmMp9bEAHAT5vZ0+5BZsXXjhN+8Pp2qn+PhfbB8WnQCHOMNrRl3ibjLfQU UoioOCbSISx9N0yeefYvgpWl9thYyjE= Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=LYxs4GeY; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com X-Rspam-User: X-Rspamd-Queue-Id: DACF918000A X-Rspamd-Server: rspam10 X-Stat-Signature: 7hd9kbbkqkob73jnm3k7w5xrfetnq9ns X-HE-Tag: 1661500373-202531 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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