All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/5] mm: clarify nofail memory allocation
@ 2024-07-24  8:55 Barry Song
  2024-07-24  8:55 ` [PATCH RFC 1/5] vpda: try to fix the potential crash due to misusing __GFP_NOFAIL Barry Song
                   ` (4 more replies)
  0 siblings, 5 replies; 49+ messages in thread
From: Barry Song @ 2024-07-24  8:55 UTC (permalink / raw)
  To: akpm, linux-mm
  Cc: 42.hyeyoo, cl, hch, iamjoonsoo.kim, lstoakes, mhocko, penberg,
	rientjes, roman.gushchin, urezki, v-songbaohua, vbabka,
	virtualization, hailong.liu, torvalds

From: Barry Song <v-songbaohua@oppo.com>

__GFP_NOFAIL carries the semantics of never failing, so its callers
do not check the return value:
  %__GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
  cannot handle allocation failures. The allocation could block
  indefinitely but will never return with failure. Testing for
  failure is pointless.

However, __GFP_NOFAIL can sometimes fail if it exceeds size limits
or is used with GFP_ATOMIC/GFP_NOWAIT in a non-sleepable context.
This can expose security vulnerabilities due to potential NULL
dereferences.

Since __GFP_NOFAIL does not support non-blocking allocation, we introduce
GFP_NOFAIL with inclusive blocking semantics and encourage using GFP_NOFAIL
as a replacement for __GFP_NOFAIL in non-mm.

If we must still fail a nofail allocation, we should trigger a BUG rather
than exposing NULL dereferences to callers who do not check the return
value.

* The discussion started from this topic:
 [PATCH RFC] mm: warn potential return NULL for kmalloc_array and
             kvmalloc_array with __GFP_NOFAIL

 https://lore.kernel.org/linux-mm/20240717230025.77361-1-21cnbao@gmail.com/

Thank you to Michal, Christoph, Vlastimil, and Hailong for all the
comments.

Barry Song (5):
  vpda: try to fix the potential crash due to misusing __GFP_NOFAIL
  mm: Document __GFP_NOFAIL must be blockable
  mm: BUG_ON to avoid NULL deference while __GFP_NOFAIL fails
  mm: Introduce GFP_NOFAIL with the inclusion of __GFP_RECLAIM
  non-mm: discourage the usage of __GFP_NOFAIL and encourage GFP_NOFAIL

 arch/powerpc/sysdev/xive/common.c             |  2 +-
 drivers/gpu/drm/drm_modeset_lock.c            |  2 +-
 drivers/gpu/drm/nouveau/nouveau_dmem.c        |  8 +++----
 drivers/gpu/drm/virtio/virtgpu_vq.c           |  2 +-
 drivers/hv/vmbus_drv.c                        |  2 +-
 drivers/infiniband/hw/cxgb4/mem.c             |  4 ++--
 drivers/md/dm-region-hash.c                   |  2 +-
 .../chelsio/inline_crypto/chtls/chtls_cm.c    |  6 ++---
 .../chelsio/inline_crypto/chtls/chtls_hw.c    |  2 +-
 drivers/target/iscsi/cxgbit/cxgbit_cm.c       |  2 +-
 drivers/tty/tty_ldisc.c                       |  2 +-
 drivers/vdpa/vdpa_user/iova_domain.c          | 24 +++++++++++++++----
 fs/bcachefs/btree_iter.c                      |  2 +-
 fs/bcachefs/fs-io-buffered.c                  |  2 +-
 fs/bcachefs/io_write.c                        |  2 +-
 fs/btrfs/extent_io.c                          |  8 +++----
 fs/buffer.c                                   |  6 ++---
 fs/erofs/fscache.c                            |  2 +-
 fs/erofs/zdata.c                              | 10 ++++----
 fs/ext4/extents.c                             |  8 +++----
 fs/ext4/extents_status.c                      |  4 ++--
 fs/ext4/mballoc.c                             | 12 +++++-----
 fs/ext4/page-io.c                             |  2 +-
 fs/f2fs/checkpoint.c                          |  2 +-
 fs/f2fs/data.c                                |  4 ++--
 fs/f2fs/f2fs.h                                |  2 +-
 fs/f2fs/node.c                                |  2 +-
 fs/fuse/dev.c                                 |  2 +-
 fs/fuse/file.c                                |  4 ++--
 fs/fuse/inode.c                               |  4 ++--
 fs/fuse/virtio_fs.c                           |  4 ++--
 fs/gfs2/meta_io.c                             |  2 +-
 fs/gfs2/rgrp.c                                |  6 ++---
 fs/gfs2/trans.c                               |  2 +-
 fs/iomap/buffered-io.c                        |  2 +-
 fs/jbd2/journal.c                             |  4 ++--
 fs/jbd2/revoke.c                              |  2 +-
 fs/jbd2/transaction.c                         |  6 ++---
 fs/notify/fanotify/fanotify.c                 |  2 +-
 fs/reiserfs/journal.c                         |  2 +-
 fs/udf/directory.c                            |  2 +-
 fs/xfs/libxfs/xfs_alloc.c                     |  2 +-
 fs/xfs/libxfs/xfs_attr_leaf.c                 |  8 +++----
 fs/xfs/libxfs/xfs_bmap.c                      |  2 +-
 fs/xfs/libxfs/xfs_btree.h                     |  2 +-
 fs/xfs/libxfs/xfs_btree_staging.c             |  2 +-
 fs/xfs/libxfs/xfs_da_btree.c                  |  8 +++----
 fs/xfs/libxfs/xfs_defer.c                     |  4 ++--
 fs/xfs/libxfs/xfs_dir2.c                      | 10 ++++----
 fs/xfs/libxfs/xfs_dir2_block.c                |  2 +-
 fs/xfs/libxfs/xfs_dir2_sf.c                   |  8 +++----
 fs/xfs/libxfs/xfs_exchmaps.c                  |  4 ++--
 fs/xfs/libxfs/xfs_iext_tree.c                 |  4 ++--
 fs/xfs/libxfs/xfs_inode_fork.c                | 14 +++++------
 fs/xfs/libxfs/xfs_refcount.c                  |  4 ++--
 fs/xfs/libxfs/xfs_rmap.c                      |  2 +-
 fs/xfs/xfs_attr_item.c                        |  8 +++----
 fs/xfs/xfs_attr_list.c                        |  2 +-
 fs/xfs/xfs_bmap_item.c                        |  6 ++---
 fs/xfs/xfs_buf.c                              |  8 +++----
 fs/xfs/xfs_buf_item.c                         |  4 ++--
 fs/xfs/xfs_buf_item_recover.c                 |  2 +-
 fs/xfs/xfs_dquot.c                            |  2 +-
 fs/xfs/xfs_exchmaps_item.c                    |  4 ++--
 fs/xfs/xfs_extent_busy.c                      |  2 +-
 fs/xfs/xfs_extfree_item.c                     | 10 ++++----
 fs/xfs/xfs_icache.c                           |  2 +-
 fs/xfs/xfs_icreate_item.c                     |  2 +-
 fs/xfs/xfs_inode_item.c                       |  2 +-
 fs/xfs/xfs_inode_item_recover.c               |  2 +-
 fs/xfs/xfs_iunlink_item.c                     |  2 +-
 fs/xfs/xfs_iwalk.c                            |  2 +-
 fs/xfs/xfs_log.c                              |  2 +-
 fs/xfs/xfs_log_cil.c                          |  2 +-
 fs/xfs/xfs_log_recover.c                      |  6 ++---
 fs/xfs/xfs_mount.c                            |  2 +-
 fs/xfs/xfs_mru_cache.c                        |  4 ++--
 fs/xfs/xfs_qm.c                               |  4 ++--
 fs/xfs/xfs_refcount_item.c                    |  8 +++----
 fs/xfs/xfs_rmap_item.c                        |  8 +++----
 fs/xfs/xfs_rtalloc.c                          |  2 +-
 fs/xfs/xfs_super.c                            |  2 +-
 fs/xfs/xfs_trans.c                            |  4 ++--
 fs/xfs/xfs_trans_dquot.c                      |  2 +-
 include/linux/buffer_head.h                   |  4 ++--
 include/linux/gfp_types.h                     |  7 ++++++
 include/linux/slab.h                          |  4 +++-
 kernel/resource.c                             |  2 +-
 lib/list-test.c                               |  8 +++----
 lib/ref_tracker.c                             |  2 +-
 lib/rhashtable.c                              |  6 ++---
 lib/test_hmm.c                                |  6 ++---
 mm/page_alloc.c                               | 10 ++++----
 mm/util.c                                     |  1 +
 net/ceph/osd_client.c                         |  2 +-
 net/ceph/osdmap.c                             |  4 ++--
 net/core/sock.c                               |  4 ++--
 net/ipv4/inet_connection_sock.c               |  2 +-
 net/ipv4/tcp_output.c                         |  2 +-
 security/smack/smackfs.c                      |  2 +-
 100 files changed, 222 insertions(+), 196 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 49+ messages in thread
* Re: [PATCH RFC 5/5] non-mm: discourage the usage of __GFP_NOFAIL and encourage GFP_NOFAIL
@ 2024-07-25  6:38 kernel test robot
  0 siblings, 0 replies; 49+ messages in thread
From: kernel test robot @ 2024-07-25  6:38 UTC (permalink / raw)
  To: oe-kbuild; +Cc: lkp, Dan Carpenter

BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20240724085544.299090-6-21cnbao@gmail.com>
References: <20240724085544.299090-6-21cnbao@gmail.com>
TO: Barry Song <21cnbao@gmail.com>

Hi Barry,

[This is a private test report for your RFC patch.]
kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Barry-Song/vpda-try-to-fix-the-potential-crash-due-to-misusing-__GFP_NOFAIL/20240724-170305
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20240724085544.299090-6-21cnbao%40gmail.com
patch subject: [PATCH RFC 5/5] non-mm: discourage the usage of __GFP_NOFAIL and encourage GFP_NOFAIL
:::::: branch date: 22 hours ago
:::::: commit date: 22 hours ago
config: i386-randconfig-141-20240725 (https://download.01.org/0day-ci/archive/20240725/202407251449.TnONiYn5-lkp@intel.com/config)
compiler: gcc-12 (Ubuntu 12.3.0-9ubuntu2) 12.3.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <error27@gmail.com>
| Closes: https://lore.kernel.org/r/202407251449.TnONiYn5-lkp@intel.com/

New smatch warnings:
lib/rhashtable.c:192 bucket_table_alloc() warn: masked condition '(gfp & ~(((((((1))) << (10)) | ((((1))) << (11)))) | (((((1))) << (15))))) != (((((((1))) << (10)) | ((((1))) << (11)))) | (((((1))) << (6))) | (((((1))) << (7))))' is always true.

Old smatch warnings:
lib/rhashtable.c:557 rhashtable_insert_one() warn: passing zero to 'ERR_CAST'
lib/rhashtable.c:624 rhashtable_try_insert() warn: passing zero to 'ERR_CAST'

vim +192 lib/rhashtable.c

da20420f83ea0f Herbert Xu         2017-02-11  176  
97defe1ecf868b Thomas Graf        2015-01-02  177  static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
b9ecfdaa1090b5 Herbert Xu         2015-03-24  178  					       size_t nbuckets,
b9ecfdaa1090b5 Herbert Xu         2015-03-24  179  					       gfp_t gfp)
7e1e77636e3607 Thomas Graf        2014-08-02  180  {
eb6d1abf1bd8bf Daniel Borkmann    2015-02-20  181  	struct bucket_table *tbl = NULL;
8f0db018006a42 NeilBrown          2019-04-02  182  	size_t size;
f89bd6f87a53ce Thomas Graf        2015-01-02  183  	int i;
149212f07856b2 NeilBrown          2019-04-02  184  	static struct lock_class_key __key;
7e1e77636e3607 Thomas Graf        2014-08-02  185  
9e54dd8b64dccd Kent Overstreet    2024-03-21  186  	tbl = alloc_hooks_tag(ht->alloc_tag,
9e54dd8b64dccd Kent Overstreet    2024-03-21  187  			kvmalloc_node_noprof(struct_size(tbl, buckets, nbuckets),
9e54dd8b64dccd Kent Overstreet    2024-03-21  188  					     gfp|__GFP_ZERO, NUMA_NO_NODE));
da20420f83ea0f Herbert Xu         2017-02-11  189  
da20420f83ea0f Herbert Xu         2017-02-11  190  	size = nbuckets;
da20420f83ea0f Herbert Xu         2017-02-11  191  
c61791509b08d1 Barry Song         2024-07-24 @192  	if (tbl == NULL && (gfp & ~GFP_NOFAIL) != GFP_KERNEL) {
da20420f83ea0f Herbert Xu         2017-02-11  193  		tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
da20420f83ea0f Herbert Xu         2017-02-11  194  		nbuckets = 0;
da20420f83ea0f Herbert Xu         2017-02-11  195  	}
2d22ecf6db1c39 Davidlohr Bueso    2018-08-21  196  
7e1e77636e3607 Thomas Graf        2014-08-02  197  	if (tbl == NULL)
7e1e77636e3607 Thomas Graf        2014-08-02  198  		return NULL;
7e1e77636e3607 Thomas Graf        2014-08-02  199  
149212f07856b2 NeilBrown          2019-04-02  200  	lockdep_init_map(&tbl->dep_map, "rhashtable_bucket", &__key, 0);
149212f07856b2 NeilBrown          2019-04-02  201  
da20420f83ea0f Herbert Xu         2017-02-11  202  	tbl->size = size;
7e1e77636e3607 Thomas Graf        2014-08-02  203  
4feb7c7a4fbb8f NeilBrown          2019-03-21  204  	rcu_head_init(&tbl->rcu);
eddee5ba34eb6c Herbert Xu         2015-03-14  205  	INIT_LIST_HEAD(&tbl->walkers);
eddee5ba34eb6c Herbert Xu         2015-03-14  206  
d48ad080ec0101 Jason A. Donenfeld 2017-06-07  207  	tbl->hash_rnd = get_random_u32();
5269b53da4d432 Herbert Xu         2015-03-14  208  
f89bd6f87a53ce Thomas Graf        2015-01-02  209  	for (i = 0; i < nbuckets; i++)
9b4f64a227b6f4 NeilBrown          2018-06-18  210  		INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
f89bd6f87a53ce Thomas Graf        2015-01-02  211  
97defe1ecf868b Thomas Graf        2015-01-02  212  	return tbl;
7e1e77636e3607 Thomas Graf        2014-08-02  213  }
7e1e77636e3607 Thomas Graf        2014-08-02  214  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 49+ messages in thread

end of thread, other threads:[~2024-08-05  7:49 UTC | newest]

Thread overview: 49+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-24  8:55 [PATCH 0/5] mm: clarify nofail memory allocation Barry Song
2024-07-24  8:55 ` [PATCH RFC 1/5] vpda: try to fix the potential crash due to misusing __GFP_NOFAIL Barry Song
2024-07-24 12:26   ` Michal Hocko
2024-07-24 22:50     ` Barry Song
2024-07-25  6:08       ` Michal Hocko
2024-07-25  7:00         ` Barry Song
2024-07-29  3:42           ` Jason Wang
2024-07-29  6:05             ` Barry Song
2024-07-30  2:48               ` Jason Wang
2024-07-30  3:08                 ` Barry Song
2024-07-30  3:13                   ` Jason Wang
2024-07-24  8:55 ` [PATCH 2/5] mm: Document __GFP_NOFAIL must be blockable Barry Song
2024-07-24 11:58   ` Michal Hocko
2024-08-03 23:09   ` Davidlohr Bueso
2024-07-24  8:55 ` [PATCH 3/5] mm: BUG_ON to avoid NULL deference while __GFP_NOFAIL fails Barry Song
2024-07-24 10:03   ` Vlastimil Babka
2024-07-24 10:11     ` Barry Song
2024-07-24 12:10   ` Michal Hocko
2024-07-24  8:55 ` [PATCH 4/5] mm: Introduce GFP_NOFAIL with the inclusion of __GFP_RECLAIM Barry Song
2024-07-24 12:12   ` Michal Hocko
2024-07-24  8:55 ` [PATCH RFC 5/5] non-mm: discourage the usage of __GFP_NOFAIL and encourage GFP_NOFAIL Barry Song
2024-07-24  9:53   ` Vlastimil Babka
2024-07-24  9:58     ` Barry Song
2024-07-24 13:14       ` Christoph Hellwig
2024-07-24 12:25     ` Michal Hocko
2024-07-24 13:13     ` Christoph Hellwig
2024-07-24 13:21       ` Michal Hocko
2024-07-24 13:23         ` Christoph Hellwig
2024-07-24 13:31           ` Michal Hocko
2024-07-24 13:33             ` Vlastimil Babka
2024-07-24 13:38               ` Christoph Hellwig
2024-07-24 13:47                 ` Michal Hocko
2024-07-24 13:55                   ` Christoph Hellwig
2024-07-24 14:39                     ` Vlastimil Babka
2024-07-24 14:41                       ` Christoph Hellwig
2024-07-25  1:47                         ` Barry Song
2024-07-29  9:56                           ` Barry Song
2024-07-29 10:03                             ` Vlastimil Babka
2024-07-29 10:16                               ` Barry Song
2024-07-24 12:17   ` Michal Hocko
2024-07-25  1:38     ` Barry Song
2024-07-25  6:16       ` Michal Hocko
2024-07-26 21:08         ` Davidlohr Bueso
2024-07-29 11:50           ` Michal Hocko
2024-08-03 22:15             ` Davidlohr Bueso
2024-08-05  7:49               ` Michal Hocko
2024-07-25  1:17   ` kernel test robot
2024-07-25 14:20   ` Dan Carpenter
  -- strict thread matches above, loose matches on Subject: below --
2024-07-25  6:38 kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.