xref: /openbmc/linux/fs/netfs/objects.c (revision 0545810f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Object lifetime handling and tracing.
3  *
4  * Copyright (C) 2022 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/slab.h>
9 #include "internal.h"
10 
11 /*
12  * Allocate an I/O request and initialise it.
13  */
14 struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,
15 					     struct file *file,
16 					     loff_t start, size_t len,
17 					     enum netfs_io_origin origin)
18 {
19 	static atomic_t debug_ids;
20 	struct inode *inode = file ? file_inode(file) : mapping->host;
21 	struct netfs_inode *ctx = netfs_inode(inode);
22 	struct netfs_io_request *rreq;
23 	int ret;
24 
25 	rreq = kzalloc(sizeof(struct netfs_io_request), GFP_KERNEL);
26 	if (!rreq)
27 		return ERR_PTR(-ENOMEM);
28 
29 	rreq->start	= start;
30 	rreq->len	= len;
31 	rreq->origin	= origin;
32 	rreq->netfs_ops	= ctx->ops;
33 	rreq->mapping	= mapping;
34 	rreq->inode	= inode;
35 	rreq->i_size	= i_size_read(inode);
36 	rreq->debug_id	= atomic_inc_return(&debug_ids);
37 	INIT_LIST_HEAD(&rreq->subrequests);
38 	refcount_set(&rreq->ref, 1);
39 	__set_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
40 	if (rreq->netfs_ops->init_request) {
41 		ret = rreq->netfs_ops->init_request(rreq, file);
42 		if (ret < 0) {
43 			kfree(rreq);
44 			return ERR_PTR(ret);
45 		}
46 	}
47 
48 	netfs_stat(&netfs_n_rh_rreq);
49 	return rreq;
50 }
51 
52 void netfs_get_request(struct netfs_io_request *rreq, enum netfs_rreq_ref_trace what)
53 {
54 	int r;
55 
56 	__refcount_inc(&rreq->ref, &r);
57 	trace_netfs_rreq_ref(rreq->debug_id, r + 1, what);
58 }
59 
60 void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async)
61 {
62 	struct netfs_io_subrequest *subreq;
63 
64 	while (!list_empty(&rreq->subrequests)) {
65 		subreq = list_first_entry(&rreq->subrequests,
66 					  struct netfs_io_subrequest, rreq_link);
67 		list_del(&subreq->rreq_link);
68 		netfs_put_subrequest(subreq, was_async,
69 				     netfs_sreq_trace_put_clear);
70 	}
71 }
72 
73 static void netfs_free_request(struct work_struct *work)
74 {
75 	struct netfs_io_request *rreq =
76 		container_of(work, struct netfs_io_request, work);
77 
78 	trace_netfs_rreq(rreq, netfs_rreq_trace_free);
79 	netfs_clear_subrequests(rreq, false);
80 	if (rreq->netfs_ops->free_request)
81 		rreq->netfs_ops->free_request(rreq);
82 	if (rreq->cache_resources.ops)
83 		rreq->cache_resources.ops->end_operation(&rreq->cache_resources);
84 	kfree(rreq);
85 	netfs_stat_d(&netfs_n_rh_rreq);
86 }
87 
88 void netfs_put_request(struct netfs_io_request *rreq, bool was_async,
89 		       enum netfs_rreq_ref_trace what)
90 {
91 	unsigned int debug_id = rreq->debug_id;
92 	bool dead;
93 	int r;
94 
95 	dead = __refcount_dec_and_test(&rreq->ref, &r);
96 	trace_netfs_rreq_ref(debug_id, r - 1, what);
97 	if (dead) {
98 		if (was_async) {
99 			rreq->work.func = netfs_free_request;
100 			if (!queue_work(system_unbound_wq, &rreq->work))
101 				BUG();
102 		} else {
103 			netfs_free_request(&rreq->work);
104 		}
105 	}
106 }
107 
108 /*
109  * Allocate and partially initialise an I/O request structure.
110  */
111 struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq)
112 {
113 	struct netfs_io_subrequest *subreq;
114 
115 	subreq = kzalloc(sizeof(struct netfs_io_subrequest), GFP_KERNEL);
116 	if (subreq) {
117 		INIT_LIST_HEAD(&subreq->rreq_link);
118 		refcount_set(&subreq->ref, 2);
119 		subreq->rreq = rreq;
120 		netfs_get_request(rreq, netfs_rreq_trace_get_subreq);
121 		netfs_stat(&netfs_n_rh_sreq);
122 	}
123 
124 	return subreq;
125 }
126 
127 void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
128 			  enum netfs_sreq_ref_trace what)
129 {
130 	int r;
131 
132 	__refcount_inc(&subreq->ref, &r);
133 	trace_netfs_sreq_ref(subreq->rreq->debug_id, subreq->debug_index, r + 1,
134 			     what);
135 }
136 
137 static void netfs_free_subrequest(struct netfs_io_subrequest *subreq,
138 				  bool was_async)
139 {
140 	struct netfs_io_request *rreq = subreq->rreq;
141 
142 	trace_netfs_sreq(subreq, netfs_sreq_trace_free);
143 	kfree(subreq);
144 	netfs_stat_d(&netfs_n_rh_sreq);
145 	netfs_put_request(rreq, was_async, netfs_rreq_trace_put_subreq);
146 }
147 
148 void netfs_put_subrequest(struct netfs_io_subrequest *subreq, bool was_async,
149 			  enum netfs_sreq_ref_trace what)
150 {
151 	unsigned int debug_index = subreq->debug_index;
152 	unsigned int debug_id = subreq->rreq->debug_id;
153 	bool dead;
154 	int r;
155 
156 	dead = __refcount_dec_and_test(&subreq->ref, &r);
157 	trace_netfs_sreq_ref(debug_id, debug_index, r - 1, what);
158 	if (dead)
159 		netfs_free_subrequest(subreq, was_async);
160 }
161