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 D2BB9C54798 for ; Tue, 5 Mar 2024 08:35:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 508106B007E; Tue, 5 Mar 2024 03:35:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B7E26B0080; Tue, 5 Mar 2024 03:35:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3308F6B0081; Tue, 5 Mar 2024 03:35:52 -0500 (EST) 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 203D76B007E for ; Tue, 5 Mar 2024 03:35:52 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5523F80BE7 for ; Tue, 5 Mar 2024 08:35:51 +0000 (UTC) X-FDA: 81862327302.02.9CF17AF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 25F454000F for ; Tue, 5 Mar 2024 08:35:48 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=f80fuWE9; spf=pass (imf11.hostedemail.com: domain of david@redhat.com designates 170.10.133.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=1709627749; 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=8d1YUHodfZWTVLWvoDOTeK1aIx0Y+TAs7cm2fq84YPM=; b=KGY850QyZZ468UMDWhA1ytncu/gWYEa2Jcuahf2WSxrBQaktFMjeCCGIVHxzziLFxz7NLN vwHeSWQKsMey3s/L4gUf7sPqWlMSm8ScL1IAbOz49Xwc4gZoHI2LJ+R3RxRongAtQXWXWO OLbfWwsh2/GdpV8o3NrlGZzn8jsEpA8= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=f80fuWE9; spf=pass (imf11.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709627749; a=rsa-sha256; cv=none; b=6L04W5+15rlI31TcYf8VsPh0hgGGgILKtJK4YRVkvHjPOhm4TnAeGzz2DNsqbbYDCvoABa lYJZWrBpJ7gVcJgtfem9qlRRoait9YlcWC0BHDg4kbljJz30fKzkwneSM9TvesVqvJSvgl LqpDlH3lW+ghxhaGqwVXNynuyWV3rrA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709627748; 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:autocrypt:autocrypt; bh=8d1YUHodfZWTVLWvoDOTeK1aIx0Y+TAs7cm2fq84YPM=; b=f80fuWE9U0wIDS/QleVAxyvNw4bo6rT2JIv/+c21b71RuL4sCovhfuvNS92CszwUV1HBVd W9RbHB1lAb5BQ760RWTtWSNjCpsLG6zOqAoRfgDqrE0ci9slWvcbB7ewuijrT4q9QLc72O yhb93OeqwHZsmEZPV8yWRg4ultV8jGA= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-470-gnT5JMDhPzaInvWD-A6DAQ-1; Tue, 05 Mar 2024 03:35:47 -0500 X-MC-Unique: gnT5JMDhPzaInvWD-A6DAQ-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4129b426bdaso35572255e9.0 for ; Tue, 05 Mar 2024 00:35:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709627746; x=1710232546; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=8d1YUHodfZWTVLWvoDOTeK1aIx0Y+TAs7cm2fq84YPM=; b=uUdNn83bkAJvSgwTZ1OSchOf83rpLXCqg746XpxEOXLrl5OF3FxHgI8hX0CNituPvx Zw2dfnzFQuveQB76/Mfgi39+J9IrvIwAsZ7Vh5xEx1NkhRe05pbI/Jlcux5adDQ26gob EcP57L6tldojN0cnxBa2MpK5iosMcYb9CwgJCh/EBxd+8CuY0XmnxYF3FSbHUzeA4fLi 2TtUAgEFyg2IMSXy1kcpl0ExBJ39hG/a7GeQA6GuU69PKWWCB0kDgJmOSvyougFDGime MeJXvmq/CUuL1EROWj+nMSeUfXvHAzJXuvNyejU790pTO1r4C0bykM6oMnnqVYn4bAjF RjJQ== X-Forwarded-Encrypted: i=1; AJvYcCWPD3QxqOl+vNZ0BXLQt9Ghy9DRmQEPF2bh7BOd1rbEOpY3+1Hb3hhKMbqykKwNpY3jrIXh/z9UC5c4viGpNCS5kgM= X-Gm-Message-State: AOJu0YyUDHRNmNT9CTJsCfMRcMzVyqA7tbRwyPbPyezty+lXNywLI9h7 b6x6W2s5SlxNAte0DfPGkvEQ8syVwV6QOWMQfyCIOTemwaO/QxfBtIYsufDjw9C0L8k/UPrTnPo +Cew25YmSJwfSj1ZE0z4xwaN2f2m1SDnlG2p0BairnzWSLKeB X-Received: by 2002:adf:e310:0:b0:33d:b627:c3ec with SMTP id b16-20020adfe310000000b0033db627c3ecmr8518475wrj.44.1709627745677; Tue, 05 Mar 2024 00:35:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IHZBtQKdW2t1vKhHjG/Koq/hUA7dl61rlzq8BNHioqIvkHGlzTSVprfjWax7VJPSYecDmi8Og== X-Received: by 2002:adf:e310:0:b0:33d:b627:c3ec with SMTP id b16-20020adfe310000000b0033db627c3ecmr8518445wrj.44.1709627745162; Tue, 05 Mar 2024 00:35:45 -0800 (PST) Received: from ?IPV6:2003:cb:c73c:8100:600:a1e5:da94:a13a? (p200300cbc73c81000600a1e5da94a13a.dip0.t-ipconnect.de. [2003:cb:c73c:8100:600:a1e5:da94:a13a]) by smtp.gmail.com with ESMTPSA id d14-20020adf9c8e000000b0033e42ab5114sm3664191wre.2.2024.03.05.00.35.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 05 Mar 2024 00:35:44 -0800 (PST) Message-ID: <9a5cb081-c4f1-4abe-bb86-02aaca4e5433@redhat.com> Date: Tue, 5 Mar 2024 09:35:43 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 1/4] mm: swap: Remove CLUSTER_FLAG_HUGE from swap_cluster_info:flags To: "Huang, Ying" , Ryan Roberts Cc: Andrew Morton , Matthew Wilcox , Gao Xiang , Yu Zhao , Yang Shi , Michal Hocko , Kefeng Wang , Hugh Dickins , linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20231025144546.577640-1-ryan.roberts@arm.com> <20231025144546.577640-2-ryan.roberts@arm.com> <6541e29b-f25a-48b8-a553-fd8febe85e5a@redhat.com> <2934125a-f2e2-417c-a9f9-3cb1e074a44f@redhat.com> <049818ca-e656-44e4-b336-934992c16028@arm.com> <949b6c22-d737-4060-9ca1-a69d8e986d90@redhat.com> <9ed743a7-0c5d-49d9-b8b2-d58364df1f5f@arm.com> <65a66eb9-41f8-4790-8db2-0c70ea15979f@redhat.com> <6cfc022a-0c7a-4fe6-aaa4-3d28aeacc982@arm.com> <3d47ae7d-297a-441e-941c-5b2e34ba8759@redhat.com> <3ae2da13-c33a-402f-9091-2c7328aea66a@arm.com> <87plw99phz.fsf@yhuang6-desk2.ccr.corp.intel.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZpZCBIaWxk ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEICGwMGCwkIBwMCBhUIAgkKCwQW AgMBAh4BAheAA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rKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <87plw99phz.fsf@yhuang6-desk2.ccr.corp.intel.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: 8bit X-Rspamd-Queue-Id: 25F454000F X-Rspam-User: X-Stat-Signature: y7e7bknaughfcxiii5mwqaxwaj9pqara X-Rspamd-Server: rspam01 X-HE-Tag: 1709627748-67279 X-HE-Meta: U2FsdGVkX18Pl/0gH9pNq5nRsjhWrhMWNH3qEvbBM8q7CnS1fK6ZdneOKK+Ae/nQ6gK7XBr6xTKvTryC5tBsCP0D+ObbY4kURdx9UVE5Vee3qyQkpJ4O1MsVWO71r1aEu9wR2rMc+3y4oK2hPrNqCCjlfPG0LuM8+ucKftxhdWXtvAXIvucZR7bZbPGhlv0X8cMztHpWee7LxlHFvF8TDwaa/3Lo3IAxF35faAWrt6p9aOOs0Kfn9ACej9TTozhfz25dB4OiEFQ2aSUcmcIKNi2GxiSSH0Pn/jGVVcDOeX+xZZ0Nb/szpbTTsd84fuLKYyS86Ip7jDRGAlvhGVUgwziv66lMZSDIZlkUPWLdeg+r2INleLDbIxm0KxCV0Euvbu+o2BVEhPM+mIRgD2AoW4PUXfwKWH4n09oSchSXCeesZrSuyfWbT9SLQuo9hWWQeu4qdWSpmK2dY4e+Agk8zkEhavN7yxnbJd8ndlw9pYihM3zJS3u8xvYtB6zctEVjvvfMxSa0IIMSmH0FYOzbvO9IcxxRiYf0v9uOyvNldWYPuxIszXi86nvTmDb++mG4BOSPVpX0NSWvSDjpgUAStVtb+I9ZT8v+uP8MCSEST7ZZAGPUjP0AXbEpTBQ2tZRim2LK2VEoztMkO54E1+/dklbVQs5SZg0J7+O22PchGYLCQ6LmI+T4KPpuC/RCjE5vFTF4FgOoDLAjk6DvoXJOS0q8uG6eDiaGP534IVurJuHoKuhGEThG6IATGmsQMsPALqRdLJ2j4J3YcfXr5K6TlTJMRtbEYQ9fY5KWwF4czLfHt4QxAMWL5I4XB+jxoGUiIlgsN2r0XPaNnXXpYy2fv33EPEJvh3R1wf35hXJA2hdbxG8VVjphmCTQMMaP6ednpGjRwQaDauGyE17z59+nNs3sWAM+yOaVMzAo1ULIWKYf+2onmrHsXB8jX+UHclwhgH68eEl9rVurYHhsEkL QfNYjCL8 K1bWctpZDDbTxQxq278yJoZDh2fGQTrHW2Z8x6pHtXThOkyuSnNhH6PoT7tD2rcckGBPxp2+JRmMKLEpmA3eR+IyU2pAlDgKlupiXOuIT6XbTcQdiYc21+Ge4ba+hIIYgQF0cY0wFnMu5/kOD+Q+iDf53RoFVVUe9wqWa7NZhSwaPBZ1fKHxDRoHUFoiOGCganqhxhIiY7o0QF3H6KEthR8LzkA== 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: List-Subscribe: List-Unsubscribe: On 05.03.24 07:11, Huang, Ying wrote: > Ryan Roberts writes: > >> + Hugh >> >> On 04/03/2024 22:02, David Hildenbrand wrote: >>> On 04.03.24 22:55, Ryan Roberts wrote: >>>> On 04/03/2024 20:50, David Hildenbrand wrote: >>>>>>>> >>>>>>>> This is the existing free_swap_and_cache(). I think _swap_info_get() would >>>>>>>> break >>>>>>>> if this could race with swapoff(), and __swap_entry_free() looks up the >>>>>>>> cluster >>>>>>>> from an array, which would also be freed by swapoff if racing: >>>>>>>> >>>>>>>> int free_swap_and_cache(swp_entry_t entry) >>>>>>>> { >>>>>>>>       struct swap_info_struct *p; >>>>>>>>       unsigned char count; >>>>>>>> >>>>>>>>       if (non_swap_entry(entry)) >>>>>>>>           return 1; >>>>>>>> >>>>>>>>       p = _swap_info_get(entry); >>>>>>>>       if (p) { >>>>>>>>           count = __swap_entry_free(p, entry); >>>>>>> >>>>>>> If count dropped to 0 and >>>>>>> >>>>>>>>           if (count == SWAP_HAS_CACHE) >>>>>>> >>>>>>> >>>>>>> count is now SWAP_HAS_CACHE, there is in fact no swap entry anymore. We >>>>>>> removed >>>>>>> it. That one would have to be reclaimed asynchronously. >>>>>>> >>>>>>> The existing code we would call swap_page_trans_huge_swapped() with the SI it >>>>>>> obtained via _swap_info_get(). >>>>>>> >>>>>>> I also don't see what should be left protecting the SI. It's not locked >>>>>>> anymore, >>>>>>> the swapcounts are at 0. We don't hold the folio lock. >>>>>>> >>>>>>> try_to_unuse() will stop as soon as si->inuse_pages is at 0. Hm ... >>>>>> >>>>>> But, assuming the caller of free_swap_and_cache() acquires the PTL first, I >>>>>> think this all works out ok? While free_swap_and_cache() is running, >>>>>> try_to_unuse() will wait for the PTL. Or if try_to_unuse() runs first, then >>>>>> free_swap_and_cache() will never be called because the swap entry will have >>>>>> been >>>>>> removed from the PTE? >>>>> >>>>> But can't try_to_unuse() run, detect !si->inuse_pages and not even bother about >>>>> scanning any further page tables? >>>>> >>>>> But my head hurts from digging through that code. >>>> >>>> Yep, glad I'm not the only one that gets headaches from swapfile.c. >>>> >>>>> >>>>> Let me try again: >>>>> >>>>> __swap_entry_free() might be the last user and result in "count == >>>>> SWAP_HAS_CACHE". >>>>> >>>>> swapoff->try_to_unuse() will stop as soon as soon as si->inuse_pages==0. >>>>> >>>>> >>>>> So the question is: could someone reclaim the folio and turn si->inuse_pages==0, >>>>> before we completed swap_page_trans_huge_swapped(). >>>>> >>>>> Imagine the following: 2 MiB folio in the swapcache. Only 2 subpages are still >>>>> references by swap entries. >>>>> >>>>> Process 1 still references subpage 0 via swap entry. >>>>> Process 2 still references subpage 1 via swap entry. >>>>> >>>>> Process 1 quits. Calls free_swap_and_cache(). >>>>> -> count == SWAP_HAS_CACHE >>>>> [then, preempted in the hypervisor etc.] >>>>> >>>>> Process 2 quits. Calls free_swap_and_cache(). >>>>> -> count == SWAP_HAS_CACHE >>>>> >>>>> Process 2 goes ahead, passes swap_page_trans_huge_swapped(), and calls >>>>> __try_to_reclaim_swap(). >>>>> >>>>> __try_to_reclaim_swap()->folio_free_swap()->delete_from_swap_cache()->put_swap_folio()-> >>>>> free_swap_slot()->swapcache_free_entries()->swap_entry_free()->swap_range_free()-> >>>>> ... >>>>> WRITE_ONCE(si->inuse_pages, si->inuse_pages - nr_entries); >>>>> >>>>> >>>>> What stops swapoff to succeed after process 2 reclaimed the swap cache but >>>>> before process 1 finished its call to swap_page_trans_huge_swapped()? >>>> >>>> Assuming you are talking about anonymous memory, process 1 has the PTL while >>>> it's executing free_swap_and_cache(). try_to_unuse() iterates over every vma in >>>> every mm, and it swaps-in a page for every PTE that holds a swap entry for the >>>> device being swapoff'ed. It takes the PTL while converting the swap entry to >>>> present PTE - see unuse_pte(). Process 1 must have beaten try_to_unuse() to the >>>> particular pte, because if try_to_unuse() got there first, it would have >>>> converted it from a swap entry to present pte and process 1 would never even >>>> have called free_swap_and_cache(). So try_to_unuse() will eventually wait on the >>>> PTL until process 1 has released it after free_swap_and_cache() completes. Am I >>>> missing something? Because that part feels pretty clear to me. >>> >>> Why should try_to_unuse() do *anything* if it already finds >>> si->inuse_pages == 0 because we (p1 } p2) just freed the swapentries and process >>> 2 managed to free the last remaining swapcache entry? >> >> Yeah ok. For some reason I thought unuse_mm() was iterating over all mms and so >> the `while (READ_ONCE(si->inuse_pages))` was only evaluated after iterating over >> every mm. Oops. >> >> So yes, I agree with you; I think this is broken. And I'm a bit worried this >> could be a can of worms; By the same logic, I think folio_free_swap(), >> swp_swapcount() and probably others are broken in the same way. > > Don't worry too much :-), we have get_swap_device() at least. We can > insert it anywhere we want because it's quite lightweight. And, because > swapoff() is so rare, the race is theoretical only. > > For this specific case, I had thought that PTL is enough. But after > looking at this more, I found a race here too. Until > __swap_entry_free() return, we are OK, nobody can reduce the swap count > because we held the PTL. But, after that, even if its return value is > SWAP_HAS_CACHE (that is, in swap cache), parallel swap_unuse() or > __try_to_reclaim_swap() may remove the folio from swap cache, so free > the swap entry. So, swapoff() can proceed to free the data structures > in parallel. > > To fix the race, we can add get/put_swap_device() in > free_swap_and_cache(). > > For other places, we can check whether get/put_swap_device() has been > called in callers, and the swap reference we held has been decreased > (e.g., swap count protected by PTL, SWAP_HAS_CACHE protected by folio > lock). Yes, sounds reasonable. We should likely update the documentation of get_swap_device(), that after decrementing the refcount, the SI might become stale and should not be touched without a prior get_swap_device(). -- Cheers, David / dhildenb