From: Yunsheng Lin <linyunsheng@huawei.com>
To: Dave Chinner <david@fromorbit.com>
Cc: Yishai Hadas <yishaih@nvidia.com>, Jason Gunthorpe <jgg@ziepe.ca>,
Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>,
Kevin Tian <kevin.tian@intel.com>,
Alex Williamson <alex.williamson@redhat.com>,
Chris Mason <clm@fb.com>, Josef Bacik <josef@toxicpanda.com>,
David Sterba <dsterba@suse.com>, Gao Xiang <xiang@kernel.org>,
Chao Yu <chao@kernel.org>, Yue Hu <zbestahu@gmail.com>,
Jeffle Xu <jefflexu@linux.alibaba.com>,
Sandeep Dhavale <dhavale@google.com>,
Carlos Maiolino <cem@kernel.org>,
"Darrick J. Wong" <djwong@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Jesper Dangaard Brouer <hawk@kernel.org>,
Ilias Apalodimas <ilias.apalodimas@linaro.org>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Trond Myklebust <trondmy@kernel.org>,
Anna Schumaker <anna@kernel.org>,
Chuck Lever <chuck.lever@oracle.com>,
Jeff Layton <jlayton@kernel.org>, Neil Brown <neilb@suse.de>,
Olga Kornievskaia <okorniev@redhat.com>,
Dai Ngo <Dai.Ngo@oracle.com>, Tom Talpey <tom@talpey.com>,
Luiz Capitulino <luizcap@redhat.com>,
Mel Gorman <mgorman@techsingularity.net>, <kvm@vger.kernel.org>,
<virtualization@lists.linux.dev>, <linux-kernel@vger.kernel.org>,
<linux-btrfs@vger.kernel.org>, <linux-erofs@lists.ozlabs.org>,
<linux-xfs@vger.kernel.org>, <linux-mm@kvack.org>,
<netdev@vger.kernel.org>, <linux-nfs@vger.kernel.org>
Subject: Re: [RFC] mm: alloc_pages_bulk: remove assumption of populating only NULL elements
Date: Tue, 18 Feb 2025 17:21:27 +0800 [thread overview]
Message-ID: <cf270a65-c9fa-453a-b7a0-01708063f73e@huawei.com> (raw)
In-Reply-To: <Z7Oqy2j4xew7FW9Z@dread.disaster.area>
On 2025/2/18 5:31, Dave Chinner wrote:
...
> .....
>
>> diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c
>> index 15bb790359f8..9e1ce0ab9c35 100644
>> --- a/fs/xfs/xfs_buf.c
>> +++ b/fs/xfs/xfs_buf.c
>> @@ -377,16 +377,17 @@ xfs_buf_alloc_pages(
>> * least one extra page.
>> */
>> for (;;) {
>> - long last = filled;
>> + long alloc;
>>
>> - filled = alloc_pages_bulk(gfp_mask, bp->b_page_count,
>> - bp->b_pages);
>> + alloc = alloc_pages_bulk(gfp_mask, bp->b_page_count - refill,
>> + bp->b_pages + refill);
>> + refill += alloc;
>> if (filled == bp->b_page_count) {
>> XFS_STATS_INC(bp->b_mount, xb_page_found);
>> break;
>> }
>>
>> - if (filled != last)
>> + if (alloc)
>> continue;
>
> You didn't even compile this code - refill is not defined
> anywhere.
>
> Even if it did complile, you clearly didn't test it. The logic is
> broken (what updates filled?) and will result in the first
> allocation attempt succeeding and then falling into an endless retry
> loop.
Ah, the 'refill' is a typo, it should be 'filled' instead of 'refill'.
The below should fix the compile error:
--- a/fs/xfs/xfs_buf.c
+++ b/fs/xfs/xfs_buf.c
@@ -379,9 +379,9 @@ xfs_buf_alloc_pages(
for (;;) {
long alloc;
- alloc = alloc_pages_bulk(gfp_mask, bp->b_page_count - refill,
- bp->b_pages + refill);
- refill += alloc;
+ alloc = alloc_pages_bulk(gfp_mask, bp->b_page_count - filled,
+ bp->b_pages + filled);
+ filled += alloc;
if (filled == bp->b_page_count) {
XFS_STATS_INC(bp->b_mount, xb_page_found);
break;
>
> i.e. you stepped on the API landmine of your own creation where
> it is impossible to tell the difference between alloc_pages_bulk()
> returning "memory allocation failed, you need to retry" and
> it returning "array is full, nothing more to allocate". Both these
> cases now return 0.
As my understanding, alloc_pages_bulk() will not be called when
"array is full" as the above 'filled == bp->b_page_count' checking
has ensured that if the array is not passed in with holes in the
middle for xfs.
>
> The existing code returns nr_populated in both cases, so it doesn't
> matter why alloc_pages_bulk() returns with nr_populated != full, it
> is very clear that we still need to allocate more memory to fill it.
I am not sure if the array will be passed in with holes in the
middle for the xfs fs as mentioned above, if not, it seems to be
a typical use case like the one in mempolicy.c as below:
https://elixir.bootlin.com/linux/v6.14-rc1/source/mm/mempolicy.c#L2525
>
> The whole point of the existing API is to prevent callers from
> making stupid, hard to spot logic mistakes like this. Forcing
> callers to track both empty slots and how full the array is itself,
> whilst also constraining where in the array empty slots can occur
> greatly reduces both the safety and functionality that
> alloc_pages_bulk() provides. Anyone that has code that wants to
> steal a random page from the array and then refill it now has a heap
> more complex code to add to their allocator wrapper.
Yes, I am agreed that it might be better to provide a common API or
wrapper if there is some clear use case that need to pass in an array
with holes in the middle by adding a new API like refill_pages_bulk()
as below.
>
> IOWs, you just demonstrated why the existing API is more desirable
> than a highly constrained, slightly faster API that requires callers
> to get every detail right. i.e. it's hard to get it wrong with the
> existing API, yet it's so easy to make mistakes with the proposed
> API that the patch proposing the change has serious bugs in it.
IMHO, if the API is about refilling pages for the only NULL elements,
it seems better to add a API like refill_pages_bulk() for that, as
the current API seems to be prone to error of not initializing the
array to zero before calling alloc_pages_bulk().
>
> -Dave.
next prev parent reply other threads:[~2025-02-18 9:21 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-17 12:31 Yunsheng Lin
2025-02-17 14:02 ` Jeff Layton
2025-02-17 14:20 ` Chuck Lever
2025-02-18 9:16 ` Yunsheng Lin
2025-02-18 14:17 ` Chuck Lever
2025-02-21 9:34 ` Yunsheng Lin
2025-02-17 21:31 ` Dave Chinner
2025-02-18 9:21 ` Yunsheng Lin [this message]
2025-02-18 21:14 ` Dave Chinner
2025-02-19 11:20 ` Yunsheng Lin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cf270a65-c9fa-453a-b7a0-01708063f73e@huawei.com \
--to=linyunsheng@huawei.com \
--cc=Dai.Ngo@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=alex.williamson@redhat.com \
--cc=anna@kernel.org \
--cc=cem@kernel.org \
--cc=chao@kernel.org \
--cc=chuck.lever@oracle.com \
--cc=clm@fb.com \
--cc=davem@davemloft.net \
--cc=david@fromorbit.com \
--cc=dhavale@google.com \
--cc=djwong@kernel.org \
--cc=dsterba@suse.com \
--cc=edumazet@google.com \
--cc=hawk@kernel.org \
--cc=horms@kernel.org \
--cc=ilias.apalodimas@linaro.org \
--cc=jefflexu@linux.alibaba.com \
--cc=jgg@ziepe.ca \
--cc=jlayton@kernel.org \
--cc=josef@toxicpanda.com \
--cc=kevin.tian@intel.com \
--cc=kuba@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-erofs@lists.ozlabs.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-nfs@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=luizcap@redhat.com \
--cc=mgorman@techsingularity.net \
--cc=neilb@suse.de \
--cc=netdev@vger.kernel.org \
--cc=okorniev@redhat.com \
--cc=pabeni@redhat.com \
--cc=shameerali.kolothum.thodi@huawei.com \
--cc=tom@talpey.com \
--cc=trondmy@kernel.org \
--cc=virtualization@lists.linux.dev \
--cc=xiang@kernel.org \
--cc=yishaih@nvidia.com \
--cc=zbestahu@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox