Debian Codegraph @

storage/innobase/buf/buf0block_hint.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 #include "buf0block_hint.h"
29 #include "buf0buf.h"
30 namespace buf {
31
32 void Block_hint::store(buf_block_t *block) {
33 ut_ad(block->page.buf_fix_count > 0);
34 m_block = block;
35 m_page_id = block->page.id;
36 }
37
38 void Block_hint::clear() { m_block = nullptr; }
39
40 void Block_hint::buffer_fix_block_if_still_valid() {
41 /* We need to check if m_block points to one of chunks. For this to be
42 meaningful we need to prevent freeing memory while we check, and until we
43 buffer-fix the block. For this purpose it is enough to latch any of the many
44 latches taken by buf_resize().
45 However, for buffer-fixing to be meaningful, the block has to contain a page
46 (as opposed to being already empty, which might mean that buf_pool_resize()
47 can proceed and free it once we free the s-latch), so we confirm that the
48 block contains a page. However, it is not sufficient to check that this is
49 just any page, because just after we check it could get freed, unless we
50 have a latch which prevents this. This is tricky because page_hash latches
51 are sharded by page_id and we don't know the page_id until we look into the
52 block. To solve this chicken-and-egg problem somewhat, we latch the shard
53 for the m_page_id and compare block->page.id to it - so if is equal then we
54 can be reasonably sure that we have the correct latch.
55 There is still a theoretical problem here, where other threads might try
56 to modify the m_block->page.id while we are comparing it, but the chance of
57 accidentally causing the old space_id == m_page_id.m_space and the new
58 page_no == m_page_id.m_page_no is minimal as compilers emit a single 8-byte
59 comparison instruction to compare both at the same time atomically, and f()
60 will probably double-check the block->page.id again, anyway.
61 Finally, assuming that we have correct hash cell latched, we should check if
62 the state of the block is BUF_BLOCK_FILE_PAGE before buffer-fixing the block,
63 as otherwise we risk buffer-fixing and operating on a block, which is already
64 meant to be freed. In particular, buf_LRU_free_page() first calls
65 buf_LRU_block_remove_hashed() under hash cell latch protection to change the
66 state to BUF_BLOCK_REMOVE_HASH and then releases the latch. Later it calls
67 buf_LRU_block_free_hashed_page() without any latch to change the state to
68 BUF_BLOCK_MEMORY and reset the page's id, which means buf_resize() can free it
69 regardless of our buffer-fixing. */
70 if (m_block != nullptr) {
71 const buf_pool_t *const pool = buf_pool_get(m_page_id);
72 rw_lock_t *latch = buf_page_hash_lock_get(pool, m_page_id);
73 rw_lock_s_lock(latch, UT_LOCATION_HERE);
74 /* If not own buf_pool_mutex, page_hash can be changed. */
75 latch = buf_page_hash_lock_s_confirm(latch, pool, m_page_id);
76 if (buf_is_block_in_instance(pool, m_block) &&
77 m_page_id == m_block->page.id &&
78 buf_block_get_state(m_block) == BUF_BLOCK_FILE_PAGE) {
79 buf_block_buf_fix_inc(m_block, UT_LOCATION_HERE);
80 } else {
81 clear();
82 }
83 rw_lock_s_unlock(latch);
84 }
85 }
86 void Block_hint::buffer_unfix_block_if_needed(buf_block_t *block) {
87 if (block != nullptr) {
88 buf_block_buf_fix_dec(block);
89 }
90 }
91 } // namespace buf