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 92BB8C4321E for ; Wed, 30 Nov 2022 09:46:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E504E6B0078; Wed, 30 Nov 2022 04:46:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DD94D6B007B; Wed, 30 Nov 2022 04:46:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C530A6B007D; Wed, 30 Nov 2022 04:46:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B09366B0078 for ; Wed, 30 Nov 2022 04:46:30 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 621CD41024 for ; Wed, 30 Nov 2022 09:46:30 +0000 (UTC) X-FDA: 80189628540.07.4F06BBB Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 1CBC814000F for ; Wed, 30 Nov 2022 09:46:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669801588; 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=+cu3xgYjWpmSRgV5LPKpXBlA+9KOBxrwF+YKe+TNV4A=; b=ChzwCLDFOg1YL5wFxIYpNzm0K9FB82NodlCPBY5nathcR9iezhxTxJvNneQxKUxynb04Og D8GGjBQ8mBCOytem/9WfTEfJIv9wNjC8Rl+pYSQMlm6kriFAV0ayVgdNBwmenxRKDs1QdJ UlXeM6KE4pH7pdUmzfyzBUoH1wO5hZg= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-369-Zl7VKRj8OM2sb36G2Epr4w-1; Wed, 30 Nov 2022 04:46:27 -0500 X-MC-Unique: Zl7VKRj8OM2sb36G2Epr4w-1 Received: by mail-wr1-f71.google.com with SMTP id d8-20020adf9b88000000b0024207f09827so2786573wrc.20 for ; Wed, 30 Nov 2022 01:46:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :content-language:references:cc:to:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+cu3xgYjWpmSRgV5LPKpXBlA+9KOBxrwF+YKe+TNV4A=; b=l32IyGv/ih+n1o8vKHI55vIqPtAkYbSPsZFkH/gU3NKpDeFvlrjdALMQPOFsNr2df8 zGyVbPp7ZHseaeQl62F5fjLotQ+Csn5hWzlf7XCRoAsdHWs6LW8tqDc9wB64NBrqMI/C SjaBU+mbqgWkw1SnE4ljrIaw6gD7Tm0wKw/Xg2ksp+6AHJN4xzGMV/cQj5xYFbCfw1NQ 1cpEODbsEEgNn4hw7kBQUl/weYEp3W0rXIwveszcZV+k+cYnpbTMrd3aZ3ZTvQQ5WFra s7mWKrR6PEMNfhfvZwbXlvFVTOvqmQZf0SZJQphZJ+o90pPk1X40XouUa+MfLv9JIdpU MBZA== X-Gm-Message-State: ANoB5pnmceOEQ9fer/pQBnWrz+GYH8CHqjFN+sz3ET+wsXkjv8UosrHZ zaAtptO2sQrGakZG0KGnxCfU7bgGm5xV322GGjRo4L4yLNWHcB9noqmJC9Oslq9HRMJ9K4oqgPW zJ8MG74OdGaM= X-Received: by 2002:a1c:790a:0:b0:3cf:e137:b31d with SMTP id l10-20020a1c790a000000b003cfe137b31dmr34366011wme.205.1669801586023; Wed, 30 Nov 2022 01:46:26 -0800 (PST) X-Google-Smtp-Source: AA0mqf4QTMF/mFsiU9u2cnVmMEzm+1p117TtofaUUL5J0DSwov5KZjImZGryPwEjNH19L8Bts0fcaQ== X-Received: by 2002:a1c:790a:0:b0:3cf:e137:b31d with SMTP id l10-20020a1c790a000000b003cfe137b31dmr34365977wme.205.1669801585633; Wed, 30 Nov 2022 01:46:25 -0800 (PST) Received: from ?IPV6:2003:cb:c703:7600:a8ea:29ce:7ee3:dd41? (p200300cbc7037600a8ea29ce7ee3dd41.dip0.t-ipconnect.de. [2003:cb:c703:7600:a8ea:29ce:7ee3:dd41]) by smtp.gmail.com with ESMTPSA id n29-20020a05600c3b9d00b003c21ba7d7d6sm1441159wms.44.2022.11.30.01.46.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 30 Nov 2022 01:46:25 -0800 (PST) Message-ID: <1eff312b-1aca-6afb-3587-f65e698b3f8c@redhat.com> Date: Wed, 30 Nov 2022 10:46:24 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 To: Peter Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: James Houghton , Jann Horn , Andrew Morton , Andrea Arcangeli , Rik van Riel , Nadav Amit , Miaohe Lin , Muchun Song , Mike Kravetz References: <20221129193526.3588187-1-peterx@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH 00/10] mm/hugetlb: Make huge_pte_offset() thread-safe for pmd unshare In-Reply-To: <20221129193526.3588187-1-peterx@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669801590; a=rsa-sha256; cv=none; b=M376VPqGxYwTP7pwLPuG5muqVTYfqwIP6GffRXkIgOouuEhyWg0qU44ibuVHIaofxb4vbe ukUqMoeEQtAy5noV3ZsOspEU1bEobhM+2O85OpnI4hruTLmkKSjC/DTxHjlMhsyD+nENRp +IsdqDwUPENdLkTxFCR3ByYweYcuUWU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ChzwCLDF; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669801590; 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=+cu3xgYjWpmSRgV5LPKpXBlA+9KOBxrwF+YKe+TNV4A=; b=Ra497szRjm8rjG+nHZ5pvHfql4i82XUXU6/PZR4pX853R4VzSE1IB/hdb0e1Vw/m8AMp7T YMdVgKL7Mbulwj5GwVvcRgf9lmjfLJDsj398xW2RKt0BmseGRf3eeQKpHbe5tP/haTdIBa W8NVR0ZnElvJze+u1/bFrR+AB1M5Wm0= X-Stat-Signature: t4bxgnr64kkeayfoausa9h7bygthxxya X-Rspamd-Queue-Id: 1CBC814000F X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ChzwCLDF; spf=pass (imf09.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam05 X-HE-Tag: 1669801588-783613 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 29.11.22 20:35, Peter Xu wrote: > Based on latest mm-unstable (9ed079378408). > > This can be seen as a follow-up series to Mike's recent hugetlb vma lock > series for pmd unsharing, but majorly covering safe use of huge_pte_offset. > > Comparing to previous rfcv2, the major change is I dropped the new pgtable > lock but only use vma lock for locking. The major reason is I overlooked > that the pgtable lock was not protected by RCU: __pmd_free_tlb() frees the > pgtable lock before e.g. call_rcu() for RCU_TABLE_FREE archs. OTOH many of > the huge_pte_offset() call sites do need to take pgtable lock. It means > the valid users for the new RCU lock will be very limited. Thanks. > > It's possible that in the future we can rework the pgtable free to only > free the pgtable lock after RCU grace period (move pgtable_pmd_page_dtor() > to be within tlb_remove_table_rcu()), then the RCU lock will make more > sense. For now, make it simple by fixing the races first. Good. > > Since this version attached a reproducer (below) and also removed the RCU > (especially, the fallback irqoff) solution, removing RFC tag. Very nice, thanks. > > Old versions: > > rfcv1: https://lore.kernel.org/r/20221030212929.335473-1-peterx@redhat.com > rfcv2: https://lore.kernel.org/r/20221118011025.2178986-1-peterx@redhat.com > > Problem > ======= > > huge_pte_offset() is a major helper used by hugetlb code paths to walk a > hugetlb pgtable. It's used mostly everywhere since that's needed even > before taking the pgtable lock. > > huge_pte_offset() is always called with mmap lock held with either read or > write. It was assumed to be safe but it's actually not. One race > condition can easily trigger by: (1) firstly trigger pmd share on a memory > range, (2) do huge_pte_offset() on the range, then at the meantime, (3) > another thread unshare the pmd range, and the pgtable page is prone to lost > if the other shared process wants to free it completely (by either munmap > or exit mm). So just that I understand correctly: Two processes, #A and #B, share a page table. Process #A runs two threads, #A1 and #A2. #A1 walks that shared page table (using huge_pte_offset()), for example, to resolve a page fault. Concurrently, #A2 triggers unsharing of that page table (replacing it by a private page table), for example, using munmap(). So #A1 will eventually read/write the shared page table while we're placing a private page table. Which would be fine (assuming no unsharing would be required by #A1), however, if #B also concurrently drops the reference to the shared page table (), the shared page table could essentially get freed while #A1 is still walking it. I suspect, looking at the reproducer, that the page table deconstructor was called. Will the page table also actually get freed already? IOW, could #A1 be reading/writing a freed page? > > The recent work from Mike on vma lock can resolve most of this already. > It's achieved by forbidden pmd unsharing during the lock being taken, so no > further risk of the pgtable page being freed. It means if we can take the > vma lock around all huge_pte_offset() callers it'll be safe. Agreed. > > There're already a bunch of them that we did as per the latest mm-unstable, > but also quite a few others that we didn't for various reasons especially > on huge_pte_offset() usage. > > One more thing to mention is that besides the vma lock, i_mmap_rwsem can > also be used to protect the pgtable page (along with its pgtable lock) from > being freed from under us. IOW, huge_pte_offset() callers need to either > hold the vma lock or i_mmap_rwsem to safely walk the pgtables. > > A reproducer of such problem, based on hugetlb GUP (NOTE: since the race is > very hard to trigger, one needs to apply another kernel delay patch too, > see below): Thanks, David / dhildenb