Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5053563ybl; Tue, 14 Jan 2020 02:42:59 -0800 (PST) X-Google-Smtp-Source: APXvYqxHEvK/hN5guAhb42IR/0PzRMd/p+VB54WJ7QAhjMZTs0RKxP/V7OjE9cga9MJtQhyg/c45 X-Received: by 2002:a9d:811:: with SMTP id 17mr16914944oty.369.1578998579863; Tue, 14 Jan 2020 02:42:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578998579; cv=none; d=google.com; s=arc-20160816; b=aNkIGhcgxZyg3lISjLor7m03cqD7ayqxlJMH5U2lFeJGEqC85YaoXYJZ0r7ic0uKJ3 nVc3/E+A8zDi/YkAw0+SC3Lb7O9hnfwGADeka2vSkKqkG+aawoGmSRf7mo0LU/R1c8ne c0kK60aM9OXRU5IOjx8uq4fcJiyO1tqRRc/Aab63ZUWKrUxxpJq/OJoPZ0XaDsINTYTb 1/6ooFl06LQSq1caJ4hcQ6oenMdkhpEU9HDJQd9UE48oTW3dHMoKCOVi8isD+AXJawQH PRdIVcCqAKhdKDFvotET05y+7KwDdGM1pVRoYT/Ym+7uh6e8XyNucJOzPxajqVojB1E2 Ueow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ciknfppj1LCOHQAPNZhrYGHRrM+p8c24oaVfpOlu9CU=; b=GdniFWYjqCzxtpO8PnLtNjwOaqgrzTLA7mD9rykWcLPAJY6dpeYdj3YPm6OM/us4Xy QGDH8rz5iBzllYod3hjPuarLIaWdS7ZvCzHiU7PLrASqLp62lWR9mcV98CeaC5djnIwi UlzZZLMZnXCZ3FaVqpK/0ZOBp0G7ktFppyPwnFHezQGMow7OupQy1zFXtlRWCs4qwvy7 7xFHZ9TmV3MLId0gEpf3jp7Iw706S+QW28jPF1D0aKUcdhPsX2qXCzo9K+pn4xG9AI8d i4QDt2DK77VG7WjZewYBe0vWXNMSetx2KrZlus2rIZP38wZofJeR2Dvgk6B2bJ6SsPU2 ky3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=D8dS1CwR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p19si7263418oic.216.2020.01.14.02.42.48; Tue, 14 Jan 2020 02:42:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=D8dS1CwR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729503AbgANKlS (ORCPT + 99 others); Tue, 14 Jan 2020 05:41:18 -0500 Received: from mail-lj1-f194.google.com ([209.85.208.194]:37443 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725956AbgANKlS (ORCPT ); Tue, 14 Jan 2020 05:41:18 -0500 Received: by mail-lj1-f194.google.com with SMTP id o13so13748775ljg.4 for ; Tue, 14 Jan 2020 02:41:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=ciknfppj1LCOHQAPNZhrYGHRrM+p8c24oaVfpOlu9CU=; b=D8dS1CwRdv4R6AuTAzh6R3DnDJF22Y3FxbdHfLb2409DLsWz8gAfHlk428NSgrdgLO NjdxkwAKeY5Kur60odrJ+vowBJOF690oPx3CzOV13Zs42tu00UlKvGCJCf2bomxCsyLG AUibM1WEOWuvhj7akWItw2LSu31aihjZxdMs5UTLicQc1SiFHWAoOpdgVK0sBFHOPkQC 9fdeHzmoVx2GUbE1NaI3qT8Z8EYUek5pEvW9cajGNwkU3X6SLvP/g2FWhbaRBsaazU0L fbSjgeECYGGRB01cbRFxqBuAai8OuSSU/9eRsTYVvsx1cHkrVVynNu6JekAACW6aZaE9 qRcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=ciknfppj1LCOHQAPNZhrYGHRrM+p8c24oaVfpOlu9CU=; b=iZZp2vHKRmNB6QxI3kTrDfvRvqDYVd+wp4MmomljDnksK8BnXoMLN7/fFgQyLA8w6q GqSKvXvWn9hdKmFtvWZTW76yYqpuCaeG5r3/Y7dGXd3eSd1t6/cks2Zmvl679Jtqcni5 S7dqedoSUmN21ziMeOhZ1NYkOSipqpNO7XMEGaE44lJxl6kde5A2hMxAZJEk2UbRydH8 6HtwdUe5nTd8FyVL1++sfPw27oj1jINmDzN0OrGFt9iml/Y6JXfgAEzvBzZ6MgmosMv+ ht3nFulH007rs0jaJG986m9Ny7SBIYjbJxo25kAsZEstv4LO45z05yKnGG3MawcAmzv4 gg0A== X-Gm-Message-State: APjAAAUQ12J9CJ6Ferm53e/6dx9o1t+qlfDiJfhcZt3vwVrDIFttcaN/ nSrlaDCVGbhv0aDj9236+2g4bQ== X-Received: by 2002:a2e:8188:: with SMTP id e8mr12524747ljg.57.1578998475904; Tue, 14 Jan 2020 02:41:15 -0800 (PST) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id r21sm7346380ljn.64.2020.01.14.02.41.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jan 2020 02:41:15 -0800 (PST) Received: by box.localdomain (Postfix, from userid 1000) id 496BF100823; Tue, 14 Jan 2020 13:41:19 +0300 (+03) Date: Tue, 14 Jan 2020 13:41:19 +0300 From: "Kirill A. Shutemov" To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Michal Hocko , Oscar Salvador Subject: Re: [PATCH v1 2/2] mm: factor out next_present_section_nr() Message-ID: <20200114104119.pybggnb4b2mq45wr@box> References: <0B77E39C-BD38-4A61-AB28-3578B519952F@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 14, 2020 at 12:02:00AM +0100, David Hildenbrand wrote: > > > > Am 13.01.2020 um 23:57 schrieb David Hildenbrand : > > > >  > > > >>> Am 13.01.2020 um 23:41 schrieb Kirill A. Shutemov : > >>> > >>> On Mon, Jan 13, 2020 at 03:40:35PM +0100, David Hildenbrand wrote: > >>> Let's move it to the header and use the shorter variant from > >>> mm/page_alloc.c (the original one will also check > >>> "__highest_present_section_nr + 1", which is not necessary). While at it, > >>> make the section_nr in next_pfn() const. > >>> > >>> In next_pfn(), we now return section_nr_to_pfn(-1) instead of -1 once > >>> we exceed __highest_present_section_nr, which doesn't make a difference in > >>> the caller as it is big enough (>= all sane end_pfn). > >>> > >>> Cc: Andrew Morton > >>> Cc: Michal Hocko > >>> Cc: Oscar Salvador > >>> Cc: Kirill A. Shutemov > >>> Signed-off-by: David Hildenbrand > >>> --- > >>> include/linux/mmzone.h | 10 ++++++++++ > >>> mm/page_alloc.c | 11 ++--------- > >>> mm/sparse.c | 10 ---------- > >>> 3 files changed, 12 insertions(+), 19 deletions(-) > >>> > >>> diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h > >>> index c2bc309d1634..462f6873905a 100644 > >>> --- a/include/linux/mmzone.h > >>> +++ b/include/linux/mmzone.h > >>> @@ -1379,6 +1379,16 @@ static inline int pfn_present(unsigned long pfn) > >>> return present_section(__nr_to_section(pfn_to_section_nr(pfn))); > >>> } > >>> > >>> +static inline unsigned long next_present_section_nr(unsigned long section_nr) > >>> +{ > >>> + while (++section_nr <= __highest_present_section_nr) { > >>> + if (present_section_nr(section_nr)) > >>> + return section_nr; > >>> + } > >>> + > >>> + return -1; > >>> +} > >>> + > >>> /* > >>> * These are _only_ used during initialisation, therefore they > >>> * can use __initdata ... They could have names to indicate > >>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c > >>> index a92791512077..26e8044e9848 100644 > >>> --- a/mm/page_alloc.c > >>> +++ b/mm/page_alloc.c > >>> @@ -5852,18 +5852,11 @@ overlap_memmap_init(unsigned long zone, unsigned long *pfn) > >>> /* Skip PFNs that belong to non-present sections */ > >>> static inline __meminit unsigned long next_pfn(unsigned long pfn) > >>> { > >>> - unsigned long section_nr; > >>> + const unsigned long section_nr = pfn_to_section_nr(++pfn); > >>> > >>> - section_nr = pfn_to_section_nr(++pfn); > >>> if (present_section_nr(section_nr)) > >>> return pfn; > >>> - > >>> - while (++section_nr <= __highest_present_section_nr) { > >>> - if (present_section_nr(section_nr)) > >>> - return section_nr_to_pfn(section_nr); > >>> - } > >>> - > >>> - return -1; > >>> + return section_nr_to_pfn(next_present_section_nr(section_nr)); > >> > >> This changes behaviour in the corner case: if next_present_section_nr() > >> returns -1, we call section_nr_to_pfn() for it. It's unlikely would give > >> any valid pfn, but I can't say for sure for all archs. I guess the worst > >> case scenrio would be endless loop over the same secitons/pfns. > >> > >> Have you considered the case? > > > > Yes, see the patch description. We return -1 << PFN_SECTION_SHIFT, so a number close to the end of the address space (0xfff...000). (Will double check tomorrow if any 32bit arch could be problematic here) > > ... but thinking again, 0xfff... is certainly an invalid PFN, so this should work just fine. > > (biggest possible pfn is -1 >> PFN_SHIFT) > > But it‘s late in Germany, will double check tomorrow :) If the end_pfn happens the be more than -1UL << PFN_SECTION_SHIFT we are screwed: the pfn is invalid, next_present_section_nr() returns -1, the next iterartion is on the same pfn and we have endless loop. The question is whether we can prove end_pfn is always less than -1UL << PFN_SECTION_SHIFT in any configuration of any arch. It is not obvious for me. -- Kirill A. Shutemov