From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44BA5C433F5 for ; Fri, 1 Apr 2022 18:39:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C66E6B0093; Fri, 1 Apr 2022 14:29:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9753B6B0095; Fri, 1 Apr 2022 14:29:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8160D8D0002; Fri, 1 Apr 2022 14:29:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id 749B86B0093 for ; Fri, 1 Apr 2022 14:29:49 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 4CD75610AD for ; Fri, 1 Apr 2022 18:29:39 +0000 (UTC) X-FDA: 79309148478.12.5B4B992 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) by imf07.hostedemail.com (Postfix) with ESMTP id C0D1B4000B for ; Fri, 1 Apr 2022 18:29:38 +0000 (UTC) Received: by mail-lf1-f43.google.com with SMTP id p10so6392353lfa.12 for ; Fri, 01 Apr 2022 11:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=IXfQ41dnhqNPjhOF8sHal/04DrHuUCqWMTWlvhP7LiU=; b=Qlup9MiUqO50pzpeRX9BgIkFwhHgGMk8YdYuHW3pr/xdHWcu2gYZ6uNq3SftLfpWtk YmVbqj3LeRampNeEKP8nsaTHHB4JhkT+GFRMYUoGqwIYF2kDcqpEUzEPt3Ob6mdcM1mc or2YNbFkWbHP3PCuHqUWhM+qri+7bdQcouFuA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=IXfQ41dnhqNPjhOF8sHal/04DrHuUCqWMTWlvhP7LiU=; b=rPSSPaQ2DggFuwYwB/7AGjcAIfpHNVsfULERrXAd+N5/l5xYNXPXkyDidLeNpbOiOP wGS8bBozT7xHfDXhmzojrempi+oj04uj+5oQWZN+aib9LYc6UplfadV1OrQ9l5TnJHAN 2j/NfmATDqdt+Lrk5q2p2tNNMlwy87VIU6TPiV13PUCskRxR43c+Ti3hP63EctIE3AKd sXaFI4U1b9MmMwSnm3bHCJUwL2pqXGkxFqo7xtqLWV2BdQehHhXRjvoGsT8hgsv6BeMD GALlJtHnoOml4cnqz/s7INQFlGPgR3UoXI1RGbm5GUAW6TBhgp2Ofd+VDroMdbdC3xD9 McbQ== X-Gm-Message-State: AOAM531VTDEbwGQ9g5owTyDvAtRdgTzi5b3GWl+zntyDmI2hfg/0/uJZ dbC6UTcOYITa5wxRHTjwpeMi562U1n333LxvG8o= X-Google-Smtp-Source: ABdhPJzitKVLyDRnkW4AIPNe4+BdEQQoNjBnBMM/7bv42auouCCofbOGc3VZCFt0s77gqpkkaJkGcg== X-Received: by 2002:a19:c20e:0:b0:44a:375c:6eb0 with SMTP id l14-20020a19c20e000000b0044a375c6eb0mr14662290lfc.5.1648837776587; Fri, 01 Apr 2022 11:29:36 -0700 (PDT) Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com. [209.85.167.52]) by smtp.gmail.com with ESMTPSA id e14-20020a19500e000000b0044a16931c7csm311322lfb.97.2022.04.01.11.29.34 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Apr 2022 11:29:34 -0700 (PDT) Received: by mail-lf1-f52.google.com with SMTP id bq24so6454603lfb.5 for ; Fri, 01 Apr 2022 11:29:34 -0700 (PDT) X-Received: by 2002:ac2:4203:0:b0:448:8053:d402 with SMTP id y3-20020ac24203000000b004488053d402mr14383562lfh.687.1648837773772; Fri, 01 Apr 2022 11:29:33 -0700 (PDT) MIME-Version: 1.0 References: <20220401181109.1477354-1-zi.yan@sent.com> <20220401181109.1477354-2-zi.yan@sent.com> In-Reply-To: <20220401181109.1477354-2-zi.yan@sent.com> From: Linus Torvalds Date: Fri, 1 Apr 2022 11:29:17 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2 2/2] mm: wrap __find_buddy_pfn() with a necessary buddy page validation. To: Zi Yan Cc: Linux-MM , Steven Rostedt , David Hildenbrand , Vlastimil Babka , Mel Gorman , Mike Rapoport , Oscar Salvador , Andrew Morton , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Stat-Signature: 9gn3dod5bpfeyhh5ctxakr6ti73yq887 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=google header.b=Qlup9MiU; spf=pass (imf07.hostedemail.com: domain of torvalds@linuxfoundation.org designates 209.85.167.43 as permitted sender) smtp.mailfrom=torvalds@linuxfoundation.org; dmarc=none X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: C0D1B4000B X-HE-Tag: 1648837778-608323 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Apr 1, 2022 at 11:11 AM Zi Yan wrote: > > Whenever the buddy of a page is found from __find_buddy_pfn(), > page_is_buddy() should be used to check its validity. Add a helper > function find_buddy_page_pfn() to find the buddy page and do the check > together. Well, this certainly looks nicer, except now: > +extern struct page *find_buddy_page_pfn(struct page *page, unsigned int order); that 'pfn' no longer makes sense in the name, and > @@ -1075,11 +1118,11 @@ static inline void __free_one_page(struct page *page, > migratetype); > return; > } > - buddy_pfn = __find_buddy_pfn(pfn, order); > - buddy = page + (buddy_pfn - pfn); > > - if (!page_is_buddy(page, buddy, order)) > + buddy = find_buddy_page_pfn(page, order); > + if (!buddy) > goto done_merging; > + buddy_pfn = page_to_pfn(buddy); This case now does two "page_to_pfn()" calls (one inside find_buddy_page_pfn(), and one explicitly on the buddy). And those page_to_pfn() things can actually be fairly expensive. It *looks* like just a subtraction, but it's a pointer subtraction that can end up generating a divide by a non-power-of-two size. NORMALLY we try very hard to make 'sizeof struct page' be exactly 8 words, but I do not believe this is actually guaranteed. And yeah, the divide-by-a-constant can be turned into a multiply, but even that is not necessarily always cheap. Now, two out of three use-cases didn't actually want the buddy_pfn(), but this one use-case does look like it might be performance-critical and a problem. Linus