Skip to content

Commit bbfd6bf

Browse files
yixinghua121unicornx
authored andcommitted
kernel: fix memory leak
This patch contains part of changes from "bc0000e8e1: fix memory leak" of branch dev-canmv-plctv2. On orginal branch, there are some additional changes, including: - components/dfs/dfs_v2/filesystems/devfs/devfs.c, which has been pulled in with RT-Thread#9899. - nfs part, which are merged with nfs changes. The remaining part still involves three modules: - dfs - lwp - libcpu/c908 Need to continue to decompose. Signed-off-by: Wang Chen <[email protected]>
1 parent 0906712 commit bbfd6bf

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

components/dfs/dfs_v2/include/dfs_file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct dfs_file
8989

9090
uint32_t flags;
9191
rt_atomic_t ref_count;
92+
rt_atomic_t fd_ref_count;
9293

9394
off_t fpos;
9495
struct rt_mutex pos_lock;

components/dfs/dfs_v2/src/dfs.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ int fdt_fd_new(struct dfs_fdtable *fdt)
223223
{
224224
file->magic = DFS_FD_MAGIC;
225225
file->ref_count = 1;
226+
file->fd_ref_count = 1;
226227
rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
227228
fdt->fds[idx] = file;
228229

@@ -253,20 +254,27 @@ void fdt_fd_release(struct dfs_fdtable *fdt, int fd)
253254

254255
file = fdt_get_file(fdt, fd);
255256

256-
if (file && file->ref_count == 1)
257+
if (file)
257258
{
258-
rt_mutex_detach(&file->pos_lock);
259-
260-
if (file->mmap_context)
259+
if (file->fd_ref_count)
261260
{
262-
rt_free(file->mmap_context);
261+
rt_atomic_sub(&(file->fd_ref_count), 1);
263262
}
263+
if (file->ref_count == 1)
264+
{
265+
rt_mutex_detach(&file->pos_lock);
264266

265-
rt_free(file);
266-
}
267-
else
268-
{
269-
rt_atomic_sub(&(file->ref_count), 1);
267+
if (file->mmap_context)
268+
{
269+
rt_free(file->mmap_context);
270+
}
271+
272+
rt_free(file);
273+
}
274+
else
275+
{
276+
rt_atomic_sub(&(file->ref_count), 1);
277+
}
270278
}
271279

272280
fdt->fds[fd] = RT_NULL;
@@ -334,6 +342,7 @@ int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file
334342

335343
/* inc ref_count */
336344
rt_atomic_add(&(file->ref_count), 1);
345+
rt_atomic_add(&(file->fd_ref_count), 1);
337346
fdt->fds[fd] = file;
338347
retfd = fd;
339348

@@ -552,6 +561,7 @@ int dfs_dup(int oldfd, int startfd)
552561

553562
/* inc ref_count */
554563
rt_atomic_add(&(fdt->fds[newfd]->ref_count), 1);
564+
rt_atomic_add(&(fdt->fds[newfd]->fd_ref_count), 1);
555565
}
556566
exit:
557567
dfs_file_unlock();
@@ -600,6 +610,7 @@ int dfs_dup_to(int oldfd, struct dfs_fdtable *fdtab)
600610

601611
/* inc ref_count */
602612
rt_atomic_add(&(fdtab->fds[newfd]->ref_count), 1);
613+
rt_atomic_add(&(fdtab->fds[newfd]->fd_ref_count), 1);
603614
}
604615
exit:
605616
dfs_file_unlock();
@@ -736,6 +747,7 @@ rt_err_t sys_dup2(int oldfd, int newfd)
736747
fdt->fds[newfd] = fdt->fds[oldfd];
737748
/* inc ref_count */
738749
rt_atomic_add(&(fdt->fds[newfd]->ref_count), 1);
750+
rt_atomic_add(&(fdt->fds[newfd]->fd_ref_count), 1);
739751
retfd = newfd;
740752
exit:
741753
dfs_file_unlock();

components/dfs/dfs_v2/src/dfs_file.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ void dfs_file_init(struct dfs_file *file)
194194
file->magic = DFS_FD_MAGIC;
195195
rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
196196
rt_atomic_store(&(file->ref_count), 1);
197+
rt_atomic_store(&(file->fd_ref_count), 1);
197198
}
198199
}
199200

@@ -233,6 +234,15 @@ static void dfs_file_unref(struct dfs_file *file)
233234
file->vnode = RT_NULL;
234235
}
235236
}
237+
if (file->fd_ref_count == 0)
238+
{
239+
rt_mutex_detach(&file->pos_lock);
240+
if (file->mmap_context)
241+
{
242+
rt_free(file->mmap_context);
243+
}
244+
rt_free(file);
245+
}
236246

237247
LOG_I("release a file: %p", file);
238248
}
@@ -684,6 +694,7 @@ int dfs_file_close(struct dfs_file *file)
684694
if (file->vnode->aspace)
685695
{
686696
dfs_aspace_flush(file->vnode->aspace);
697+
dfs_aspace_clean(file->vnode->aspace);
687698
}
688699
#endif
689700
ret = file->fops->close(file);

components/lwp/lwp_futex.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ static rt_futex_t _sftx_create(struct shared_futex_key *key, struct rt_lwp *lwp)
261261
futex->mutex = RT_NULL;
262262
rt_list_init(&(futex->waiting_thread));
263263
futex->custom_obj = obj;
264+
// todo fix multithread share futex
265+
lwp_user_object_add(lwp, obj);
264266
}
265267
}
266268
}

libcpu/risc-v/t-head/c908/plic.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "ioremap.h"
2222

2323
static void *plic_regs = RT_NULL;
24+
static void *priority_base;
2425
extern struct rt_irq_desc isr_table[];
2526

2627
struct plic_handler
@@ -32,22 +33,14 @@ struct plic_handler
3233

3334
rt_inline void plic_toggle(struct plic_handler *handler, int hwirq, int enable);
3435
struct plic_handler plic_handlers[C908_NR_CPUS];
35-
static void *plic_irq_priority[INTERRUPTS_MAX] = {RT_NULL};
3636

3737
rt_inline void plic_irq_toggle(int hwirq, int enable)
3838
{
3939
int cpu = 0;
4040
void *priority_addr;
4141

4242
/* set priority of interrupt, interrupt 0 is zero. */
43-
priority_addr = (void *)((rt_size_t)plic_regs + PRIORITY_BASE + hwirq * PRIORITY_PER_ID);
44-
#ifdef RT_USING_SMART
45-
if (plic_irq_priority[hwirq] == RT_NULL)
46-
{
47-
plic_irq_priority[hwirq] = (void *)rt_ioremap(priority_addr, 0x1000);
48-
}
49-
priority_addr = plic_irq_priority[hwirq];
50-
#endif
43+
priority_addr = (void *)((rt_size_t)priority_base + hwirq * PRIORITY_PER_ID);
5144
writel(enable, priority_addr);
5245
struct plic_handler *handler = &plic_handlers[cpu];
5346

@@ -203,8 +196,8 @@ void plic_init(void)
203196
handler->hart_base = (void *)((rt_size_t)plic_regs + CONTEXT_BASE + i * CONTEXT_PER_HART);
204197
handler->enable_base = (void *)((rt_size_t)plic_regs + ENABLE_BASE + i * ENABLE_PER_HART);
205198
#ifdef RT_USING_SMART
206-
handler->hart_base = (void *)rt_ioremap(handler->hart_base, 0x1000);
207-
handler->enable_base = (void *)rt_ioremap(handler->enable_base, 0x1000);
199+
handler->hart_base = (void *)rt_ioremap(handler->hart_base, CONTEXT_PER_HART);
200+
handler->enable_base = (void *)rt_ioremap(handler->enable_base, ENABLE_PER_HART);
208201
#endif
209202
done:
210203
/* priority must be > threshold to trigger an interrupt */
@@ -215,6 +208,11 @@ void plic_init(void)
215208
}
216209
}
217210

211+
priority_base = (void *)((rt_size_t)plic_regs + PRIORITY_BASE);
212+
#ifdef RT_USING_SMART
213+
priority_base = (void *)rt_ioremap(priority_base, INTERRUPTS_MAX * PRIORITY_PER_ID);
214+
#endif
215+
218216
/* Enable supervisor external interrupts. */
219217
set_csr(sie, SIE_SEIE);
220218
}

0 commit comments

Comments
 (0)