Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2540402lqz; Wed, 3 Apr 2024 00:30:11 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW+sQhxeaeOW41UNGTU+GE/ec+gIy2UTCTEI4BWvZXKkQklwGJLYOuOLDnXm6nIcAZr615qTzNHav/V0xflDnGPBS3t5z/AuLbYAqIcdw== X-Google-Smtp-Source: AGHT+IH4AFQ7AvQqeG6BvHL/Wg0AU4Fy7NjDqS6X5snr7TGzz/iR2D9WmkI8oaKXFyu2CeA2AMi3 X-Received: by 2002:a17:903:22c2:b0:1e0:9926:aded with SMTP id y2-20020a17090322c200b001e09926adedmr2101575plg.24.1712129410891; Wed, 03 Apr 2024 00:30:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712129410; cv=pass; d=google.com; s=arc-20160816; b=SXpdTXFUZzARyHw1YnJLLVdCqaQzua/DQNLFRhNDSgzefkUd/I2ozoVpZAqd2XMhOq fgljIaas/dJ70BpRHZpiX/Y2xUWDK3wzZxy5r3QrqHGqcBujYqMW1/ap18Y0VdzTDKcZ q1HgXqI8PvsZ1jzCy5lB+EJCpZOpzLfCPgSfym+qL8hSqH8TTwrc25OiwDXru04wq742 gROQCBkGIM/pq4u7vExQwWZ4YVI3ttIlwobpUUVybozBYvh19LDgBNcCuXmvhzuBkhe3 ZiZArT2PYf5MmX1GQtRZLBvyeaHZsBBirr7DYBN/02/ck7GO037lCyojp2ZsT2qHIP/U Rv1g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=RH7hiU2+lqB45HtkbzCsBLctEA0lOItltldrW3en6cc=; fh=xjbpvM1x8hhNpcdK7xzSATKjvrfFvEId0i/M0PzZh5Q=; b=lsE2pEuMQe+gxzO8k5pJ8Qqfa3GdaGTP3QPHOEoPrNItHf6+YLm7XKHsiyetoqdj2y dMwEi1764ecbs97DHdJFhjCBJO1FhtJYhUGrAG5FaWoy6zXrduveJoviA3X7o1pWaI2B kvC3rTIf2K3vSs9SXxfb7VY6ncYLOnUUkJvadePzRqaXX4tQHwG6IpgK3D/I/ob4B9q+ BWj6xaH1mViOpslkU7HeRztmWyQwpD4CgVPD14TmvA/TWUEM+suMgsULQU8weLDwhzpj B+ilamOUwRUCCr/gu3kIUfTEzQ9GaUA7vME852JLbCxiEUAbowpLBGpTHIg6iUGF0Nte Cf/Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-129155-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129155-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id q15-20020a170902a3cf00b001e2666e6705si3087623plb.336.2024.04.03.00.30.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 00:30:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-129155-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-129155-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-129155-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0CA15285D93 for ; Wed, 3 Apr 2024 07:21:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EFAD758AAC; Wed, 3 Apr 2024 07:21:20 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ADB9A4D133 for ; Wed, 3 Apr 2024 07:21:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712128880; cv=none; b=JdJOfB4P6cTAi0Qw3FfCZbeVxPK7GyQ8h38ffwe6wVd8JOZR7w4CFVGIYRIKclPkgMeNFuPnWZNIKQ9XkPlhECEaQm16uwqq/T9aGOrCRgxlbiDAFQ0pnt6ti3XYWMBhM2txoMKXD9N9RERTOqNa70vYG/9Pf4cFFkIri900N2c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712128880; c=relaxed/simple; bh=Wa5Oh1EEA+Je2ED21aem5RZr/wby3lpBIZHvUQczWJg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=XVvI2yKcDrVINR9ryoc3KOHLTw7lZoF/jvJF1g2+5lsqR70Yzzg8yK0C6uznAFWntjmZMiAg16JSjxdlAwHTgnjn7LJH6sQjXBd1DssKJAndHtHiI9SXlF/R1gR2htZ+1bdKtHUmlG4KLgtgszf2SHIycJ7thrpJD+AI44gccy0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2B4D91007; Wed, 3 Apr 2024 00:21:49 -0700 (PDT) Received: from [10.57.72.245] (unknown [10.57.72.245]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id F2B3B3F7B4; Wed, 3 Apr 2024 00:21:15 -0700 (PDT) Message-ID: <4020c532-d20d-4624-8ea6-607de423396c@arm.com> Date: Wed, 3 Apr 2024 08:21:14 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 2/6] mm: swap: free_swap_and_cache_nr() as batched free_swap_and_cache() Content-Language: en-GB To: Zi Yan Cc: Andrew Morton , David Hildenbrand , Matthew Wilcox , Huang Ying , Gao Xiang , Yu Zhao , Yang Shi , Michal Hocko , Kefeng Wang , Barry Song <21cnbao@gmail.com>, Chris Li , Lance Yang , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240327144537.4165578-1-ryan.roberts@arm.com> <20240327144537.4165578-3-ryan.roberts@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 03/04/2024 01:30, Zi Yan wrote: > On 27 Mar 2024, at 10:45, Ryan Roberts wrote: > >> Now that we no longer have a convenient flag in the cluster to determine >> if a folio is large, free_swap_and_cache() will take a reference and >> lock a large folio much more often, which could lead to contention and >> (e.g.) failure to split large folios, etc. >> >> Let's solve that problem by batch freeing swap and cache with a new >> function, free_swap_and_cache_nr(), to free a contiguous range of swap >> entries together. This allows us to first drop a reference to each swap >> slot before we try to release the cache folio. This means we only try to >> release the folio once, only taking the reference and lock once - much >> better than the previous 512 times for the 2M THP case. >> >> Contiguous swap entries are gathered in zap_pte_range() and >> madvise_free_pte_range() in a similar way to how present ptes are >> already gathered in zap_pte_range(). >> >> While we are at it, let's simplify by converting the return type of both >> functions to void. The return value was used only by zap_pte_range() to >> print a bad pte, and was ignored by everyone else, so the extra >> reporting wasn't exactly guaranteed. We will still get the warning with >> most of the information from get_swap_device(). With the batch version, >> we wouldn't know which pte was bad anyway so could print the wrong one. >> >> Signed-off-by: Ryan Roberts >> --- >> include/linux/pgtable.h | 28 +++++++++++++++ >> include/linux/swap.h | 12 +++++-- >> mm/internal.h | 48 +++++++++++++++++++++++++ >> mm/madvise.c | 12 ++++--- >> mm/memory.c | 13 +++---- >> mm/swapfile.c | 78 ++++++++++++++++++++++++++++++----------- >> 6 files changed, 157 insertions(+), 34 deletions(-) >> >> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h >> index 09c85c7bf9c2..8185939df1e8 100644 >> --- a/include/linux/pgtable.h >> +++ b/include/linux/pgtable.h >> @@ -708,6 +708,34 @@ static inline void pte_clear_not_present_full(struct mm_struct *mm, >> } >> #endif >> >> +#ifndef clear_not_present_full_ptes >> +/** >> + * clear_not_present_full_ptes - Clear consecutive not present PTEs. >> + * @mm: Address space the ptes represent. >> + * @addr: Address of the first pte. >> + * @ptep: Page table pointer for the first entry. >> + * @nr: Number of entries to clear. >> + * @full: Whether we are clearing a full mm. >> + * >> + * May be overridden by the architecture; otherwise, implemented as a simple >> + * loop over pte_clear_not_present_full(). >> + * >> + * Context: The caller holds the page table lock. The PTEs are all not present. >> + * The PTEs are all in the same PMD. >> + */ >> +static inline void clear_not_present_full_ptes(struct mm_struct *mm, >> + unsigned long addr, pte_t *ptep, unsigned int nr, int full) >> +{ >> + for (;;) { >> + pte_clear_not_present_full(mm, addr, ptep, full); >> + if (--nr == 0) >> + break; >> + ptep++; >> + addr += PAGE_SIZE; >> + } >> +} >> +#endif >> + > > Would the code below be better? > > for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) > pte_clear_not_present_full(mm, addr, ptep, full); I certainly agree that this is cleaner and more standard. But I'm copying the pattern used by the other batch helpers. I believe this pattern was first done by Willy for set_ptes(), then continued by DavidH for wrprotect_ptes() and clear_full_ptes(). I guess the benefit is that ptep and addr are only incremented if we are going around the loop again. I'd rather continue to be consistent with those other helpers. > > -- > Best Regards, > Yan, Zi