Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp815146rwb; Thu, 1 Dec 2022 08:41:59 -0800 (PST) X-Google-Smtp-Source: AA0mqf4bXwAaHvCbwvArtVwYcUZh91LnMdBHBZ14xixzHmT/yjcHNFyBrA/X1Z4EK4Vp4ZSgsu5F X-Received: by 2002:aa7:c94a:0:b0:46b:74e1:872c with SMTP id h10-20020aa7c94a000000b0046b74e1872cmr12853175edt.301.1669912918763; Thu, 01 Dec 2022 08:41:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669912918; cv=none; d=google.com; s=arc-20160816; b=VABg7KxcmiXBpHMARXf+hiCAhra7xUEA5nYdAgm9jMeQdSMp2nIsVrHhF/gajyL7+Z dpfdkN/tJyK+eWfkXp8nYo/KcslM/dgrunbIVMbZt5JICEeba14tO6Rjdspu9ahzVf0t T/vFaDMKJV20uWFGzBn5SETLlnqDvttacsQpUXX2aM8YyicjVfYuslckiXVQqV0pxWUH jFPs6K1qqi295FIDYY0iD52jbl296Iw/9SfFplWglcxDMrZciIhxCBJ6FS/EIgaQnFzz OyyHrPZTT83BnSNvhNnvgsS5UbIpI/Wn7AzqkZrI4dNoNx09m4k0FwK+GIo3OmFofBQW nJHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=QZICexc8xsGDU62se8MLzCQs14y/akiq818luXMxAoY=; b=UYDuazMchaA2NoV8Xs0Pyfz2Vyv+Mrk8l0PUXF7uabfTONylRKqKV3LtrnDhTbEbhu jbhlvlMhjTPu+6RU6zbRaLyQkBwGLA8tu7IVtiLyncBKIN54pUECHNAMePFS2kPkYLgv J9XPSxOqhMyLg7DmvYfCvTSDO9AOYJ+KHva+jdocKf3mOmorUtIgedydeiSJvyj2Pfsq JNi9dEFAISLq4OOsFooAq/1m9Gv/ySiYcAkgDCaaSjt9lc5Lu0VyU//0zrV9IW0t4cIy zX9cgcPD0hq+OcKhgmn4BR9b8NK6Am47j5vDperdmmRE/DnQ2aDKGM8JUnWYK9K9tBn9 Pl0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=nLdF3MgZ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ho19-20020a1709070e9300b007ade664ee53si4715480ejc.148.2022.12.01.08.41.38; Thu, 01 Dec 2022 08:41:58 -0800 (PST) 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=@cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=nLdF3MgZ; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230143AbiLAPwA (ORCPT + 82 others); Thu, 1 Dec 2022 10:52:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231468AbiLAPv5 (ORCPT ); Thu, 1 Dec 2022 10:51:57 -0500 Received: from mail-qt1-x82c.google.com (mail-qt1-x82c.google.com [IPv6:2607:f8b0:4864:20::82c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBF62A6CD8 for ; Thu, 1 Dec 2022 07:51:55 -0800 (PST) Received: by mail-qt1-x82c.google.com with SMTP id fp23so988907qtb.0 for ; Thu, 01 Dec 2022 07:51:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20210112.gappssmtp.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=QZICexc8xsGDU62se8MLzCQs14y/akiq818luXMxAoY=; b=nLdF3MgZ41GcFh+AMHGP6e37c5gGALVjl1T7QxmWOXvPoDybB4EyOcncA+v8e1aQ3y kF43faFVT5i2ogbHi8Xh9fRkjxddevBI+Xa801fZJ6sogb5BSFkZJynRCMCnm5Hcln+z h6ULkbiQ8wFSdz2qqMnvaApw9XIUQJO5Lce4eaS39G3bbtx350i2m055PGe4Y/Q8k7Xk UGBqPrjAwCr73TCtjw2ewuQuCFGRRNKnBzkRxQrFdjLXxArH9Scphip7TWKAeFVRTA7S NukPVM39vrjE/Z0SjdhAePvS10HX5K8//ggATbKtCj25ubtQYITvpperZj74K9mcTju9 lu+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=QZICexc8xsGDU62se8MLzCQs14y/akiq818luXMxAoY=; b=Nu4SG8h4untOjLoIf9DYA8SSSuw31vk3Z6X0D/VKLGOiQeZlBs03XMpE6rn4dS82MX hYU3UChH51GmlLsJSSJvPH7Rllh8JttqMW7uFQvQ7aUIa/Ry1J7OqFLDUqMM+PZbKo90 k6UYT9o5aKzwgpuzJhTa6kE3MKRgtz7zozGmvvLgb4YYXzMtUufQYLl9sxtKJyTLK5XY Ten06eGyS1MZayE8AU6R+fxv3mpMPCMcESTZTubEkoaVVHikoON7iKk0OCxYLhetWcCZ eSfBVAI7DAuYVu9ix9cS7nWvORDUUCDJPe+LHSwkYFRza0EOTgbKuvkFpNxjEqu2Iw/f wkxg== X-Gm-Message-State: ANoB5plrvmUTdiRw4eZf1jOIrvqEVUxSmlzUhiOLL7eq5ESUep1QU2of MSkImI4d7ak4gRtBIyf33PNitA== X-Received: by 2002:ac8:7eed:0:b0:3a5:40ab:5952 with SMTP id r13-20020ac87eed000000b003a540ab5952mr42915558qtc.254.1669909915024; Thu, 01 Dec 2022 07:51:55 -0800 (PST) Received: from localhost (2603-7000-0c01-2716-3663-3884-f85a-44bb.res6.spectrum.com. [2603:7000:c01:2716:3663:3884:f85a:44bb]) by smtp.gmail.com with ESMTPSA id y6-20020a05620a25c600b006b9c9b7db8bsm3767587qko.82.2022.12.01.07.51.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 07:51:54 -0800 (PST) Date: Thu, 1 Dec 2022 10:52:31 -0500 From: Johannes Weiner To: Hugh Dickins Cc: Shakeel Butt , Andrew Morton , Linus Torvalds , Michal Hocko , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm: remove lock_page_memcg() from rmap Message-ID: References: <20221123181838.1373440-1-hannes@cmpxchg.org> <16dd09c-bb6c-6058-2b3-7559b5aefe9@google.com> <3659bbe0-ccf2-7feb-5465-b287593aa421@google.com> <33f2f836-98a0-b593-1d43-b289d645db5@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <33f2f836-98a0-b593-1d43-b289d645db5@google.com> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,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 On Wed, Nov 30, 2022 at 04:13:23PM -0800, Hugh Dickins wrote: > On Wed, 30 Nov 2022, Johannes Weiner wrote: > > > > Hm, I think the below should work for swap pages. Do you see anything > > obviously wrong with it, or scenarios I haven't considered? > > > > I think you're overcomplicating it, with the __swap_count(ent) business, > and consequent unnecessarily detailed comments on the serialization. > > Page/folio lock prevents a !page_mapped(page) becoming a page_mapped(page), > whether it's in swap cache or in file cache; it does not stop the sharing > count going further up, or down even to 0, but we just don't need to worry > about that sharing count - the MC_TARGET_PAGE case does not reject pages > with mapcount > 1, so why complicate the swap or file case in that way? > > (Yes, it can be argued that all such sharing should be rejected; but we > didn't come here to argue improvements to memcg charge moving semantics: > just to minimize its effect on rmap, before it is fully deprecated.) > > Or am I missing the point of why you add that complication? No, it just seemed odd to move shared swap *unless* it's partially faulted. But you're right, it's probably not worth the hassle. I'll cut this down to the page_mapped() check. The struggle of writing code for Schroedinger's User... > > @@ -5637,6 +5645,46 @@ static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, > > Don't forget to trylock the page in the device_private case before this. Yep, thanks! > > * we call find_get_page() with swapper_space directly. > > */ > > page = find_get_page(swap_address_space(ent), swp_offset(ent)); > > + > > + /* > > + * Don't move shared charges. This isn't just for saner move > > + * semantics, it also ensures that page_mapped() is stable for > > + * the accounting in mem_cgroup_mapcount(). > > mem_cgroup_mapcount()?? mem_cgroup_move_account() of course! Will fix. > > + * We have to serialize against the following paths: fork > > + * (which may copy a page map or a swap pte), fault (which may > > + * change a swap pte into a page map), unmap (which may cause > > + * a page map or a swap pte to disappear), and reclaim (which > > + * may change a page map into a swap pte). > > + * > > + * - Without swapcache, we only want to move the charge if > > + * there are no other swap ptes. With the pte lock, the > > + * swapcount is stable against all of the above scenarios > > + * when it's 1 (our pte), which is the case we care about. > > + * > > + * - When there is a page in swapcache, we only want to move > > + * charges when neither the page nor the swap entry are > > + * mapped elsewhere. The pte lock prevents our pte from > > + * being forked or unmapped. The page lock will stop faults > > + * against, and reclaim of, the swapcache page. So if the > > + * page isn't mapped, and the swap count is 1 (our pte), the > > + * test results are stable and the charge is exclusive. ... and edit this down accordingly. > > + */ > > + if (!page && __swap_count(ent) != 1) > > + return NULL; > > + > > + if (page) { > > + if (!trylock_page(page)) { > > + put_page(page); > > + return NULL; > > + } > > + if (page_mapped(page) || __swap_count(ent) != 1) { > > + unlock_page(page); > > + put_page(page); > > + return NULL; > > + } > > + } > > + > > entry->val = ent.val; > > > > return page; > > Looks right, without the __swap_count() additions and swap count comments. > > And similar code in mc_handle_file_pte() - or are you saying that only > swap should be handled this way? I would disagree. Right, same rules apply there. I only pasted the swap one to make sure we get aligned on the basic strategy. > And matching trylock in mc_handle_present_pte() (and get_mctgt_type_thp()), > instead of in mem_cgroup_move_account(). Yes. > I haven't checked to see where the page then needs to be unlocked, > probably some new places. Yes, the callers of get_mctgt_type*() need to unlock (if target is passed and the page is returned). It looks straight-forward, they already have to do put_page(). > And I don't know what will be best for the preliminary precharge pass: > doesn't really want the page lock at all, but it may be unnecessary > complication to avoid taking it then unlocking it in that pass. We could make it conditional on target, which precharge doesn't pass, but I agree it's likely not worth optimizing that code at this point. Thanks for taking a look, Hugh, that's excellent input. I'll finish this patch, rebase the rmap patch on it, and add a new one to issue a deprecation warning in mem_cgroup_move_charge_write(). Johannes