pagemap: Add alert to mapping_set_release_always() for mapping with no release_folio
Subsystems (3)
PATCH 1/1 pagemap: Add alert to mapping_set_release_always() for mapping with no release_folio
AS_RELEASE_ALWAYS tells the memory management code to always call ->release_folio() when releasing a folio, even if it has no private data. Setting this flag without providing release_folio callback leads to try_to_free_buffers() being called on folios without buffer_heads, causing a NULL pointer dereference.
Add a VM_WARN_ONCE() alert to mapping_set_release_always() to catch this programming error early and help prevent similar bugs in other filesystems.
--- include/linux/pagemap.h | 3 +++ 1 file changed, 3 insertions(+)
include/linux/pagemap.h +3 -0
@@ -296,6 +296,9 @@ static inline bool mapping_release_always(const struct address_space *mapping)static inline void mapping_set_release_always (struct address_space *mapping) { +/* Alert while setting the flag with no release_folio callback */ +VM_WARN_ONCE (!mapping->a_ops->release_folio, +"Setting AS_RELEASE_ALWAYS with no release_folio" );set_bit (AS_RELEASE_ALWAYS , &mapping->flags); }
Signature · 1 line
2.52.0
Matthew Wilcox
maintainerinclude/linux/pagemap.h static inline bool mapping_release_always(const struct address_space *mapping) · line 299
Quoted 3 lines · Deepakkumar Karn
static inline void
mapping_set_release_always(struct address_space *mapping){include/linux/pagemap.h +1
-0+/* Alert while setting the flag with no release_folio callback */
The comment is superfluous.
include/linux/pagemap.h static inline bool mapping_release_always(const struct address_space *mapping) · line 301
Quoted 2 lines
include/linux/pagemap.h +2
-0+VM_WARN_ONCE (!mapping->a_ops->release_folio, +"Setting AS_RELEASE_ALWAYS with no release_folio" );
But you haven't said why we need to do this. Surely the NULL pointer splat is enough to tell you that you did something stupid?
Quoted 7 lines
set_bit(AS_RELEASE_ALWAYS, &mapping->flags);
}Signature · 1 line
2.52.0
Jan Kara
maintainerQuoted 6 lines · Matthew Wilcox
include/linux/pagemap.h static inline bool mapping_release_always(const struct address_space *mapping) · line 299
Quoted 3 lines · Deepakkumar Karn
static inline void
mapping_set_release_always(struct address_space *mapping){include/linux/pagemap.h +1
-0+/* Alert while setting the flag with no release_folio callback */ The comment is superfluous.
Agreed.
Quoted 5 lines
include/linux/pagemap.h static inline bool mapping_release_always(const struct address_space *mapping) · line 301
Quoted 2 lines
include/linux/pagemap.h +2
-0+VM_WARN_ONCE (!mapping->a_ops->release_folio, +"Setting AS_RELEASE_ALWAYS with no release_folio" );But you haven't said why we need to do this. Surely the NULL pointer splat is enough to tell you that you did something stupid?
Well, but this will tell it much earlier and it will directly point to the place were you've done the mistake (instead of having to figure out why drop_buffers() is crashing on you). So I think this assert makes sense to ease debugging and as kind of self-reminding documentation :).
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 14 lines · Jan Kara
Quoted 6 lines · Matthew Wilcox
Quoted 3 lines · Deepakkumar Karn
static inline void
mapping_set_release_always(struct address_space *mapping){
+ /* Alert while setting the flag with norelease_foliocallback */The comment is superfluous.
Agreed.
Quoted 3 lines
Quoted 2 lines
include/linux/pagemap.h +2
-0+VM_WARN_ONCE (!mapping->a_ops->release_folio, +"Setting AS_RELEASE_ALWAYS with no release_folio" );
Quoted 8 lines
Quoted 2 lines
But you haven't said why we need to do this. Surely the NULL pointer splat is enough to tell you that you did something stupid?
Well, but this will tell it much earlier and it will directly point to the place were you've done the mistake (instead of having to figure out why
drop_buffers()is crashing on you). So I think this assert makes sense to ease debugging and as kind of self-reminding documentation :).
Thank you Jan for your response, as you highlighted adding this change will help with early warning, making it immediately clear during filesystem initialization.
Also as Matthew stated, the comment is superfluous and doesn't serve any purpose.
I will remove that comment in another version.
Regards,
Deepakkumar Karn
Matthew Wilcox
maintainerQuoted 16 lines · Jan Kara
Quoted 6 lines · Matthew Wilcox
include/linux/pagemap.h static inline bool mapping_release_always(const struct address_space *mapping) · line 299
Quoted 3 lines · Deepakkumar Karn
static inline void
mapping_set_release_always(struct address_space *mapping){include/linux/pagemap.h +1
-0+/* Alert while setting the flag with no release_folio callback */ The comment is superfluous.
Agreed.
Quoted 5 lines
include/linux/pagemap.h static inline bool mapping_release_always(const struct address_space *mapping) · line 301
Quoted 2 lines
include/linux/pagemap.h +2
-0+VM_WARN_ONCE (!mapping->a_ops->release_folio, +"Setting AS_RELEASE_ALWAYS with no release_folio" );But you haven't said why we need to do this. Surely the NULL pointer splat is enough to tell you that you did something stupid?
Quoted 4 lines
Well, but this will tell it much earlier and it will directly point to the place were you've done the mistake (instead of having to figure out why
drop_buffers()is crashing on you). So I think this assert makes sense to ease debugging and as kind of self-reminding documentation :).
Oh. So the real problem here is this:
if (mapping && mapping->a_ops->release_folio)
return mapping->a_ops->release_folio(folio, gfp);
return try_to_free_buffers(folio);We should have a block_release_folio(), change all the BH-based filesystems to add it to their aops, and then change filemap_release_folio() to do:
if (mapping) return mapping->a_ops->release_folio(folio, gfp); return true;
(actually, can the !mapping case be hit? surely this can't be called for folios which have already been truncated?)
Then you get a nice NULL pointer dereference instead of calling into try_to_free_buffers() which is as confusing as hell.
Jan Kara
maintainerQuoted 28 lines · Matthew Wilcox
Quoted 16 lines · Jan Kara
Quoted 6 lines · Matthew Wilcox
include/linux/pagemap.h static inline bool mapping_release_always(const struct address_space *mapping) · line 299
Quoted 3 lines · Deepakkumar Karn
static inline void
mapping_set_release_always(struct address_space *mapping){include/linux/pagemap.h +1
-0+/* Alert while setting the flag with no release_folio callback */ The comment is superfluous.
Agreed.
Quoted 5 lines
include/linux/pagemap.h static inline bool mapping_release_always(const struct address_space *mapping) · line 301
Quoted 2 lines
include/linux/pagemap.h +2
-0+VM_WARN_ONCE (!mapping->a_ops->release_folio, +"Setting AS_RELEASE_ALWAYS with no release_folio" );But you haven't said why we need to do this. Surely the NULL pointer splat is enough to tell you that you did something stupid?
Quoted 4 lines
Well, but this will tell it much earlier and it will directly point to the place were you've done the mistake (instead of having to figure out why
drop_buffers()is crashing on you). So I think this assert makes sense to ease debugging and as kind of self-reminding documentation :).Oh. So the real problem here is this:
if (mapping && mapping->a_ops->release_folio) return mapping->a_ops->release_folio(folio, gfp); return try_to_free_buffers(folio);
Quoted 7 lines
We should have a
block_release_folio(), change all the BH-based filesystems to add it to their aops, and then changefilemap_release_folio()to do:if (mapping) return mapping->a_ops->release_folio(folio, gfp); return true;
OK, yes, this would work for me and I agree it looks like a nice cleanup.
Quoted 2 lines
(actually, can the !mapping case be hit? surely this can't be called for folios which have already been truncated?)
You'd think ;). There's some usage of try_to_free_buffers() from the dark ages predating git era in jbd2 (back then jbd) which is specifically run when we are done with journalling buffers on a page that was truncated - see fs/jbd2/commit.c:release_buffer_page(). Also there's an interesting case in migrate_folio_unmap() which calls try_to_free_buffers() for a truncated page. All the other users seem to have a valid mapping.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 43 lines · Jan Kara
Quoted 28 lines · Matthew Wilcox
Quoted 13 lines · Jan Kara
Quoted 6 lines · Matthew Wilcox
Quoted 3 lines · Deepakkumar Karn
static inline void
mapping_set_release_always(struct address_space *mapping){
+ /* Alert while setting the flag with norelease_foliocallback */The comment is superfluous.
Agreed.
Quoted 3 lines
Quoted 2 lines
include/linux/pagemap.h +2
-0+VM_WARN_ONCE (!mapping->a_ops->release_folio, +"Setting AS_RELEASE_ALWAYS with no release_folio" );Quoted 7 lines
Quoted 2 lines
But you haven't said why we need to do this. Surely the NULL pointer splat is enough to tell you that you did something stupid?
Well, but this will tell it much earlier and it will directly point to the place were you've done the mistake (instead of having to figure out why
drop_buffers()is crashing on you). So I think this assert makes sense to ease debugging and as kind of self-reminding documentation :).Oh. So the real problem here is this:
if (mapping && mapping->a_ops->release_folio) return mapping->a_ops->release_folio(folio, gfp); return try_to_free_buffers(folio);Quoted 7 lines
We should have a
block_release_folio(), change all the BH-based filesystems to add it to their aops, and then changefilemap_release_folio()to do:if (mapping) return mapping->a_ops->release_folio(folio, gfp); return true;OK, yes, this would work for me and I agree it looks like a nice cleanup.
Quoted 2 lines
(actually, can the !mapping case be hit? surely this can't be called for folios which have already been truncated?)
Quoted 7 lines
You'd think ;). There's some usage of
try_to_free_buffers()from the dark ages predating git era in jbd2 (back then jbd) which is specifically run when we are done with journalling buffers on a page that was truncated - see fs/jbd2/commit.c:release_buffer_page(). Also there's an interesting case inmigrate_folio_unmap()which callstry_to_free_buffers()for a truncated page. All the other users seem to have a valid mapping.
Thank you Matthew for the suggested change. Your suggestion seems to make more sense to cure the case. If my understanding is correct, with this change the failure will
happens in filemap_release_folio(). The current change isn't solving the case, as it
doesn't mandate FS to declare release.
As Jan mentioned, about the jbd2 and migrate_folio_unmap() cases (honestly new learning
for me, @Jan) this change should be okay. I will try and do some
tests with this. I will share
the updated patch.
Do let me know if I missed anything.
Regards,
Deepakkumar Karn