Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3180472rwb; Wed, 30 Nov 2022 16:50:37 -0800 (PST) X-Google-Smtp-Source: AA0mqf7sCr2RnNkYuJQLxwfnrhHglq7Yoe0ENC9tQuDR8jbEik+WHVOMxvaU5ny89ZGDzVkvCXqi X-Received: by 2002:a63:5261:0:b0:477:e6ef:2ed2 with SMTP id s33-20020a635261000000b00477e6ef2ed2mr24436719pgl.219.1669855837605; Wed, 30 Nov 2022 16:50:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669855837; cv=none; d=google.com; s=arc-20160816; b=RfAFMQkz09ENZLmmYn+K7BrXZBSuynklenGsfafmnbBKGifV69UN81JfqQkBG2RsPP ggXfjLLVLqBkN7D3PaDOCitm2g9ZA07n96gpjOjf9KndhGCgW5mcq3N1cCK6GmeLm3Bz as2D8CWXzdGa50dvtj2kO/C7cHqthQy8wvyV6/O7QWn/9vDm66QKqA9wa3MHQBAgxl0j wmNG+on2LvEIo1iqkTdT10lQ6wJhuJ7InUjRS4CzfIvVVp2qM2wTaQY26wkYGF2pjIxx uQtqCgcG7bm1KKP4bs7byL1TcTXhIUV2Cjny7q7g70osSOmuN9I8R/voAobT/xf2a4n6 ibZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:message-id:in-reply-to :subject:cc:to:from:date:dkim-signature; bh=aHV8O4q/YJYqi3y8OK2heA1MTuVYBE9Z71+HHXjgjL4=; b=tbGWxA1gv95OezLZd96EDFZMvYDoLqfYRcKPgIiM9dgzljQW5512z8s3+gbBlDIzf3 xQZfYVN1BflXF+JHuG1ax/TLjUefhSJb5iqfu3vrJQyaZPNl0F/JlZY7nhGyOVdRpRmK IzlxzEKYZEodhcuGzkEyEjJYI5+tJJXQqKug+sS8r/aCMBoLbt2ql7R2BSpkdrIy85DJ jcZiP47R2L5hEgm6RF3Q73RsQQP+6o6dOU25K457AZdriEQk01mQzTIEQimSEKKF6tBL qkTU6za1KwIuxUyK+id1FvrrKEGxhZCGQmg/n1j1Uvs/h9jXnT1moWE0ujApfPgHpFPn WGZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=IdVVPMRb; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l15-20020a654c4f000000b0047701a2244esi2718495pgr.773.2022.11.30.16.50.26; Wed, 30 Nov 2022 16:50:37 -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=@google.com header.s=20210112 header.b=IdVVPMRb; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230447AbiLAARV (ORCPT + 82 others); Wed, 30 Nov 2022 19:17:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230494AbiLAAQ6 (ORCPT ); Wed, 30 Nov 2022 19:16:58 -0500 Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E9978D649 for ; Wed, 30 Nov 2022 16:13:37 -0800 (PST) Received: by mail-qk1-x72e.google.com with SMTP id j26so72002qki.10 for ; Wed, 30 Nov 2022 16:13:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=aHV8O4q/YJYqi3y8OK2heA1MTuVYBE9Z71+HHXjgjL4=; b=IdVVPMRbeJD3ERLIYatf7Qhl7gSloz9FAjQA5VPxurI7mOMGOlRe1SmMkxP8nXen+h ctCSsHsndDMVyouwDaQm0v7TXfHq0aby4aifQ/E+OXRoPNWWRgh40xnSMefoKPKxsmiQ ZtIHvUylWyyCoco0II+oxM9XH6gfMt1tybat6VJivkyIA44CQk1UoG15QByOzCWQKUUR 2w4ENrKjCgXgpRcCgsQFoYOFzLSHwZSBMSIxZC5+Qjkn+HaH+uJfn1bOX7w8JWghI2MW 3oH9d2vIdfEElOxXDK/quYJ7tUiZOVe1aS7CsaLh9UpRs/BE+dX7qVyUmLRqtQSSj0EE N5TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aHV8O4q/YJYqi3y8OK2heA1MTuVYBE9Z71+HHXjgjL4=; b=ZH3Qv8+YQStm7hwZqeZCt1Uj5dSRa+xgEOVGHqxnq69GO6qk3zS8AlrCPMz3P8cW5K 0p35kOmHVy3X2ilh0xP2vz6tZe6Kri+aWbFqknE1V9a5GPS79vpFJI0wMh0msHZn5sEl p3ZgpXZPZDGJey5oNk4RA2Y06VjfFRtSkhRH35HcterNUtACiH0jWcs/GilJoEEvG79z c7eHPNttKM0XqY3BTHPNblCsXwpTuKxKhLQqja+sckrdB04ZbgUNomKvTSXOCZcHjiDW rtP+fR7IXFSGLX9EEuHMID17XD1qhnGy9bUvRgqHLAu5qmGbujmvDufz6clPk1m/avUe KZNg== X-Gm-Message-State: ANoB5pnO44JARTmYgK0JR499MtCiP6uDG2AG71QHxf6WWnrSdujV2qUM lWX4utkekH1azHEclBgS/FAFFg== X-Received: by 2002:a37:34f:0:b0:6fc:a68e:43ec with SMTP id 76-20020a37034f000000b006fca68e43ecmr185704qkd.436.1669853616057; Wed, 30 Nov 2022 16:13:36 -0800 (PST) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id s12-20020a05620a0bcc00b006ee7e223bb8sm2328277qki.39.2022.11.30.16.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Nov 2022 16:13:35 -0800 (PST) Date: Wed, 30 Nov 2022 16:13:23 -0800 (PST) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Johannes Weiner cc: Hugh Dickins , 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 In-Reply-To: Message-ID: <33f2f836-98a0-b593-1d43-b289d645db5@google.com> References: <20221123181838.1373440-1-hannes@cmpxchg.org> <16dd09c-bb6c-6058-2b3-7559b5aefe9@google.com> <3659bbe0-ccf2-7feb-5465-b287593aa421@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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, 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? > @@ -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. > * 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()?? > + * > + * 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. > + */ > + 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. And matching trylock in mc_handle_present_pte() (and get_mctgt_type_thp()), instead of in mem_cgroup_move_account(). I haven't checked to see where the page then needs to be unlocked, probably some new places. 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. Hugh