Received: by 10.223.176.46 with SMTP id f43csp2023513wra; Thu, 25 Jan 2018 03:47:08 -0800 (PST) X-Google-Smtp-Source: AH8x227p5TP8iCZBX6mIymsFYYHtoLkCW/gkL6qEyPY71AQfk+mBJ6RF4s4lwEs2DPxEla95HXsO X-Received: by 10.99.4.216 with SMTP id 207mr12891220pge.45.1516880828384; Thu, 25 Jan 2018 03:47:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516880828; cv=none; d=google.com; s=arc-20160816; b=QAmvGy+9YeU42VyOjZ7NHbj0biWiWP1wuhXAD/s1P1kZynhqkw5FVkhqahp/h1/cjX gNV5bH5A4OFXX7lyumODkiErrc/WpiSioV0/1dqkqvUpHT57ymbcDjQzhLDlBq6MgkVE 9razmp0W0w9rRv9+oI9DW/ohn0ySZb0S7MbxAlpzpRsRvmzyjfuTLiUT/pL4lnujT7Nc SxiIllKRNltMQJLodhdGwBR2c4N91Nz05aYXqhH6WVerygd+snYGqK5Jkp7FMoOr4KB7 LrX08UdKaS97DAeubMtBwUQ6RtTdW7YsHz4nrpwkQgdltyY+3ysyxzK6g1DipEG8jwWS dMKA== 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-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=SVlOMtFuAUnmZopwCJNRkJrdrq7T7T/KWnw+f3xhQMw=; b=wQPkb4FBbQnz0yp77pjLgM1pHBWth9oTSdQG589/50d3VkI7LBCR/owe3HgaH63Kkv m5IX0+7VsBsxSn8rFZJK5ERH8eOooZ7dVj5nIFYJt4u3j6biMgjXpkNurPZY9S17OWh5 XtE2lCIFk4ONxlhY0VdU8njvNBYQVFQxp5f5FQPWo9Lw1V/J3b4WlbNNyXwTmJRJoJbe rvEPfYQYV96YUE7EwxjkXB9LMYmjcI3q4nZZ7QjntzowbZxs0iYGU3e9l+3oyDJLrSwP OlLXdWcfTEWvEGl8MUcL84QLPySu2LwIfW6Rb4qHVo2nrfCS0kYeckL7izB7yRBuwkcH dqYw== ARC-Authentication-Results: i=1; mx.google.com; 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 h13si1427725pgq.776.2018.01.25.03.46.53; Thu, 25 Jan 2018 03:47:08 -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; 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 S1751855AbeAYLqO (ORCPT + 99 others); Thu, 25 Jan 2018 06:46:14 -0500 Received: from mx2.suse.de ([195.135.220.15]:37337 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751712AbeAYLqN (ORCPT ); Thu, 25 Jan 2018 06:46:13 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 3A950ACF9; Thu, 25 Jan 2018 11:46:12 +0000 (UTC) Date: Thu, 25 Jan 2018 12:46:10 +0100 From: Michal Hocko To: Petr Tesarik Cc: linux-mm@kvack.org, Andrew Morton , Vlastimil Babka , linux-kernel@vger.kernel.org, Mel Gorman , Johannes Weiner , Kemi Wang , YASUAKI ISHIMATSU , Andrey Ryabinin , Nikolay Borisov Subject: Re: [PATCH v2] Fix explanation of lower bits in the SPARSEMEM mem_map pointer Message-ID: <20180125114610.GQ28465@dhcp22.suse.cz> References: <20180119080908.3a662e6f@ezekiel.suse.cz> <20180119123956.GZ6584@dhcp22.suse.cz> <20180119142133.379d5145@ezekiel.suse.cz> <20180124124353.GE28465@dhcp22.suse.cz> <20180124145711.4f3f219d@ezekiel.suse.cz> <20180125100516.589ea6af@ezekiel.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180125100516.589ea6af@ezekiel.suse.cz> User-Agent: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu 25-01-18 10:05:16, Petr Tesarik wrote: > The comment is confusing. On the one hand, it refers to 32-bit > alignment (struct page alignment on 32-bit platforms), but this > would only guarantee that the 2 lowest bits must be zero. On the > other hand, it claims that at least 3 bits are available, and 3 bits > are actually used. > > This is not broken, because there is a stronger alignment guarantee, > just less obvious. Let's fix the comment to make it clear how many > bits are available and why. > > Although memmap arrays are allocated in various places, the > resulting pointer is encoded eventually, so I am adding a BUG_ON() > here to enforce at runtime that all expected bits are indeed > available. > > I have also added a BUILD_BUG_ON to check that PFN_SECTION_SHIFT is > sufficient, because this part of the calculation can be easily > checked at build time. > > Signed-off-by: Petr Tesarik Thank you. The check is much simpler than I originally thought. Acked-by: Michal Hocko > --- > include/linux/mmzone.h | 12 ++++++++++-- > mm/sparse.c | 6 +++++- > 2 files changed, 15 insertions(+), 3 deletions(-) > > diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h > index 67f2e3c38939..7522a6987595 100644 > --- a/include/linux/mmzone.h > +++ b/include/linux/mmzone.h > @@ -1166,8 +1166,16 @@ extern unsigned long usemap_size(void); > > /* > * We use the lower bits of the mem_map pointer to store > - * a little bit of information. There should be at least > - * 3 bits here due to 32-bit alignment. > + * a little bit of information. The pointer is calculated > + * as mem_map - section_nr_to_pfn(pnum). The result is > + * aligned to the minimum alignment of the two values: > + * 1. All mem_map arrays are page-aligned. > + * 2. section_nr_to_pfn() always clears PFN_SECTION_SHIFT > + * lowest bits. PFN_SECTION_SHIFT is arch-specific > + * (equal SECTION_SIZE_BITS - PAGE_SHIFT), and the > + * worst combination is powerpc with 256k pages, > + * which results in PFN_SECTION_SHIFT equal 6. > + * To sum it up, at least 6 bits are available. > */ > #define SECTION_MARKED_PRESENT (1UL<<0) > #define SECTION_HAS_MEM_MAP (1UL<<1) > diff --git a/mm/sparse.c b/mm/sparse.c > index 2609aba121e8..6b8b5e91ceef 100644 > --- a/mm/sparse.c > +++ b/mm/sparse.c > @@ -264,7 +264,11 @@ unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn, > */ > static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum) > { > - return (unsigned long)(mem_map - (section_nr_to_pfn(pnum))); > + unsigned long coded_mem_map = > + (unsigned long)(mem_map - (section_nr_to_pfn(pnum))); > + BUILD_BUG_ON(SECTION_MAP_LAST_BIT > (1UL< + BUG_ON(coded_mem_map & ~SECTION_MAP_MASK); > + return coded_mem_map; > } > > /* > -- > 2.13.6 -- Michal Hocko SUSE Labs