Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1734669rwd; Mon, 15 May 2023 02:11:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5F+NKbw6EGE5sA5YQxUpVfuJFt8f08jD/ETekXB3nJnTA/ZIluqSzs8wWqGcaupGMXigyr X-Received: by 2002:a17:90a:f309:b0:24d:f739:d62a with SMTP id ca9-20020a17090af30900b0024df739d62amr32841308pjb.23.1684141871733; Mon, 15 May 2023 02:11:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684141871; cv=none; d=google.com; s=arc-20160816; b=xBfBtFWi8ZRVVwfPmtPrmSXfDNbsDXUr18fVf1cz3bflCJEHvDyJjPOdvmk/fI+BER WfQpxnxPxI2V+Z1v8Oh7yngd0u8PBUKBgWaEZeZ8v2UthPRI39B3Rerv3ZXyzkfH3HB5 MEfdX58pucvsmM9s1+Qgw8BI8l+alFR2e0JjJ3JqIn0eRvOot0iZsV9vA74SBEJPQMIc amDGEFxI2WcL0zYIbW26O3/GIFvTmLrFeCsG+297+PViryiiwZH6cTKgW30C6n6AEPM5 PWxh/qfyZR4oJrmiVFVbXQpGEj/IOB/vM0pzLYbJARxJBcU5BM7OP8n3CRPGs8SmH2Or q/TQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:message-id:in-reply-to :date:references:subject:cc:to:from:dkim-signature; bh=lv6Xd/Y7XESC1jt2h68SrQefttY0i4tRmGoEw7VoNDE=; b=MaZEsA2mEx7vB6OA8J/Czhlrhi9T6kzcQYBi9Pjh9XJpp/Wsl7xrr8hcSASgXUMsUk E/pIuEJ2/TDQPGxPsaEqt9NxhOhnyE1mO3skpPe5B3Sqbow4O9Zf0JsnH5SC9nSoq8uS QwdtG9mAYnc2r3wMsPpSMzmuRem47SnMWQwoTL5m4A1rn78qnRJMdwBzFhXRhs1Z8yhm UItTA4F162g8eI5kxqHM2aOKFfBHuGvcKQ3SA4HYcQYmCTK3rMhdeWG06k3r0rpJ2gv/ DiNJAalBHBUN8zoVExRAVNugE0FS7/vHk9TT4/CSpadO2NBPSyB+fKY15VuGNN8RYOiB HhXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=noL66pbr; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o19-20020a17090ad25300b0023d22d0f0fdsi29681571pjw.19.2023.05.15.02.10.59; Mon, 15 May 2023 02:11:11 -0700 (PDT) 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=@intel.com header.s=Intel header.b=noL66pbr; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238019AbjEOItN (ORCPT + 99 others); Mon, 15 May 2023 04:49:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238433AbjEOIsn (ORCPT ); Mon, 15 May 2023 04:48:43 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35C901BDD for ; Mon, 15 May 2023 01:48:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684140513; x=1715676513; h=from:to:cc:subject:references:date:in-reply-to: message-id:mime-version; bh=NP9grGktDYmSW/y0bEvt2e7TpvQ9W2Et0cmTgeeaOa8=; b=noL66pbr4bYDXddk7Y3UG8CGKmf5WYSDiILIIiYKK0mYJt1EgZnzD4kB h1JbHN+NdETPmqIz27b05yh/jFE/QZzZqLHP3CjbypwxucG3zXVogbT5T Zc0/LVk+S3Ju2ZZzsibWdmsD6eDGhbejGbcdctBNrr4lJWk2ThqUn7d+D lX+kDdDHQQR5bzj2gdD3T7X8XYXZKb//S8P9PIBMLflp+MJz2iQRKaY4m NTnyV7khhrzJ9kP7ShH7m9YWYPVrluUT246rQjGvofXMtohSEL3tjisHk +h2ZP8DGURh8OeXQlaEsy6ItFqcF+x3yQ75hqS76re1OoCYVE9312v2JI w==; X-IronPort-AV: E=McAfee;i="6600,9927,10710"; a="379307406" X-IronPort-AV: E=Sophos;i="5.99,276,1677571200"; d="scan'208";a="379307406" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 May 2023 01:48:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10710"; a="845163661" X-IronPort-AV: E=Sophos;i="5.99,276,1677571200"; d="scan'208";a="845163661" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 May 2023 01:48:27 -0700 From: "Huang, Ying" To: Khalid Aziz Cc: akpm@linux-foundation.org, willy@infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Steve Sistare , Khalid Aziz , Mel Gorman Subject: Re: [PATCH v2] mm, compaction: Skip all non-migratable pages during scan References: <20230512190000.103737-1-khalid.aziz@oracle.com> Date: Mon, 15 May 2023 16:47:17 +0800 In-Reply-To: <20230512190000.103737-1-khalid.aziz@oracle.com> (Khalid Aziz's message of "Fri, 12 May 2023 13:00:00 -0600") Message-ID: <871qji7zhm.fsf@yhuang6-desk2.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 Hi, Khalid, Cced Mel. Khalid Aziz writes: > Pages pinned in memory through extra refcounts can not be migrated. > Currently as isolate_migratepages_block() scans pages for > compaction, it skips any pinned anonymous pages. All non-migratable > pages should be skipped and not just the anonymous pinned pages. > This patch adds a check for extra refcounts on a page to determine > if the page can be migrated. This was seen as a real issue on a > customer workload where a large number of pages were pinned by vfio > on the host and any attempts to allocate hugepages resulted in > significant amount of cpu time spent in either direct compaction or > in kcompatd scanning vfio pinned pages over and over again that can s/kcompatd/kcompactd/ > not be migrated. With the patch below, the cycles for kcompactd disappeared? > Signed-off-by: Khalid Aziz > Suggested-by: Steve Sistare > Cc: Khalid Aziz > --- > v2: > - Update comments in the code (Suggested by Andrew) > - Use PagePrivate() instead of page_has_private() (Suggested > by Matthew) > - Pass mapping to page_has_extrarefs() (Suggested by Matthew) > - Use page_ref_count() (Suggested by Matthew) > - Rename is_pinned_page() to reflect its function more > accurately (Suggested by Matthew) > > mm/compaction.c | 36 ++++++++++++++++++++++++++++++++---- > 1 file changed, 32 insertions(+), 4 deletions(-) > > diff --git a/mm/compaction.c b/mm/compaction.c > index 5a9501e0ae01..837f20df2bbb 100644 > --- a/mm/compaction.c > +++ b/mm/compaction.c > @@ -764,6 +764,34 @@ static bool too_many_isolated(pg_data_t *pgdat) > return too_many; > } > > +/* > + * Check if this base page should be skipped from isolation because > + * it has extra refcounts that will prevent it from being migrated. > + * This function is called for regular pages only, and not > + * for THP or hugetlbfs pages. This code is inspired by similar code > + * in migrate_vma_check_page(), can_split_folio() and > + * folio_migrate_mapping() > + */ > +static inline bool page_has_extrarefs(struct page *page, Better to be named as page_has_extra_refs()? > + struct address_space *mapping) > +{ > + unsigned long extra_refs; > + > + /* anonymous page can have extra ref from swap cache */ > + if (mapping) > + extra_refs = 1 + PagePrivate(page); > + else > + extra_refs = PageSwapCache(page) ? 1 : 0; IIUC, mapping != NULL if PageSwapCache(page) is true. Please check the implementation of page_mapping(). And even if mapping == NULL, the extra_refs should be 1, because we have elevated the page refcount in isolate_migratepages_block() before checking whether the page is pinned. IIUC, this is the original behavior. Or, we can add "- 1" in the following checking. > + > + /* > + * This is an admittedly racy check but good enough to determine > + * if a page is pinned and can not be migrated > + */ > + if ((page_ref_count(page) - extra_refs) > page_mapcount(page)) > + return true; > + return false; > +} > + > /** > * isolate_migratepages_block() - isolate all migrate-able pages within > * a single pageblock > @@ -992,12 +1020,12 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, > goto isolate_fail; > > /* > - * Migration will fail if an anonymous page is pinned in memory, > - * so avoid taking lru_lock and isolating it unnecessarily in an > - * admittedly racy check. > + * Migration will fail if a page has extra refcounts > + * preventing it from migrating, so avoid taking > + * lru_lock and isolating it unnecessarily > */ > mapping = page_mapping(page); > - if (!mapping && (page_count(page) - 1) > total_mapcount(page)) > + if (page_has_extrarefs(page, mapping)) > goto isolate_fail_put; > > /* Best Regards, Huang, Ying