1
votes

I need to drop CEIL macro used in kernel module and use a ceil function in kernel to replace the caller of the macro.

I tried replacing ceil function in reference to Linux manual page

http://man7.org/linux/man-pages/man3/ceil.3.html#DESCRIPTION.

My question is whether it is possible to convert this macro into in kernel function i.e. ceil or not? Also, here in the code variable 'o' and 'rsz' are declared as unsigned int, where as ceil function's return type is double. If it's possible, how can I change it?

#define CEIL(a, b)     (((a) + (b-1)) / (b))
o += (((CEIL(rsz, 1024)) << 1) << n);

On editing above code to

o += ((ceil(rsz) << 1) << n)

Also, I tried including math header in .c file, which is

# include <math.h>

On executing, it gives a compilation error

fatal error: math.h: No such file or directory
2
You generally can't use floating point in the kernel. - Barmar
(a) What compilation error resulted from the code? (b) The ceil function in <math.h> is for floating-point double, but the CEIL macro you show is for integers. Do you need a floating-point ceiling function or an integer ceiling function? There is certainly no reason you could not use an integer ceiling function in kernel code. Whatever compilation error you got is from something wrong with the code, not from trying to calculate the ceiling of a ratio. (c) You should show enough code that somebody else could compile it and see the error for themselves. - Eric Postpischil
There is no compilation error if I go with above macro. Since kernel consider to use inline function, I'm trying to drop the macro and use in kernel ceil function directly. Therefore, I was trying to implement it. - Madhumitha
You wrote that “The above code gives compilation error”, and the above code shows a macro definition and code that uses it. Now you write that there is no compilation error if you “go with above macro.” That is confusing; we do not know what code gives an error. Edit your question to show exactly the code that gives a compilation error, and show exactly the error message. - Eric Postpischil
Sorry, I just edited the question. - Madhumitha

2 Answers

1
votes

No, one can't convert above macro into ceil in-kernel function, as there is no such function in kernel. Though if one look carefully the above macro, which is

#define CEIL(a, b)     (((a) + (b-1)) / (b))

The above function is alternative for DIV_ROUND_UP in-kernel function. So, one can drop above macro in the kernel and use the DIV_ROUND_UP in caller function.

This is true, if a and b both are integers.

0
votes

The CEIL(a, b) kernel macro is completely different from the ceil() C library function. It doesn't even take the same number of arguments!

Additionally, floating-point functions (and floating-point math in general) are not available within the kernel. The <math.h> header cannot be used in this environment.