Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

390
Views
linux kernel: is vfs_write thread safe?

In my program, I need to write file in kernel space due to some special reason although I know it's not recommended.

I'm using vfs_write to write files in kernel space and it works fine. In one case, there are two threads need to write to the same file.

From the internet, it seems that user-space write is thread safe, however, I cannot find whether vfs_write is thread safe or not. Could some one help with this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Yes, vfs_write is thread safe.

The only thing you should care is that file's position, pointer to which you pass to the function as pos argument, shouldn't be changed during the call to the function.

You can, e.g., use local variable as file's position, load actual position into it before the call, pass pointer to it as the function's argument, and update actual position after the call. This technique is used in write syscall implementation:

loff_t pos = file_pos_read(f.file);
ret = vfs_write(f.file, buf, count, &pos);
if (ret >= 0)
   file_pos_write(f.file, pos);

As you can see, vfs_write doesn't syncrhonize offsets in file between concurrent writers. Possible useful usage scenarios of concurrent writers include:

  1. Append only: set O_APPEND flag for the file. In that case position in the file, passed to vfs_write is ignored and each writer appends data to the file.
  2. Rewrite only: do not set O_APPEND flag for the file and allow each concurrent writer to modify only its own part of the file.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!